开发者

Direct X Rotation / C# how to get two mesh objects to always face each other

Is there a way given the center point of a mesh object to always have it face another given point? For 开发者_Python百科example if a cylinder is drawn so it looks like a cone always have the tip of the cone face some other given point?


Well thats definitely doable. You need to set up an orthonormal basis such that the 2 objects point each other. I'm going to assume that your cone is set up such that the point is down the Z-Axis (You need to bear this in mind).

Cone A is at position P
Cone B is at position Q

The direction from A to B is = Q - P and B to A is P - Q. Firstly we need to normalise both of these vectors so they are now unit direction vectors. We'll call them A' and B', respectively, for convenience (These are both now direction vectors).

We can assume, for now, that the up vector (We'll call it U) is 0, 1, 0 (Be warned that the maths here will fall over if A' or B' is very close to this up vector but for now I won't worry about that).

So we now need the side vector and true up vector. Fortunately we can calculate something that is perpendicular to the plane formed by the Up vector and A' or B' using a cross product.

Thus the side vector (S) is calculated as follows ... A' x U. Now we have this side vector we can calculate the true Up vector by doing A' x S. This now provides us with the 3 vectors we need for an orthonormal basis. You could normalise thse 2 vectors to remove any errors that may have accumulated but a cross product of 2 unit vectors should always be a unit vector so any errors would be slight so its probably not worth doing.

Using this information we can now build the matrix for Cone A.

S.x,  S.y,  S.z,  0
U.x,  U.y,  U.z,  0
A'.x, A'.y, A'.z, 0
P.x, P.y, P.z, 1

Perform the same calculations for both cones and they will now point towards each other. If you move either cone re-calculate as above again for both cones and they will still point towards each other.

Its worth noting that the matrix format I've used is DirectX's default (row major) layout. Its quite possible that C# (and thus XNA?) uses a column major format. If so you need to lay the amtrices out as follows:

S.x, U.x, A'.x, P.x
S.y, U.y, A'.y, P.y
S.z, U.z, A'.z, P.z
0,   0,   0,    1

Note the only difference between the 2 matrices is the fact that the rows and columns have been swapped. This makes the second matrix the transpose of the 1st matrix (And vice versa).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜