3

I just started with 3D coding in XNA and am trying to get my head around a few things.

My goal with XNA is to make a space sim game (original, I know) I am able to draw models and my camera is working as I'd like it to, where I am running into trouble is in understanding how to move my enemy ships. I have done some worth with steering behaviors in 2d, but not 3d.

My question is:

If I am trying to move ships to 'seek' a location, how does this movement effect the ship's world matrix (if at all)? I am using vector3s, and adding the acceleration to the velocity, and then the velocity to the position. Is this the right approach?

I do not have to post right now or I would, I am merely trying to understand what approach to take.

Thanks

Nic Foster
  • 2,864
  • 1
  • 27
  • 45
  • 2
    As far as integrating velocity/accel, the correct formula for Newtonian motion over time dt at constant accel a, with starting velocity v0 and starting position x0 is: x = x0 + v0*dt + 0.5*a*dt*dt. The new velocity is v = v0 + a*dt. These equations are valid for 3D vectors (where velocity and accel are themselves vectors.) – Dan Bryant Nov 21 '11 at 22:22
  • Thanks for your reply, using this formula would I still need to maintain a world matrix for the ships to transform into world space or is this not necessary? – Matt Weichselbaum Nov 21 '11 at 23:12
  • Typically you construct the World matrix based on the position and orientation of your actor (ship, in this case). Assuming that the position you're tracking is the world position, you just replace the translation portion of the World matrix with the actor's position. – Dan Bryant Nov 21 '11 at 23:14

1 Answers1

4

Give your object/entity/ship a position (Vector3) and rotation (Matrix), and then you can use the following code (and the sample at the bottom of this answer) to move the ship around.

For example to move the ship forward 5 units:

Entity myShip = new Entity();
myShip.GoForward(5.0f);

To make your ship barrel roll 90 degrees

myShip.Roll(MathHelper.PiOver2);

And here's the sample code

public class Entity
{
    Vector3 position = Vector3.Zero;
    Matrix rotation = Matrix.Identity;

    public void Yaw(float amount)
    {
       rotation *= Matrix.CreateFromAxisAngle(rotation.Up, amount);
    }

    public void YawAroundWorldUp(float amount)
    {
       rotation *= Matrix.CreateRotationY(amount);
    }

    public void Pitch(float amount)
    {
       rotation *= Matrix.CreateFromAxisAngle(rotation.Right, amount);
    }

    public void Roll(float amount)
    {
       rotation *= Matrix.CreateFromAxisAngle(rotation.Forward, amount);
    }

    public void Strafe(float amount)
    {
       position += rotation.Right * amount;
    }

    public void GoForward(float amount)
    {
       position += rotation.Forward * amount;
    }

    public void Jump(float amount)
    {
       position += rotation.Up * amount;
    }

    public void Rise(float amount)
    {
       position += Vector3.Up * amount;
    }
}
Nic Foster
  • 2,864
  • 1
  • 27
  • 45