Search in sources :

Example 21 with Body

use of com.badlogic.gdx.physics.box2d.Body in project Entitas-Java by Rubentxu.

the class GameSceneState method initialize.

@Override
public void initialize() {
    atlas = assetsManager.getTextureAtlas(SPRITE_ATLAS);
    Array<TextureAtlas.AtlasRegion> heroWalking = atlas.findRegions("Andando");
    Array<TextureAtlas.AtlasRegion> heroJump = atlas.findRegions("Saltando");
    Array<TextureAtlas.AtlasRegion> heroFall = atlas.findRegions("Cayendo");
    Array<TextureAtlas.AtlasRegion> heroIdle = atlas.findRegions("Parado");
    Animation walking = new Animation(RUNNING_FRAME_DURATION, heroWalking, Animation.PlayMode.LOOP);
    Animation jump = new Animation(RUNNING_FRAME_DURATION * 7, heroJump, Animation.PlayMode.NORMAL);
    Animation fall = new Animation(RUNNING_FRAME_DURATION * 5, heroFall, Animation.PlayMode.NORMAL);
    Animation idle = new Animation(RUNNING_FRAME_DURATION * 4, heroIdle, Animation.PlayMode.LOOP);
    Map animationHero = Collections.createMap(String.class, Animation.class);
    animationHero.put("WALKING", walking);
    animationHero.put("JUMPING", jump);
    animationHero.put("FALL", fall);
    animationHero.put("HURT", fall);
    animationHero.put("IDLE", idle);
    animationHero.put("HIT", fall);
    Body body = bodyBuilder.fixture(new FixtureDefBuilder().boxShape(0.5f, 0.5f)).type(BodyDef.BodyType.KinematicBody).build();
    systems.add(new InputsControllerSystem(entitas.input, guiFactory)).add(new AnimationSystem(entitas)).add(new TextureRendererSystem(entitas, engine.batch));
    entitas.game.getPlayerEntity().addTextureView(null, new Bounds(0.8f, 1.2f), false, false, 1, 1, Color.WHITE).addAnimations(animationHero, walking, 0.02f).addRigidBody(body);
}
Also used : FixtureDefBuilder(ilargia.egdx.util.FixtureDefBuilder) AnimationSystem(com.indignado.games.states.game.system.AnimationSystem) Bounds(com.indignado.games.states.game.data.Bounds) Animation(com.badlogic.gdx.graphics.g2d.Animation) Map(java.util.Map) Body(com.badlogic.gdx.physics.box2d.Body) TextureRendererSystem(com.indignado.games.states.game.system.render.TextureRendererSystem) InputsControllerSystem(com.indignado.games.states.game.system.InputsControllerSystem)

Example 22 with Body

use of com.badlogic.gdx.physics.box2d.Body in project Entitas-Java by Rubentxu.

the class DestroySystem method execute.

@Override
protected void execute(List<GameEntity> entities) {
    for (GameEntity e : entities) {
        if (e.hasRigidBody()) {
            Body body = e.getRigidBody().body;
            e.getRigidBody().body = null;
            body.setUserData(null);
            removeBodies.add(body);
        }
        context.destroyEntity(e);
    }
}
Also used : GameEntity(ilargia.egdx.logicbricks.gen.game.GameEntity) Body(com.badlogic.gdx.physics.box2d.Body)

Example 23 with Body

use of com.badlogic.gdx.physics.box2d.Body in project Entitas-Java by Rubentxu.

the class DestroySystem method cleanup.

@Override
public void cleanup() {
    physics.step(Gdx.graphics.getDeltaTime(), preferences.VELOCITY_ITERATIONS, preferences.POSITION_ITERATIONS);
    for (Body removeBody : removeBodies) {
        for (int i = 0; i < 5; i++) {
            if (inputManager.joints[i] != null) {
                physics.destroyJoint(inputManager.joints[i]);
                inputManager.joints[i] = null;
            }
        }
        physics.destroyBody(removeBody);
    }
    removeBodies.clear();
}
Also used : Body(com.badlogic.gdx.physics.box2d.Body)

Example 24 with Body

use of com.badlogic.gdx.physics.box2d.Body in project Entitas-Java by Rubentxu.

the class Mariano method create.

@Override
public GameEntity create(Engine engine, Entitas entitas) {
    PhysicsManagerGDX physics = engine.getManager(PhysicsManagerGDX.class);
    BodyBuilder bodyBuilder = physics.getBodyBuilder();
    TextureAtlas textureAtlas = assetsManager.getTextureAtlas(atlas);
    ParticleEffect dustEffect = assetsManager.get(effect, ParticleEffect.class);
    Array<TextureAtlas.AtlasRegion> heroWalking = textureAtlas.findRegions("Andando");
    Array<TextureAtlas.AtlasRegion> heroJump = textureAtlas.findRegions("Saltando");
    Array<TextureAtlas.AtlasRegion> heroFall = textureAtlas.findRegions("Cayendo");
    Array<TextureAtlas.AtlasRegion> heroIdle = textureAtlas.findRegions("Parado");
    Map<String, Animation<TextureRegion>> animationStates = EntitasCollections.createMap(String.class, Animation.class);
    animationStates.put("walking", new Animation(0.02f, heroWalking, Animation.PlayMode.LOOP));
    animationStates.put("jump", new Animation(0.02f * 7, heroJump, Animation.PlayMode.NORMAL));
    animationStates.put("fall", new Animation(0.02f * 5, heroFall, Animation.PlayMode.NORMAL));
    animationStates.put("idle", new Animation(0.02f * 4, heroIdle, Animation.PlayMode.LOOP));
    Body bodyPlayer = bodyBuilder.fixture(bodyBuilder.fixtureDefBuilder().boxShape(0.35f, 1).density(1)).type(BodyDef.BodyType.DynamicBody).fixedRotation().position(0, 5).mass(1).build();
    GameEntity entity = entitas.game.createEntity();
    entity.addRigidBody(bodyPlayer).addAnimations(animationStates, animationStates.get("idle"), 0).addTags("Mariano").setInteractive(true).addTextureView(null, new Bounds(0.9f, 1.15f), false, false, 1, 1, Color.WHITE).addInputController((inputManager, context) -> {
        boolean isGround = false;
        SensorEntity sensor = Indexed.getSensorsEntity(entity, "CollisionGround");
        if (sensor.getCollisionSensor().collisionSignal)
            isGround = true;
        Vector2 impulse = new Vector2();
        if (inputManager.isKeyDown(Input.Keys.D)) {
            impulse.x = 2;
            if (isGround)
                entity.getAnimations().currentAnimation = entity.getAnimations().animationStates.get("walking");
            entity.getTextureView().flipX = false;
        } else if (inputManager.isKeyDown(Input.Keys.A)) {
            impulse.x = -2;
            if (isGround)
                entity.getAnimations().currentAnimation = entity.getAnimations().animationStates.get("walking");
            entity.getTextureView().flipX = true;
        }
        if (inputManager.isKeyDown(Input.Keys.W)) {
            if (isGround)
                impulse.y = 4;
            entity.getAnimations().currentAnimation = entity.getAnimations().animationStates.get("jump");
        }
        Vector2 vel = bodyPlayer.getLinearVelocity();
        if (!inputManager.isKeyDown(Input.Keys.A) && !inputManager.isKeyDown(Input.Keys.D) && isGround) {
            bodyPlayer.setLinearVelocity(new Vector2(0, vel.y));
            entity.getAnimations().currentAnimation = entity.getAnimations().animationStates.get("idle");
        }
        if (Math.abs(vel.x) > 7) {
            vel.x = Math.signum(vel.x) * 7;
            bodyPlayer.setLinearVelocity(new Vector2(vel.x, vel.y));
        }
        if (!isGround && vel.y < 0) {
            entity.getAnimations().currentAnimation = entity.getAnimations().animationStates.get("fall");
        }
        bodyPlayer.applyLinearImpulse(impulse, bodyPlayer.getWorldCenter(), false);
    });
    entitas.sensor.createEntity().addCollisionSensor("Ground").addLink(entity.getCreationIndex(), "CollisionGround");
    entitas.actuator.createEntity().addCameraActuator(((EngineGDX) engine).getCamera(), (short) 0.3f, 0.08f, 6, 1, "Mariano").addLink(entity.getCreationIndex(), "CameraActuator", true);
    entitas.actuator.createEntity().addParticleEffectActuator(dustEffect, true, -1, -1).addLink(entity.getCreationIndex(), "EffectActuator", true);
    return entity;
}
Also used : Bounds(ilargia.egdx.logicbricks.data.Bounds) EngineGDX(ilargia.egdx.impl.EngineGDX) BodyBuilder(ilargia.egdx.util.BodyBuilder) PhysicsManagerGDX(ilargia.egdx.impl.managers.PhysicsManagerGDX) ParticleEffect(com.badlogic.gdx.graphics.g2d.ParticleEffect) GameEntity(ilargia.egdx.logicbricks.gen.game.GameEntity) Vector2(com.badlogic.gdx.math.Vector2) SensorEntity(ilargia.egdx.logicbricks.gen.sensor.SensorEntity) TextureAtlas(com.badlogic.gdx.graphics.g2d.TextureAtlas) Animation(com.badlogic.gdx.graphics.g2d.Animation) Body(com.badlogic.gdx.physics.box2d.Body)

Example 25 with Body

use of com.badlogic.gdx.physics.box2d.Body in project RubeLoader by tescott.

the class RubeLoaderTest method createPolySpatialsFromRubeFixtures.

/**
    * Creates an array of PolySpatials based on fixture information from the scene. Note that
    * fixtures create aligned textures.
    * 
    * @param scene
    */
private void createPolySpatialsFromRubeFixtures(RubeScene scene) {
    Array<Body> bodies = scene.getBodies();
    EarClippingTriangulator ect = new EarClippingTriangulator();
    if ((bodies != null) && (bodies.size > 0)) {
        polySpatials = new Array<PolySpatial>();
        Vector2 bodyPos = new Vector2();
        // for each body in the scene...
        for (int i = 0; i < bodies.size; i++) {
            Body body = bodies.get(i);
            bodyPos.set(body.getPosition());
            Array<Fixture> fixtures = body.getFixtureList();
            if ((fixtures != null) && (fixtures.size > 0)) {
                // for each fixture on the body...
                for (int j = 0; j < fixtures.size; j++) {
                    Fixture fixture = fixtures.get(j);
                    String textureName = (String) scene.getCustom(fixture, "TextureMask", null);
                    if (textureName != null) {
                        String textureFileName = "data/" + textureName;
                        Texture texture = textureMap.get(textureFileName);
                        TextureRegion textureRegion = null;
                        if (texture == null) {
                            texture = new Texture(textureFileName);
                            texture.setWrap(TextureWrap.Repeat, TextureWrap.Repeat);
                            textureMap.put(textureFileName, texture);
                            textureRegion = new TextureRegion(texture);
                            textureRegionMap.put(texture, textureRegion);
                        } else {
                            textureRegion = textureRegionMap.get(texture);
                        }
                        // only handle polygons at this point -- no chain, edge, or circle fixtures.
                        if (fixture.getType() == Shape.Type.Polygon) {
                            PolygonShape shape = (PolygonShape) fixture.getShape();
                            int vertexCount = shape.getVertexCount();
                            float[] vertices = new float[vertexCount * 2];
                            // static bodies are texture aligned and do not get drawn based off of the related body.
                            if (body.getType() == BodyType.StaticBody) {
                                for (int k = 0; k < vertexCount; k++) {
                                    shape.getVertex(k, mTmp);
                                    mTmp.rotate(body.getAngle() * MathUtils.radiansToDegrees);
                                    // convert local coordinates to world coordinates to that textures are
                                    mTmp.add(bodyPos);
                                    // aligned
                                    vertices[k * 2] = mTmp.x * PolySpatial.PIXELS_PER_METER;
                                    vertices[k * 2 + 1] = mTmp.y * PolySpatial.PIXELS_PER_METER;
                                }
                                short[] triangleIndices = ect.computeTriangles(vertices).toArray();
                                PolygonRegion region = new PolygonRegion(textureRegion, vertices, triangleIndices);
                                PolySpatial spatial = new PolySpatial(region, Color.WHITE);
                                polySpatials.add(spatial);
                            } else {
                                // all other fixtures are aligned based on their associated body.
                                for (int k = 0; k < vertexCount; k++) {
                                    shape.getVertex(k, mTmp);
                                    vertices[k * 2] = mTmp.x * PolySpatial.PIXELS_PER_METER;
                                    vertices[k * 2 + 1] = mTmp.y * PolySpatial.PIXELS_PER_METER;
                                }
                                short[] triangleIndices = ect.computeTriangles(vertices).toArray();
                                PolygonRegion region = new PolygonRegion(textureRegion, vertices, triangleIndices);
                                PolySpatial spatial = new PolySpatial(region, body, Color.WHITE);
                                polySpatials.add(spatial);
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : PolygonShape(com.badlogic.gdx.physics.box2d.PolygonShape) EarClippingTriangulator(com.badlogic.gdx.math.EarClippingTriangulator) Texture(com.badlogic.gdx.graphics.Texture) TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) Vector2(com.badlogic.gdx.math.Vector2) Fixture(com.badlogic.gdx.physics.box2d.Fixture) Body(com.badlogic.gdx.physics.box2d.Body) PolygonRegion(com.badlogic.gdx.graphics.g2d.PolygonRegion)

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