SFML community forums
Help => Graphics => Topic started by: Samyboy on February 01, 2010, 09:32:39 pm
-
Hello,
I am currently trying to write my second game using SFML (my first one was a simple chess -> no action ;)).
Well, I have walked into a problem, that I've tried to solve for a quite a while now.
I am trying to move my Player, once the User presses the Left key. Well, the moving actually does work (God bless SFML!), but my problem is, that my movement is not smooth at all, I think I am doing something very wrong!
Here is the piece of my code that handles the movement:
if (App.GetInput().IsKeyDown(sf::Key::Left)) m_sprite.Move(6, 0);
Is there anyway to move my Player exactly that fast, but smooth?
Thanks for your time!
-
Yeah, well, action games are all about physics :lol:
Instead of changing position with a constant value, use a variable for velocity, and update your position later with it:
Something like:
game loop:
if (leftKeyDown && velocity < maxVel) // max vel is something like 6
velocity += acceleration; // something like 0.1
sprite.Move( velocity, 0 );
-
You need something called Acceleration and Speed (or Velocity) :-)
Every step you should do this:
// Edit: 02/02/2010
accelerationV = 0; // If no key is pressed, we won't accelerate
if KeyUp pressed, accelerationV = 6; // A constant value
else if KeyDown pressed, accelerationV = -6;
speedV += accelerationV;
if(speedV > maxSpeedV) speedV = maxSpeedV;
else if(speedV < -maxSpeedV) speedV = -maxSpeedV;
sprite.x += speedV;
As you see, when you move when your acceleration is different than zero. Now you can use 2D vectors and so!
-
Thanks for you fast answers!
I've tried to implement velocity with an acceleration of 0.3, but it still doesn't work as I want it to:
- The player is still somewhat not smooth when velocity reaches 6
- The player is not smooth when changing his direction to move (like from right to left)
// MOVEMENT
if (theWindow.GetInput().IsKeyDown(sf::Key::Left)) {
if (velocity > -6)
velocity -= acceleration;
player.Move(velocity, 0);
}
if (theWindow.GetInput().IsKeyDown(sf::Key::Right)) {
if (velocity < 6)
velocity += acceleration;
player.Move(velocity, 0);
}
I really appreciate your help!
-
Instead of
if (velocity > -6) velocity -= acceleration;
try
velocity -= acceleration; if(velocity < -6) velocity = -6;
Now maybe you want some kind of friction in velocity. Just make your velocity decrease (or increase) to zero every step, no matter you accelerate or not.
-
Where are you updating the player's position?
It's not smooth if you update it on the Event-loop:
while (Window.GetEvent(Event)) {
// Updating the position here
}
Instead try updating it at the Window-Opened-loop:
// Start game loop
while (Window.IsOpened()) {
// Update position here.
}
-
Still not working correctly -.-
Could someone please write me a simple program with a smooth moving rectangle, that the user can move?
Would be really appreciated...
-
This works perfect for me:
#include <SFML/Graphics.hpp>
int main()
{
// Create the main rendering window
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Graphics");
sf::Shape Rectangle = sf::Shape::Rectangle(150, 150, 300, 300, sf::Color::Blue, 1.0f, sf::Color::White);
// Start game loop
while (App.IsOpened())
{
// Process events
sf::Event Event;
while (App.GetEvent(Event))
{
// Close window : exit
if (Event.Type == sf::Event::Closed)
App.Close();
// A key has been pressed
if (Event.Type == sf::Event::KeyPressed)
{
// Escape key : exit
if (Event.Key.Code == sf::Key::Escape)
App.Close();
}
}
// Clear the screen
App.Clear(sf::Color(0, 0, 0));
if (App.GetInput().IsKeyDown(sf::Key::Left)) {
Rectangle.Move(150 * App.GetFrameTime() * -1, 0);
} else if (App.GetInput().IsKeyDown(sf::Key::Right)) {
Rectangle.Move(150 * App.GetFrameTime(), 0);
}
App.Draw(Rectangle);
// Display window contents on screen
App.Display();
}
return EXIT_SUCCESS;
}
Don't know what you really mean with 'Smooth', but it is.
-
This works perfect for me:
#include <SFML/Graphics.hpp>
int main()
{
// Create the main rendering window
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Graphics");
sf::Shape Rectangle = sf::Shape::Rectangle(150, 150, 300, 300, sf::Color::Blue, 1.0f, sf::Color::White);
// Start game loop
while (App.IsOpened())
{
// Process events
sf::Event Event;
while (App.GetEvent(Event))
{
// Close window : exit
if (Event.Type == sf::Event::Closed)
App.Close();
// A key has been pressed
if (Event.Type == sf::Event::KeyPressed)
{
// Escape key : exit
if (Event.Key.Code == sf::Key::Escape)
App.Close();
}
}
// Clear the screen
App.Clear(sf::Color(0, 0, 0));
if (App.GetInput().IsKeyDown(sf::Key::Left)) {
Rectangle.Move(150 * App.GetFrameTime() * -1, 0);
} else if (App.GetInput().IsKeyDown(sf::Key::Right)) {
Rectangle.Move(150 * App.GetFrameTime(), 0);
}
App.Draw(Rectangle);
// Display window contents on screen
App.Display();
}
return EXIT_SUCCESS;
}
Don't know what you really mean with 'Smooth', but it is.
Thank you very much!