Libgdx - Trying To Implement Collision Between 2 Sprites
what the title says basically Bit of background about the current game: Player is always moving from left to right, if the screen is tapped he jumps if it's dragged he dashes forwa
Solution 1:
The constructor of the Rectangle
class requires x, y, width, height
But you are supplying coordinates of opposite corners to it.
Replace
// Rectangle of Player
playerRect = newRectangle(player.getxPos(), player.getyPos(), player
.getSprite().getWidth() + player.getxPos(), player.getSprite()
.getHeight() + player.getyPos());
// Rectangle of Platform
platformRect = newRectangle(getxPos(), getyPos(), getSprite().getWidth()
+ getxPos(), getSprite().getHeight() + getxPos());
with
// Rectangle of Player
playerRect = newRectangle(player.getxPos(), player.getyPos(),
player.getSprite().getWidth(),
player.getSprite().getHeight());
// Rectangle of Platform
platformRect = newRectangle(getxPos(), getyPos(),
getSprite().getWidth(), getSprite().getHeight());
Hope this helps.
Solution 2:
is what you can see if the rectangles are where you think and if the player stops by colliding or otherwise try something, I put it in update but you can put anywhere else draw ect.
I hope you understand.
Variable class.
privateShapeRenderersRDebugRectangelPlayer=newShapeRenderer();
privateShapeRenderersRDebugRectangelPlatform=newShapeRenderer();
privateRectangleplayerRect=newRectangle();
privateRectangleplatformRect=newRectangle();
Your method
publicPlatform(Sprite spr) {
super(spr);
player = Player.getInstance(null); // Initialises the Player class (a Singleton)// Set position of sprite
setxPos(getWidth() - 400);
setyPos(getHeight() / 2 - 100);
spr.setX(getxPos());
spr.setY(getyPos());
//add// Rectangle of Player
playerRect = newRectangle(player.getxPos(), player.getyPos(),
player.getSprite().getWidth(),
player.getSprite().getHeight());
// Rectangle of Platform
platformRect = newRectangle(getxPos(), getyPos(),
getSprite().getWidth(),
getSprite().getHeight());
}
Your method
publicvoidupdate() {
playerRect.setPosition(player.getxPos(), player.getyPos());
platformRect.setPosition(getxPos(), getyPos());
// Make Player stop moving if the two rectangles collide
isOverlapping = playerRect.overlaps(platformRect);
if (isOverlapping) {
player.setxSpeed(0);
}
}
//add for test in update or draw
sRDebugRectangelPlayer.begin(ShapeType.Filled);
sRDebugRectangelPlayer.identity();
sRDebugRectangelPlayer.rect(player.getxPos(), player.getyPos(),
player.getSprite().getWidth(),
player.getSprite().getHeight());
sRDebugRectangelPlayer.end();
//add for test update or draw
sRDebugRectangelPlatform.begin(ShapeType.Filled);
sRDebugRectangelPlatform.identity();
sRDebugRectangelPlatform.rect(getxPos(), getyPos(),
getSprite().getWidth(),
getSprite().getHeight());
sRDebugRectangelPlatform.end();
Post a Comment for "Libgdx - Trying To Implement Collision Between 2 Sprites"