1
$\begingroup$

I'm using this python code to rotate a bone in the blender game engine, the code works fine until bone angle reaches 10.0 or -10.0 radians then it stops

import bge
import mathutils

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

up_pressed = cont.sensors['up_sensor'].positive
down_pressed = cont.sensors['down_sensor'].positive
currentAngle = armature.channels['Bone'].rotation_euler 

speed = 0.1
increment = mathutils.Vector((0.0, speed, 0.0))

if up_pressed:
    armature.channels['Bone'].rotation_mode = bge.logic.ROT_MODE_XYZ
    armature.channels['Bone'].rotation_euler  = currentAngle + increment
    armature.update()

if down_pressed:
    armature.channels['Bone'].rotation_mode = bge.logic.ROT_MODE_XYZ
    armature.channels['Bone'].rotation_euler  = currentAngle - increment
    armature.update()    

print(currentAngle)

I guess it has something to do with the mathutils.Vector type but I can't figure it out.

is there another way to handle .rotation_euler data other than vectors?

$\endgroup$

1 Answer 1

1
$\begingroup$

I think the easy answer is just replace your actual rotation statements with an if statement that says something like the following:

up_pressed:

if armature.channels['Bone'].rotation_euler.y < 2*math.pi:
    armature.channels['Bone'].rotation_euler.y = currentAngle + increment
else:
    armature.channels['Bone'].rotation_euler.y = 0

down_pressed:

if armature.channels['Bone'].rotation_euler.y > 0:
    armature.channels['Bone'].rotation_euler.y = currentAngle - increment
else:
    armature.channels['Bone'].rotation_euler.y = 2*math.pi
$\endgroup$
3
  • 1
    $\begingroup$ thank you, you led me to an even better solution for more smooth rotation: "else: armature.channels['Bone'].rotation_euler = currentAngle - 2*math.pi", pi cannot be written directly, so I used math,pi after importing "math" module $\endgroup$
    – Ahmed Ali
    Commented Feb 17, 2016 at 15:59
  • $\begingroup$ @AhmedAli Good call on the math.pi, I will edit for future correctness. I'm glad that helped you. $\endgroup$
    – Rick Riggs
    Commented Feb 17, 2016 at 16:04
  • 1
    $\begingroup$ sorry, I forgot something else, 'armature.channels['Bone'].rotation_euler' is a vector, it can't be compared to zero directly, I needed to compare the component value instead, for example 'armature.channels['Bone'].rotation_euler.y > 0' $\endgroup$
    – Ahmed Ali
    Commented Feb 17, 2016 at 18:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .