How to rotate one object to face another, then move toward it?

I’ve been thinking about this for a while, and I’m finally going to break down and ask for help.

Let’s say there are two cubes (one is the pursuer, the other the pursued). Both can move freely in the x and z. Both have a ‘front’ side where, if there was a texture of a face, it would be there.

I want the ‘pursuer’ to be able to rotate to face the pursued and then move toward it.

I appreciate any input here :slight_smile:

Thinking about it, this is the way I might do it:

  1. Create a center point coordinate of the pursued cube. (p1)
  2. Create a center point coordinate of the chaser cube. (p2)
  3. Find the vertical and horizontal angles of that line (p1,p2)
  4. Find the center point of the side of the cube that represents the face. (p3)
  5. Find the vertical and horizontal angles of that line (p1,p3)
  6. Rotate the cube until angles of line (p1,p3) matches those of (p1,p2)
  7. The line to move along would be (p1,p2), but taking into the (p1,p3) of each cube as to identify collision, assuming head on collision.

To take into account all approach points, then the distance between the two cubes would be:
(p1,p2) - (p1, point of intersection of line on p1’s surface) - (p2, point of intersection of line on p2’s surface)

Note this is purely theoretical and haven’t actually put it to the test.

Examples of rotation can be found here:
http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=05

Let me know if this works for you.

I worked out a method to get this done. I’ll put the basic steps here in case someone else out there is interested. The ‘model’ being refered to here is just a plain cube with a glut sphere sticking out of the front so I know which is the front.

  1. Maintain a float that keeps track of the rotation along the y axis of the model. Starts at zero.
  2. Maintain x, y, z origin for the model and it’s target.
  3. Maintain a vector protruding from the front of the model with a length of 1.
  4. Based on elapsed time since last frame and how many degrees of rotation you want to allow per second, calculate rotation amount.
  5. Apply rotation in + direction, then get distance from vector end to target origin. Do the same in the - direction.
  6. Move the model toward the target BEFORE doing any rotation based on elapsed time etc as before.
  7. Now, rotate in which ever direction provides the shortest distance between vector end and target origin. Rotation amount equals maintained += new this frame.

I thought about rotating first, and then moving along the transformed z axis, but I wanted to keep track of the model’s origin so I would know where it ‘really’ was figureing I’m most likely going to need this information as I learn more about having my model interact with it’s world.

If anyone can give a better approach I’m all ears.

Keith