我正在使用Libgdx库创建一个RPG游戏,我在设置碰撞检测的矩形方面遇到了困难。我能够在我的主角周围绘制一个矩形,并且我知道如何在碰撞发生时处理碰撞的代码。问题是我需要在碰撞层中的瓷砖周围绘制矩形(我的碰撞层不是对象层)。如果有人能够举例说明如何写这个,甚至指出我正确的方向,这将是非常有帮助的。
图块大小为32x32
这是我到目前为止用于绘制指定图块周围的矩形的代码
public void collisionSetUp(){
TiledMapTileLayer layer;
layer = (TiledMapTileLayer) map.getLayers().get("Bushes");
float tileWidth = layer.getTileWidth(); //the tile is a perfect square so only one side is required
//go through the entire layer and assign a rectangle to each tile
for(int row = 0; row < layer.getHeight(); row++) {
for(int col = 0; col < layer.getWidth(); col++) {
debugRenderer.begin(ShapeType.Line);
collisonRect = new Rectangle(row*32, col*32, 32, 32);
debugRenderer.setColor(new Color(0, 1, 0, 1));
debugRenderer.rect(collisonRect.getX(), collisonRect.getY(), collisonRect.width, collisonRect.height);
debugRenderer.end();
System.out.println("new Rectangle");
}
}
}
处理碰撞的代码
Rectangle playerRect = new Rectangle(player.position.x, player.position.y, 32, 32);
debugRenderer.setColor(new Color(0, 1, 0, 1));
debugRenderer.rect(player.position.x, player.position.y, playerRect.width, playerRect.height);
debugRenderer.end();
if(playerRect.overlaps(collisonRect)){
player.position.x = player.prePosition.x;
player.position.y = player.prePosition.y;
}
答案 0 :(得分:0)
大家好抱歉我的英文,希望本指南你,最终变量被阻止,重命名你在tilesSet中的字符串图层模式,点击按钮右边的propiedes,属性赞助人,名字字符串你的被阻止的名字,必须是等于最终变量。
public class pruebasActor extends Sprite{
TiledMapTileLayer collisionLayer;
Rectangle rectangle;
//tu string en las propiedades del tilesmap
//your string in the properties of tilesmap
private final String BLOCKEDKEY = "blockedKey";
private float yourIncrementW;
private float yourIncrementH;
pruebasActor(TiledMapTileLayer collisionLayer){
//YOUR OTHER CODE ETC
this.collisionLayer = collisionLayer;
rectangle = new Rectangle(getX(), getY(), getWidth(), getHeight());
yourIncrementW = collisionLayer.getTileWidth();
yourIncrementH = collisionLayer.getTileHeight();
}
//YOUR OTHER CODE UPDATE DRAW MOVEMET
检查此单元格或已解锁
private boolean isCellBlocked(float x, float y) {
Cell celda = collisionLayer.getCell((int) (x / collisionLayer.getTileWidth()), (int) (y / collisionLayer.getTileHeight()));
return celda != null && celda.getTile()
!= null && celda.getTile().getProperties().containsKey(BLOCKEDKEY);
}
检查是否与右侧碰撞
public boolean collidesRight() {
for(float step = 0; step <= getHeight(); step += yourIncrementH){
if(isCellBlocked(getX() + getWidth(), getY() + step)){
Gdx.app.log("collidesRight", "X"+ getX() + getWidth()+"Y"+getY() + step);
return true;
}
}
return false;
}
检查是否与左侧碰撞
public boolean collidesLeft() {
for(float step = 0; step <= getHeight(); step += yourIncrementH)
if(isCellBlocked(getX(), getY() + step)){
Gdx.app.log("collidesRight", "X"+ getX()+"Y"+ getY() + step);
return true;
}
return false;
}
检查是否与Top
发生碰撞public boolean collidesTop() {
for(float step = 0; step <= getWidth(); step += yourIncrementW)
if(isCellBlocked(getX() + step, getY() + getHeight())){
Gdx.app.log("collidesTop", "X"+getX() + step+"Y"+getY() + getHeight());
return true;
}
return false;
}
检查是否与Bottom碰撞
public boolean collidesBottom() {
for(float step = 0; step <= getWidth(); step += yourIncrementW)
if(isCellBlocked(getX() + step, getY())){
Gdx.app.log("collidesBottom", "X"+getX() + step+"Y"+getY());
return true;
}
return false;
}
}
现在使用上述方法,您可以调用以确定播放器的行为。例如,在玩家的更新方法中,创建玩家在Y轴上移动的重力,直到碰撞为止,希望已经解释过。