Jump to content
  • Advertisement
Sign in to follow this  
WaZz86

XNA 4.0 2D car simulation

This topic is 2237 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

Hello,

I am currently working on a XNA 4.0 2D game project where you the player would be able to get into a car and drive around in the little window for now. My problem with this is that i cant get the car to move forward in the direction it is facing after being rotated. So far it just goes up the Y axis or the wrong angle. i will post what i am working with in code for this functionality.


//////////////////////////////////// Game1.cs //////////////////////////////////////////////////////////////////////

myCar.vec2Position.X += (float)(Math.Cos(mCarRotation));
myCar.vec2Position.Y += (float)(Math.Sin(mCarRotation));
myCar.Rotation = mCarRotation + (float)(Math.PI / 2);
rotation += .22f / 2;
if (leftKey)
{
if (PlayersDriving != true)
{
myPlayer.Location += new Vector2(-2, 0);
}
else
{
mCarRotation -= (float)(1 * 3.0f * gameTime.ElapsedGameTime.TotalSeconds);
}
}
if (rightKey)
{
if (PlayersDriving != true)
{
myPlayer.Location += new Vector2(2, 0);
}
else
{
mCarRotation += (float)(1 * 3.0f * gameTime.ElapsedGameTime.TotalSeconds);
}
}



/////////////////////////////////// AnitmatedSprites.cs /////////////////////////////////////////

void UpdateRotation()
{
//rotation += .2f / 2;
if (bRotateByPosition)
{
Rotation = (float)Math.Atan2(vec2Position.Y - vec2LastPosition.Y, vec2Position.X - vec2LastPosition.X);
}
}
public void MoveBy(int x, int y)
{
vec2LastPosition = vec2Position;
vec2Position.X += x;
vec2Position.Y += y;
UpdateRotation();
}



I got some of that code from some tutorial i was learning off of online.

So what i need help with is having the car move forward in the direction it is facing.

P.S my math isnt that great i've taken trigonometry and calculus but i'm a bit rusty since then..

Thanks to those who help.

Share this post


Link to post
Share on other sites
Advertisement
Anyone know a formula or something i could use for this??? making the sprite move forward in the direction it has rotated to in a 2D world.

Share this post


Link to post
Share on other sites
You need to calculate your forward facing vector. Right now you are just moving based upon arbitrary x and y inputs, without taking your current rotation into account.

First, revisit 2D movement mathematics. If you want mildly realistic car movement (acceleration/deceleration, max speed, brake power), you will need more than this, though. If you just want super basic movement, its not that big of a deal, but you can use this as a learning exercise.

This is the first Google result looking up 2D movement. Might be what you need.

Share this post


Link to post
Share on other sites
I finally got it to work the way i intended it to. with the help of my professor

public float mCarRotation = 0; // Used to rotate car sprite
public float mPlayerRotation = 0; // used to rotate player sprite
public bool PlayersDriving = false; // to determine whether the player is drving or not
public int InorOut; // to determine whether the player is in the car or not
public bool PlayersCoasting; // to determine whether the player has let go of the gas pedal
bool Driving; // to know if the car is moving forward
bool Reversing; //to know if the car is reversing
float CarsAcceleration; // used to simulated the press of a gas pedal
float minAcceleration = 0.2f; // the minimun speed
float maxAcceleration = 2.2f; // the maximun speed
float CarsReverse; // used to simulated the press of a gas pedal
float minReverse = -0.2f; // the minimun speed
float maxReverse = -1.0f; // the maximun speed

protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();

var newState = Keyboard.GetState();
KeyboardState keyState = Keyboard.GetState();
myPlayer.Rotation = mPlayerRotation + (float)(Math.PI / 2);
myCar.Rotation = mCarRotation + (float)(Math.PI / 2);
myPlayer.Update(gameTime);
myCar.Update(gameTime);
bool leftKey = keyState.IsKeyDown(Keys.Left);
bool rightKey = keyState.IsKeyDown(Keys.Right);
bool upKey = keyState.IsKeyDown(Keys.Up);
bool upKeyup = keyState.IsKeyUp(Keys.Up);
bool downKey = keyState.IsKeyDown(Keys.Down);
// TODO: Add your update logic here
if (upKey)
{
if (PlayersDriving == false)
{
//PlayerMove = 0;
//PlayerMove += (int)(90 * gameTime.ElapsedGameTime.TotalSeconds);
myPlayer.MoveBy(0, -2); //= new Vector2(0, 1);
}
else
{
minAcceleration += 0.10f;
CarsAcceleration = minAcceleration;
if (minAcceleration >= maxAcceleration)
{
Reversing = false;
Driving = true;
PlayersCoasting = true;
CarsAcceleration = maxAcceleration;
CarsAcceleration += (int)(100 * gameTime.ElapsedGameTime.TotalSeconds);
}
}
}
if (downKey)
{
if (PlayersDriving == false)
{
//PlayerMove = 0;
//PlayerMove -= (int)(90 * gameTime.ElapsedGameTime.TotalSeconds);
myPlayer.MoveBy(0, 2);
}
else
{
if (CarsAcceleration <= 0)
{
minReverse -= 0.05f;
CarsReverse = minReverse;
if (minReverse <= maxReverse)
{
Driving = false;
Reversing = true;
CarsReverse = maxReverse;
CarsReverse -= (int)(105 * gameTime.ElapsedGameTime.TotalSeconds);
}
}
}
}

//////////////////////////////////////////////////Left Keys//////////////////////////////////////////////////////////////
if ((leftKey) || (rightKey))
{
if (PlayersDriving == false && leftKey)
{
mPlayerRotation = (myPlayer.vec2Position.X - myPlayer.vec2Position.Y);
myPlayer.MoveBy(-2, 0);
}
if (PlayersDriving == true && upKey && leftKey || PlayersDriving == true && downKey && rightKey)
{
mCarRotation -= (float)(1 * 2.7f * gameTime.ElapsedGameTime.TotalSeconds);
}
if (PlayersDriving == true && PlayersCoasting == true && leftKey)
{
if (upKey == false)
mCarRotation -= (float)(1 * 2.0f * gameTime.ElapsedGameTime.TotalSeconds);
}
}
///////////////////////////////////////////////// Right Keys ///////////////////////////////////////////////////////////////////
if ((rightKey) || (leftKey))
{
if (PlayersDriving == false && rightKey)
{
//mPlayerRotation += ( myPlayer.vec2Position.X - myPlayer.vec2Position.Y);
myPlayer.MoveBy(2, 0);
}
if (PlayersDriving == true && upKey && rightKey || PlayersDriving == true && downKey && leftKey)
{
mCarRotation += (float)(1 * 2.7f * gameTime.ElapsedGameTime.TotalSeconds);
}
if (PlayersDriving == true && PlayersCoasting == true && rightKey)
{
if (upKey == false)
mCarRotation += (float)(1 * 2.0f * gameTime.ElapsedGameTime.TotalSeconds);
}
}
if (upKeyup)
{
minAcceleration = 0.2f;
}
if (keyState.IsKeyDown(Keys.Space))
{
CarsAcceleration *= 0.910f;
PlayersCoasting = false;
}
if (myPlayer.BoundingBoxRec.Intersects(myCar.BoundingBoxRec)
&& newState.IsKeyDown(Keys.E) && !oldState.IsKeyDown(Keys.E))
{
InorOut += 1;
if (InorOut == 1)
{
PlayersDriving = true;
}
if (InorOut == 2)
{
PlayersDriving = false;
InorOut = 0;
}
}
if (PlayersDriving == true)
{
myPlayer.Position = myCar.Position;
}
if (Driving == true)
{
myCar.vec2Position.X += (float)(CarsAcceleration * Math.Cos(mCarRotation));
myCar.vec2Position.Y += (float)(CarsAcceleration * Math.Sin(mCarRotation));
if (CarsAcceleration <= 0.99f)
{
PlayersCoasting = false;
CarsAcceleration = 0;
}
}
if (Reversing == true)
{
myCar.vec2Position.X += (float)(CarsReverse * Math.Cos(mCarRotation));
myCar.vec2Position.Y += (float)(CarsReverse * Math.Sin(mCarRotation));
if (CarsReverse >= -0.99f)
{
PlayersCoasting = false;
CarsReverse = 0;
}
}
myPlayer.Location = myPlayer.vec2Position;
myCar.Location = myCar.vec2Position;
//PlayerMove *= .9f;
CarsAcceleration *= 0.990f;
CarsReverse *= -0.990f;
oldState = newState;

base.Update(gameTime);
}


P.S this is not a class project....

Share this post


Link to post
Share on other sites
I decided to post the working code for all of those who need something just like this for their game or project.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!