[solved with a pen] Fire and forget (and rotate)

Hello,

I’m doing a tank game, and i was wondering how to fire a bullet from the turret.
I’ve found teh britzl sample which do that well ( https://github.com/britzl/publicexamples/tree/master/examples/rotate_and_move ).

But there is something i don’t understand in the code.
Two things are puzzling me.

-- OK: get current rotation of player
local rotation = go.get_rotation()

-- hmm… why do we need to apply rotation to the position ? 
-- Is for creating a matrix of quaternion ?
local position = go.get_position() + vmath.rotate(rotation, vmath.vector3(10, 40, 0))

-- I don't understand the second parameter of vmath.rotate function.
-- the 1000 value is on y axis, so i don't see the point with adding coordinates.
-- if i lower 1000 value, the bullet doesn't go far.
local to = position + vmath.rotate(rotation, vmath.vector3(0, 1000, 0))

-- OK for the rest.
local bullet = factory.create("#bulletfactory", position, rotation)
go.animate(bullet, "position", go.PLAYBACK_ONCE_FORWARD, to, go.EASING_LINEAR, 0.75, 0, function()
					go.delete(bullet)
end)

Thank you.

vmath.rotate rotates a vector (not a position, but they use the same data format)
it’s used in this case, to change the direction of the velocity according to the firing object

the first call is to set the initial position of the bullet to slightly in front of the tank, then the second call sets the position the bullet will go to

2 Likes

thank you for the answer.
So , for destination computing:
position + vmath.rotate(rotation, vmath.vector3(0, 1000, 0))

I don’t understand. As it’s the destination position, we should rotate a position and then translate the rotated « point ».
Above, it seems we add a position vector and a rotation vector. I don’t know what we get with this operation (obviously it works, though :slight_smile: )

I think i have understood.

position + vmath.rotate is a matrix addition which gives a final matrix
with both position and rotation updated.

Tell me if i’m wrong, otherwise, i will mark the subject as solved.

1 Like

the final value does not include any rotational data, only position, other than that, it’s not quite matrix addition, since vector3 values aren’t matrices, but the process is kinda the same either way.

the best way i could put the function in a not so math heavy way is such: standup, and hold a pen (or other object) in front of you. now, turn yourself to the right some amount. your body is like the tank GO. the pen is the position of the bullet, and your arm is the vector that you rotate with the function. the function acts like your shoulder, which keeps your arm attached to you and locked with how your body is rotated so that the pen is always in front of you. (the function doesn’t actually lock things in place, but it gives the values which could)

3 Likes