Rotation using euler.z

Hi,

I am having a problem using the euler.z for rotation. I am making a Tetris clone for the purpose of learning the Defold framework.

See the following gif:
http://makeagif.com/i/7QMgRt

The rotation is fine for the first 2 clicks, where the brick went clockwise -90deg and -180deg, but when it is -270deg it suddenly rotate anti clockwise instead. I have no idea how to fix this.

My code is below.

go.animate(“brick”, “euler.z”, go.PLAYBACK_ONCE_FORWARD, -90*x, go.EASING_LINEAR, 0.5)

where x is a number between 1 and 4. (i,e the rotation angle is multiples of -90degrees: -90, -180, -270 and -360), so that each click of the mouse will cause a further -90degree rotation.

The problem is as you’ve probably figured out that you rotate from -270 (x=4) to -90 (x=1) instead of from -270 to -360. One way would be to treat the transition from x=4 to x=1 differently, or simply not putting any limitation on x at all and simply decrease/increase it on each rotation.

1 Like

Hi britzl,

Thank you so much for your reply. The rotation from -270 to -90 is indeed a problem, we can see the sudden swing at the last rotation due to this.

There seem to have a problem between the rotation from -180 (x=2) to -270 (x=3) too. The brick rotated anticlockwise between these 2 angles, which I can’t figure out why. But I will continue to ponder on the use of x and see if there is a better way to keep track of the rotation at each mouse click

@Ken: I did a quick test and noticed that the euler rotation isn’t working as I would have expected. I need to investigate this a bit more and get back to you.

Hello @britzl , @Ken

Is there any progress about this behavior or workaround if anyone experienced same.

EDIT : When i try to rotate Z with editor handles 0 to 135 , it rotates to (180,180,45) so somehow engine treats my 0,0,135 to 180 180 45 internally.

EDIT : Finally found the solution :
Arrow 1 euler (0,0,45)
Arrow 2 euler (0,0,135) (this is the buggy euler prints (180,180,45) )

Quaternion rotations of this two objects is :

DEBUG:SCRIPT: vmath.quat(0, 0, 0.38268342614174, 0.9238795042038)	hash: [/g_food_garden/trap_arrow1]
DEBUG:SCRIPT: vmath.quat(0, 0, 0.9238795042038, 0.38268342614174)	hash: [/g_food_garden/trap_arrow2]

What does it mean :
arrow1 rotated go.get_rotation().w = 0.9238795042038 with in (0, 0, 0.38268342614174) axis
arrow2 rotated go.get_rotation().w = 0.38268342614174 with in (0, 0, 0.9238795042038) axis

Arrow 1
w = cos( angle / 2 ) = 0.9238795042038
angle / 2 =math.acos ( 0.9238795042038 )
angle = 0.78539831133951 (45 degrees )
Arrow 2
w = cos( angle / 2 ) = 0.38268342614174
angle / 2 =math.acos ( 0.38268342614174 )
>angle = 0.38268342614174 (135 degrees )

So, now i have the right rotation of this 2 objects. We can now use the angle to vector functions to move this object kinematicaly.

angle_to_vector = function (angle, magnitude) --angle in radians, i use magnitude to apply speed.
	magnitude = magnitude or 1 -- if no magnitude supplied make it a unit vector
	local x = math.cos ( angle ) * magnitude
	local y = math.sin ( angle ) * magnitude
	return x, y
end

EDİT : angle_to_vector (angle) gives normalized direction. I don’t know if this solution may optimized.

Best Regards,

2 Likes