Created
February 18, 2014 16:00
-
-
Save fallahn/9073776 to your computer and use it in GitHub Desktop.
Sample vehicle handling in SFML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/********************************************************************* | |
Example code to accompany article at: | |
http://trederia.blogspot.com/2014/02/space-racers-breakdown-part-3-vehicle.html | |
*********************************************************************/ | |
#include <SFML/System.hpp> | |
#include <SFML/Window.hpp> | |
#include <SFML/Graphics.hpp> | |
#include <Helpers.h> | |
#include <sstream> | |
int main() | |
{ | |
sf::RenderWindow renderWindow(sf::VideoMode(800u, 600u), "Vehicle Handling"); | |
sf::Clock deltaClock, frameClock; | |
//set up visual for vehicle | |
sf::RectangleShape car(sf::Vector2f(10.f, 30.f)); | |
car.setOrigin(5.f ,15.f); | |
car.setPosition(400.f, 300.f); | |
//some movement values | |
sf::Vector2f movementVec; //normal vector based on current direction | |
const sf::Vector2f forwardVec(0.f, 1.f); //normal vec pointing forward | |
float currentSpeed = 0.f; | |
//consts are units per second as each value is multiplied by frame time | |
const float acceleration = 360.f; | |
const float deceleration = 250.f; | |
const float maxSpeed = 800.f; | |
const float rotateAmount = 245.f; | |
//-----------------------------------// | |
while(renderWindow.isOpen()) | |
{ | |
//poll input | |
sf::Event event; | |
while(renderWindow.pollEvent(event)) | |
{ | |
if (event.type == sf::Event::Closed | |
|| (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)) | |
renderWindow.close(); | |
} | |
//------------------------ | |
float dt = deltaClock.restart().asSeconds(); | |
//set speed and direction from keyboard input | |
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) | |
car.rotate(-rotateAmount *dt); | |
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) | |
car.rotate(rotateAmount * dt); | |
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) | |
{ | |
if(currentSpeed < maxSpeed) | |
{ | |
currentSpeed += acceleration * dt; | |
} | |
//rotate direction vector | |
sf::Vector2f oldVec = movementVec; | |
sf::Transform t; | |
t.rotate(car.getRotation()); | |
movementVec = t.transformPoint(forwardVec); | |
//calc the dot product so any rotation reduces the | |
//current speed - gives the impression of wheel spin | |
//when accelerating after a tight turn | |
currentSpeed *= Helpers::Vectors::Dot(oldVec, movementVec); | |
} | |
else | |
{ | |
currentSpeed -= deceleration * dt; | |
if(currentSpeed < 0.f) currentSpeed = 0.f; | |
} | |
//finally, move car | |
car.move(movementVec * currentSpeed * dt); | |
//-------draw------------- | |
frameClock.restart(); | |
renderWindow.clear(); | |
renderWindow.draw(car); | |
renderWindow.display(); | |
//---------------------- | |
const float time = 1.f / frameClock.getElapsedTime().asSeconds(); | |
std::stringstream stream; | |
stream << "Current fps: " << time << std::endl; | |
renderWindow.setTitle(stream.str()); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment