public void defineMario() { BodyDef bDef = new BodyDef(); bDef.position.set(32 / MarioBros.PPM, 32 / MarioBros.PPM); bDef.type = BodyDef.BodyType.DynamicBody; b2body = world.createBody(bDef); FixtureDef fdef = new FixtureDef(); CircleShape shape = new CircleShape(); shape.setRadius(6 / MarioBros.PPM); fdef.filter.categoryBits = MarioBros.MARIO_BIT; fdef.filter.maskBits = MarioBros.GROUND_BIT | MarioBros.COIN_BIT | MarioBros.BRICK_BIT | MarioBros.ENEMY_BIT | MarioBros.OBJECT_BIT | MarioBros.ENEMY_HEAD_BIT | MarioBros.ITEM_BIT; fdef.shape = shape; b2body.createFixture(fdef); EdgeShape head = new EdgeShape(); head.set( new Vector2(-2 / MarioBros.PPM, 6 / MarioBros.PPM), new Vector2(2 / MarioBros.PPM, 6 / MarioBros.PPM)); fdef.filter.categoryBits = MarioBros.MARIO_HEAD_BIT; fdef.filter.maskBits = MarioBros.BRICK_BIT | MarioBros.COIN_BIT; fdef.shape = head; fdef.isSensor = true; b2body.createFixture(fdef).setUserData(this); }
public void defineBigMario() { Vector2 marioPosition = b2body.getPosition(); world.destroyBody(b2body); BodyDef bDef = new BodyDef(); bDef.position.set(marioPosition.add(0 / MarioBros.PPM, 10 / MarioBros.PPM)); bDef.type = BodyDef.BodyType.DynamicBody; b2body = world.createBody(bDef); FixtureDef fdef = new FixtureDef(); CircleShape shape = new CircleShape(); shape.setRadius(6 / MarioBros.PPM); fdef.filter.categoryBits = MarioBros.MARIO_BIT; fdef.filter.maskBits = MarioBros.GROUND_BIT | MarioBros.COIN_BIT | MarioBros.BRICK_BIT | MarioBros.ENEMY_BIT | MarioBros.OBJECT_BIT | MarioBros.ENEMY_HEAD_BIT | MarioBros.ITEM_BIT; fdef.shape = shape; b2body.createFixture(fdef); shape.setPosition(new Vector2(0, -14 / MarioBros.PPM)); fdef.shape = shape; b2body.createFixture(fdef).setUserData(this); EdgeShape head = new EdgeShape(); head.set( new Vector2(-2 / MarioBros.PPM, 6 / MarioBros.PPM), new Vector2(2 / MarioBros.PPM, 6 / MarioBros.PPM)); fdef.filter.categoryBits = MarioBros.MARIO_HEAD_BIT; fdef.filter.maskBits = MarioBros.BRICK_BIT | MarioBros.COIN_BIT; fdef.shape = head; fdef.isSensor = true; b2body.createFixture(fdef).setUserData(this); timeToDefineBigMario = false; }
private void definePlayer() { BodyDef bodyDef = new BodyDef(); bodyDef.position.set(32 / Gyaya.PPM, 32 / Gyaya.PPM); bodyDef.type = BodyDef.BodyType.DynamicBody; b2body = world.createBody(bodyDef); FixtureDef fixtureDef = new FixtureDef(); CircleShape shape = new CircleShape(); shape.setRadius(8f / Gyaya.PPM); fixtureDef.filter.categoryBits = Gyaya.PLAYER_BIT; fixtureDef.filter.maskBits = Gyaya.GROUND_BIT | Gyaya.BRICK_BIT | Gyaya.COIN_BIT | Gyaya.OBJECT_BIT | Gyaya.ENEMY_BIT | Gyaya.ENEMY_HEAD_BIT; fixtureDef.shape = shape; b2body.createFixture(fixtureDef); shape.setPosition(new Vector2(0, 4 / Gyaya.PPM)); shape.setRadius(6f / Gyaya.PPM); b2body.createFixture(fixtureDef); EdgeShape head = new EdgeShape(); head.set( new Vector2(-3 / Gyaya.PPM, 10f / Gyaya.PPM), new Vector2(3 / Gyaya.PPM, 10f / Gyaya.PPM)); fixtureDef.shape = head; fixtureDef.isSensor = true; b2body.createFixture(fixtureDef).setUserData("head"); EdgeShape feet = new EdgeShape(); feet.set( new Vector2(-6 / Gyaya.PPM, -9f / Gyaya.PPM), new Vector2(6 / Gyaya.PPM, -9f / Gyaya.PPM)); fixtureDef.shape = feet; fixtureDef.isSensor = true; b2body.createFixture(fixtureDef).setUserData("feet"); }
/** * * Filtered version of createRectangularBody Category is who I am, Group filters GameObjects of * the same class as mine so we dont collide, Mask is who is allowed to collide With Me Default * filter values: categoryBits = 0x0001, maskBits = -1, groupIndex = 0 Leaving them at default * would be the same as not passing such argument (use if that is the intended result) ** */ public static Body createArbitraryWorldBoundary( float x, float y, float x2, float y2, short category, short mask, short group, GameEntity myGameObjectIndentifier) { bodyDef.type = BodyType.StaticBody; // Calculate length of the line segment float length = (MathGlobals.euclideanDistance(x, y, x2, y2) / PPM); EdgeShape shape = new EdgeShape(); // Setting the points as offset distance from center shape.set(-length / 2f, 0, length / 2f, 0); fixtureDef.shape = shape; fixtureDef.filter.categoryBits = category; fixtureDef.filter.maskBits = mask; fixtureDef.filter.groupIndex = group; // Attach fixture to Body Body body = WORLD.createBody(bodyDef); body.createFixture(fixtureDef).setUserData(myGameObjectIndentifier); body.setLinearDamping(1); fixtureDef.shape.dispose(); // Calculate center of the line segment float posx = (x + x2) / 2f; float posy = (y + y2) / 2f; // Calculate angle of the line segment body.setTransform(posx / PPM, posy / PPM, MathUtils.atan2(y2 - y, x2 - x)); return body; }
@Override public void create() { batch = new SpriteBatch(); img = new Texture("badlogic.jpg"); // Create two identical sprites slightly offset from each other vertically sprite = new Sprite(img); sprite.setPosition(-sprite.getWidth() / 2, -sprite.getHeight() / 2 + 200); sprite2 = new Sprite(img); sprite2.setPosition(-sprite.getWidth() / 2 + 20, -sprite.getHeight() / 2 + 400); world = new World(new Vector2(0, -1f), true); // Sprite1's Physics body BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyDef.BodyType.DynamicBody; bodyDef.position.set( (sprite.getX() + sprite.getWidth() / 2) / PIXELS_TO_METERS, (sprite.getY() + sprite.getHeight() / 2) / PIXELS_TO_METERS); body = world.createBody(bodyDef); // Sprite2's physics body BodyDef bodyDef2 = new BodyDef(); bodyDef2.type = BodyDef.BodyType.DynamicBody; bodyDef2.position.set( (sprite2.getX() + sprite2.getWidth() / 2) / PIXELS_TO_METERS, (sprite2.getY() + sprite2.getHeight() / 2) / PIXELS_TO_METERS); body2 = world.createBody(bodyDef2); // Both bodies have identical shape PolygonShape shape = new PolygonShape(); shape.setAsBox( sprite.getWidth() / 2 / PIXELS_TO_METERS, sprite.getHeight() / 2 / PIXELS_TO_METERS); // Sprite1 FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = shape; fixtureDef.density = 0.1f; fixtureDef.restitution = 0.5f; // Sprite2 FixtureDef fixtureDef2 = new FixtureDef(); fixtureDef2.shape = shape; fixtureDef2.density = 0.1f; fixtureDef2.restitution = 0.5f; body.createFixture(fixtureDef); body2.createFixture(fixtureDef2); shape.dispose(); // Now the physics body of the bottom edge of the screen BodyDef bodyDef3 = new BodyDef(); bodyDef3.type = BodyDef.BodyType.StaticBody; float w = Gdx.graphics.getWidth() / PIXELS_TO_METERS; float h = Gdx.graphics.getHeight() / PIXELS_TO_METERS; bodyDef3.position.set(0, 0); FixtureDef fixtureDef3 = new FixtureDef(); EdgeShape edgeShape = new EdgeShape(); edgeShape.set(-w / 2, -h / 2, w / 2, -h / 2); fixtureDef3.shape = edgeShape; bodyEdgeScreen = world.createBody(bodyDef3); bodyEdgeScreen.createFixture(fixtureDef3); edgeShape.dispose(); camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); /*world.setContactListener(new ContactListener() { @Override public void beginContact(Contact contact) { // Check to see if the collision is between the second sprite and the bottom of the screen // If so apply a random amount of upward force to both objects... just because if((contact.getFixtureA().getBody() == bodyEdgeScreen && contact.getFixtureB().getBody() == body2) || (contact.getFixtureA().getBody() == body2 && contact.getFixtureB().getBody() == bodyEdgeScreen)) { body.applyForceToCenter(0,MathUtils.random(20,50),true); body2.applyForceToCenter(0, MathUtils.random(20,50), true); } } @Override public void endContact(Contact contact) { } @Override public void preSolve(Contact contact, Manifold oldManifold) { } @Override public void postSolve(Contact contact, ContactImpulse impulse) { } });*/ }
public Box2DTestLayer() { super(); useDebugDraw(); this.setIsTouchEnabled(true); this.setIsAccelerometerEnabled(true); CGSize s = CCDirector.sharedDirector().winSize(); // Vector2 lower = new Vector2(-BUFFER, -BUFFER); // Vector2 upper = new Vector2(scaledWidth+BUFFER, // scaledHeight+BUFFER); // Define the ground body. BodyDef bxGroundBodyDef = new BodyDef(); bxGroundBodyDef.position.set(0.0f, 0.0f); // Call the body factory which allocates memory for the ground body // from a pool and creates the ground box shape (also from a pool). // The body is also added to the world. Body groundBody = world.createBody(bxGroundBodyDef); // Define the ground box shape. Vector2 bottomLeft = new Vector2(0f, 0f); Vector2 topLeft = new Vector2(0f, scaledHeight); Vector2 topRight = new Vector2(scaledWidth, scaledHeight); Vector2 bottomRight = new Vector2(scaledWidth, 0f); EdgeShape bottom = new EdgeShape(); // bottom bottom.set(bottomLeft, bottomRight); groundBody.createFixture(bottom, 0); // // top EdgeShape top = new EdgeShape(); top.set(topLeft, topRight); groundBody.createFixture(top, 0); // left EdgeShape left = new EdgeShape(); left.set(topLeft, bottomLeft); groundBody.createFixture(left, 0); // right EdgeShape right = new EdgeShape(); right.set(topRight, bottomRight); groundBody.createFixture(right, 0); BodyDef bxLineDef = new BodyDef(); bxLineDef.type = BodyType.StaticBody; Body lineBody = world.createBody(bxLineDef); EdgeShape line = new EdgeShape(); line.set(topRight, bottomLeft); lineBody.createFixture(line, 0); BodyDef bxDef1 = new BodyDef(); bxDef1.type = BodyType.StaticBody; Body body = world.createBody(bxLineDef); PolygonShape p = new PolygonShape(); Vector2[] vertices = new Vector2[2]; vertices[0] = new Vector2(5, 5); vertices[1] = new Vector2(10, 5); p.set(vertices); body.createFixture(p, 0); p = new PolygonShape(); vertices = new Vector2[2]; vertices[0] = new Vector2(5, 10); vertices[1] = new Vector2(10, 10); p.set(vertices); body.createFixture(p, 0); // Set up sprite CCSpriteSheet mgr = CCSpriteSheet.spriteSheet("blocks.png", 150); addChild(mgr, 0, kTagSpriteManager); addNewSpriteWithCoords(CGPoint.ccp(s.width / 2.0f, s.height / 2.0f)); CCLabel label = CCLabel.makeLabel("Tap screen", "DroidSans", 32); label.setPosition(CGPoint.make(s.width / 2f, s.height - 50f)); label.setColor(new ccColor3B(0, 0, 255)); addChild(label); }