我发现来自http://www.iforce2d.net/b2dtut/jumpability的教程非常有用它识别当一个玩家接触到ContactListener时,我遵循所有指令并且所有工作都很好但问题是,当我开始移动多边形形状MapObject时我让传感器在地面上不合适,所以它认为我在播出。 http://prntscr.com/7frzj3
联络听众
public class Contact implements ContactListener{
private boolean playerOnGround=false;
public void beginContact(com.badlogic.gdx.physics.box2d.Contact c) {
Fixture fa = c.getFixtureA();
Fixture fb = c.getFixtureB();
if(fa.getUserData() != null && fa.getUserData().equals("foot")) {
playerOnGround = true;
}
if(fb.getUserData() != null && fb.getUserData().equals("foot")) {
playerOnGround = true;
}
}
@Override
public void endContact(com.badlogic.gdx.physics.box2d.Contact c) {
Fixture fa = c.getFixtureA();
Fixture fb = c.getFixtureB();
if(fa.getUserData() != null && fa.getUserData().equals("foot")) {
playerOnGround = false;
}
if(fb.getUserData() != null && fb.getUserData().equals("foot")) {
playerOnGround = false;
}
}
public boolean isPlayerOnGround() { return playerOnGround; }
@Override
public void preSolve(com.badlogic.gdx.physics.box2d.Contact contact,Manifold oldManifold) {
}
@Override
public void postSolve(com.badlogic.gdx.physics.box2d.Contact contact,ContactImpulse impulse) {
}
}
initializeMapObjectsIntoWorld
protected void initializeMapObjectsIntoWorld() {
MapLayer layer = map.getLayers().get("Collision");
MapObjects objects = layer.getObjects();
for(MapObject object : objects) {
if (object instanceof TextureMapObject) continue;
Shape shape = null;
Vector2 vector=null;;
if (object instanceof RectangleMapObject) {
RectangleMapObject rectangleObj = (RectangleMapObject)object;
Rectangle rectangle = rectangleObj.getRectangle();
vector= new Vector2((rectangle.x+rectangle.getWidth()*0.5f)/Game.SCALE_PIXELS_ToMETERS, (rectangle.y+rectangle.getHeight()*0.5f)/Game.SCALE_PIXELS_ToMETERS);
PolygonShape polygon = new PolygonShape();
polygon.setAsBox(rectangle.width * 0.5f / Game.SCALE_PIXELS_ToMETERS,rectangle.height * 0.5f / Game.SCALE_PIXELS_ToMETERS);
shape=polygon;
}
else if (object instanceof PolygonMapObject) {
PolygonMapObject polygonObj = (PolygonMapObject)object;
PolygonShape polygon = new PolygonShape();
float[] vertices = polygonObj.getPolygon().getTransformedVertices();
float[] worldVertices = new float[vertices.length];
for (int i = 0; i < vertices.length; ++i) {
worldVertices[i] = vertices[i] / Game.SCALE_PIXELS_ToMETERS;
}
polygon.set(worldVertices);
shape=polygon;
vector=null;
}
else if (object instanceof PolylineMapObject) {
PolylineMapObject polyLineObj = (PolylineMapObject)object;
Polyline polyLine=polyLineObj.getPolyline();
vector= new Vector2(polyLineObj.getPolyline().getX(),polyLineObj.getPolyline().getY());
//vector=scaleToMeters(vector,polyLine.getLength(),polyLineObj.height);
shape = getPolyline((PolylineMapObject)object);
}
else if (object instanceof CircleMapObject) {
CircleMapObject circleObj = (CircleMapObject)object;
Circle circle = circleObj.getCircle();
CircleShape circleShape = new CircleShape();
circleShape.setRadius(circle.radius / Game.SCALE_PIXELS_ToMETERS);
circleShape.setPosition(new Vector2(circle.x / Game.SCALE_PIXELS_ToMETERS, circle.y / Game.SCALE_PIXELS_ToMETERS));
vector= new Vector2(circle.x,circle.y);
shape = circleShape;
}
Body body=null;
if(object.getName().equals("StaticWall")) createWallObject(shape,object.getName(),object,vector);
if(body!=null) dynamicBodies.add(body);
}
}
playerInitializationBody
private void initializeEntityIntoWorld() {
BodyDef bodyDef = new BodyDef();
FixtureDef fixtureDef = new FixtureDef();
PolygonShape boxShape = new PolygonShape();
bodyDef.position.set(0, 50);
bodyDef.type=BodyType.DynamicBody;
bodyDef.fixedRotation=true;
body = world.createBody(bodyDef);
boxShape.setAsBox(WIDTH_METER, HEIGHT_METER);
fixtureDef.shape=boxShape;
fixtureDef.filter.categoryBits = BIT_PLAYER;
fixtureDef.filter.maskBits = BIT_GROUND;
fixtureDef.friction=50;
body.createFixture(fixtureDef).setUserData("player");
//Sensor
boxShape.setAsBox(WIDTH_METER/2, HEIGHT_METER/2,new Vector2(0, -HEIGHT_METER),0);
fixtureDef.shape=boxShape;
fixtureDef.filter.categoryBits = BIT_PLAYER;
fixtureDef.filter.maskBits = BIT_GROUND;
fixtureDef.isSensor=true;
fixtureDef.friction=50;
body.createFixture(fixtureDef).setUserData("foot");
boxShape.dispose();
}