简体   繁体   English

libGDX-尝试实现2个精灵之间的碰撞

[英]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 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 已经停留了一段时间,在此先感谢您的任何投入

The constructor of the Rectangle class requires x, y, width, height Rectangle类的构造函数需要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. 希望这可以帮助。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM