Rotation limits on objects via Python

(Rubbernuke) #1

Hi all

Like the title suggests, how (Python wise) do you impose limits on an object rotating? I have several areas within my game that would benefit from this but I have no clue as to how to do it.

I have played with the logic constraints, but they seem to be global in scope which is a problem for me (unless I missed a setting somewhere)- I need the limits to affect the object on a local axis.

Any thoughts appreciated!



(retro world) #2

I have a way you can do it.

create some float properties and call them rotx, roty, and rotz. these are for rotating the different axis.
then set up the controllers to use a script and use this code

import GameLogic as g
cont = g.getCurrentController()

cont.owner.orientation = [cont.owner["rotx"],cont.owner["rotxy"],cont.owner["rotxz"]]

if you don’t want to rotate some of the axis you can use 0 instead.

Tell me if you need more help

(Rubbernuke) #3

Nice! Thanks for that retro world.

How would you stop an object rotating past a certain point though? For example, I want an object to bank left and right, but the banking should stop before the object rolls completely over.

(retro world) #4

use an if statements.

for example “if the rotation value is past a certain point, then don’t change the rotation, else if it isn’t past that limit update the rotation.”

do you know how to use if statements?

(Rubbernuke) #5

Here is an example blend of me playing around. It has two objects- a cube that saves its orientation to a Dict, and a slave object that uses this Dict value to govern how far it banks into a corner.

I suppose what I am struggling to get my head around is what exactly influences what- in the example I would like the banking to stop before it flips over (SPACE goes forward, L/R cursor steers / banks).

Cheers for the input so far too!


Example.blend (335 KB)

(Rubbernuke) #6

Almost got this working!

My final question is why when the box reaches its maximum turn value (either -2 or 2) the block gets stuck and no longer rotates?

Cheers for your time



Test.blend (319 KB)

(agoose77) #7

import bge
def rot(cont):

    sensor = cont.sensors["Keyboard"]
    sensor = cont.sensors["Keyboard1"]
    obj = cont.owner
    curRot = obj.localOrientation.to_euler()[2]

    for key,status in
        if key == and curRot < 2:
            rotation = [ 0.0, 0.0, 0.1]
            obj.applyRotation(rotation, True)           

        if key == and curRot > -2 :
            rotation = [ 0.0, 0.0, -0.1]
            obj.applyRotation(rotation, True)

(Rubbernuke) #8

Duh! I’m being a bit of a divvy lately- I think fatherhood is taking its toll. Thanks agoose!

Out of interest, I changed applyRotation to angular velocity and the constraints no longer worked- is something else required for this?