private Body initPhysicsBody(World world, float x, float y) { BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyType.DYNAMIC; bodyDef.position = new Vec2(0, 0); Body body = world.createBody(bodyDef); PolygonShape shape = new PolygonShape(); Transform fx = new Transform(); fx.position.set(100f, 100f); shape.centroid(fx); shape.setAsBox( sprite.layer().width() * GameScreen.M_PER_PIXEL / 2, sprite.layer().height() * GameScreen.M_PER_PIXEL / 2); FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = shape; fixtureDef.density = 0.4f; fixtureDef.friction = 0.1f; fixtureDef.restitution = 0f; body.createFixture(fixtureDef); body.setLinearDamping(0.2f); body.setTransform(new Vec2(x, y), 0f); // MassData md = body.getMassData(); // massD.center.set(2f, 0); body.setMassData(massD); return body; }
public void update(int delta) { if (!hasLoaded) return; sprite.layer().setRotation(body.getAngle()); // float a= 0.6f; // float i; // for(i=0f;i<a;i=i+0.1f){ // body.setTransform(body.getPosition(),i);} }
public void paint(Clock clock) { if (!hasLoaded) return; // body.setLinearVelocity(new Vec2(30 * MathUtils.cos(GameScreen.angle), 30 * // MathUtils.sin(GameScreen.angle))); sprite .layer() .setTranslation( (body.getPosition().x / GameScreen.M_PER_PIXEL), (body.getPosition().y / GameScreen.M_PER_PIXEL)); Vec2 delta = new Vec2(80f - body.getPosition().x, 400f - body.getPosition().y); float angle = MathUtils.atan2(delta.x, delta.y); body.setLinearVelocity(new Vec2(8 * -MathUtils.cos(angle), 8 * MathUtils.sin(angle))); }
public void closebody() { body.setActive(false); sprite.layer().destroy(); }
public Layer layer() { return sprite.layer(); }