简体   繁体   中英

Collision Detection in Slick 2D for Java

I'm new to Java Game Development, as well as Slick 2D, and I'm making a platforming game like Mario or Sonic. Anyways, I'm having trouble with collision detection. I have the code set up, but my player keeps going through the map. I am using Tiled Map editor to make the maps. Here is the code for movement and collision detection

Variables

   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 class

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")
                                );
        }
    }
   }
 }
}

Blocks Class

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

Are you sure you're taking the tiles from the right layer? As in your code, you're taking the tiles from layer 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")
               );
        }
    }
}

You could try changing this line:

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

... to this:

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

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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