Search in sources :

Example 16 with Body

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);
}
Also used : PhysicsComponent(com.gemserk.commons.artemis.components.PhysicsComponent) Body(com.badlogic.gdx.physics.box2d.Body)

Example 17 with 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();
}
Also used : Entity(com.artemis.Entity) PhysicsComponent(com.gemserk.commons.artemis.components.PhysicsComponent) Fixture(com.badlogic.gdx.physics.box2d.Fixture) Body(com.badlogic.gdx.physics.box2d.Body) Contact(com.gemserk.commons.gdx.box2d.Contacts.Contact)

Example 18 with Body

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);
    }
}
Also used : PhysicsComponent(com.gemserk.commons.artemis.components.PhysicsComponent) Vector2(com.badlogic.gdx.math.Vector2) Body(com.badlogic.gdx.physics.box2d.Body) LinearVelocityLimitComponent(com.gemserk.commons.artemis.components.LinearVelocityLimitComponent)

Example 19 with Body

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);
}
Also used : Entity(com.artemis.Entity) Body(com.badlogic.gdx.physics.box2d.Body)

Example 20 with Body

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);
    }
}
Also used : Vector2(com.badlogic.gdx.math.Vector2) BoundingBox(com.badlogic.gdx.math.collision.BoundingBox) Vector3(com.badlogic.gdx.math.Vector3) Ray(com.badlogic.gdx.math.collision.Ray) Body(com.badlogic.gdx.physics.box2d.Body)

Aggregations

Body (com.badlogic.gdx.physics.box2d.Body)49 Vector2 (com.badlogic.gdx.math.Vector2)21 BodyDef (com.badlogic.gdx.physics.box2d.BodyDef)16 PolygonShape (com.badlogic.gdx.physics.box2d.PolygonShape)14 FixtureDef (com.badlogic.gdx.physics.box2d.FixtureDef)9 CircleShape (com.badlogic.gdx.physics.box2d.CircleShape)7 EdgeShape (com.badlogic.gdx.physics.box2d.EdgeShape)7 Fixture (com.badlogic.gdx.physics.box2d.Fixture)7 BasePhysicsManager (com.ilargia.games.entitas.egdx.base.managers.BasePhysicsManager)6 GameEntity (com.indignado.games.states.game.gen.GameEntity)6 BodyBuilder (ilargia.egdx.util.BodyBuilder)6 GameEntity (ilargia.egdx.logicbricks.gen.game.GameEntity)5 FixtureDefBuilder (ilargia.egdx.util.FixtureDefBuilder)5 Entity (com.artemis.Entity)4 Texture (com.badlogic.gdx.graphics.Texture)4 Animation (com.badlogic.gdx.graphics.g2d.Animation)4 World (com.badlogic.gdx.physics.box2d.World)4 PhysicsComponent (com.gemserk.commons.artemis.components.PhysicsComponent)4 OrthographicCamera (com.badlogic.gdx.graphics.OrthographicCamera)3 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)3