public CirclePlatform (int x, int y, float radius, float da) { platform = createCircle(BodyType.KinematicBody, radius, 1); platform.setTransform(x, y, 0); platform.getFixtureList().get(0).setUserData("p"); platform.setAngularVelocity(da); platform.setUserData(this); }
public MovingPlatform (float x, float y, float width, float height, float dx, float dy, float da, float maxDist) { platform = createBox(BodyType.KinematicBody, width, height, 1); pos.x = x; pos.y = y; dir.x = dx; dir.y = dy; this.maxDist = maxDist; platform.setTransform(pos, 0); platform.getFixtureList().get(0).setUserData("p"); platform.setAngularVelocity(da); platform.setUserData(this); }
/** Destroy a rigid body given a definition. No reference to the definition is retained. This function is locked during * callbacks. * @warning This automatically deletes all associated shapes and joints. * @warning This function is locked during callbacks. */ public void destroyBody (Body body) { Array<JointEdge> jointList = body.getJointList(); while (jointList.size > 0) destroyJoint(body.getJointList().get(0).joint); jniDestroyBody(addr, body.addr); body.setUserData(null); this.bodies.remove(body.addr); Array<Fixture> fixtureList = body.getFixtureList(); while(fixtureList.size > 0) { Fixture fixtureToDelete = fixtureList.removeIndex(0); this.fixtures.remove(fixtureToDelete.addr).setUserData(null); freeFixtures.free(fixtureToDelete); } freeBodies.free(body); }
bd.position.set(xs[j] + x, 0.752f + 1.54f * i); Body body = world.createBody(bd); body.setUserData(n);
//get rid of all bodies (toBeDeleted is an Array<Body>) public void sweepDeadBodies() { if(!world.isLocked()){ // KEY FACTOR Array<Body> wB = getBodies(); for (Body body : wB) { if(body!=null && toBeDeleted.contains(body, false)) { body.setUserData(null); world.destroyBody(body); toBeDeleted.removeValue(body, false); } } } }
public class Ball extends Sprite{ final FixtureDef ballFixtureDef = PhysicsFactory.createFixtureDef(1.0f, 0.0f, 0.0f, false, Main.CATEGORYBIT_BALL, Main.MASKBITS_BALL, (short)0); Body body; float velocityX, velocityY; int type; public Ball(float pX, float pY, TextureRegion pTextureRegion, PhysicsWorld pWorld, float velocityX, float velocityY, int type) { super(pX, pY, pTextureRegion); this.type = type; this.velocityX = velocityX; this.velocityY = velocityY; body = PhysicsFactory.createCircleBody(pWorld, this, BodyType.DynamicBody, ballFixtureDef); body.setUserData(Ball.this); pWorld.registerPhysicsConnector(new PhysicsConnector(this, body, true, true)); } }
final AnimatedSprite face; final Body body; final FixtureDef objectFixtureDef = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f); face = new AnimatedSprite(CAMERA_WIDTH/2+200, CAMERA_HEIGHT/2, this.mCircleFaceTextureRegion, this.getVertexBufferObjectManager()); face.setScale(3); body = PhysicsFactory.createCircleBody(this.mPhysicsWorld, face, BodyType.DynamicBody, objectFixtureDef); this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face, body, true, true)); face.animate(new long[]{200,200}, 0, 1, true); face.setUserData(body); body.setUserData("player"); this.mScene.registerTouchArea(face); this.mScene.attachChild(face);
@Override public void beginContact(Contact contact) { // TODO Auto-generated method stub Fixture fixtureA = contact.getFixtureA(); Fixture fixtureB = contact.getFixtureB(); Body bodyA = fixtureA.getBody(); Body bodyB = fixtureB.getBody(); if(bodyA == pointerBody && bodyB != groundBody) bodyB.setUserData(true); else if(bodyB == pointerBody && bodyA != groundBody) bodyA.setUserData(true); }
@Override public Body createFixture(World world, boolean overrideStatic) { BodyType type = overrideStatic ? BodyType.StaticBody : bodyType; Body body = PhysicsHelper.createCircle(world, radius, center, type, friction, restitution, density, getFilter().maskBits, getFilter().categoryBits, getFilter().groupIndex); body.setUserData(name); return body; }
public Enemy(Texture texture, float x, float y) { this.texture = texture; enemyBody = createPolygon(BodyType.DynamicBody, x, y, 1f, 0f, 0.8f, ENEMY_WIDTH * 0.5f, ENEMY_HEIGHT * 0.5f, CATEGORY_ENEMY, MASK_ENEMY); //To identify the specific instance when collisions happen enemyBody.setUserData(this); // Sensor shape to detect other bodies getting close to him CircleShape circle = new CircleShape(); circle.setRadius(1f); FixtureDef sensorFD = new FixtureDef(); sensorFD.isSensor = true; sensorFD.shape = circle; sensorFD.filter.categoryBits = CATEGORY_SENSOR; sensorFD.filter.maskBits = MASK_SENSOR; enemyBody.createFixture(sensorFD); }
/** * @param overrideStatic use BodyType.StaticBody no matter what bodyType is set to */ public Body createFixture(World world, boolean overrideStatic){ FixtureDef fd = new FixtureDef(); fd.density = density; fd.friction = friction; fd.restitution = restitution; BodyType type = overrideStatic ? BodyType.StaticBody : bodyType; Body body = PhysicsHelper.createFixture(world, fd, type, vertices, new PolygonShape(), getFilter().maskBits, getFilter().categoryBits, getFilter().groupIndex); if(body != null){ body.setTransform(center, 0); body.setUserData(name); } return body; }
public static Body createGround(World world) { BodyDef bodyDef = new BodyDef(); bodyDef.position.set(new Vector2(Constants.GROUND_X, Constants.GROUND_Y)); Body body = world.createBody(bodyDef); PolygonShape shape = new PolygonShape(); shape.setAsBox(Constants.GROUND_WIDTH / 2, Constants.GROUND_HEIGHT / 2); body.createFixture(shape, Constants.GROUND_DENSITY); body.setUserData(new GroundUserData(Constants.GROUND_WIDTH, Constants.GROUND_HEIGHT)); shape.dispose(); return body; }
private Body createPolygon(int num, BodyType type, float x, float y, float d, float r, float f, float halfwidth, float halfheight) { BodyDef bodyDef = new BodyDef(); bodyDef.type = type; bodyDef.position.set(x,y); bodyDef.angle=0; bodyDef.fixedRotation = true; Body square = world.createBody(bodyDef); square.setUserData("Box"+num); FixtureDef fixtureDef=new FixtureDef(); fixtureDef.density=d; fixtureDef.restitution=r; fixtureDef.friction=f; fixtureDef.shape=new PolygonShape(); ((PolygonShape) fixtureDef.shape).setAsBox(halfwidth, halfheight); square.createFixture(fixtureDef); fixtureDef.shape.dispose(); return square; }
private Body createPolygon(int num, BodyType type, float x, float y, float d, float r, float f, float halfwidth, float halfheight) { BodyDef bodyDef = new BodyDef(); bodyDef.type = type; bodyDef.position.set(x,y); bodyDef.angle=0; bodyDef.fixedRotation = true; Body square = world.createBody(bodyDef); square.setUserData("Box"+num); FixtureDef fixtureDef=new FixtureDef(); fixtureDef.density=d; fixtureDef.restitution=r; fixtureDef.friction=f; fixtureDef.shape=new PolygonShape(); ((PolygonShape) fixtureDef.shape).setAsBox(halfwidth, halfheight); square.createFixture(fixtureDef); fixtureDef.shape.dispose(); return square; }
private void createBalloon() { float x = MathUtils.random(minWidth, maxWidth); float y = MathUtils.random(minHeight, maxHeight); // Instantiate the loader with the created JSON data BodyEditorLoader loader = new BodyEditorLoader(Gdx.files.internal("data/box2D/balloon.json")); // Create the balloon body definition and place it in within the world BodyDef bd = new BodyDef(); bd.type = BodyType.DynamicBody; bd.position.set(x, y); // Create the balloon body Body balloonBody = world.createBody(bd); balloonBody.setUserData(false); // Set to true if it must be destroyed, false means active // Create balloon fixture loader.attachFixture(balloonBody, "balloon", balloonFD, BALLOON_WIDTH); balloons.add(balloonBody); }
public static Body createRunner(World world) { BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyDef.BodyType.DynamicBody; bodyDef.position.set(new Vector2(Constants.RUNNER_X, Constants.RUNNER_Y)); PolygonShape shape = new PolygonShape(); shape.setAsBox(Constants.RUNNER_WIDTH / 2, Constants.RUNNER_HEIGHT / 2); Body body = world.createBody(bodyDef); body.setGravityScale(Constants.RUNNER_GRAVITY_SCALE); body.createFixture(shape, Constants.RUNNER_DENSITY); body.resetMassData(); body.setUserData(new RunnerUserData(Constants.RUNNER_WIDTH, Constants.RUNNER_HEIGHT)); shape.dispose(); return body; }
/** Destroy a rigid body given a definition. No reference to the definition is retained. This function is locked during * callbacks. * @warning This automatically deletes all associated shapes and joints. * @warning This function is locked during callbacks. */ public void destroyBody (Body body) { Array<JointEdge> jointList = body.getJointList(); while (jointList.size > 0) destroyJoint(body.getJointList().get(0).joint); jniDestroyBody(addr, body.addr); body.setUserData(null); this.bodies.remove(body.addr); Array<Fixture> fixtureList = body.getFixtureList(); while(fixtureList.size > 0) { Fixture fixtureToDelete = fixtureList.removeIndex(0); this.fixtures.remove(fixtureToDelete.addr).setUserData(null); freeFixtures.free(fixtureToDelete); } freeBodies.free(body); }
private void createGround() { ChainShape chainShape = new ChainShape(); chainShape.createLoop(new Vector2[] { new Vector2(.0f, .0f), new Vector2(SCENE_WIDTH, .0f), new Vector2(SCENE_WIDTH, 7f), new Vector2(SCENE_WIDTH-1f, 7f), new Vector2(SCENE_WIDTH-1f, 1.5f), new Vector2(1f, 1.5f), new Vector2(1f, 7f), new Vector2(0f, 7f),}); BodyDef chainBodyDef = new BodyDef(); chainBodyDef.type = BodyType.StaticBody; chainBodyDef.position.set(.0f, .0f); groundBody = world.createBody(chainBodyDef); FixtureDef groundFD = new FixtureDef(); groundFD.shape = chainShape; groundFD.density = 0; groundBody.createFixture(groundFD); groundBody.setUserData("Ground Body"); chainShape.dispose(); }
private void createGround() { ChainShape chainShape = new ChainShape(); chainShape.createLoop(new Vector2[] { new Vector2(.0f, .0f), new Vector2(SCENE_WIDTH, .0f), new Vector2(SCENE_WIDTH, 7f), new Vector2(SCENE_WIDTH-1f, 7f), new Vector2(SCENE_WIDTH-1f, 1.5f), new Vector2(1f, 1.5f), new Vector2(1f, 7f), new Vector2(0f, 7f),}); BodyDef chainBodyDef = new BodyDef(); chainBodyDef.type = BodyType.StaticBody; chainBodyDef.position.set(.0f, .0f); groundBody = world.createBody(chainBodyDef); FixtureDef groundFD = new FixtureDef(); groundFD.shape = chainShape; groundFD.density = 0; groundBody.createFixture(groundFD); groundBody.setUserData("Ground Body"); chainShape.dispose(); }
public static Body createEnemy(World world) { EnemyType enemyType = RandomUtils.getRandomEnemyType(); BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyDef.BodyType.KinematicBody; bodyDef.position.set(new Vector2(enemyType.getX(), enemyType.getY())); PolygonShape shape = new PolygonShape(); shape.setAsBox(enemyType.getWidth() / 2, enemyType.getHeight() / 2); Body body = world.createBody(bodyDef); body.createFixture(shape, enemyType.getDensity()); body.resetMassData(); EnemyUserData userData = new EnemyUserData(enemyType.getWidth(), enemyType.getHeight(), enemyType.getRegions()); body.setUserData(userData); shape.dispose(); return body; }