To find the angle of turn in a car racing game in C++/SFML
-
in the code below i would like to know how the logic of finding the angle at a turn works.
int main()
{
RenderWindow app(VideoMode(640, 480), "Car Racing Game!");
app.setFramerateLimit(60);Texture t1, t2; t1.loadFromFile("images/background.png"); t2.loadFromFile("images/car.png"); Sprite sBackground(t1), sCar(t2); sCar.setPosition(300, 300); sCar.setOrigin(22, 22); float x = 300, y = 300; float speed = 0, angle = 0; float maxSpeed = 12.0; float acc = 0.2, dec = 0.3; float turnSpeed = 0.08; while (app.isOpen()) { Event event; while (app.pollEvent(event)) { if (event.type == Event::Closed) app.close(); if (Keyboard::isKeyPressed(Keyboard::Escape)) app.close(); } bool Up = false, Right = false, Down = false, Left = false; if (Keyboard::isKeyPressed(Keyboard::Up)) Up = true; if (Keyboard::isKeyPressed(Keyboard::Right)) Right = true; if (Keyboard::isKeyPressed(Keyboard::Down)) Down = true; if (Keyboard::isKeyPressed(Keyboard::Left)) Left = true; // Car Movements if (Up && speed < maxSpeed) if (speed < 0) speed += dec; else speed += acc; if (Down && speed > -maxSpeed) if (speed > 0) speed -= dec; else speed -= acc; if (!Up && !Down) if (speed - dec > 0) speed -= dec; else if (speed + dec < 0) speed += dec; else speed = 0; //--------- How this logic works ? if (Right && speed != 0) angle += turnSpeed \* speed / maxSpeed; if (Left && speed != 0) angle -= turnSpeed \* speed / maxSpeed; x += sin(angle) \* speed ; y -= cos(angle) \* speed ; //------------- // // Draw app.clear(Color::White); app.draw(sBackground); sCar.setPosition(x, y); sCar.setRotation(angle \* 180 / 3.141592) ; sCar.setColor(Color::Red); app.draw(sCar); app.display(); } return 0;
}
How the below logic works ?
Quote:
if (Right && speed != 0) angle += turnSpeed * speed / maxSpeed; if (Left && speed != 0) angle -= turnSpeed * speed / maxSpeed; x += sin(angle) * speed ; y -= cos(angle) * speed ;
Thank you
-
in the code below i would like to know how the logic of finding the angle at a turn works.
int main()
{
RenderWindow app(VideoMode(640, 480), "Car Racing Game!");
app.setFramerateLimit(60);Texture t1, t2; t1.loadFromFile("images/background.png"); t2.loadFromFile("images/car.png"); Sprite sBackground(t1), sCar(t2); sCar.setPosition(300, 300); sCar.setOrigin(22, 22); float x = 300, y = 300; float speed = 0, angle = 0; float maxSpeed = 12.0; float acc = 0.2, dec = 0.3; float turnSpeed = 0.08; while (app.isOpen()) { Event event; while (app.pollEvent(event)) { if (event.type == Event::Closed) app.close(); if (Keyboard::isKeyPressed(Keyboard::Escape)) app.close(); } bool Up = false, Right = false, Down = false, Left = false; if (Keyboard::isKeyPressed(Keyboard::Up)) Up = true; if (Keyboard::isKeyPressed(Keyboard::Right)) Right = true; if (Keyboard::isKeyPressed(Keyboard::Down)) Down = true; if (Keyboard::isKeyPressed(Keyboard::Left)) Left = true; // Car Movements if (Up && speed < maxSpeed) if (speed < 0) speed += dec; else speed += acc; if (Down && speed > -maxSpeed) if (speed > 0) speed -= dec; else speed -= acc; if (!Up && !Down) if (speed - dec > 0) speed -= dec; else if (speed + dec < 0) speed += dec; else speed = 0; //--------- How this logic works ? if (Right && speed != 0) angle += turnSpeed \* speed / maxSpeed; if (Left && speed != 0) angle -= turnSpeed \* speed / maxSpeed; x += sin(angle) \* speed ; y -= cos(angle) \* speed ; //------------- // // Draw app.clear(Color::White); app.draw(sBackground); sCar.setPosition(x, y); sCar.setRotation(angle \* 180 / 3.141592) ; sCar.setColor(Color::Red); app.draw(sCar); app.display(); } return 0;
}
How the below logic works ?
Quote:
if (Right && speed != 0) angle += turnSpeed * speed / maxSpeed; if (Left && speed != 0) angle -= turnSpeed * speed / maxSpeed; x += sin(angle) * speed ; y -= cos(angle) * speed ;
Thank you
Consider the two chunks of code separately. The first two lines are updating angle by a fraction of turnSpeed. If speed were == maxSpeed then speed/maxSpeed = 1. So angle += turnSpeed for a right-turn. turnSpeed = 0.08 so that's in Radians which is 4.58 degrees. This means if you're turning right or left at max speed, it'll add or remove 4.58 degrees each time through. If speed is less than the max, then the angle will change by a smaller amount (a fraction of .08 radians). Try it in the debugger and see the change. The last two lines are updating the x,y location of the car using it's orientation (angle). cos(angle) is typically the x portion of an angle so I'm not sure why it modifies the y coor with that but that's what it does. sin(angle) or cos(angle) produces a value from 0-1 (which it uses as a percentage) * speed. That means x or y changes by a fraction of speed in the direction of angle. It would probably help you to print out the values or use the debugger. Hope this helps, John
-
Consider the two chunks of code separately. The first two lines are updating angle by a fraction of turnSpeed. If speed were == maxSpeed then speed/maxSpeed = 1. So angle += turnSpeed for a right-turn. turnSpeed = 0.08 so that's in Radians which is 4.58 degrees. This means if you're turning right or left at max speed, it'll add or remove 4.58 degrees each time through. If speed is less than the max, then the angle will change by a smaller amount (a fraction of .08 radians). Try it in the debugger and see the change. The last two lines are updating the x,y location of the car using it's orientation (angle). cos(angle) is typically the x portion of an angle so I'm not sure why it modifies the y coor with that but that's what it does. sin(angle) or cos(angle) produces a value from 0-1 (which it uses as a percentage) * speed. That means x or y changes by a fraction of speed in the direction of angle. It would probably help you to print out the values or use the debugger. Hope this helps, John