Sign in to follow this  
EternityZA

making a homing projectile always face the target

Recommended Posts

Hey. i have a arrow that always travels the shortest distance to its target by comparing its position to that of its target each time it gets to move. This way the arrow will follow a moving target and always hit (like arrows in warcraft III or World of Warcraft) this works fine and all except that im only ever translating the arrow object, never rotating it so regardles of what direction its traveling in its always pointing in the same direction. im kinda stuck on this. How can i calculate the correct local angle for my arrow object so that the arrow would always be pointing at the target.

Share this post


Link to post
Share on other sites
You can always get the direction vector by subtracting the position of the target from your arrows position and normalize. From this based on the Up direction in your world you should be able to build an orientation matrix to rotate the arrow.

Share this post


Link to post
Share on other sites
thnx for the replay but thats exactly what im unsure of.

how do i actualy build the orientation matrix using the direction vector.

i allready have the direction vector since i use it in the arrows translation as wel

Share this post


Link to post
Share on other sites
Someone really needs to write an article on this topic because it comes up alll the time.

I touched on it briefly here: http://www.gamedev.net/community/forums/viewreply.asp?ID=3500842

Basically the transform is composed of 4 vectors, 3 basis and a position. You already have the position, so we'll just focus on the other 3 vectors. We'll refer to them as XYZ. So a matix has 4 vectors, XYZP where P is the position.

Your arrow model is aligned in a coordinate space so that it faces one of these vectors. That is if the transform was an identity, the arrow would either point in the X, Y, or Z direction. Lets assume it points in the X direction.

Like the previous poster mentioned, finding the X direction vector is easy, you just subtract the arrow position from the target position and normalize it. So now we're left with computing the other two vectors, Y and Z. Which unfortunately is a completely arbitrary process. It's just like using LookAt when creating a view matrix. Lots of people just use the world Up vector which will work but can lead itself to artifacts in some cases, such as if the view direction and up direction align. We'll accept those consequences for now.

So assuming that Y is up in the world sense, we'll use the world up vector to compute the arrow Y vector. Initialy we'll assume Y = WorldUp but the vectors in the transform must be perpindicular to one another. If the arrow is anything but horizontal than this would be invalidated. So we're going to eventually replace Y with the true Y.

The first step in doing that is computing the Z which is perpindicular to X and Y, so you use the cross product. Z = Cross(X, (Y=WorldUp)). So now that we know Z is perpindicular to Y and X, we can compute the true Y which is perpindicular to both X and Z, again using the cross product. Y = Cross(Z, X).

To make it readable:


X = Normalize(Target - ArrowPosition)
TempY = WorldUp
Z = Normalize(Cross(X, TempY))
Y = Cross(Z, X)

Transform = Matrix(X,Y,Z,ArrowPosition)


Note: If the arrow ever pointed straight up, Z would be a zero lengthed vector which would build an invalid transform. You may want to derive the orientation from some other property, such as the arrow velocity, rather than the world up vector. But then this will build an invalid transform if the arrow velocity was ever zero lengthed or aligned with certain axes. You can basically consider it coordinate space gimbal lock. It's always going to exist, you just have to find a way which works in most case, and when it fails, have a check for the failure and a back up plan.

[Edited by - bzroom on August 13, 2009 5:57:46 PM]

Share this post


Link to post
Share on other sites
Wow I never thought it is so easy to create a transformation matrix. But shouldn't X, Y and Z all be normalized (maybe you have already answered it by using the term 'basis' on them)?

Share this post


Link to post
Share on other sites
Yup sorry i was just coming back to mention that. If you do this:

Z = Normalize(Cross(X, TempY))

Then all will be well. You can get away with only the two normalizations.

Share this post


Link to post
Share on other sites
"Wow I never thought it is so easy to create a transformation matrix"

Yeah, it's one of those things that's never in any of the textbooks, but the minute someone explains that the 3x3 part of the matrix are just the X,Y and Z axes rotated into place the whole of the rest of using matricies starts making sense.

I don't know if it's just supposed to be so obvious no-one needs to mention it or if it's a way of thinking that most users don't need or what but it took me ages to work it out...


There's a similar one about determinants. People go on and on about how to compute them for ages. It was years before I found a mention in a book about what it actually represents; if you use the matrix as a transformation, the determinant tells you about the change in volume of the transformed object. So for almost all 3D graphics work, the determinant is 1.[1]

Now, determinants make sense suddenly. My husband is a mathematician at heart -- he seems to think the determinant of a matrix is somehow an actual thing and he thinks I'm odd because of my tendency to look at maths stuff thinking about what it does to "real" objects.




[1] And suddenly all the matrix inversion algorithms get a lot simpler..

Share this post


Link to post
Share on other sites
Thanks for your reply, girl!
I think there was something back in my mind about the determinant and the basis->tranform. matrix things, but I guess it was too long ago (or more likely I was sleeping on those lectures). And yes, statics course->rigid body motion matrix determinant is one!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this