# making a homing projectile always face the target

This topic is 3329 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).

To make it readable:

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

1. 1
2. 2
3. 3
Rutin
18
4. 4
JoeJ
14
5. 5

• 14
• 10
• 23
• 9
• 47
• ### Forum Statistics

• Total Topics
632636
• Total Posts
3007574
• ### Who's Online (See full list)

There are no registered users currently online

×