ERROR: attempt to perform arithmetic on a userdata value (SOLVED)

I’m trying to write a bit of code that will rotate the Game Object when the right arrow key is held. My code is:

function on_input(self, action_id, action)
	-- Add input-handling code here
	-- Remove this function if not needed
	if action_id == hash("right") then
		rotation = go.get_rotation() + vmath.quat(0, 0, 1, 0)
		print(go.get_rotation())
		go.set_rotation(rotation)
	end
end

Whenever I build the game and press the right arrow key, it gives me the error:
03%20PM
Line 39 being where I define “rotation”, and line 35 is the on_input function.

Quaternions should be multiplied, not added.

Furthermore, the vmath.quat(0,0,1,0) does not yield a rotation quaternion around z. For that, use vmath.quat_rotation_z(). See https://www.defold.com/ref/vmath/#vmath.quat_rotation_z

2 Likes

Quaternions is something a lot of users are struggling with. I wonder if we could create a couple of samples in the Examples section where quaternions are used?

2 Likes

Sure, good idea. And maybe a tutorial on rotation.

3 Likes

That’s a great idea! Considering I have no clue what any of that stuff is.

4 Likes

I’m having a vision of the future… Somebody, very shortly, is going to be asking about euler.z and rotation.z!

3 Likes

I’ve got the rotation thing down, but my idea was to rotate a dynamic collision object. Is there any way to do that, considering you can’t control dynamics?

You can rotate by applying forces to the collision object. Like this: https://github.com/britzl/publicexamples/tree/master/examples/rotate_collision_object

Thank you!