Java 如何在box2d中实现传感器之间的交互?
我不知道如何使传感器平台的交互变得活跃。大概是这样的:Java 如何在box2d中实现传感器之间的交互?,java,libgdx,box2d,Java,Libgdx,Box2d,我不知道如何使传感器平台的交互变得活跃。大概是这样的: BeginContact { if (sensorA && sensorB){ platform.getBody () setActive (true); } } 但我不知道怎么做。请帮帮我 public class WorldUtils { public static World createWorld() { return new World(Co
BeginContact {
if (sensorA && sensorB){
platform.getBody ()
setActive (true);
}
}
但我不知道怎么做。请帮帮我
public class WorldUtils {
public static World createWorld() {
return new World(Constants.WORLD_GRAVITY, true);
}
public static Body createPlatform(World world) {
SpawnType spawnType = RandomUtils.getRandomSpawnType();
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.StaticBody;
bodyDef.position.set(new Vector2(GameStage.pointToCreate, spawnType.getY()));
PolygonShape shape = new PolygonShape();
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.density = spawnType.getDensity();
fixtureDef.shape = shape;
shape.setAsBox(spawnType.getWidth() / 2, spawnType.getHeight() / 2);
PolygonShape polygonShapeSensor = new PolygonShape();
polygonShapeSensor.setAsBox(Constants.PLATFORM_WIDTH/2+0.5f, Constants.PLATFORM_HEIGHT/2-1.5f,
new Vector2(Constants.PLATFORM_WIDTH/2-0.5f, Constants.PLATFORM_HEIGHT/2-2f), bodyDef.angle);
FixtureDef myPolygonFixtureDef = new FixtureDef();
myPolygonFixtureDef.shape = polygonShapeSensor;
myPolygonFixtureDef.isSensor = true;
Body body = world.createBody(bodyDef);
body.createFixture(fixtureDef);
body.createFixture(myPolygonFixtureDef);
body.resetMassData();
PlatformUserData userData = new PlatformUserData(spawnType.getWidth(), spawnType.getHeight());
body.setUserData(userData);
shape.dispose();
return body;
}
public static Body createCharacter(World world) {
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.DynamicBody;
bodyDef.position.set(new Vector2(Constants.CHARACTER_X, Constants.CHARACTER_Y));
PolygonShape shape = new PolygonShape();
shape.setAsBox(Constants.CHARACTER_WIDTH / 2, Constants.CHARACTER_HEIGHT / 2);
PolygonShape polygonShapeSensor = new PolygonShape();
polygonShapeSensor.setAsBox(Constants.CHARACTER_WIDTH/2+0.5f, Constants.CHARACTER_HEIGHT/2-1.5f,
new Vector2(Constants.CHARACTER_WIDTH/2-0.5f, Constants.CHARACTER_HEIGHT/2-2f), bodyDef.angle);
FixtureDef myPolygonFixtureDef = new FixtureDef();
myPolygonFixtureDef.shape = polygonShapeSensor;
myPolygonFixtureDef.isSensor = true;
Body body = world.createBody(bodyDef);
body.setGravityScale(Constants.CHARACTER_GRAVITY_SCALE);
body.createFixture(shape, Constants.CHARACTER_DENSITY);
body.createFixture(myPolygonFixtureDef);
body.resetMassData();
body.setUserData(new CharacterUserData(Constants.CHARACTER_WIDTH, Constants.CHARACTER_HEIGHT));
shape.dispose();
body.setBullet(true);
body.setSleepingAllowed(false);
return body;
}
}
public class GameStage extends Stage implements ContactListener {
...............................................................
@Override
public void beginContact(Contact contact) {
Body a = contact.getFixtureA().getBody();
Body b = contact.getFixtureB().getBody();
if ((BodyUtils.bodyIsRunner(a) && BodyUtils.bodyIsGround(b)) ||
(BodyUtils.bodyIsGround(a) && BodyUtils.bodyIsRunner(b))) {
}
}
}
public class BodyUtils {
public static boolean bodyIsRunner(Body body) {
UserData userData = (UserData) body.getUserData();
return userData!=null && userData.getUserDataType() == UserDataType.CHARACTER;
}
public static boolean bodyIsGround (Body body) {
UserData userData = (UserData) body.getUserData();
return userData != null && userData.getUserDataType() == UserDataType.PLATFORM;
}
这是一个很难回答的问题?