I have this bullet game object that I want to code to continually move in the direction of the bullet tip.
In other words, no matter what the rotation of the object is, I want the bullet to move in the direction of the bullet’s rounded end.
I know I have to use the update function and there’s something involved with vectors, but I don’t know how to specify a vector that always matches the current rotation of the bullet game object.
Apparently go.get_rotation() returns only quaternions and not eulers, which I can’t visualize properly, so I decided to just create a separate bullet for each rotation.
I’m not sure how to work with Quaternions, let alone convert them to Euler, so I wasn’t able to figure out how to lock forward movement to only one rotation dimension.
Ok, I see. Why do you need to convert it to Euler? In your original question you asked how to move a bullet in the direction of the bullet tip.
So if you set the rotation of the game object you can use that same rotation to move it forward in that direction:
function init(self)
local angle_in_degrees = 45
go.set_rotation(vmath.quat_rotation_z(math.rad(angle_in_degrees))
self.linear_velocity = 200 -- pixels/s
end
function update(self, dt)
local rotation = go.get_rotation()
local distance = self.linear_velocity * dt
local direction = vmath.rotate(rotation, vmath.vector3(0, distance, 0))
go.set_position(go.get_position() + direction)
end
They are not the same.
A quaternion is defined in 4D and the euler representation is defined in 3D.
So you cannot use the quat.z and expect it to be a rotation around z.
If you read the documentation for vmath.quat_rotation_z, you see that it’s a helper function that makes it easier for you to create the desired rotation. It would be equivalent to vmath.quat_axis_angle(vmath.vector3(0,0,1), rad)