Alternative to quaternions (SOLVED)

It would be great to have a more simple alternative to quaternions for rotation. I had not come across them before and it was immediately daunting when reading up on them.

If you could rotate game objects using Euler angles and a relative position, it would be much simpler.

It’s possible

-- get current euler
go.get(".", "euler") 
-- set new euler
go.set(".", "euler", vmath.vector3(0,0,20)) 
-- animate euler
go.animate(".", "euler", go.PLAYBACK_LOOP_FORWARD, vmath.vector3(0,0,90),go.EASING_INCIRC, 1) 

 -- set just z
go.set(".", "euler.z", 20)
-- animate just z
go.animate(".", "euler.z", go.PLAYBACK_LOOP_FORWARD, 30, go.EASING_INCIRC, 1) 

link to property documentation

4 Likes

Thanks a lot!

Although when I put this in the object’s init() it didn’t work:

I changed it to:

go.animate(".", "euler.z", go.PLAYBACK_LOOP_FORWARD, 30, go.EASING_LINEAR, 1)

Not sure why this worked, I just noticed it was implemented like that in the Spawning section of “Examples project”:

http://www.defold.com/tutorials/examples/

Thanks again.

1 Like

You should be aware of a few things when using go.animate on euler.z: https://forum.defold.com/search?q=euler

I would recommend that you get used to using quaternions for rotation. Here’s a discussion about euler vs quaternions: Rotation and moving forward ? simple?

Here’s another alternative, although it’s not a one-liner:

-- in this example we track the angle using a go.property
go.property("angle", 0)

function init(self)
	-- since the angle is defined using go.property we can use it in calls to go.animate
	go.animate("#", "angle", go.PLAYBACK_LOOP_FORWARD, 360, go.EASING_LINEAR, 1, 0)
end

function update(self, dt)
	-- here we set the rotation around the z-axis every frame using the angle
	go.set_rotation(vmath.quat_rotation_z(math.rad(self.angle)))
	
	-- we could also do it like this (although go.set_rotation is the recommended way)
	--go.set(".", "euler.z", self.angle)
end
4 Likes

Thanks a lot @britzl !

Do you know of a way you could pivot the game object from a certain point relative to it?

Do you mean to not rotate around the center point? You could offset your sprite components or your could create a collection and have a root object that you rotate and offset a child game object from the root object.

Correct, I want to rotate from the bottom of the sprite for example. How do you offset the sprite component from the game object?

What do you think is the best approach?

Thanks.

If you select the sprite component there should be fields in the properties window that allows you to change its x,y,z. You can’t change this at run-time though.

1 Like

Thanks a lot :slight_smile: