making a homing projectile always face the target

This topic is 3044 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

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 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 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 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).

X = Normalize(Target - ArrowPosition)TempY = WorldUpZ = 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 on other sites
Thnx a lot!

I'ts working perfectly;P

Share on other sites
Well that was quick.. good to hear!

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 on other sites
Yeah they all need to be normalized.

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 on other sites
lol i just normalized them anyway for good measure :P without knowing if it was nescesary or not

thnx again

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 on other sites
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 on other sites

This topic is 3044 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Create an account

Register a new account

• Forum Statistics

• Total Topics
628684
• Total Posts
2984222

• 12
• 13
• 13
• 10
• 10