Hello everybody,
I am making a 2d car race game and I am a bit stuck in steering the car.
Since now I have a scrolling race track and the car.
You can move the car forward by pressing W and the car will start gaining speed and if you release W the car will slow down till it stops moving. You can move it backward by pressing S and on the same principle like moving forward. The car will move back gaining speed when the button is pressed and will stop slowly when is released.
I have problems implementing the right and left steering.. I think there is involved some trigonometry but I have no ideea how to start.
Here is my update() function:
void update (sf::Time dt)
{
//score
output.score += dt.asSeconds();
output.print_score.setString(to_string((int)output.score));
//movement of the player
sf::Vector2f velocity;
//forwards and backwards
if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
player.speed += 1.5;
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
player.speed -= 0.5;
else if(player.speed < 0)
{
do
player.speed += 0.5;
while(player.speed == 0);
}
else if(player.speed > 0)
{
do
player.speed -= 0.5;
while(player.speed == 0);
}
if(player.speed > 400)
player.speed = 400;
if(player.speed < -300)
player.speed = -300;
//right and left. Here are my problems..
if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
velocity.x = -player.speed * dt.asSeconds();;
if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
velocity.x = player.speed * dt.asSeconds();
velocity.y = -player.speed * dt.asSeconds();
player.spr.move(velocity);
//keeping the car on the road
if(player.spr.getPosition().x > bg[0].tex.getSize().x - 200 - player.spr.getTexture()->getSize().x)
player.spr.setPosition(bg[0].tex.getSize().x - 200 - player.spr.getTexture()->getSize().x, player.spr.getPosition().y);
if(player.spr.getPosition().x < bg[0].tex.getSize().x - 600)
player.spr.setPosition(bg[0].tex.getSize().x - 600, player.spr.getPosition().y);
if(player.spr.getPosition().y + player.tex.getSize().y > 600)
player.spr.setPosition(player.spr.getPosition().x, 600 - player.tex.getSize().y);
}
I dont want ready code. Just some information about steering in 2d.
The game can be downloaded here:https://www.dropbox.com/s/txo3wz5ph2z45n4/game.rar
[attachment=20438:Capture.PNG]