List of usage examples for com.badlogic.gdx.physics.box2d Body getMass
public float getMass()
From source file:com.agateau.pixelwheels.racer.SpinningComponent.java
License:Open Source License
@Override public void act(float delta) { if (!mActive) { return;/*from www. j a va2 s. com*/ } Body body = mVehicle.getBody(); // Slow down body.applyLinearImpulse(body.getLinearVelocity().nor().scl(-body.getMass()), body.getWorldCenter(), true); // Spin float nextAngle = body.getAngle() + body.getAngularVelocity() * GameWorld.BOX2D_TIME_STEP; if (nextAngle > mTargetBodyAngle) { stopSpinning(); return; } float totalRotation = mTargetBodyAngle - nextAngle; float desiredAngularVelocity = totalRotation / GameWorld.BOX2D_TIME_STEP; if (desiredAngularVelocity < 0) { desiredAngularVelocity = MathUtils.clamp(desiredAngularVelocity, -MAX_ANGULAR_VELOCITY, -MIN_ANGULAR_VELOCITY); } else { desiredAngularVelocity = MathUtils.clamp(desiredAngularVelocity, MIN_ANGULAR_VELOCITY, MAX_ANGULAR_VELOCITY); } float impulse = body.getInertia() * (desiredAngularVelocity - body.getAngularVelocity()); body.applyAngularImpulse(impulse, true); }
From source file:com.badlogic.gdx.tests.box2d.ApplyForce.java
License:Apache License
@Override protected void createWorld(World world) { world.setGravity(new Vector2(0, 0)); float k_restitution = 0.4f; Body ground;//from www . ja va 2 s . c o m { BodyDef bd = new BodyDef(); bd.position.set(0, 20); ground = world.createBody(bd); EdgeShape shape = new EdgeShape(); FixtureDef sd = new FixtureDef(); sd.shape = shape; sd.density = 0; sd.restitution = k_restitution; shape.set(new Vector2(-20, -20), new Vector2(-20, 20)); ground.createFixture(sd); shape.set(new Vector2(20, -20), new Vector2(20, 20)); ground.createFixture(sd); shape.set(new Vector2(-20, 20), new Vector2(20, 20)); ground.createFixture(sd); shape.set(new Vector2(-20, -20), new Vector2(20, -20)); ground.createFixture(sd); shape.dispose(); } { Transform xf1 = new Transform(new Vector2(), 0.3524f * (float) Math.PI); xf1.setPosition(xf1.mul(new Vector2(1, 0))); Vector2[] vertices = new Vector2[3]; vertices[0] = xf1.mul(new Vector2(-1, 0)); vertices[1] = xf1.mul(new Vector2(1, 0)); vertices[2] = xf1.mul(new Vector2(0, 0.5f)); PolygonShape poly1 = new PolygonShape(); poly1.set(vertices); FixtureDef sd1 = new FixtureDef(); sd1.shape = poly1; sd1.density = 4.0f; Transform xf2 = new Transform(new Vector2(), -0.3524f * (float) Math.PI); xf2.setPosition(xf2.mul(new Vector2(-1, 0))); vertices[0] = xf2.mul(new Vector2(-1, 0)); vertices[1] = xf2.mul(new Vector2(1, 0)); vertices[2] = xf2.mul(new Vector2(0, 0.5f)); PolygonShape poly2 = new PolygonShape(); poly2.set(vertices); FixtureDef sd2 = new FixtureDef(); sd2.shape = poly2; sd2.density = 2.0f; BodyDef bd = new BodyDef(); bd.type = BodyType.DynamicBody; bd.angularDamping = 5.0f; bd.linearDamping = 0.1f; bd.position.set(0, 2); bd.angle = (float) Math.PI; bd.allowSleep = false; m_body = world.createBody(bd); m_body.createFixture(sd1); m_body.createFixture(sd2); poly1.dispose(); poly2.dispose(); } { PolygonShape shape = new PolygonShape(); shape.setAsBox(0.5f, 0.5f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 1.0f; fd.friction = 0.3f; for (int i = 0; i < 10; i++) { BodyDef bd = new BodyDef(); bd.type = BodyType.DynamicBody; bd.position.set(0, 5 + 1.54f * i); Body body = world.createBody(bd); body.createFixture(fd); float gravity = 10.0f; float I = body.getInertia(); float mass = body.getMass(); float radius = (float) Math.sqrt(2 * I / mass); FrictionJointDef jd = new FrictionJointDef(); jd.localAnchorA.set(0, 0); jd.localAnchorB.set(0, 0); jd.bodyA = ground; jd.bodyB = body; jd.collideConnected = true; jd.maxForce = mass * gravity; jd.maxTorque = mass * radius * gravity; world.createJoint(jd); } shape.dispose(); } }
From source file:com.dongbat.game.util.MovementUtil.java
public static void newMovementMechanism(World world, Entity e) { UnitMovement unitMovement = EntityUtil.getComponent(world, e, UnitMovement.class); Physics physicsComponent = EntityUtil.getComponent(world, e, Physics.class); Body body = physicsComponent.getBody(); float mass = body.getMass(); if (unitMovement.getDirectionVelocity() == null) { // body.setLinearVelocity(new Vector2(0, 0)); return;//from w ww. j a v a 2 s . co m } Vector2 directionVel = unitMovement.getDirectionVelocity().cpy(); //heading float angleRad = body.getLinearVelocity().angleRad(); body.setTransform(body.getPosition(), angleRad); // TODO: calculate based on radius, use fixed mass // or make a steering-like behavior with real mass float desiredSpd = calculalteDesiredSpeed(world, e); Vector2 currentVector = body.getLinearVelocity(); directionVel.nor().scl(desiredSpd).sub(currentVector); Vector2 impulse = directionVel.scl(mass); PhysicsUtil.applyImpulse(world, e, impulse); }
From source file:com.dongbat.game.util.MovementUtil.java
/** * Move unit to specific location on map * * @param entity entity want to move//from www . j av a 2 s. co m * @param destination location that entity will move to */ public static void moveTo(Entity entity, Vector2 destination) { World world = entity.getWorld(); Physics physicsComponent = EntityUtil.getComponent(world, entity, Physics.class); Stats stat = EntityUtil.getComponent(world, entity, Stats.class); Body body = physicsComponent.getBody(); float mass = body.getMass(); Vector2 currentVelocity = body.getLinearVelocity(); Vector2 position = body.getPosition(); float scale = stat.getBaseMovementSpeed() + stat.getModifierSpeed(); Vector2 desiredVelocity = destination.cpy().sub(position).nor().scl(scale); Vector2 impulse = desiredVelocity.sub(currentVelocity).scl(mass * 10); PhysicsUtil.applyImpulse(entity.getWorld(), entity, impulse); }
From source file:com.dongbat.invasion.util.MovementUtil.java
public static void moveTo(Entity entity, Vector2 destination) { Physics physicsComponent = EntityUtil.getComponent(entity, Physics.class); Body body = physicsComponent.getBody(); float mass = body.getMass(); Vector2 currentVelocity = body.getLinearVelocity(); Vector2 position = body.getPosition(); Vector2 desiredVelocity = destination.cpy().sub(position).nor().scl(physicsComponent.getMaxSpeed()); Vector2 impulse = desiredVelocity.sub(currentVelocity).scl(mass); PhysicsUtil.applyImpulse(entity, impulse); }
From source file:com.stercore.code.net.dermetfan.utils.libgdx.box2d.Autopilot.java
License:Apache License
/** applies the force from {@link #calculateForce(Vector2, Vector2)} * @see #calculateForce(Vector2, Vector2) * @see #move(Body, Vector2, Vector2, Interpolation, float, boolean) */ public void move(Body body, Vector2 destination, Vector2 force, boolean wake) { Vector2 position = positionAccessor.apply(body); Vector2 apply = calculateForce(moveRelative ? destination : vec2_0.set(destination).sub(position), adaptForceToMass ? vec2_1.set(force).scl(body.getMass()) : force); body.applyForce(apply, position, wake); }
From source file:com.stercore.code.net.dermetfan.utils.libgdx.box2d.Autopilot.java
License:Apache License
/** applies the force of {@link #calculateForce(Vector2, Vector2, float, Interpolation)} * @param body the body to move//from w w w. j a v a2 s . c om * @param destination the destination of the body * @param force the force used to move the body * @param interpolation the interpolation used to interpolate the given {@code force} based on the {@code distanceScalar} * @param distanceScalar the distance at which the force should be fully applied * @param wake if the body should be woken up in case it is sleeping * @see #move(Body, Vector2, Vector2, boolean) * @see #calculateForce(Vector2, Vector2, float, Interpolation) */ public void move(Body body, Vector2 destination, Vector2 force, Interpolation interpolation, float distanceScalar, boolean wake) { Vector2 position = positionAccessor.apply(body); body.applyForce(calculateForce(moveRelative ? destination : vec2_0.set(destination).sub(position), adaptForceToMass ? vec2_1.set(force).scl(body.getMass()) : force, distanceScalar, interpolation), position, wake); }
From source file:com.stercore.code.net.dermetfan.utils.libgdx.box2d.Autopilot.java
License:Apache License
/** @param wake if the body should be woken up if its sleeping * @see #calculateTorque(Vector2, float, float, float, float, float) */ public void rotate(Body body, Vector2 target, float angle, float force, float delta, boolean wake) { body.applyTorque(/*from w w w .j a v a2 s. com*/ calculateTorque(rotateRelative ? target : vec2_0.set(positionAccessor.apply(body)).sub(target), body.getTransform().getRotation() + angle, body.getAngularVelocity(), body.getInertia(), adaptForceToMass ? force * body.getMass() : force, delta), wake); }
From source file:com.strategames.engine.gameobject.types.Balloon.java
License:Open Source License
@Override protected void setupBody(Body body) { /**//from w ww . j a va 2 s . c o m * TODO do we really need a separate loader for each object? Replace with static? */ BodyEditorLoader loader = new BodyEditorLoader(Gdx.files.internal("fixtures/balloon.json")); loader.setupVertices("Balloon", WIDTH); //Balloon body body.setAngularDamping(1f); body.setLinearDamping(1f); FixtureDef fixtureBalloon = new FixtureDef(); fixtureBalloon.density = 0.1786f; // Helium density 0.1786 g/l == 0.1786 kg/m3 fixtureBalloon.friction = 0.6f; fixtureBalloon.restitution = 0.4f; // Make it bounce a little bit loader.attachFixture(body, "Balloon", 0, fixtureBalloon); this.upwardLiftPosition = body.getLocalCenter(); this.upwardLiftPosition.y += 0.1f; this.upwardLift = -body.getMass() * this.liftFactor; }
From source file:net.dermetfan.utils.libgdx.box2d.Autopilot.java
License:Apache License
/** applies the force from {@link #calculateForce(Vector2, Vector2, float)} * @see #calculateForce(Vector2, Vector2, float, float, Interpolation) * @see #move(Body, Vector2, Vector2, float, float, Interpolation, boolean) */ public void move(Body body, Vector2 destination, Vector2 force, boolean wake) { Vector2 position = positionAccessor.access(body); body.applyForce(calculateForce(moveRelative ? destination : vec2_0.set(destination).sub(position), adaptForceToMass ? vec2_1.set(force).scl(body.getMass()) : force), position, wake); }