Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. To find the angle of turn in a car racing game in C++/SFML

To find the angle of turn in a car racing game in C++/SFML

Scheduled Pinned Locked Moved Managed C++/CLI
c++game-devperformancequestion
3 Posts 2 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tarun Jha
    wrote on last edited by
    #1

    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

    J 1 Reply Last reply
    0
    • T Tarun Jha

      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

      J Offline
      J Offline
      John Schroedl
      wrote on last edited by
      #2

      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

      T 1 Reply Last reply
      0
      • J John Schroedl

        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

        T Offline
        T Offline
        Tarun Jha
        wrote on last edited by
        #3

        thank you

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups