To make an object spin (SOLVED)

Hi,

I want to make an object spin, I tried this way

    function update (self, dt) 
      local rot = go.get_rotation()
      rot.z = rot.z + 1
      go.set_rotation(rot)
    end

But it makes my object scale up oddly

Everything I have seen before about rotation aims to set a deremined rotation with an angle but I need to define a relative rotation on every frame, I found nothing about the deeper behavior of the rotation system.

You want to use

function init(self)
	self.rotation = 0
end

function update(self, dt)
	self.rotation = self.rotation + 10 * dt
	go.set(".", "euler.z", self.rotation)
end

go.set_rotation is for quaternions

1 Like

Did it work with euler.z? If not, use quaternion.

local rotation = go.get_rotation(object)
go.set_rotation(rotation * vmath.quat_rotation_z(0.04), object)
3 Likes

I would recommend to use quaternions as in @sergey.lerg example. Some additional comments:

  • In your example you’re rotating by 1 per frame/update. You should instead define a number of degrees to rotate per second and multiply that by dt (delta time) to get an even rotation regardless of fps or any lag.
  • For constant rotations it’s recommended to use go.animate instead of running Lua code every frame.
4 Likes

Thank you guys for help !

thats bloody brilliant!

1 Like