How to: Arrow

So I was wondering how to make arrows (like bow & arrow) in BGE. After some thinking, I came to a solution.

First, turn on physics for your arrow. Then when you create it, you can give it a starting velocity and such.

Then write a short python script that makes it point in the direction it’s traveling, hook that script up to an always sensor, no actuators. Here’s mine (still a bit of a noob at scripting, so it’s not the best, but it works):


import bge
import mathutils


def main():

    cont = bge.logic.getCurrentController()
    own = cont.owner
    
    
    vel = own.worldLinearVelocity
    print(vel)
    dir = vel.normalized()
    q = mathutils.Quaternion((0.0, 0.0, 0.0, 0.0))
    q = dir.to_track_quat('Y', 'Z')
    print(q)
    mat = q.to_matrix()
    print(mat   )

    
    
    own.worldOrientation = mat
    print(own.worldOrientation)

main()

You’re welcome, attached is the test file.

Attachments

arrow_test.blend (350 KB)

So the main idea was (?) to figure out how to make the shaft of the arrow a tangent to it’s motion.

Nice… although I’ve gotta say I’ve never been (and probably never will be that ambitious) to use matrices and functions such as “to_track_quat()”.

However you have had to use bge physics and the object needs to be dynamic or rigid body.

It could of course be done with no bge physics and single static persistent arrow shaft (ie no adding of objects required). It’s parabolic flight would be described with simple laws of motion along with it’s initial properties of direction and speed. And using a timer or just counting frames.

OK, this leaves the problem of how to have the arrow point in the direction of it’s motion.

You could have a simple invisible object which will always be at the arrow’s tip. This would be the object which is fired. The arrow is then given the velocity of the dummy object and made to always track to the dummy object.

Yours is an interesting exercise but you might find that when modelling it into a scene so that you can aim with a mouse and draw the bow (say with the mouse wheel) that some of these suggestions make things easier.