Shooting in 3D, vector math problem

:bust_in_silhouette: Reply From: wombatstampede

Hi sxkod,
I put up a general info about local/global transforms and some common calculations here:

To your problem:

Positioning the bullet:
You’re setting the local translation/origin of the bullet with the global origin of the firepoint. This might work if the bullet is child a parent which has the position (0,0,0).
It is safer to set either


…to set projis position and rotation equal to firepoint) or…


…to just set projis position equal to firepoint. (but not rotation)

If you would want to offset the bullit i.e. 0.5m along the firepoints z-axis then you could use:

proji.global_transform.origin += firepoint.global_transform.basis.z * 0.5


proji.global_transform.origin += firepoint.global_transform.basis.z * -0.5

… for the other direction.

Be the force with you:
apply_impulse receives two parameters:
-1: An offset in global coordinate space to the rigidbodies origin. So in this case this would probably simply be (0,0,0), or you could use apply_central_impulse() as well.
-2: Is the force/impulse vector in the global coordinates space (so rotation is global).

So applying the impulse would be:

const MY_FORCE = 100

proji.apply_central_impulse(firepoint.global_transform.basis.z * MY_FORCE * -1)

This applies a force of “100” to the bullit (you may use more or less depending on the mass you set on the bullet and how far and fast you want it to go) in the direction of -z of the firepoint. Should the bullet go “backwards” then remove the * -1 of the line above.

Hi wombatstampede,

That worked. I also read your write up. For someone mathematically challenged as me, that is a clear explanation. So, thanks so much.

sxkod | 2019-02-16 13:46

Glad that it worked. Thank you for answering.

wombatstampede | 2019-02-16 15:30

1 Like