public void createCheckpointSensor(float x, float y, Vector2 heading) { // First we create a body definition BodyDef bodyDef = new BodyDef(); // We set our body to dynamic, for something like ground which doesn't move we would set it to // StaticBody bodyDef.type = BodyDef.BodyType.StaticBody; // Set our body's starting position in the world bodyDef.position.set(x, y); // Create our body in the world using our body definition Body body = world_.createBody(bodyDef); // Create a circle shape and set its radius to 6 CircleShape circle = new CircleShape(); circle.setRadius(0.25f); // Create a fixture definition to apply our shape to FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = circle; fixtureDef.isSensor = true; fixtureDef.filter.maskBits = ENTITY_ENEMY; fixtureDef.filter.categoryBits = SENSOR_NAVIGATION; // Create our fixture and attach it to the body Fixture fix = body.createFixture(fixtureDef); fix.setUserData(heading); // Remember to dispose of any shapes after you're done with them! // BodyDef and FixtureDef don't need disposing, but shapes do. circle.dispose(); }
public void initBody(World world, int playerNum) { BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyDef.BodyType.DynamicBody; // bodyDef.fixedRotation = true; bodyDef.linearDamping = 0.0f; bodyDef.position.set(getX() + getWidth() / 2, getY() + getHeight() / 2); body = world.createBody(bodyDef); body.setUserData(this); // ((Sprite)body.getUserData()).setPosition(body.getPosition().x,body.getPosition().y); // PolygonShape shape = new PolygonShape(); CircleShape shape = new CircleShape(); shape.setRadius(getWidth() / 2); // shape.setAsBox(player.getWidth()/2 / 1, player.getHeight()/2 / 1); FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = shape; fixtureDef.density = 1.1f; fixtureDef.restitution = 0.6f; fixtureDef.friction = 0.0f; if (playerNum == 0) { fixtureDef.filter.categoryBits = PECES; fixtureDef.filter.maskBits = ENEMIGO; } else { fixtureDef.filter.categoryBits = ENEMIGO; fixtureDef.filter.maskBits = PECES; } // fixtureDef.isSensor = true; --> use it on towers not to react but detect collision body.createFixture(fixtureDef); shape.dispose(); }
private Fixture createPhysicsObject(Rectangle bounds, ModelType type, Integer id) { UserData userData = new UserData(numEnterPoints, type, null); CircleShape objectPoly = new CircleShape(); objectPoly.setRadius(bounds.width / 2); BodyDef enemyBodyDef = new BodyDef(); enemyBodyDef.type = BodyType.DynamicBody; enemyBodyDef.position.x = bounds.x; enemyBodyDef.position.y = bounds.y; Body objectBody = world.createBody(enemyBodyDef); FixtureDef objectFixtureDef = new FixtureDef(); objectFixtureDef.shape = objectPoly; // objectBody.setUserData(userData); objectFixtureDef.restitution = .025f; Fixture fixture = objectBody.createFixture(objectFixtureDef); fixture.setUserData(userData); objectBody.setLinearDamping(2f); objectBody.setGravityScale(.4f); objectBody.setFixedRotation(true); objectPoly.dispose(); numEnterPoints++; // add a sensor on the bottom to check if touching ground (for jumping) PolygonShape polygonShape = new PolygonShape(); polygonShape.setAsBox( bounds.width / 3, bounds.height / 8, new Vector2(0, -bounds.height / 2), 0); objectFixtureDef = new FixtureDef(); objectFixtureDef.shape = polygonShape; objectFixtureDef.isSensor = true; Fixture footSensorFixture = objectBody.createFixture(objectFixtureDef); footSensorFixture.setUserData(new UserData(numEnterPoints, ModelType.FOOT_SENSOR, id)); // add a sensor on left side to check if touching wall (for grappling) PolygonShape polygonShape2 = new PolygonShape(); polygonShape2.setAsBox( bounds.width / 8, bounds.height / 3, new Vector2(-bounds.width / 2, 0), 0); objectFixtureDef = new FixtureDef(); objectFixtureDef.shape = polygonShape2; objectFixtureDef.isSensor = true; Fixture leftSideSensorFixture = objectBody.createFixture(objectFixtureDef); leftSideSensorFixture.setUserData(new UserData(numEnterPoints, ModelType.LEFT_SIDE_SENSOR, id)); // add a sensor on right side to check if touching wall (for grappling) polygonShape2.setAsBox( bounds.width / 8, bounds.height / 3, new Vector2(bounds.width / 2, 0), 0); objectFixtureDef = new FixtureDef(); objectFixtureDef.shape = polygonShape2; objectFixtureDef.isSensor = true; Fixture rightSideSensorFixture = objectBody.createFixture(objectFixtureDef); rightSideSensorFixture.setUserData( new UserData(numEnterPoints, ModelType.RIGHT_SIDE_SENSOR, id)); return fixture; }
private Entity buildPuffin(World world) { Entity e = engine.createEntity(); e.add(new PuffinComponent()); AnimationComponent a = new AnimationComponent(); a.animations.put( "DEFAULT", new Animation(1f / 16f, Assets.getPuffinArray(), Animation.PlayMode.LOOP)); a.animations.put( "RUNNING", new Animation(1f / 16f, Assets.getPuffinRunArray(), Animation.PlayMode.LOOP)); e.add(a); StateComponent state = new StateComponent(); state.set("DEFAULT"); e.add(state); TextureComponent tc = new TextureComponent(); e.add(tc); TransformComponent tfc = new TransformComponent(); tfc.position.set(10f, 10f, 1f); tfc.rotation = 15f; tfc.scale.set(0.25f, 0.25f); e.add(tfc); BodyComponent bc = new BodyComponent(); BodyDef bodyDef = new BodyDef(); // We set our body to dynamic, for something like ground which doesn't move we would set it to // StaticBody bodyDef.type = BodyDef.BodyType.DynamicBody; // Set our body's starting position in the world bodyDef.position.set(10f, 23f); // Create our body in the world using our body definition bc.body = world.createBody(bodyDef); bc.body.applyAngularImpulse(50f, true); // Create a circle shape and set its radius to 6 CircleShape circle = new CircleShape(); circle.setRadius(2f); // Create a fixture definition to apply our shape to FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = circle; fixtureDef.density = 20f; fixtureDef.friction = 0.4f; fixtureDef.restitution = 0.6f; // Make it bounce a little bit // Create our fixture and attach it to the body bc.body.createFixture(fixtureDef); // Remember to dispose of any shapes after you're done with them! // BodyDef and FixtureDef don't need disposing, but shapes do. circle.dispose(); e.add(bc); return e; }
private void createVisionFixture() { CircleShape circleShape = new CircleShape(); circleShape.setRadius(TURRET_VISION_RADIUS); FixtureDef fixtureDef = new FixtureDef(); fixtureDef.isSensor = true; fixtureDef.shape = circleShape; body.createFixture(fixtureDef); circleShape.dispose(); }
public void createIceBall() { // gear base BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyType.StaticBody; bodyDef.position.x = x; bodyDef.position.y = y; CircleShape circleShape = new CircleShape(); circleShape.setRadius(1); FixtureDef fixtureDef = new FixtureDef(); fixtureDef.isSensor = true; fixtureDef.shape = circleShape; gearBody = world.createBody(bodyDef); gearBody.createFixture(fixtureDef); circleShape = new CircleShape(); circleShape.setRadius(size); BodyDef boxBodyDef = new BodyDef(); boxBodyDef.type = BodyType.DynamicBody; boxBodyDef.position.x = x; boxBodyDef.position.y = y; fixtureDef = new FixtureDef(); fixtureDef.shape = circleShape; fixtureDef.friction = 0f; fixtureDef.density = 4.10f; fixtureDef.restitution = 1.5f; fixtureDef.isSensor = true; fixtureDef.filter.groupIndex = -1; gearBase = world.createBody(boxBodyDef); SpriteInfo spriteTemp = new SpriteInfo(assets.getSprite("star")); spriteTemp.setName("star"); gearBase.setUserData(spriteTemp); gearBase.createFixture(fixtureDef); // iceBody.createFixture(circleShape, .1f); RevoluteJointDef wheeljointDef = new RevoluteJointDef(); wheeljointDef.bodyA = gearBase; wheeljointDef.bodyB = gearBody; wheeljointDef.localAnchorA.y = 0; wheeljointDef.localAnchorB.y = 0; wheeljointDef.motorSpeed = .5f; wheeljointDef.enableMotor = true; wheeljointDef.maxMotorTorque = 100000f; // Joint joint = // wheeljointDef.bodyA.getWorld().createJoint(wheeljointDef); world.createJoint(wheeljointDef); circleShape.dispose(); }
private void addBody(World world, float x, float y, boolean dirt) { final BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyDef.BodyType.StaticBody; final Body body = world.createBody(bodyDef); body.setTransform(x + 0.5f, y + 0.5f, 0); final CircleShape shape = new CircleShape(); shape.setRadius(0.1f); final FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = shape; final Fixture fixture = body.createFixture(fixtureDef); shape.dispose(); if (dirt) { dirts.add(fixture); } }
private void createBalls() { BodyDef ballBodyDef = new BodyDef(); ballBodyDef.type = BodyType.DynamicBody; CircleShape shape = new CircleShape(); shape.setRadius(BALL_RADIUS); FixtureDef fd = new FixtureDef(); fd.density = 1; fd.friction = 0.5f; fd.friction = 1f; fd.restitution = 0.5f; fd.shape = shape; fd.filter.categoryBits = CATEGORY_BALLS; fd.filter.maskBits = MASK_BALLS; ballModels = new Body[MAX_BALLS]; for (int i = 0; i < MAX_BALLS; i++) { ballModels[i] = world.createBody(ballBodyDef); ballModels[i].createFixture(fd); } shape.dispose(); }
public Beaver(World world, Island island) { this.island = island; float scale = 2.0f; float radius = 1.0f; BodyDef bd = new BodyDef(); bd.type = BodyDef.BodyType.DynamicBody; Vector2 pos = island.physicsBody.getPosition(); ang = island.physicsBody.getAngle(); float islandW = island.getPhysicsWidth(); float islandH = island.getPhysicsHeight(); Vector2 off = new Vector2(islandW * MathUtils.random(0.3f, 0.6f), islandH); off.rotate(MathUtils.radiansToDegrees * ang); bd.position.set(new Vector2(pos.x + off.x, pos.y + off.y)); bd.angle = ang; off.set(islandW * 0.075f, islandH); off.rotate(MathUtils.radiansToDegrees * ang); Vector2 rightEdge = new Vector2(pos.x + off.x, pos.y + off.y).scl(Mane.PTM_RATIO); off.set(islandW * 0.85f, islandH); off.rotate(MathUtils.radiansToDegrees * ang); Vector2 leftEdge = new Vector2(pos.x + off.x, pos.y + off.y).scl(Mane.PTM_RATIO); float speed = MathUtils.random(1.0f, 3.0f); addAction( Actions.delay( MathUtils.random(0.0f, 1.0f), Actions.forever( Actions.sequence( Actions.moveTo(rightEdge.x, rightEdge.y, speed), Actions.moveTo(leftEdge.x, leftEdge.y, speed))))); bd.linearDamping = 0.2f; Body body = world.createBody(bd); FixtureDef fd = new FixtureDef(); fd.density = 1.0f; fd.filter.categoryBits = Collision.BEAVER; fd.filter.maskBits = Collision.SHARK; fd.restitution = 0.0f; fd.friction = 0.0f; fd.isSensor = true; CircleShape cs = new CircleShape(); cs.setRadius(radius); cs.setPosition(new Vector2(radius, radius)); fd.shape = cs; body.createFixture(fd); cs.dispose(); super.initPhysicsBody(body); setSize(scale * Mane.PTM_RATIO, scale * Mane.PTM_RATIO); setOrigin(0.0f, 0.0f); }
@Override public void resize(int width, int height) { PPuX = width / 10; System.out.println("width = " + width); System.out.println("height = " + height); PPuY = height / 10; world = new World(new Vector2(0, -9.8f), false); renderer = new Box2DDebugRenderer(); camera = new OrthographicCamera(width, height); debugMatrix = new Matrix4(camera.combined); Circle.setBounds( Circle.getX() * PPuX, Circle.getY() * PPuY, Circle.getWidth() * PPuX, Circle.getHeight() * PPuY); Circle.setOrigin(Circle.getWidth() / 2, Circle.getHeight() / 2); BodyDef circleDef = new BodyDef(); circleDef.type = BodyType.DynamicBody; // To allign the Circle sprite with box 2d circleDef.position.set( convertToBox(Circle.getX() + Circle.getWidth() / 2), convertToBox(Circle.getY() + Circle.getHeight() / 2)); circleBody = world.createBody(circleDef); // box2d builds around 0,0 you can see -X and -Y, this makes sure that you only see X,Y debugMatrix.translate(-camera.viewportWidth / 2, -camera.viewportHeight / 2, 0); // scale the debug matrix by the scaling so everything looks normal debugMatrix.scale(BOX_TO_WORLD, BOX_TO_WORLD, 0); circleShape = new CircleShape(); circleShape.setRadius(convertToBox(Circle.getWidth() / 2)); FixtureDef circleFixture = new FixtureDef(); circleFixture.shape = circleShape; circleFixture.density = 0.4f; circleFixture.friction = 0.2f; circleFixture.restitution = 1f; circleBody.createFixture(circleFixture); circleBody.setUserData(Circle); // create ground BodyDef groundDef = new BodyDef(); groundDef.position.set(convertToBox(camera.viewportWidth / 2), 0); Body groundBody = world.createBody(groundDef); PolygonShape groundBox = new PolygonShape(); groundBox.setAsBox(convertToBox(camera.viewportWidth / 2), 0); groundBody.createFixture(groundBox, 0); BodyDef def = new BodyDef(); def.type = BodyType.DynamicBody; def.position.set(0, 0); Body box = world.createBody(def); PolygonShape poly = new PolygonShape(); poly.setAsBox(0.1f, 0.2f); playerPhysicsFixture = box.createFixture(poly, 1); poly.dispose(); CircleShape circle = new CircleShape(); circle.setRadius(0.1f); circle.setPosition(new Vector2(0, -0.2f)); playerSensorFixture = box.createFixture(circle, 0); circle.dispose(); box.setBullet(true); player = box; player.setTransform(1.0f, 2.0f, 0); player.setFixedRotation(true); }