Tilting camera using python. Very close need that last little bit of help

I wanted to add the .blend file as attachement (as stated in the post), but I get an error uploading it. What can I do about that?

Okay I’ll start with what I want it do do then I’ll point out where it goes wrong

I want a script that if you press and hold W the camera slowly tilts forward untill you reach about 70 degrees and if you keep holding the W key than you keep a steady 70 degrees descend.

I got pretty close myself, but there are a few kinks that I would like to improve.

So here is the code for the tilting (and leveling back to 90 degrees) when you hold the W key:

import GameLogic
import bge


cont = GameLogic.getCurrentController()
own = cont.owner


rotspeedWS = 0.00005
retspeed = 0.0001

move = cont.actuators["move"]
W = cont.sensors["W"]

Wbool = 0
Sbool = 0

e = own.orientation.to_euler()

angleB = move.dRot[0]

if W.positive:
    print(e.x)
    if e.x > 1.2:
        angleB = angleB - rotspeedWS
        move.dRot = [angleB, 0, 0]
        cont.activate(move)
        Wbool = 1
    
else:
    Wbool = 0
    
   
if Wbool == 0:
    
        
    
    if angleB > 0:
        angleB = angleB - rotspeedWS
        move.dRot = [angleB, 0, 0]
        cont.activate(move)
    if angleB < 0:
        angleB = angleB + rotspeedWS
        move.dRot = [angleB, 0, 0]
        cont.activate(move)
    if e.x > 1.58:
        e.x = e.x - 0.004
        own.orientation = e
    if e.x < 1.56:
        e.x = e.x + 0.007
        own.orientation = e

And this works pretty smooth. You gradially tilt further forward till you hit 1.2 radians (about 68 degrees).

But when you keep holding it it flips out. because e.x constantly goes over and under 1.2 because of the 2 different if statements. (I added the .blend that shows that if you hold W after to 1.2 it flips out)

I don’t think I can explain it any better so please check the blend if you don’t know what I mean with “flip out” (haha)

If anyone knows a better way to get the result I want (the .blend shows precisly want I want, I just need a tweak at the 1.2 point so it doesn’t go berserk)

Don’t mind the awesome world creation in the .blend file :stuck_out_tongue:
If you need any other information please ask and I’ll see if I can supply it.

Thank you if you read it all, I know it’s a long read.

Hoping for a reply

Greetings,
H34DhUnT3r

Is there anyone who can help me?
Please

I did start looking at it, but had to go to work, now I really need to sleep. If I get a chance I’ll certainly give it a go.

Oh, it didn’t ‘flip out’ for me, it just kinda twitched a little bit when rotated all the way with the key held down and then rotated back past it’s initial position. Isn’t this something that would be easier with an IPO or action set to flipper?

Can you explain better your requirements?
It is not clear, for example, what you want to achieve in case ‘W’ is released before 70 degrees are reached.

Exactly what it is doing now.
What I mean with “flip out” is indeed the twitch, but it is really annoying if you see that on a big screen. Causes a headache real quick XD

Isn’t this something that would be easier with an IPO or action set to flipper?

What do you mean with that?

Greetings,
H34DhUnT3r

Here’s a quick demo tilting a cube 70 degrees using an f-curve (Blender 2.62):
tilting cube.blend (437 KB)

The press the W key to tilt and release for it to move back. It’s a much simpler solution than using python.

@battery

That’s pretty awesome. There is just one problem with that thing. When it is moving back and you again press the W key it first resets back to the start of the animation.

  • The W key is a just a part of it. When you press the S key it needs to look up and when you press A and D it needs to go left and right accordingly

So to sum it up. I need like a flight script using the WASD keys. If that is possible with animations than please tell me how to make it look smooth.

Thank you for your replies, I really appreciate it. Sorry that I’m not satisfied yet, haha.

Greetings,
H34DhUnT3r

Ah, you need to tick ‘continue’ on the action logic brick - I forget that. You might be able to do a flight script with actions, though I don’t think actions would be the best way to go in that case.

Anyway, if you’re still interested, here’s my python solution:



import bge

cont = bge.logic.getCurrentController()
own = cont.owner

#key sensor is set to positive and negative pulse mode
w = cont.sensors['w']

orientation = own.orientation.to_euler()

# sets how fast the cube tilts back and forth
steps = 100
tilt_angle = 1.2

xrot = 0

if w.positive and orientation.x < tilt_angle:
    xrot  = tilt_angle/steps
elif not w.positive and orientation.x > 0:
    xrot  = -tilt_angle/steps
    #correct orientation if it goes past 0
    if orientation.x + xrot < 0:
        xrot  = 0 - orientation.x

own.applyRotation([xrot,0,0], False)

All it needs is a keyboard sensor set to positive and negative pulse mode. There shouldn’t be any twitching this time (the if not w.positive condition fixes that). Also, it corrects on returning so that it always comes back to 0.

if you didnt want it to go les than 1.2 then i just use

if x < 1.2
__x = 1.2

wont let it go past 1.2 on my stuff.

or tie it in with your keyboard event

if key.event and x > 1.2:
_____actuator

I was really busy last week so wasn’t able to reply.

Anyway I tried your above code, but it doesn’t do anything. I believe I have connected everything correctly so I don’t get why it isn’t working
Can you or anyone please put it in a .blend so I can see what I did wrong?
Thank you so much for your help! Really appreciate it.

Greetings,
H34DhUnT3r

No worries, here’s the .blend showing it in action. The tilt is linked to the W key.

tilting cube 2.blend (484 KB)

Let me know if you run into any trouble. Hope this helps! :smiley:

Thanks a lot for that, so:

It’s working now, but I need something else, haha :stuck_out_tongue:
I modified your code to seperate the 1.2 tilt in 5 (0.2, 0.4, 0.6, 0.8 and 1.2) and assigned q w e r t to those 5 angles.

Now it is working, but for some reason I have the same problem as before:
It is quivering when you hold it at a specific point.
Now I know the quivering is very slight. You hardly notice it, but if you attach a camera to it you will get headache pretty darn fast XD

I added the .blend to show you what I mean.

I’m really thankful for all your help and I’m confident you can help me again :smiley:

Greetings,
H34DhUnT3r

Attachments

CodingTest.blend (418 KB)

I suggest following:


print (xrot)

you will see:

  • this code is called way to often (performance)
  • xrot gets a a lot of different values (design)

If you really want to use code (for whatever reason) I suggest to completely redesign it. It contains way to much repeating code and conflicting conditions.

I recommend to use the action method as suggested by battery.

Alternative

  • let an invisible parent do the tilt (in one step)
  • use slow parent to smooth the animation of the visible child

Short Demo of the Slow-Parent Method:

Attachments

SlowMoTilt.blend (341 KB)

Very interesting
Thank you for that Monster.
I’ll check it out and see how this will benefit me.

Greatest thanks to all of you. I’ll probably be back :wink: