Slick 2D for Java中的碰撞检测

时间:2012-10-26 22:35:51

标签: java collision-detection game-physics slick2d

我是Java Game Development的新手,也是Slick 2D的新手,我正在制作像Mario或Sonic这样的平台游戏。无论如何,我在碰撞检测方面遇到了麻烦。我已经设置了代码,但是我的播放器一直在浏览地图。我正在使用Tiled Map编辑器制作地图。这是移动和碰撞检测的代码

变量

   private float marioX = 12;
   private float marioY = 590;
   boolean jumping = false;        
   float verticalSpeed = 0.0f;     
   private Polygon playerPoly;
   public BlockMap map2;


Input input = gc.getInput();

if (input.isKeyDown(Input.KEY_LEFT)){
    mario = marioLeft;
    marioX-=5;
    playerPoly.setX(marioX);
    if (entityCollisionWith()){
        marioX+=5;
        playerPoly.setX(marioX);
    }

}   
if (input.isKeyDown(Input.KEY_RIGHT)){
    mario = marioRight;
    marioX+=5;
    playerPoly.setX(marioX);
    if (entityCollisionWith()){
        marioX-=5;
        playerPoly.setX(marioX);
    }
}


if (input.isKeyDown(Input.KEY_SPACE) && !jumping) {
    mario = marioJumpRight;
    verticalSpeed = -.4f * delta;
    jumping = true;
}
if (jumping){
    verticalSpeed += .01f * delta;
}

marioY += verticalSpeed;
playerPoly.setY(marioY);

if (entityCollisionWith()){
    jumping = false;
    verticalSpeed = 0;
}
entityCollisionWith()

public boolean entityCollisionWith() throws SlickException {
for (int i = 0; i < BlockMap.entities.size(); i++) {
    Blocks entity1 = (Blocks) BlockMap.entities.get(i);
    if (playerPoly.intersects(entity1.poly)) {
        return true;
    }       
}       
return false;
}

BlockMap类

import java.util.ArrayList;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.tiled.TiledMap;

public class BlockMap {
public static TiledMap tmap;
public static int mapWidth;
public static int mapHeight;
private int square[] = {1,1,15,1,15,15,1,15}; //square shaped tile
public static ArrayList<Object> entities;

public BlockMap(String ref) throws SlickException {
entities = new ArrayList<Object>();
tmap = new TiledMap(ref, "res");
mapWidth = tmap.getWidth() * tmap.getTileWidth();
mapHeight = tmap.getHeight() * tmap.getTileHeight();

for (int x = 0; x < tmap.getWidth(); x++) {
    for (int y = 0; y < tmap.getHeight(); y++) {
        int tileID = tmap.getTileId(x, y, 0);
        if (tileID == 1) {
            entities.add(
                                new Blocks(x * 16, y * 16, square, "square")
                                );
        }
    }
   }
 }
}

阻止类

import org.newdawn.slick.Graphics;
import org.newdawn.slick.geom.Polygon;

public class Blocks  {
public Polygon poly;
public Blocks(int x, int y, int test[],String type) {
poly = new Polygon(new float[]{
        x+test[0], y+test[1],
        x+test[2], y+test[3],
        x+test[4], y+test[5],
        x+test[6], y+test[7],
 });   
 }

public void update(int delta) {
}

public void draw(Graphics g) {
   g.draw(poly);
 }
}

1 个答案:

答案 0 :(得分:1)

你确定要从正确的层中取出瓷砖吗?与在代码中一样,您将从图层ID“0”中获取图块:

for (int x = 0; x < tmap.getWidth(); x++) {
    for (int y = 0; y < tmap.getHeight(); y++) {
        int tileID = tmap.getTileId(x, y, 0);
        if (tileID == 1) {
            entities.add(
               new Blocks(x * 16, y * 16, square, "square")
               );
        }
    }
}

您可以尝试更改此行:

int tileID = tmap.getTileId(x, y, 0);

......对此:

int layerID = tmap.getLayerIndex( "CollisionLayer" ); // Or w/e your layer is called
int tileID = tmap.getTileId( x, y, layerID );

希望这有帮助!