Original Post
Hello community,
Project: Pong, Lang + Library: C++, SFML
I am having a small issue with the Ball movement. It works, when a collision is detected, I negate the Xspeed or Yspeed of the ball so that it reverses direction. Currently I only do this when it hits the screen edge. as I want to understand the issue before I continue. My issue is that when Breakpoint my code (at any place), I find that the X/Y coordinates inflate to a massive size, depending on where the ball was before a break occurred. For example; If the ball is about to hit the right-edge and it breaks on hit, then cycling through the breakpoint makes it jump from 800 (Screen Edge) to 3400, which is way outside the bounds of the play area. It is also same for the Y, even if a collision doesn't occur, it's like the debugger interrupts the logic in some way.
Also, when I break and then return to the window, the sprites on the screen no longer move and nothing happens. I don't remember anything like this happening when I was using Allegro 4, is this some sort of issue with SFML (the display, not so much the logic)?
I have included the code for the Entire ball class and the portion which deals with Collision from the Game loop:
[source lang="cpp"]#ifndef BALL_H
#define BALL_H
class Ball
{
public:
Ball(sf::Vector2f position, float speed); // constructor
~Ball(); // destructor
void MoveBall(float elapsedTime); // Move the ball.
void ReverseSpeed(int colType); // Set the speed to different values depending on what has occured.
bool LoadImage(std::string filename);
void DisplayBall(sf::RenderWindow &targetWindow);
sf::Rect getArea();
private:
sf::Vector2f position_;
float speed_;
sf::Image ballImage_;
sf::Sprite ballSprite_;
bool isFileLoaded_;
float xSpeed_, ySpeed_;
};
#endif BALL_H[/source]
[source lang="cpp"]#include "stdafx.h"
#include "Ball.h"
Ball::Ball(sf::Vector2f position, float speed)
{
position_ = position;
speed_ = 35.0f;
isFileLoaded_ = false;
xSpeed_ = -65.0f;
ySpeed_ = 65.0f;
}
Ball::~Ball()
{
}
void Ball::MoveBall(float elapsedTime)
{
position_.x += (xSpeed_ * elapsedTime);
position_.y += (ySpeed_ * elapsedTime);
}
void Ball::ReverseSpeed(int colType)
{
if(colType == 1)
xSpeed_ = -xSpeed_;
else if(colType == 2)
ySpeed_ = -ySpeed_;
}
bool Ball::LoadImage(std::string filename)
{
// Load an image using the string provided.
if(!ballImage_.LoadFromFile(filename))
{
// If the file cannot be found, then the method returns
// false, could be used to load a default graphic or exit.
return false;
}
else
{
// Else, set the sprite to the image loaded.
// and set file loaded to True.
ballSprite_.SetImage(ballImage_);
// set the sprite position to that of the class position/centre.
ballSprite_.SetCenter(ballSprite_.GetSize().x/2, ballSprite_.GetSize().y/2);
ballSprite_.SetPosition(position_);
// set the file loaded boolean to true, for other functions to use.
isFileLoaded_ = true;
// Return successful.
return true;
}
}
void Ball::DisplayBall(sf::RenderWindow &targetWindow)
{
if(isFileLoaded_)
{
ballSprite_.SetPosition(position_.x, position_.y);
sf::Shape rect = sf::Shape::Rectangle(position_, sf::Vector2f(position_.x + 32, position_.y + 32), sf::Color(255,255,0), 0, sf::Color(0,0,0));
targetWindow.Draw(rect);
targetWindow.Draw(ballSprite_);
}
}
sf::Rect Ball::getArea()
{
/*sf::Shape colRect = sf::Shape::Rectangle(position_.x - (ballSprite_.GetSize().x/2),
position_.y - (ballSprite_.GetSize().y/2),
position_.x + (ballSprite_.GetSize().x/2),
position_.y + (ballSprite_.GetSize().y/2),
sf::Color(0, 0, 0),
0,
sf::Color(0, 0, 0));*/
sf::Rect colRect(position_.x - (ballSprite_.GetSize().x/2),
position_.y - (ballSprite_.GetSize().y/2),
position_.x + (ballSprite_.GetSize().x/2),
position_.y + (ballSprite_.GetSize().y/2));
return colRect;
}[/source]
[source lang="cpp"]//TODO: collision does not work for negative values.
if(ball_->getArea().Left <= 0 || ball_->getArea().Right >= 800)
{
ball_->ReverseSpeed(1);
}
else if(ball_->getArea().Top <= 0 || ball_->getArea().Bottom >= 600)
{
ball_->ReverseSpeed(2);
}
ball_->MoveBall(gameWindow_.GetFrameTime());[/source]
Please ignore some of the hard-coded values for collision/the fact ball handles drawing, it's stuff I intend to work on once I get the basic game down.
Regards,
Stitchs.
Project: Pong, Lang + Library: C++, SFML
I am having a small issue with the Ball movement. It works, when a collision is detected, I negate the Xspeed or Yspeed of the ball so that it reverses direction. Currently I only do this when it hits the screen edge. as I want to understand the issue before I continue. My issue is that when Breakpoint my code (at any place), I find that the X/Y coordinates inflate to a massive size, depending on where the ball was before a break occurred. For example; If the ball is about to hit the right-edge and it breaks on hit, then cycling through the breakpoint makes it jump from 800 (Screen Edge) to 3400, which is way outside the bounds of the play area. It is also same for the Y, even if a collision doesn't occur, it's like the debugger interrupts the logic in some way.
Also, when I break and then return to the window, the sprites on the screen no longer move and nothing happens. I don't remember anything like this happening when I was using Allegro 4, is this some sort of issue with SFML (the display, not so much the logic)?
I have included the code for the Entire ball class and the portion which deals with Collision from the Game loop:
[source lang="cpp"]#ifndef BALL_H
#define BALL_H
class Ball
{
public:
Ball(sf::Vector2f position, float speed); // constructor
~Ball(); // destructor
void MoveBall(float elapsedTime); // Move the ball.
void ReverseSpeed(int colType); // Set the speed to different values depending on what has occured.
bool LoadImage(std::string filename);
void DisplayBall(sf::RenderWindow &targetWindow);
sf::Rect
private:
sf::Vector2f position_;
float speed_;
sf::Image ballImage_;
sf::Sprite ballSprite_;
bool isFileLoaded_;
float xSpeed_, ySpeed_;
};
#endif BALL_H[/source]
[source lang="cpp"]#include "stdafx.h"
#include "Ball.h"
Ball::Ball(sf::Vector2f position, float speed)
{
position_ = position;
speed_ = 35.0f;
isFileLoaded_ = false;
xSpeed_ = -65.0f;
ySpeed_ = 65.0f;
}
Ball::~Ball()
{
}
void Ball::MoveBall(float elapsedTime)
{
position_.x += (xSpeed_ * elapsedTime);
position_.y += (ySpeed_ * elapsedTime);
}
void Ball::ReverseSpeed(int colType)
{
if(colType == 1)
xSpeed_ = -xSpeed_;
else if(colType == 2)
ySpeed_ = -ySpeed_;
}
bool Ball::LoadImage(std::string filename)
{
// Load an image using the string provided.
if(!ballImage_.LoadFromFile(filename))
{
// If the file cannot be found, then the method returns
// false, could be used to load a default graphic or exit.
return false;
}
else
{
// Else, set the sprite to the image loaded.
// and set file loaded to True.
ballSprite_.SetImage(ballImage_);
// set the sprite position to that of the class position/centre.
ballSprite_.SetCenter(ballSprite_.GetSize().x/2, ballSprite_.GetSize().y/2);
ballSprite_.SetPosition(position_);
// set the file loaded boolean to true, for other functions to use.
isFileLoaded_ = true;
// Return successful.
return true;
}
}
void Ball::DisplayBall(sf::RenderWindow &targetWindow)
{
if(isFileLoaded_)
{
ballSprite_.SetPosition(position_.x, position_.y);
sf::Shape rect = sf::Shape::Rectangle(position_, sf::Vector2f(position_.x + 32, position_.y + 32), sf::Color(255,255,0), 0, sf::Color(0,0,0));
targetWindow.Draw(rect);
targetWindow.Draw(ballSprite_);
}
}
sf::Rect
{
/*sf::Shape colRect = sf::Shape::Rectangle(position_.x - (ballSprite_.GetSize().x/2),
position_.y - (ballSprite_.GetSize().y/2),
position_.x + (ballSprite_.GetSize().x/2),
position_.y + (ballSprite_.GetSize().y/2),
sf::Color(0, 0, 0),
0,
sf::Color(0, 0, 0));*/
sf::Rect
position_.y - (ballSprite_.GetSize().y/2),
position_.x + (ballSprite_.GetSize().x/2),
position_.y + (ballSprite_.GetSize().y/2));
return colRect;
}[/source]
[source lang="cpp"]//TODO: collision does not work for negative values.
if(ball_->getArea().Left <= 0 || ball_->getArea().Right >= 800)
{
ball_->ReverseSpeed(1);
}
else if(ball_->getArea().Top <= 0 || ball_->getArea().Bottom >= 600)
{
ball_->ReverseSpeed(2);
}
ball_->MoveBall(gameWindow_.GetFrameTime());[/source]
Please ignore some of the hard-coded values for collision/the fact ball handles drawing, it's stuff I intend to work on once I get the basic game down.
Regards,
Stitchs.