I am writing a RPG game similar to the style of Pokemon (top down view). I am now working on the issue of collision detection. I am wanting to create the collision detection b
If you want to detect collision between two rectangles, there is an example I used with sprites.
public class TestSpriteOne extends Sprite{
private Rectangle rectangle;
Add in the constructor.
rectangle = new Rectangle(getX(), getY(), getWidth(), getHeight());
In update Method
rectangle.setPosition(getX(), getY());
Is a new nethod
public Rectangle getColliderActor(){
return this.rectangle;
}
Other classes
public class TestSpriteTwo extends Sprite{
private Rectangle rectangle;
In the constructor.
rectangle = new Rectangle(getX(), getY(), getWidth(), getHeight());
In update method
rectangle.setPosition(getX(), getY());
In new method
public Rectangle getColliderActor(){
return this.rectangle;
}
// Call in the TestSpriteOne class
boolean hasCollided = TestSpriteTwo.getColliderActor().overlaps(getColliderActor());
//Call in the GameScreen class
boolean hasCollided = TestSpriteOne.getColliderActor().overlaps(TestSpriteTwo.getColliderActor());
It overlaps whether this rectangle overlaps the other rectangle, so hasCollided variable will become true.
Edit: if the animation changes its width or height, you could resize the rectangle in the update method
rectangle.setSize (width, height);