use of com.badlogic.gdx.physics.box2d.Body in project commons-gdx by gemserk.
the class PhysicsSystem method removed.
@Override
protected void removed(Entity e) {
PhysicsComponent physicsComponent = Components.getPhysicsComponent(e);
if (physicsComponent == null)
return;
Body body = physicsComponent.getBody();
body.setUserData(null);
// removes contact from the other entity
PhysicsUtils.releaseContacts(physicsComponent.getContact());
physicsWorld.destroyBody(body);
}
use of com.badlogic.gdx.physics.box2d.Body in project commons-gdx by gemserk.
the class PhysicsUtils method releaseContacts.
public static void releaseContacts(Contacts contacts) {
// removes contact from the other entity
for (int i = 0; i < contacts.getContactCount(); i++) {
Contact contact = contacts.getContact(i);
Fixture myFixture = contact.getMyFixture();
Fixture otherFixture = contact.getOtherFixture();
contactsToRemove.add(contact);
Body otherBody = otherFixture.getBody();
if (otherBody == null)
continue;
Entity otherEntity = (Entity) otherBody.getUserData();
if (otherEntity == null)
continue;
// entitiesInContact.add(otherEntity);
// removes the contact from the other entity.
PhysicsComponent otherPhyiscsComponent = Components.getPhysicsComponent(otherEntity);
otherPhyiscsComponent.getContact().removeContact(otherFixture, myFixture);
}
for (int i = 0; i < contactsToRemove.size; i++) {
Contact contact = contactsToRemove.get(i);
contacts.removeContact(contact.getMyFixture(), contact.getOtherFixture());
}
contactsToRemove.clear();
}
use of com.badlogic.gdx.physics.box2d.Body in project commons-gdx by gemserk.
the class LimitLinearVelocitySystem method process.
@Override
protected void process(Entity e) {
PhysicsComponent physicsComponent = Components.getPhysicsComponent(e);
Body body = physicsComponent.getPhysics().getBody();
LinearVelocityLimitComponent limitComponent = e.getComponent(LinearVelocityLimitComponent.class);
Vector2 linearVelocity = body.getLinearVelocity();
float speed = linearVelocity.len();
float maxSpeed = limitComponent.getLimit();
if (speed > maxSpeed) {
float factor = maxSpeed / speed;
linearVelocity.mul(factor);
body.setLinearVelocity(linearVelocity);
}
}
use of com.badlogic.gdx.physics.box2d.Body in project commons-gdx by gemserk.
the class PhysicsContactListener method beginContact.
@Override
public void beginContact(Contact contact) {
if (!contact.isTouching())
return;
Body bodyA = contact.getFixtureA().getBody();
Body bodyB = contact.getFixtureB().getBody();
Entity entityA = (Entity) bodyA.getUserData();
Entity entityB = (Entity) bodyB.getUserData();
addBodyToContacts(entityA, contact, true);
addBodyToContacts(entityB, contact, false);
}
use of com.badlogic.gdx.physics.box2d.Body in project libgdx-inGameConsole by StrongJoshua.
the class Box2DTest method createExplosion.
/**
* Creates an explosion that applies forces to the bodies relative to their
* position and the given x and y values.
*
* @param maxForce
* The maximum force to be applied to the bodies (diminishes as
* distance from touch increases).
*/
private void createExplosion(float x, float y, float maxForce) {
float force;
Vector2 touch = new Vector2(x, y);
for (int i = 0; i < bodies.length; i++) {
Body b = bodies[i];
Vector2 v = b.getPosition();
float dist = v.dst2(touch);
if (dist == 0)
force = maxForce;
else
force = MathUtils.clamp(maxForce / dist, 0, maxForce);
float angle = v.cpy().sub(touch).angle();
float xForce = force * MathUtils.cosDeg(angle);
float yForce = force * MathUtils.sinDeg(angle);
Vector3 touch3, v3, boundMin, boundMax, intersection;
touch3 = new Vector3(touch.x, touch.y, 0);
v3 = new Vector3(v.x, v.y, 0);
boundMin = new Vector3(v.x - 1, v.y - 1, 0);
boundMax = new Vector3(v.x + 1, v.y + 1, 0);
intersection = Vector3.Zero;
Intersector.intersectRayBounds(new Ray(touch3, v3), new BoundingBox(boundMin, boundMax), intersection);
b.applyForce(new Vector2(xForce, yForce), new Vector2(intersection.x, intersection.y), true);
}
}
Aggregations