Jump to content
  • Advertisement
Sign in to follow this  
emforce

XNA C# model movement

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys I have been trying to figure out this problem I have been having with model movement in C# and XNA. My problem is that I can't get the model to move towards the position of the models "nose". Its a ship model and I would like to be able to move it in any direction. My thoughts were that I could do some sort of rotation to the model so that its whole matrix is rotated, like instead of just rotating the mesh around the y axis, I rotate everything around the y axis and then I would be able to move forward in that direction. But I don't really know how I would go about doing this. This is my current result from an old screenshot. it will spin round but I wont be able to move it towards the direction that it is facing. here is some source code :)
 public void PlayerInput()
        {
            KeyboardState keyboardstate = Keyboard.GetState();
            
            if (keyboardstate.IsKeyDown(Keys.W))
            {
                Ship.position.Z -= 50.0f;
                cameraPosition.Z  -= 50.0f;
                cameraLookAt.Z -= 50.0f;
                
                
                
            }
            if (keyboardstate.IsKeyDown(Keys.S))
            {
                Ship.position.Z += 50.0f;
                cameraPosition.Z += 50.0f;
                cameraLookAt.Z += 50.0f;
            }
            if (keyboardstate.IsKeyDown(Keys.A))
            {
               
               Ship.rotation.Y -= 50.0f; 
               
            }
            if (keyboardstate.IsKeyDown(Keys.D))
            {
                
                Ship.rotation.Y += 50.0f;
            }

I am using the XNA tutorial's gameobject class to create my models :)

Share this post


Link to post
Share on other sites
Advertisement
You should store a "forward" vector for the ship and rotate that vector in the same way you do the ship. Then, to move the ship forward you can do: ShipPos += ForwardDir;

Share this post


Link to post
Share on other sites
I tried that but then my ship seems to accelerate out of view in about a second.

This is my new code:


public void PlayerInput()
{
KeyboardState keyboardstate = Keyboard.GetState();

if (keyboardstate.IsKeyDown(Keys.W))
{
Ship.velocity.Z -= 5.0f;
Ship.position.Z -= 5.0f;
cameraPosition.Z -= 50.0f;
cameraLookAt.Z -= 50.0f;




}
if (keyboardstate.IsKeyDown(Keys.S))
{
Ship.position.Z += 50.0f;
cameraPosition.Z += 50.0f;
cameraLookAt.Z += 50.0f;
}
if (keyboardstate.IsKeyDown(Keys.A))
{

Ship.rotation.Y -= 50.0f;

}
if (keyboardstate.IsKeyDown(Keys.D))
{

Ship.rotation.Y += 50.0f;
}
UpdateCamera();

Ship.position = Ship.position + Ship.velocity;
}

Share this post


Link to post
Share on other sites
That's not quite what I meant.

The forward vector is normalised and merely points in the direction the ship is facing. To make it move in that direction you could do this:


if (keyboardstate.IsKeyDown(Keys.W))
{
Ship.Position += Ship.Forward;

}



Or if you want to use your velocity method:



Ship.Velocity = Vector2.Zero;


if (keyboardstate.IsKeyDown(Keys.W))
{
Ship.Velocity = Ship.Forward * Ship.Speed;

}

Ship.Position += Velocity;



The reason your ship moves straight out of the view is because
1) You aren't resetting the velocity back to zero each frame and redefining it based on key presses
2) The key presses are adding/subtracting to the velocity so if you held a key down the ship will naturally keep accelerating.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!