GameObject euler.z problem

Hello! I made a gameobject and set its rotation manually to 0,0,105. But when I’m trying to get euler.z in my script using go.get(".", “euler.z”) it gives me 75. Why?

I’m just trying to make a rotating object which I can start and stop. To make it I use example from here https://www.defold.com/examples/animation/spinner/.

I am not sure what’s the current situation with the euler property, but it wasn’t working properly. You can use quaternions that are inside the rotation property. To set a rotation on the Z axis you would do

go.set_rotation(vmath.quat_rotation_z(math.pi))

Ok. I need to rotate object constantly with same speed. Rotation should start from current rotation value. How can I achieve these result? Example in the docs works only when initial z angle of object is 0.

May be I should get angle from quaternion somehow if

go.get(".", "euler.z")

is buggy?
Something like:

    local rot = go.get_rotation()

    -- this is the main line
    local original_angle = vmath.get_angle_from_quaternion_somehow(rot)

    self.t = self.t + dt 
    local step = math.floor(self.t * self.speed) 
    local angle = math.pi / 6 * step + original_angle
    local rot = vmath.quat_rotation_z(-angle) 
    go.set_rotation(rot) 

I think this post (and links in this post) can help you Alternative to quaternions (SOLVED)

So, ok! Huh! I solved it. Works with go.set_rotation(). If you want to make a constantly rotating object you should add its initial rotation to angle calculated for next frame. And finally that’s what I’ve got:

function init(self)
    self.original_rot = go.get_rotation()
    self.rotate = false
    self.t = 0
end

function rotate(self, dt)
    self.t = self.t + dt
    local step = math.floor(self.t * self.speed)
    local angle = math.pi / 360 * step
    local rot = vmath.quat_rotation_z(-angle)

    -- this line is key to dzen
    go.set_rotation(rot * self.original_rot)
end

function update(self, dt)
    if self.rotate then
        rotate(self, dt)
    end
end


function stop_rotation(self)
    self.rotate = false
end


function start_rotation(self)
    self.rotate = true
    self.original_rot = go.get_rotation()
end

You can do it a bit simpler.

function update(self, dt)
    if self.rotate then
        go.set_rotation(go.get_rotation() * vmath.quat_rotation_z(self.step_angle))
    end
end

Define self.step_angle as a rotation increment in radians.

1 Like

So, what is your version defining self.stap_angle? :wink:

Whatever value suits you, like maybe 0.001

And perhaps also apply dt to the step_angle before use.

2 Likes