Rotates an object until it reaches a desired angle

Hi everyone!

I have been reading many posts, tutorials, documentation, but I really need a little help.

I don’t know if maybe is the logic or I’m not using the right sentences for my logic.

What I’m trying to do is to rotate the empty object until it reaches 60 degrees and when it reaches the angle, stop the rotation, but I have continous rotation or just one rotation.

This is the logic I’m using.

 if centro_mouse.positive:
     for grados in range (60):
         rotZ= 0.02*(60-grados)
         centro_motion.dRot=[0, 0, rotZ]          

cont.activate(centro_motion)

         print(rotZ)
         cont.activate(centro_motion)
else:
    cont.deactivate(centro_motion)     

interfaz1.blend (447 KB)

Thanks!

Anyone please can help me?
Just an orientation, tell me if I’m doing it right or doing wrong.
Anything you can tell me I’m sure it will be of great help.

Thanks!

I made an example for Chronox, which demonstrates how to cap rotation:

http://www.nilunder.com/forums/blender/139003/b/0

Hi All!

Thanks Goran!

I have been working on this with your code for example and it was really helpful!

I created 3 important properties:

  • accum= to have the accumulation of rotations of the object
  • ini_state= to determine the first positive pulse from the sensor and control the next movement with flags
  • arriba = to know if i do a “wheel up” in the mouse sensor.

This was the logic implement:

from math import pi

def main(cont):

up = cont.sensors["up"]

own = cont.owner
limit = pi/3 # 60 degrees
elementos=6
rota=0.01
next_accum = 0

veces=1 * (up.positive)

if up.positive and own["ini_state"]:
    own["ini_state"]=False
    own["lui"] += veces
    own["arriba"] = True      

if  own["arriba"]:  
    next_accum = own["accum"] + rota    
    if next_accum > limit and own["arriba"]:  
        own["ini_state"]=True    
        rota = limit - own["accum"]
        own["arriba"] = False
    else:
        own["arriba"] = True
        print('false')    
    own.applyRotation([rota, 0, 0])   
    own["accum"] += rota   
      
print('Rota = ',rota)
print('Next_accum=',next_accum)
print('Veces :',own["lui"])

Here is the .blend : rotlimitada.blend (367 KB)

Greetings and once again thanks!

sorry to bump this …but isnt there a way to just set the rotation (in degrees) of an object in python. Its what I was looking for…didnt want to start a new thread.

Say I wanted a cube just facing y. and when I keypress its facing 45 degrees of y. Without actually rotating

I think this is what you’re wanting.


# Get rotation in degrees
degrees = ob.worldOrientation.to_euler()

# Set Y to 45 degrees
degrees[1] = math.radians(45)
ob.worldOrientation = degrees

Tested with 2.62


import bge
import math

def m(cn):
    ow=cn.owner
    
    s1=cn.sensors["1"]
    s2=cn.sensors["2"]
    s3=cn.sensors["+"]
    
    if s1.status==1:
        ori=ow.orientation.to_euler()
        ori[2] = math.radians(45)
        ow.orientation=ori
        
    if s2.status==1:
        ori=ow.orientation.to_euler()
        ori[2] = math.radians(90)
        ow.orientation=ori
        
    if s3.status==1:
        ori=ow.orientation.to_euler()
        ori[2]=math.radians(math.degrees(ori[2])+45)
        ow.orientation=ori

:RocknRoll:

exactly what i needed ty

is the part more obscure of blender the rotation !! :wink: