private void add_Box_Face() { float random_x = (float) (28 + (int)(Math.random() * ((this.CAMERA_WIDTH - 28*2) + 1))); final Body rectangle_face_body; rectangle_face[this.mSpriteCounter] = new Sprite(random_x, y, this.mRectangleFaceTextureRegion, this.getVertexBufferObjectManager()); rectangle_face_body = PhysicsFactory.createBoxBody(this.m_PhysicsWorld, rectangle_face[this.mSpriteCounter], BodyType.DynamicBody, this.BOX_FIXTURE_DEF); rectangle_face_body.setUserData("target"); rectangle_face_body.setLinearVelocity(0, -5); this.m_PhysicsWorld.registerPhysicsConnector(new PhysicsConnector(rectangle_face[this.mSpriteCounter], rectangle_face_body, true, false)); this.mscene.attachChild(rectangle_face[this.mSpriteCounter]); }
characterBodyDef.type = BodyDef.BodyType.KinematicBody; //I suppose your body is kinematic but it can be dynamic too Body body = world.createBody(characterBodyDef); body.setUserData(characterView); //display object of your character which is linked to the physical body in this way characterView.setBody(body); // you can create this setter to have a reference to the physical body from display object body.createFixture(fixtureCharacter);
treeBodyDef.type = BodyDef.BodyType.StaticBody; treeBodyDef.position.set(treeX, treeY); //you can set tree position here , or later using setTransform(x, y, angle) function Body treeBody = world.createBody(treeBodyDef); treeBody.setUserData(treeView); //display object of your tree linked to the body treeView.setBody(treeBody); // you can create this setter to have a reference to the physical body from display object treeBody.createFixture(fixtureTree);
public void beginContact(Contact contact) { Body a = contact.getFixtureA().getBody(); Body b = contact.getFixtureB().getBody(); if (a == mBall) { a.setUserData(a.getLinearVelocity().len()); } else if (b == mBall) { b.setUserData(b.getLinearVelocity().len()); } }
CopyOnWriteArrayList<Entity> entities = new CopyOnWriteArrayList<Entity>(); public void deleteEntities() { for(Entity entity: entities){ Body body = entity.getBody(); if (body != null) { EntityData data = (EntityData) body.getUserData(); if (data.isFlaggedForDelete()) { final Array<JointEdge> list = body.getJointList(); //delete all joints attached while (list.size > 0) { myWorld.getWorld().destroyJoint(list.get(0).joint); } //nullify everything, remove the entity from entities and destroy the body body.setUserData(null); myWorld.getWorld().destroyBody(body); entities.remove(entity); body = null; } } } }
body.setUserData(makeUserData(OBSTRUCTION_TYPE,body,myObstruction)); myObstruction.setUserData(makeUserData(OBSTRUCTION_TYPE,body,myObstruction));
private Body box(float a) { BodyDef bodyDef = new BodyDef(); bodyDef.position.set(0,0); bodyDef.type = BodyType.DynamicBody; PolygonShape polygonShape=new PolygonShape(); polygonShape.setAsBox(a,a); FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape=polygonShape; fixtureDef.density=2; fixtureDef.restitution=0.5f; fixtureDef.friction=0.5f; Body box=world.createBody(bodyDef); box.createFixture(fixtureDef); sprite = new Sprite(new Texture("android.jpg")); sprite.setSize(a*2,a*2); sprite.setOrigin(sprite.getWidth() / 2, sprite.getHeight() / 2); box.setUserData(sprite); worldBodies = new Array<Body>(); return box; }
Rectangle rect1 = new Rectangle((CAMERA_WIDTH/2) - 100, (CAMERA_HEIGHT/2) + 50, 200, 10, vertexBufferObjectManager); slabWallBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, rect1, BodyType.StaticBody, wallFixtureDef); slabWallBody.setUserData("slab"); mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(rect1, slabWallBody, false, false)); smileyBody.setUserData("smiley"); mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(smileySprite, smileyBody, true, false));
paddleBody.setUserData(4); paddleBody2.setUserData(5);
bird.setUserData(BodyData.BIRD); rocket1.setUserData(BodyData.ROCKET); rocket2.setUserData(BodyData.ROCKET);