How to constrain rotation using python?

How do I make it so in a python script, an object rotates till 45 degrees on the X axis, and then just remains at 45 degrees rotation? It will just spin around in a full circle…

Example: Cube or some other object.

    import math
    own = cont.owner
    
    rot = own.worldOrientation.to_euler()
    rot.x += math.radians(45)
    
    own.worldOrientation = rot

Ooh till 45, so it need a motion lets say from 0 to 45 degree?

this will do it:

from bge import logic
from math import radians

def test(cont):
    
    own = cont.owner
    
    if not 'rot' in own:
        own['rot'] = 0.0

    if own['rot'] < 45:
            
        rot = own.worldOrientation.to_euler()
        rot.x += radians(1)
            
        own.worldOrientation = rot
        own['rot'] +=1
2 Likes

check angle value AFTER the input modification. if too high, set to MAX, then set matrix all the time.

  • init variable if needed
  • modify variable
  • check if variable in limits
    • set variable to limit if past
  • use variable to create orientation mathutils.Euler((x,y,z))
1 Like

I did it. This thread should now be closed. Thanks for the answer.

I agree, but in this case he just wanted an example on how to rotate an object to/till a certain degree.
And this won’t fail either if the steps stay at 1 degree for each I value.