Moving object with SFML - c++

I started studying SFML from the book "SFML Game Development" by Arthur Moreira. I'm trying to move a circle object on the screen using the keyboard. Here is the code that is given:
#include <SFML/Graphics.hpp>
class Game
{
public:
Game();
void run();
private:
void ProcessEvents();
void update();
void render();
void handlePlayerInput(sf::Keyboard::Key key, bool isPressed);
private:
sf::RenderWindow myWindow;
sf::CircleShape myPlayer;
bool movingLeft, movingUp, movingRight, movingDown;
};
Game::Game() : myWindow(sf::VideoMode(640, 480), "SFML Game"), myPlayer()
{
myPlayer.setRadius(40.f);
myPlayer.setFillColor(sf::Color::Red);
myPlayer.setPosition(100.f, 100.f);
}
void Game::run()
{
while (myWindow.isOpen())
{
ProcessEvents();
update();
render();
}
}
void Game::ProcessEvents()
{
sf::Event event;
while (myWindow.pollEvent(event))
{
switch(event.type)
{
case sf::Event::KeyPressed : handlePlayerInput(event.key.code, true); break;
case sf::Event::KeyReleased : handlePlayerInput(event.key.code, false); break;
case sf::Event::Closed : myWindow.close(); break;
}
}
}
void Game::handlePlayerInput(sf::Keyboard::Key key, bool isPressed)
{
if (key == sf::Keyboard::W) movingUp = isPressed;
else if (key == sf::Keyboard::S) movingDown = isPressed;
else if (key == sf::Keyboard::A) movingLeft = isPressed;
else if (key == sf::Keyboard::D) movingRight = isPressed;
}
void Game::update()
{
sf::Vector2f movement(0.f, 0.f);
if (movingUp) movement.y -= 1.f;
if (movingDown) movement.y += 1.f;
if (movingLeft) movement.x -= 1.f;
if (movingRight) movement.x += 1.f;
myPlayer.move(movement);
}
void Game::render()
{
myWindow.clear();
myWindow.draw(myPlayer);
myWindow.display();
}
int main()
{
Game game;
game.run();
return 0;
}
Here is what my problem is:
In the function update(), I'm updating the direction on which the cicle should go. But the first time when I try to move for example to left, the circle goes to right. The opposite is the same. If I try to go to right in the beginning, the circle goes to left. When I try to go Up and Down the result is the same. So, as I said, when I go to left, it goes to right. But if I press again left, the movement stops and I can move the circle correctlyto left and right. I should do the same thing to repair the direction if I want to go Up and Down. Do I have a mistake in my code?
And my second question is:
In the update function I have written the code that is in the book. But if I want to go Up, I think I should add 1 to 'y', not to substract 1. And if I want to go down, shouldn't I substract 1? I think that the update function should look like this:
void Game::update()
{
sf::Vector2f movement(0.f, 0.f);
if (movingUp) movement.y += 1.f; //Here we should add 1
if (movingDown) movement.y -= 1.f; //Here we should substract 1
if (movingLeft) movement.x -= 1.f;
if (movingRight) movement.x += 1.f;
myPlayer.move(movement);
}
Maybe it will be better for you to run the code if you are able in order to see what exactly is happening.

Your moving variables aren't initialized, so some of them may initially have a true value.
You should set them all to false in your Game constructor.

Related

Cannot get smooth movement in SFML

I searched on this topic and found that using sf::Clock to implement frame independent update(using deltaTime) can help to solve it. However even after adding it the paddle movement stutters a bit. On the other hand when I shift the entire Playing case to the event polling loop without using deltatime the game seems to run smoothly.
How do I go about using sf::Clock properly in my code and why does my game seems to run smoothly when I shift the Playing case in event pool loop without even using deltatime?
Game initialization:
void Game::start() {
if (_gameState != Uninitialized) {
return;
}
//Creating window
_mainWindow.create(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32), "Pong");
_gameState = ShowingMenu;
//Adding game entities to the manager
VisibleGameObject *paddle1 = new PlayerPaddle("Player1", 1.0f, sf::Keyboard::Key::Up, sf::Keyboard::Key::Down);
VisibleGameObject *paddle2 = new PlayerPaddle("Player2", 1.0f, sf::Keyboard::Key::W, sf::Keyboard::Key::S);
VisibleGameObject *background = new Background("Background", 0.0f);
paddle1 -> setPosition(SCREEN_WIDTH - (paddle1 -> getWidth()), 0);
paddle2->setPosition(0, 0);
manager.addObject(paddle1);
manager.addObject(paddle2);
manager.addObject(background);
//Starting Clock
deltaTime = 0.0f;
frameClock.restart();
while (!isExiting()) {
gameLoop();
}
_mainWindow.close();
}
Game Loop :
void Game::gameLoop()
{
static bool firstPass = true;
sf::Event currentEvent;
//Event loop
while(_mainWindow.pollEvent(currentEvent) || firstPass)
{
if (firstPass) {
currentEvent = sf::Event();
currentEvent.type = sf::Event::GainedFocus;
firstPass = false;
}
if (currentEvent.type == sf::Event::Closed)
{
_gameState = Exiting;
}
switch (_gameState)
{
case ShowingMenu:
{
showingMenu();
break;
}
case Paused:
{
break;
}
default:
break;
}
}
//Extracting deltaTime to update game logic
deltaTime = frameClock.restart().asSeconds();
if(_gameState == Playing)
{
manager.updateAllLayers(deltaTime);
manager.drawAllLayers(_mainWindow);
_mainWindow.display();
}
}
Paddle Update Logic:
void PlayerPaddle::update(const float & elapsedTime)
{
sf::Vector2f currentPos = getPosition();
float displacement = 0.0f;
if (sf::Keyboard::isKeyPressed(controls.up))
{
displacement = -speed * elapsedTime;
}
else if (sf::Keyboard::isKeyPressed(controls.down))
{
displacement = speed * elapsedTime;
}
if (displacement + currentPos.y < 0.0f)
{
setPosition(currentPos.x, 0.0f);
return;
}
else if (displacement + currentPos.y + getHeight() > Game::SCREEN_HEIGHT)
{
setPosition(currentPos.x, Game::SCREEN_HEIGHT - getHeight());
return;
}
setPosition(currentPos.x, currentPos.y + displacement);
}
I don't have enough reputation to post a comment so I'll have to make this an answer...
You update (and draw) every frame. Your game loop doesn't make sense if you want to implement a fixed update time step.
This is how your game loop should look like. I use your variable names for your convenience:
// ...
sf::Clock frameClock;
const sf::Time timePerFrame = sf::seconds(1.0f / 60.0f);
sf::Time timeSinceLastUpdate = sf::Time::Zero;
while (_mainWindow.isOpen()) {
timeSinceLastUpdate += frameClock.restart();
// events
{
sf::Event evt;
while (_mainWindow.pollEvent(evt)) {
//...
}
}
// update
{
while (timeSinceLastUpdate > timePerFrame) {
timeSinceLastUpdate -= timePerFrame;
manager.updateAllLayers(timePerFrame);
}
}
// render
{
_mainWindow.clear();
// ...
_mainWindow.display();
}
}
// ...
This updates your game at a fixed rate (1/60s) and renders it whenever possible (you can use setFramerateLimit() to limit the FPS; this won't affect the fixed update interval, so no problem there).
You now pass sf::Time to your manager.updateAllLayers which is used like your elapsedTime (it has functions like .asSeconds() so essentially nothing changes but you'll obviously have to adjust the values for speed)

SFML: Projectile stops

I am very new to SFML and C++. My problem is that I successfully fire projectiles towards the mouse. However as I move the mouse the projectiles also move.
EDIT:
Okay I solved my previous problem. However the projectile stops while it reaches the mouse position. How can the bullets continue to go further than its destination?
Code:
if (isFiring == true) {
sf::Vector2f startPos = sf::Vector2f(myPipe.getX(), myPipe.getY());
sf::Vector2i mousePos = sf::Mouse::getPosition(window);
sf::Vector2f endPos = sf::Vector2f(mousePos.x, mousePos.y);
double projSpeed{0.3};
//float projSpeed = projSpeed * (myPipe.getY() - mousePos.y) / 100;
Projectile newProjectile(20, startPos, endPos, projSpeed);
projVec.push_back(newProjectile);
isFiring = false;
}
for (int i = 0; i < projVec.size(); i++) {
projVec[i].fire(delta.asMilliseconds());
projVec[i].draw(window);
clock.restart();
}
fire function:
void fire( float delta) {
sf::Vector2f pos = projectile.getPosition();
float veloX = (_end.x - pos.x);
float veloY = (_end.y - pos.y);
float distance = sqrt((veloX*veloX + veloY * veloY));
float dx = veloX / distance;
float dy = veloY / distance;
projectile.move(dx * delta, dy * delta);
}
One more question, am I doing it correct with multiplying with delta? The bullets are really laggy and weird.
(Original reply:)
I don't have enough reputation to comment so I'll have to put it here...
So if I understand correctly you're saying you want to shoot the projectiles towards the mouse's position but don't want them to follow its position once fired? In this case:
I see you have the code to draw the projectiles inside that for-loop, which leads me to believe you are calling the loop continuously. But the same loop also contains the code used to shoot the projectiles, which means that you are also continuously calling this "fire" code. So you are steering the projectiles towards the current mouse position in every loop iteration which means they will always follow the current mouse position.
What you should be doing is move the code to shoot the projectiles and only call it once (you can only shoot a projectile once, right?) so a projectile's directional vector is not constantly altered, resulting in the problem you stated.
And make sure to always separate logic and rendering. Ideally you should be updating the physics (for example the projectile updating) at a fixed interval so that the game will run the same on all machines, and draw all your stuff separately.
If you do want the projectiles to always follow the mouse, you can use this (semi pseudo) code (I'm using this code in one of my games):
sf::Vector2f normalizedVector = normalize(mousePos - projectilePos);
normalizedVector.x *= speed * deltaTime;
normalizedVector.y *= speed * deltaTime;
projectile.move(normalizedVector);
(Edit:)
I made a quick example project that works. This doesn't use all of your code but hopefully gives you an idea how it can be done.
Here's a short video that shows what the code below does: https://webmshare.com/play/jQqvd
main.cpp
#include <SFML/Graphics.hpp>
#include "projectile.h"
int main() {
const sf::FloatRect viewRect(0.0f, 0.0f, 800.0f, 600.0f);
sf::RenderWindow window(sf::VideoMode(viewRect.width, viewRect.height), "Test");
window.setFramerateLimit(120);
window.setVerticalSyncEnabled(false);
window.setKeyRepeatEnabled(false);
sf::Clock deltaClock;
const sf::Time timePerFrame = sf::seconds(1.0f / 60.0f);
sf::Time timeSinceLastUpdate = sf::Time::Zero;
std::vector<Projectile> projectiles;
while (window.isOpen()) {
timeSinceLastUpdate += deltaClock.restart();
// process events
{
sf::Event evt;
while (window.pollEvent(evt)) {
switch (evt.type) {
case sf::Event::Closed: { window.close(); } break;
// shoot with left mouse button
case sf::Event::MouseButtonPressed: { switch (evt.mouseButton.button) { case sf::Mouse::Button::Left: {
const sf::Vector2f center(viewRect.left + viewRect.width / 2, viewRect.top + viewRect.height / 2);
const sf::Vector2f mousePos(window.mapPixelToCoords(sf::Mouse::getPosition(window)));
const float angle = atan2(mousePos.y - center.y, mousePos.x - center.x);
projectiles.push_back(Projectile(center, angle));
} break; default: {} break; } } break;
default: {} break;
}
}
}
// update
{
while (timeSinceLastUpdate > timePerFrame) {
timeSinceLastUpdate -= timePerFrame;
// update projectiles
{
for (std::size_t i = 0; i < projectiles.size(); ++i) {
Projectile &proj = projectiles[i];
proj.update(timePerFrame);
if (!viewRect.intersects(proj.getBoundingBox())) { proj.destroy(); }
}
projectiles.erase(std::remove_if(projectiles.begin(), projectiles.end(), [](Projectile const &p) { return p.getCanBeRemoved(); }), projectiles.end());
}
}
}
// render
{
window.clear();
for (std::size_t i = 0; i < projectiles.size(); ++i) {
window.draw(projectiles[i]);
}
window.display();
}
}
return EXIT_SUCCESS;
}
projectile.h:
#ifndef PROJECTILE_H_INCLUDED
#define PROJECTILE_H_INCLUDED
#include <SFML/Graphics.hpp>
class Projectile : public sf::Drawable {
public:
Projectile();
Projectile(const sf::Vector2f pos, const float angle);
virtual ~Projectile();
const bool &getCanBeRemoved() const;
const sf::FloatRect &getBoundingBox() const;
void destroy();
void update(const sf::Time dt);
private:
virtual void draw(sf::RenderTarget &renderTarget, sf::RenderStates renderStates) const;
bool canBeRemoved_;
sf::FloatRect boundingBox_;
float angle_;
float speed_;
sf::RectangleShape shape_;
};
#endif
projectile.cpp:
#include "projectile.h"
Projectile::Projectile() :
canBeRemoved_(true),
boundingBox_(sf::FloatRect()),
angle_(0.0f),
speed_(0.0f)
{
}
Projectile::Projectile(const sf::Vector2f pos, const float angle) {
canBeRemoved_ = false;
boundingBox_ = sf::FloatRect(pos, sf::Vector2f(10.0f, 10.0f));
angle_ = angle;
speed_ = 0.5f;
shape_.setPosition(sf::Vector2f(boundingBox_.left, boundingBox_.top));
shape_.setSize(sf::Vector2f(boundingBox_.width, boundingBox_.height));
shape_.setFillColor(sf::Color(255, 255, 255));
}
Projectile::~Projectile() {
}
const bool &Projectile::getCanBeRemoved() const {
return canBeRemoved_;
}
const sf::FloatRect &Projectile::getBoundingBox() const {
return boundingBox_;
}
void Projectile::destroy() {
canBeRemoved_ = true;
}
void Projectile::update(const sf::Time dt) {
boundingBox_.left += static_cast<float>(std::cos(angle_) * speed_ * dt.asMilliseconds());
boundingBox_.top += static_cast<float>(std::sin(angle_) * speed_ * dt.asMilliseconds());
shape_.setPosition(boundingBox_.left, boundingBox_.top);
}
void Projectile::draw(sf::RenderTarget &renderTarget, sf::RenderStates renderStates) const {
renderTarget.draw(shape_);
}

SFML Sprite isn't showing on the window but it isn't giving me any errors

I'm using SFML for creating a small game for practice, a replica of the old Tanks game I played when I was young on some type of console.
I've encountered a strage thing when I'm trying to draw the player. I'm saying strange because at first it was showing, so I don't think something is wrong with my function, but after I made a derived class for the enemy tanks and made a new file for that I had some nesting problems with linking all the files togheter(5 files, 1 cpp and 4 headers). After I figured that out, I ran into this problem and can't find any solution to it. It was working before that problem and now it doesn't anymore.
I checked the texture, if it is loading, the position I set it to, they are ok.
Here is the class for the player tank
class tank{
protected:
sf::FloatRect boundingBox;
sf::Sprite tankSprite;
bullet *mBullet;
public :
tank(); // constructor
bullet * get_ptr(){return mBullet;}
sf::FloatRect get_boundingBox(){return boundingBox;}
void setRotation(float);
void show(){game.draw(tankSprite);}
void update();
void shoot(){mBullet = new bullet(tankSprite);}
};
And this is the code in my main where I update the world and draw on my window
if(!stop)
Me.update();
if(Me.get_ptr()->isFired()==true)
Me.get_ptr()->update();
// Output on the window
game.clear();
Me.show();
if(Me.get_ptr()->isFired()==true)
Me.get_ptr()->show();
game.display();
Side note : game is the render window and is globally declared( I know it's bad practice, I'm slowing getting to ditch this bad habit)
main.cpp
#include <SFML/Graphics.hpp>
#include "Tank.h"
int main()
{
tank Me;
init();
while (game.isOpen())
{
sf::Event event;
while (game.pollEvent(event))
{
if (event.type == sf::Event::Closed)
game.close();
else if (event.type == sf::Event::KeyPressed)
{
stop=false;
switch(event.key.code)
{
case sf::Keyboard::W:
{
Me.setRotation(0);
break;
}
case sf::Keyboard::D:
{
Me.setRotation(90);
break;
}
case sf::Keyboard::S:
{
Me.setRotation(180);
break;
}
case sf::Keyboard::A:
{
Me.setRotation(270);
break;
}
case sf::Keyboard::Escape:
{
game.close();
break;
}
case sf::Keyboard::Space:
{
if(Me.get_ptr()->isFired()==false)Me.shoot();
break;
}
case sf::Keyboard::LControl:
{
stop=true;
break;
}
default:
break;
}
}
}
if(!stop)
Me.update();
if(Me.get_ptr()->isFired()==true)
Me.get_ptr()->update();
// Output on the window
game.clear();
Me.show();
if(Me.get_ptr()->isFired()==true)
Me.get_ptr()->show();
game.display();
}
return 0;
}
init.h
#include<iostream>
bool stop;
sf::RenderWindow game(sf::VideoMode(400, 400), "SFML");
sf::Texture myTankTexture;
sf::Texture bulletTexture;
void init(){
srand(time(NULL));
stop = true;
game.setVerticalSyncEnabled(true);
game.setFramerateLimit(60);
if(!myTankTexture.loadFromFile("tank.jpg"))
{
std::cout<<"eroare la textura tank"<<std::endl;
}
myTankTexture.setSmooth(true);
if(!bulletTexture.loadFromFile("bullet.jpg"))
{
std::cout<<"bullet texture error"<<std::endl;
}
bulletTexture.setSmooth(true);
}
sf::Vector2f rotationToDirection(int rotation){
sf::Vector2f dir;
switch (rotation)
{
case 0:
{
dir.x=0.0;
dir.y=-1.0;
break;
}
case 90:
{
dir.x=1.0;
dir.y=0.0;
break;
}
case 180:
{
dir.x=0.0;
dir.y=1.0;
break;
}
case 270:
{
dir.x=-1.0;
dir.y=0.0;
break;
}
}
return dir;
}
bullet.h
#include "init.h"
class bullet{
protected:
sf::Sprite bulletSprite;
sf::FloatRect boundingBox;
bool isBFired = false;
public:
bullet(sf::Sprite); // constructor
~bullet(){isBFired=false;}
sf::FloatRect get_boundingBox(){return boundingBox;}
bool isFired(){if(isBFired)return true;else return false;}
int collision();
void del(){delete this;}
void update();
void show(){game.draw(bulletSprite);}
};
bullet::bullet(sf::Sprite sprite){
isBFired = true;
bulletSprite.setTexture(bulletTexture);
bulletSprite.setOrigin(2.5,5.0);
bulletSprite.setRotation(sprite.getRotation());
bulletSprite.setPosition(sprite.getPosition().x+rotationToDirection(bulletSprite.getRotation()).x*5.0,sprite.getPosition().y+rotationToDirection(bulletSprite.getRotation()).y*5.0);
boundingBox = bulletSprite.getLocalBounds();
}
int bullet::collision(){
if(bulletSprite.getPosition().x < 0
|| bulletSprite.getPosition().x > 400
|| bulletSprite.getPosition().y > 400
|| bulletSprite.getPosition().y < 0 )return 1;
else
return 0;
}
void bullet::update(){
bulletSprite.move(rotationToDirection(bulletSprite.getRotation()).x*6.0,rotationToDirection(bulletSprite.getRotation()).y*6.0);
if(collision()==1)
delete this;
else
boundingBox = bulletSprite.getLocalBounds();
}
tank.h
#include "bullet.h"
class tank{
protected:
sf::FloatRect boundingBox;
sf::Sprite tankSprite;
bullet *mBullet;
public :
tank(); // constructor
bullet * get_ptr(){return mBullet;}
sf::FloatRect get_boundingBox(){return boundingBox;}
void setRotation(float);
void show(){game.draw(tankSprite);}
void update();
void shoot(){mBullet = new bullet(tankSprite);}
};
tank::tank(){
tankSprite.setTexture(myTankTexture);
tankSprite.setOrigin(20,20);
tankSprite.setRotation(0);
tankSprite.setPosition(200,200);
boundingBox = tankSprite.getLocalBounds();
}
void tank::update(){
tankSprite.move(rotationToDirection(tankSprite.getRotation()).x*3,rotationToDirection(tankSprite.getRotation()).y*3);
boundingBox = tankSprite.getLocalBounds();
}
void tank::setRotation(float rotation){
tankSprite.setRotation(rotation);
}
You are not setting the texture rectangle as far as I see, see if this helps you. When you set your tank sprite try :
tankSprite.setTexture(myTankTexture, true);
Passing true will reset your texture rectangle to the size of the sprite, see the SFML docs for more information.
Also, you are creating the Tank class before calling init(), myTankTexture will be empty so the tank constructor uses that, and your texture is loaded later.
As they say in your language, "multa bafta" :)

How can I set gravity using this code?

I am trying to make a game and am stuck on gravity..... In the following code a rectangle stands for a player and when I press up key it moves in y-axis but when I activate gravity on it (i.e resetting its previous position) it does not animate (i.e. It does not jumps) instead it just stays in its position. I am using SFML library of C++ and that's a game development tool. Please Help!
#include <SFML/Graphics.hpp>
int main(){
sf::RenderWindow window(sf::VideoMode(800, 600, 32), "Gravity");
sf::RectangleShape rectangle;
rectangle.setSize(sf::Vector2f(100, 100));
rectangle.setFillColor(sf::Color::Black);
rectangle.setPosition(sf::Vector2f(10, 350));
while(window.isOpen())
{
sf::Event Event;
while(window.pollEvent(Event))
{
if(Event.type == sf::Event::Closed)
{
window.close();
}
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
rectangle.move(0, -1);
}
if(rectangle.getPosition().y >= 350-1)
{
rectangle.setPosition(0, 350);
}
window.display();
window.clear(sf::Color::Cyan);
window.draw(rectangle);
}
}
Theoretically your code would work, but there's one significant problem:
Your initial position is 350.
Now your "jumping code" (which will allow the player to fly indefinitely!) triggers and your position is changed to 349.
However, your code keeping the player from dropping off the screen (y >= 350-1) essentially resolves to the check y >= 349, which will be true, so your position is permanently reset to 350.
To solve this, just remove the -1 or replace the >= operator with >.
While your approach should be working (once the fix above is applied), you should rethink your strategy and store a velocity in addition to a position. I've recently written the following example code. It's far from being perfect, but it should teach you a few basics for a jump and run game (not necessarily the only way to do such things):
Allow the player to jump.
Apply gravity.
Allow the player to determine jump height based on how long he holds down a key.
#include <SFML/Graphics.hpp>
int main(int argc, char **argv) {
sf::RenderWindow window;
sf::Event event;
sf::RectangleShape box(sf::Vector2f(32, 32));
box.setFillColor(sf::Color::White);
box.setOrigin(16, 32);
box.setPosition(320, 240);
window.create(sf::VideoMode(640, 480), "Jumping Box [cursor keys + space]");
window.setFramerateLimit(60);
window.setVerticalSyncEnabled(false);
// player position
sf::Vector2f pos(320, 240);
// player velocity (per frame)
sf::Vector2f vel(0, 0);
// gravity (per frame)
sf::Vector2f gravity(0, .5f);
// max fall velocity
const float maxfall = 5;
// run acceleration
const float runacc = .25f;
// max run velocity
const float maxrun = 2.5f;
// jump acceleration
const float jumpacc = -1;
// number of frames to accelerate in
const unsigned char jumpframes = 10;
// counts the number of frames where you can still accelerate
unsigned char jumpcounter = 0;
// inputs
bool left = false;
bool right = false;
bool jump = false;
while (window.isOpen()) {
while (window.pollEvent(event)) {
switch(event.type) {
case sf::Event::KeyPressed:
case sf::Event::KeyReleased:
switch (event.key.code) {
case sf::Keyboard::Escape:
window.close();
break;
case sf::Keyboard::Left:
left = event.type == sf::Event::KeyPressed;
break;
case sf::Keyboard::Right:
right = event.type == sf::Event::KeyPressed;
break;
case sf::Keyboard::Space:
jump = event.type == sf::Event::KeyPressed;
break;
}
break;
case sf::Event::Closed:
window.close();
break;
}
}
// logic update start
// first, apply velocities
pos += vel;
// determine whether the player is on the ground
const bool onground = pos.y >= 480;
// now update the velocity by...
// ...updating gravity
vel += gravity;
// ...capping gravity
if (vel.y > maxfall)
vel.y = maxfall;
if (left) { // running to the left
vel.x -= runacc;
}
else if (right) { // running to the right
vel.x += runacc;
}
else { // not running anymore; slowing down each frame
vel.x *= 0.9;
}
// jumping
if (jump) {
if (onground) { // on the ground
vel.y += jumpacc * 2;
jumpcounter = jumpframes;
}
else if (jumpcounter > 0) { // first few frames in the air
vel.y += jumpacc;
jumpcounter--;
}
}
else { // jump key released, stop acceleration
jumpcounter = 0;
}
// check for collision with the ground
if (pos.y > 480) {
vel.y = 0;
pos.y = 480;
}
// check for collision with the left border
if (pos.x < 16) {
vel.x = 0;
pos.x = 16;
}
else if (pos.x > 624) {
vel.x = 0;
pos.x = 624;
}
// logic update end
// update the position
box.setPosition(pos);
window.clear();
window.draw(box);
window.display();
}
return 0;
}

SFML Auto Movement

First of all, I already did ask this in SFML forums but they couldn't help, they actually copied my code and it worked perfectly, even with someone with the same OS as me (Windows 8)
I'm currently reading the SFML Game Development book, in the first chapter, when movement is introduced, I encountered some problems, I spawn a white circle in the centre of the window, now If i press any directional arrow, the circle keeps going in the other direction till I press it, and then it gets back to normal, and horizontal and vertical ones are seperate.
Example: I start "Game", press up, the circle keeps going down, i hold up, the circle stands still, I release, it keeps going down, I press down, it stops, now it responds normally to both keys, it will happen again with the right and left, it gets fixed after the initial error, but I wish to know how I can remove it
#include <SFML/Graphics.hpp>
using namespace sf;
class Game
{
public:Game();
void run();
private:
void processEvents();
void update(Time);
void render();
void handlePlayerInput(Keyboard::Key,bool);
bool mIsMovingUp, mIsMovingRight, mIsMovingLeft, mIsMovingDown;
float playerSpeed;
Time TimePerFrame;
private:
RenderWindow mWindow;
CircleShape mPlayer;
};
Game::Game():mWindow(VideoMode(640, 480), "SFML Application"),mPlayer(), playerSpeed(20.f), TimePerFrame(seconds(1.f / 60.f))
{
mPlayer.setRadius(20.f);
mPlayer.setPosition(220.f, 220.f);
mPlayer.setFillColor(Color::White);
}
void Game::handlePlayerInput(Keyboard::Key key, bool isPressed)
{
if (key == Keyboard::W || key == Keyboard::Up)
mIsMovingUp = isPressed;
else if (key == Keyboard::S || key == Keyboard::Down)
mIsMovingDown = isPressed;
else if (key == Keyboard::A || key == Keyboard::Left)
mIsMovingLeft = isPressed;
else if (key == Keyboard::D || key == Keyboard::Right)
mIsMovingRight = isPressed;
}
void Game::run()
{
Clock clock;
Time timeSinceLastUpdate = Time::Zero;
while (mWindow.isOpen())
{
processEvents();
timeSinceLastUpdate += clock.restart();
while (timeSinceLastUpdate > TimePerFrame)
{
timeSinceLastUpdate -= TimePerFrame;
processEvents();
update(TimePerFrame);
}
render();
}
}
void Game::processEvents()
{
Event event;
while (mWindow.pollEvent(event))
{
switch (event.type)
{
case Event::KeyPressed:
handlePlayerInput(event.key.code, true);
break;
case sf::Event::KeyReleased:
handlePlayerInput(event.key.code, false);
break;
case sf::Event::Closed:
mWindow.close();
break;
}
}
}
void Game::update(Time deltaTime)
{
Vector2f movement(0.f, 0.f);
if (mIsMovingUp)
movement.y -= playerSpeed;
if (mIsMovingDown)
movement.y += playerSpeed;
if (mIsMovingLeft)
movement.x -= playerSpeed;
if (mIsMovingRight)
movement.x += playerSpeed;
mPlayer.move(movement * deltaTime.asSeconds());
}
void Game::render()
{
mWindow.clear();
mWindow.draw(mPlayer);
mWindow.display();
}
int main()
{
Game game;
game.run();
}
Answer:
By default, these variables must be true:
bool mIsMovingUp, mIsMovingRight, mIsMovingLeft, mIsMovingDown;
In Game::Game(), declare them all as false. That should solve your problem.
Explanation:
In other words, when mIsMovingUp and down are mIsMovingDown true, it stays still. When you press up and let go, it makes mIsMovingUp false but leaves mIsMovingDown true, so the ball moves mIsMovingDown. After you press and let go of mIsMovingDown, it makes mIsMovingDown false, and then they are both false and movement ceases. Same for isMovingLeft and isMovingRight.