Followers 0

Whats wrong with my Collision function?

3 posts in this topic

void Move(){

//Check if there is collision
if(!(box.x >= SCREEN_W) && box.y >= SCREEN_H) {

xDir = '+';
yDir = '-';
}

if(!(box.y >= SCREEN_H) && box.x >= SCREEN_W) {

xDir = '-';
yDir = '+';
}

if(!(box.x <= 0) && box.y <= 0){

xDir = '-';
yDir = '+';
}

if(!(box.y <= 0) && box.x <= 0){

xDir = '-';
yDir = '+';
}

//Move
if(xDir == '+'){

box.x++;
}

else if(xDir == '-'){

box.x--;
}

if(yDir =='+'){

box.y++;
}

else if(yDir == '-'){

box.y--;
}

}

This is supposed to make a square bounce around the window. The first time it hits the bottom it works, then it goes right through.

Edited by Mathew Bergen
1

Share on other sites

Another important note, you are not considering the size of the object.

If x is the coord of the bottom-left a box, to check for collisions with the right part or the top part of the box you need to add the box size to it.

1

Create an account

Register a new account