libGDX - Trying to implement collision between 2 sprites

断了今生、忘了曾经 提交于 2019-12-11 07:52:05

问题


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 forward. I added a tile (Platform object) and am now trying to make my Player stop moving if he hits it.

The Player starts on the bottom left of the screen, and the Platform is on the top right. What happens is that the Player stops when he reaches the middle of the screen, rather than the actual Platform. Can anyone have a look at my code and see if they can figure out why? Would be highly appreciated.

public class Platform extends GameObject {
private Rectangle playerRect;
private Rectangle platformRect;
private Player player;
private boolean isOverlapping;

public Platform(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()); 

}

public void update() {
// Rectangle of Player
    playerRect = new Rectangle(player.getxPos(), player.getyPos(), player
            .getSprite().getWidth() + player.getxPos(), player.getSprite()
            .getHeight() + player.getyPos());
// Rectangle of Platform
    platformRect = new Rectangle(getxPos(), getyPos(), getSprite().getWidth()
            + getxPos(), getSprite().getHeight() + getxPos());

// Make Player stop moving if the two rectangles collide
    isOverlapping = playerRect.overlaps(platformRect);
    if (isOverlapping) {
        player.setxSpeed(0);

    }
}
    }

Have been stuck on this for a while, thanks in advance for any input


回答1:


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.

   private ShapeRenderer sRDebugRectangelPlayer = new ShapeRenderer();
   private ShapeRenderer sRDebugRectangelPlatform = new ShapeRenderer();

   private Rectangle playerRect = new Rectangle();
   private Rectangle platformRect = new Rectangle();

Your method

public Platform(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 = new Rectangle(player.getxPos(), player.getyPos(),
                           player.getSprite().getWidth(),
                           player.getSprite().getHeight());

// Rectangle of Platform
platformRect = new Rectangle(getxPos(), getyPos(), 
                             getSprite().getWidth(), 
                             getSprite().getHeight());
}

Your method

public void update() {

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();



回答2:


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 = new Rectangle(player.getxPos(), player.getyPos(), player
        .getSprite().getWidth() + player.getxPos(), player.getSprite()
        .getHeight() + player.getyPos());

// Rectangle of Platform
platformRect = new Rectangle(getxPos(), getyPos(), getSprite().getWidth()
        + getxPos(), getSprite().getHeight() + getxPos());

with

// Rectangle of Player
playerRect = new Rectangle(player.getxPos(), player.getyPos(),
                           player.getSprite().getWidth(),
                           player.getSprite().getHeight());

// Rectangle of Platform
platformRect = new Rectangle(getxPos(), getyPos(), 
                             getSprite().getWidth(), getSprite().getHeight());

Hope this helps.



来源:https://stackoverflow.com/questions/27091077/libgdx-trying-to-implement-collision-between-2-sprites

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!