I have finally gotten collision detection working by using intersects() between 2 rectangles, and it seems to be working. However the player is just getting stuck in the rectangle and can't move. So I am now trying to check collision before the player moves.
Here is what I have tried:
if(up == true){
Rectangle futurerect = new Rectangle(px,py-=5,81,150);
if(!futurerect.intersects(wallexample)){
py-=5;
repaint();
}
}
if(down == true){
Rectangle futurerect = new Rectangle(px,py+=5,81,150);
if(!futurerect.intersects(wallexample)){
py+=5;
repaint();
}
}
if(left == true){
Rectangle futurerect = new Rectangle(px-=5,py,81,150);
if(!futurerect.intersects(wallexample)){
px-=5;
repaint();
}
}
if(right == true){
Rectangle futurerect = new Rectangle(px+=5,py,81,150);
if(!futurerect.intersects(wallexample)){
px+=5;
repaint();
}
}
I just create a new rectangle but at where it would be if the player moved, and check if it collides. If it does, don't move.
The problem is, when the player moves into the rectangle, it just slows down. It still moves through the wall but just moves at a slower pace for some reason.
What is causing this issue?