Simple collision problem utilizing C++ and sfml - c++

I coded a simple program with two cubes on screen, one the user can move, the other is stationary. I just started using sfml and have never touched on collision, so this was completely new to me. In my code, I aim to have a warning window pop up when the user steers the cube into the stationary one. the problem, however, is that the warning window appears as soon as I start the program, even though It is in an if loop. Here is my code:
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <iostream>
using namespace std;
bool isCollision(int x, int y, int x2, int y2){ // borrowed function, all credits go to whom ever made it
if (abs(x2 - x) > 20 || abs(y2 - y) > 20)
return false;
else
return true;
}
int main()
{
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "My SFML Window");
sf::RenderWindow Warning(sf::VideoMode(400, 225, 32), "WARNING!");
sf::Shape Rect = sf::Shape::Rectangle(0, 0, 20, 20, sf::Color::Red);
sf::Shape Rect2 = sf::Shape::Rectangle(50, 0, 70, 20, sf::Color::Blue);
while (App.IsOpened())
{
sf::Event event;
while (App.GetEvent(event)) // I now know the shorter way to handle events, just haven't edited it yet. No functional difference
{
if (event.Type == sf::Event::Closed)
App.Close();
if ((event.Type == sf::Event::KeyPressed) && (event.Key.Code == sf::Key::Escape))
App.Close();
if ((event.Type == sf::Event::KeyPressed) && (event.Key.Code == sf::Key::Right))
Rect.Move(5.0, 0);
if ((event.Type == sf::Event::KeyPressed) && (event.Key.Code == sf::Key::Left))
Rect.Move(-5.0, 0);
if ((event.Type == sf::Event::KeyPressed) && (event.Key.Code == sf::Key::Down))
Rect.Move(0, 5.0);
if ((event.Type == sf::Event::KeyPressed) && (event.Key.Code == sf::Key::Up))
Rect.Move(0, -5.0);
}
int x = Rect.GetPosition().x;
int y = Rect.GetPosition().y;
int x2 = Rect2.GetPosition().x;
int y2 = Rect2.GetPosition().y;
isCollision(x, y, x2, y2);
if (isCollision(x, y, x2, y2) == true) // if loop that I am messing up somehow
}
Warning.Clear(sf::Color::White);
}
App.Clear();
App.Draw(Rect);
App.Draw(Rect2);
App.Display();
}
return EXIT_SUCCESS;
}
I got the bool isCollision function from a tut I was watching, but the tut was done in allegro, so I scraped from it what I could. (My logic in using his function was that our cubes are the exact same size, and identical in their properties [one moving one stationary]. The problem must lie, I assume, in how I call the function. Any and all help is greatly appreciated

In C++, it is up to the caller to collect the returned value.
isCollision(x, y, x2, y2);
Now the call to the above function, does nothing useful. You need to collect the returned value and serve it as a flag for an if condition. Or directly place the function call in the if condition itself.
if ( isCollision(x, y, x2, y2) ){
// Code here
}

i think your function isnt returning properly, you should try it like this. (FYI 2 returns is real bad practice)
bool isCollision(int x, int y, int x2, int y2){
bool exitVal; // maybe make it static if this is being called over and over
if (abs(x2 - x) > 20 || abs(y2 - y) > 20)
exitVal = false;
else
exitVal = true;
return exitVal;
}
Hope this helps :D

in your code sample:
isCollision(x, y, x2, y2);
if (isCollision(x, y, x2, y2) == true) // if loop that I am messing up somehow
}
Warning.Clear(sf::Color::White);
}
Why are you calling the function isCollision twice?
You can just do like this:
bool is_coll = isCollision(x, y, x2, y2);
if(is_coll) {
//....
}
Also the brace } after if statement look like not matched ....
moreover in the implementation of function isCollision, what is abs(x2 - x)
I feel like abs() is probably a macro, so check if the macro is defined properly or not?
Two macro calls in the if() statement can really mess things up.

I suspect that the RenderWindow constructor creates and shows the window.
You shouldn't create it (or at least delay displaying it) until there has been a collision.
I will leave it as an exercise for you to figure out how to accomplish that.

Try to use a more simple way to figure out where exactly the mistake lies. molbdnilo might be right, so instead of using the new window to check whether your collision code is correct you could do something that will definately work. I usually just output messages with cout:
#include <iostream>
if (isCollision(x, y, x2, y2) == true)
{
cout << "squares r colliding!!!";
Warning.Clear(sf::Color::White);
}
Just make sure you can see the console (sometimes you need to use debug mode for that)
Also there are no if loops ;)

Related

SFML Collisions never register in my system

Trying to make a collision system in sfml for the first time in SFML without using a tutorial, using a array-based thing like so:
bool moright, moleft, moup, xcollision, ycollision;
float xvel, yvel;
int position, predictx, predicty, cm, arraynum;
class playerClass{
public:
playerClass(){
}
void update()
{
if (moright == true){
xvel = 2;}
if (moleft == true){
xvel = -2;}
if (!(moright || moleft)){
if (xvel < 0)
xvel = 0;
if (xvel > 0)
xvel = 0;}
}
};
int main()
{
playerClass playerObject;
// Create the main window
RenderWindow window(VideoMode(1080, 720), "SFML window");
// Load a sprite to display
Texture texture;
if (!texture.loadFromFile("gsquare100x100.png"))
return EXIT_FAILURE;
Sprite sprite(texture);
Sprite floor(texture);
Sprite wall(texture);
floor.setPosition(Vector2f(0.f, 498.f));
wall.setPosition(Vector2f(598.f,0.f));
floor.setColor(Color(0, 255, 0));
floor.setScale(12.f, 12.f);
wall.setScale(12.f, 12.f);
wall.setColor(Color(0, 0, 255));
int collisions[2][4]{
{0, 400, 500, 600},
};
// Start the game loop
while (window.isOpen())
{
Vector2f position = sprite.getPosition();
cout << position.y << endl;
predictx = position.x + xvel;
predicty = position.y + yvel;
yvel = 1;
for (arraynum = 0; arraynum < 2; arraynum++){
if ((predictx > collisions[arraynum][0])&&(predictx < collisions[arraynum][1])&&(predicty > collisions[arraynum][2])&&(predicty < collisions[arraynum][3])){
if ((position.y > collisions[arraynum][3])||(position.y < collisions[arraynum][2])){
xcollision = true;}
if ((position.x > collisions[arraynum][1])||(position.x < collisions[arraynum][0])){
ycollision = true;}
}
}
if (xcollision == true)
xvel = 0;
xcollision = false;
if (ycollision == true)
yvel = 0;
ycollision = false;
sprite.move(sf::Vector2f(0.f+xvel, 0.f+yvel));
// Process events
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == Event::KeyPressed)
{if (event.key.code == Keyboard::D)
moright = true;
if (event.key.code == Keyboard::A)
moleft = true;}
if (event.type == Event::KeyReleased)
{if (event.key.code == Keyboard::D)
moright = false;
if (event.key.code == Keyboard::A)
moleft = false;}
playerObject.update();
}
However the collision never registers, removing the bit that checks from which direction the sprite is moving in from doesn't help.
Very new to c++ so apologies if this is a stupid question and for my likely overly elaborate collision system.
I've written simple collisions with SFML before, and here's my advice to you: make your code as readable as possible! Things are going to get more complicated, and you need to have a system is reusable and easy to understand.
I've read your code but I don't understand why you've used an array. I assume you're trying to check if a smaller rectangle sprite is about to exit the collisions array?
For this purpose I suggest using a FloatRect object. It has useful functions like .contains() and .intersects() that you might need in the future. One downside it that is has top and left only, and to make it more and short, we'll define a simple struct to handle that part for us, as well as work for rectangular sprites as well.
I've left comments that explain the code, but haven't tested it personally. You can do that and integrate what you've learned into your project. Good luck
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
using namespace sf;
//using a struct is not necessarily faster. BUT it does give your code more readability and is reusable for future needs
//this struct just stores a floatRect of the given sprite/Floatrecct, defining some useful functions allowing for shorter code and more readability
struct rectangularShape
{
FloatRect containingRectangle;
//constructor with sprite input
rectangularShape(Sprite sprite)
{
this -> containingRectangle = FloatRect(Vector2f(sprite.getGlobalBounds().left, sprite.getGlobalBounds().top),
Vector2f(sprite.getGlobalBounds().left + sprite.getGlobalBounds().width,sprite.getGlobalBounds().top + sprite.getGlobalBounds().height));
}
//constructor with floatrect
rectangularShape(FloatRect rect)
{
this -> containingRectangle = rect;
}
//any useful functions for rectangular shapes-- you could add more if you want
float getTop() {return containingRectangle.top;}
float getbottom() {return containingRectangle.top + containingRectangle.height;}
float getleft() {return containingRectangle.left;}
float getright() {return containingRectangle.left + containingRectangle.width;}
};
//note the use of a FloatRect instead of the arrays you were using, this just makes it easier to understand
FloatRect inclusionArea(TopLeftVector, BottomRightVector);
Sprite sprite(texture);
//declare rectangularShapes, here we check if smallRectangle is exiting it's container
rectangularShape containingRectangle(inclusionArea);
rectangularShape smallRectangle(sprite);
//alternatively you can use the sprite's next position:
/*
spriteCopy = sprite;
spriteCopy.move(deltaTime * Vector2f(xSpeed, ySpeed));
rectangularShape smallRectangle(spriteCopy);
*/
//do the check:
if (smallRectangle.getTop() < containingRectangle.getTop() or smallRectangle.getBottom() > containingRectangle.getBottom())
//exiting in Y axis
//do something;
;
if (smallRectangle.getLeft() < containingRectangle.getLeft() or smallRectangle.getRight() > containingRectangle.getRight())
//exiting in X axis
//do something;
;
I can't comment due to low reputation.
From the code presented, it's seems like you never set xcollision or ycollision to true anywhere.

Vector of unique pointers error, cannot be referenced after initialization

I'm trying to create a matrix of rectangles in SFML and have them look like a floor. To do that, I want to use a vector of unique pointers. However, there seems to be an error I don't quite understand.
After successfully initializing the vector, it is declared deleted in the very same function. When I do a similar implementation but using new or shared pointers, there are no problems.
What is causing this error, and how can I fix it? The place of occurance is visible below:
Code:
sf::Texture texture;
texture.loadFromFile("./resources/wood.jpg");
std::vector<std::unique_ptr<sf::Sprite>> floor;
unsigned counter = 0;
float posX = 0.f, posY = 0.f;
for (int i = 0; i < 50; i++) {
floor.push_back(std::make_unique<sf::Sprite>());
floor[i]->setTexture(texture);
floor[i]->setTextureRect(sf::IntRect(1, 1, 100, 100));
floor[i]->setPosition(sf::Vector2f(posX, posY));
counter++;
posX += 100.f;
if (counter == 10) {
posY += 100.f;
posX = 0.f;
counter = 0;
}
}
while (window.isOpen()) {
sf::Event eH;
for (auto &sprite : floor)
window.draw(*sprite.get());
while (window.pollEvent(eH)) {
if (eH.type == sf::Event::Closed)
window.close();
if (eH.type == sf::Event::KeyReleased && eH.key.code == sf::Keyboard::Escape)
window.close();
if (eH.type == sf::Event::Resized)
glViewport(0, 0, eH.size.width, eH.size.height);
}
window.display();
window.clear(sf::Color::Black);
Error description:
'std::unique_ptr<sf::Sprite,std::default_delete<_Ty>>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)': attempting to reference a deleted function
This line
for (auto sprite : floor)
attempts to copy each unique_ptr into sprite one by one, and unique_ptrs aren't copyable.
Use
for (auto &sprite : floor)
instead.
sf::Sprite is lightweight, you don't need to use std::unique_ptr<sf::Sprite>, just use sf::Sprite directly: std::vector<sf::Sprite> floor;
std::vector<sf::Sprite> floor;
...
floor.push_back(sf::Sprite());
floor[i].set...
...
for (auto const& sprite : floor)
window.draw(sprite);

C++ bouncing ball goes out of screen

I tried making a simple bouncing ball with constant velocity, that is supposed to bounce of the screen edges.
I have succeeded in making the ball move forward and down, but it does not stop and instead keeps moving and goes out of the screen.
Also, I just started c++ graphics yesterday and the program is quite primitive and thus I'd appreciate it if the answer could be simple.
(All Header files are included)
void main()
{
int gdriver = DETECT, gmode, xmax, ymax, Vx=10, Vy=10, Cx=150, Cy=150, t=1;
initgraph(&gdriver, &gmode, "");
xmax = getmaxx();
ymax = getmaxy();
setfillstyle(SOLID_FILL, YELLOW);
a:
while((Cx!=xmax-5 && Cx!=0) || (Cy!=ymax-5 && Cy!=0))
{
Cx += (Vx * t);
Cy += (Vy * t);
clrscr();
pieslice(Cx,Cy,0,360,10);
delay(100);
}
if (Cx==xmax-5 || Cx==0)
{
Vx = - Vx;
}
else if (Cy==ymax-5 || Cy==0)
{
Vy = - Vy;
}
goto a;
}
while(Cx!=xmax-5 || Cx!=0 || Cy!=ymax-5 || Cy!=0)
This is always true!
Whatever the value of Cx, it is always either "not xmax-5" or "not zero" (well, unless xmax-5 itself is zero, which it is probably isn't).
Similarly, your Cy conditions always hold.
So, the ball always moves.
Instead:
while ((Cx!=xmax-5 && Cx!=0) || (Cy!=ymax-5 && Cy!=0))
Now you still have the problem that the horizontal and vertical movement is checked at once, which only works if the ball hits an exact corner. But I'll leave you to work that one out on your own.

Fill the QRect on enter event

I have a task: QRect object should be painted when mouse cursor entering it.
After a couple of hours I made this.
void myObj::mouseMoveEvent(QMouseEvent* event){
int x1, y1, x2, y2;
QPoint point = event->pos();
rect->getCoords(&x1, &y1, &x2, &y2);
if((point.x() >= x1) && (point.x() <= x2) && (point.y() >= y1) && (point.y() <= y2)){
changeRectColour();
}else{
brush->setColor(Qt::green);
repaint();
}
}
myObj is inherited from QWidget.
But I think that my idea isn't efficient. Because on every mouse move outside the QRect it changes color to green(even if it's green).
Unfortunatelly, QRect hasn't enterEvent() function.
Can you, please, give an advice how to do this properly.
QWidget::repaint() means "paint now!!! I can't wait!". Use QWidget::update() instead, which will fold several paint requests into one (better explanation in the doc).
Btw you are basically reimplementing QRect::contains(). Your new code will be
void myObj::mouseMoveEvent(QMouseEvent* event){
QPoint point = event->pos();
if(rect->contains(point, true)){
changeRectColour();
}
else{
brush->setColor(Qt::green);
update();
}
}
You could create a boolean class member e.g. _lastPositionWasInsideRect, initialize that to false and program your if statement somewhat like this:
bool positionIsInsideRect = (point.x() >= x1) && (point.x() <= x2) && (point.y() >= y1) && (point.y() <= y2));
if( positionIsInsideRect && !_lastPositionWasInsideRect )
{
_lastPositionWasInsideRect = true;
// do the changes which are required when entering the rect
}
else if( !positionIsInsideRect && _lastPositionWasInsideRect )
{
_lastPositionWasInsideRect = false;
// do the changes which are required when leaving the rect
}
A much easier alternative would be to consider using the QGraphicsView framework.

OpenGL VS2008 on C++: Same code bevahes differently even on the same computer

I have two exact same codes OpenGL C++ , compiled using VS2008 in different project , but when i compile them, it behaves differently. One of them can recognize the condition if ( mod == GLUT_ACTIVE_CTRL && button == GLUT_WHEEL_UP ) and one of them is not.
Here is the complete function:
void mouse(int button, int state, int x, int y) {
int mod = glutGetModifiers();
mouseState = state;
mouseButton = button;
double modelview[16], projection[16];
int viewport[4];
float z = 0 ;
/*Read the projection, modelview and viewport matrices
using the glGet functions.*/
glGetIntegerv( GL_VIEWPORT, viewport );
glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
//glGetDoublev( GL_PROJECTION_MATRIX, projection );
//Read the window z value from the z-buffer
glReadPixels( x, viewport[3]-y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &z );
// Used for wheels, has to be up
if (state == GLUT_UP ) {
if ( mod == GLUT_ACTIVE_CTRL && button == GLUT_WHEEL_UP ){
printf("Wheel Up\n");
zoom += 0.1;
}
else if (mod == GLUT_ACTIVE_CTRL && button == GLUT_WHEEL_DOWN ){
printf("Wheel Down\n");
zoom -= 0.1;
}
else if (mod == GLUT_ACTIVE_ALT && button == GLUT_WHEEL_UP) {
//printf("Z++\n");
translation_z = translation_z + 0.1;
//printf("Z = %f", translation_z);
}
else if (mod == GLUT_ACTIVE_ALT && button == GLUT_WHEEL_DOWN) {
//printf("Z--\n");
translation_z = translation_z - 0.1;
}
else if (mod == GLUT_ACTIVE_SHIFT && button == GLUT_WHEEL_UP) {
//printf("Shift Wheel Up. Z axis rotation goes here.\n");
zrotation += (5*(z - oldZ)); // about x-axis
}
else if (mod == GLUT_ACTIVE_SHIFT && button == GLUT_WHEEL_DOWN) {
//printf("Shift Wheel Down. Z Axis rotation goes here\n");
zrotation -= (5*(z - oldZ)); // about x-axis
//translation_z = translation_z - 0.1;
}
}
else if (state == GLUT_DOWN) {
//printf("Glut Down before z processing\n");
if (button == GLUT_LEFT_BUTTON){
cursorX = x;
cursorY = y;
mode = SELECT;
//printf("Left is down\n");
}
oldX = x;
oldY = y;
}
}
`
and I uploaded both of the project here.
Both the source and the project settings appear to be exactly the same.
I can't see any added include paths though, do you have a copy of opengl locally in both projects?
Only other thing I can think of right now is this comment from your source...
//Use patched version of GLUT (http://www.realmtech.net/opengl/glut.php) in case the wheel interaction does not work.
1) This sounds like it might relate to the exact problem you are having.
2) Did you maybe use that patched version in one project, but not the other?
Edit:
In one of the projects you have the printf lines for wheel down and wheel up commented out, is this by any chance how you tell that one doesn't recognize the wheel?
I found the problem, In the project that can not recognize wheel up/down condition (despite that it should), I included:
freeglut.lib glut32.lib opengl32.lib user32.lib
This can be solved by including only the following libs:
opengl32.lib user32.lib
Thank you #melak47 for suggesting to check the project settings.