- Joined
- Nov 30, 2007
- Messages
- 1,202
I'm having issues with players bouncing on the world bounds. Basically the first function "isInsideWorld" checks if a object is still inside worldBounds and returns a pointer to which side its colliding with. The second function "handleWallCollision" resolves the collision and inverts the direction that the object is moving. This works for all cases except for corners where the object simply disapears through the world bounds. And I'm not sure how to go about solving it, any pointers?
JASS:
bool isInsideWorld(struct Object* o, int *side)
{
int w = o->w / 2;
int h = o->h / 2;
int x = o->center_x;
int y = o->center_y;
int window_w = getWindowWidth();
int window_h = getWindowHeight();
int topX = x - w;
int topY = y - h;
int botX = x + w;
int botY = y + h;
bool isInside = true;
isInside = (topX > 0 && topY > 0 && botX < window_w && botY < window_h);
if (isInside == false)
{
if (x > window_w - 30) {
*side = WORLD_RIGHT;
}
else if (x < 30) {
*side = WORLD_LEFT;
}
else if (y > window_h - 40) {
*side = WORLD_BOT;
}
else if (y < 40) {
*side = WORLD_TOP;
}
}
return isInside;
}
void handleWallCollision(int i, int side)
{
if (side == WORLD_TOP) {
object[i].delta_y = object[i].delta_y *-1;
object[i].speed_y = object[i].speed_y *-1;
}
if (side == WORLD_BOT) {
object[i].delta_y = object[i].delta_y *-1;
object[i].speed_y = object[i].speed_y *-1;
}
if (side == WORLD_LEFT) {
object[i].delta_x = object[i].delta_x *-1;
object[i].speed_x = object[i].speed_x *-1;
}
if (side == WORLD_RIGHT) {
object[i].delta_x = object[i].delta_x *-1;
object[i].speed_x = object[i].speed_x *-1;
}
object_move(&object[i]);
}