hi guys, i am doing a game where i have to detect whether my sprite collides with the wall horizontally or vertically, so in that case reverse the velocity of x or the velocity in y or both in some cases.
what i have done is that with an array of rectangles created the walls and with another rectangle around the sprite i am able to detect whether is colliding or not. but i dont know how to code whether is colliding horizontally or vertically so that i can change the velocities.
this is what i have :
the variable rectangle is the rectangle that surrounds the sprite.
public boolean rectcollision(){
boolean collision = false;
for (int i = 0; i < Game.m.rect.length; i++)
{
Rectangle r=Game.m.rect[i];
if(r.intersects(rectangle))
{
System.out.println("colliding");
collision = true;
}
}
return collision;
}
it loops through all the walls that are in the map and checks if the sprite collides with a wall.
i would really appreciate if someone can help me out. i am hoping i explained myself clear. thanks in advance