Example usage for com.badlogic.gdx.physics.box2d Body setTransform

List of usage examples for com.badlogic.gdx.physics.box2d Body setTransform

Introduction

In this page you can find the example usage for com.badlogic.gdx.physics.box2d Body setTransform.

Prototype

public void setTransform(Vector2 position, float angle) 

Source Link

Document

Set the position of the body's origin and rotation.

Usage

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;/*ww  w  .ja  v a2  s  .  c  om*/
    }

    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.PhysicsUtil.java

/**
 * Set position for an entity/*w  w  w  .  j  a v  a  2 s. c  om*/
 *
 * @param world artemis world
 * @param entity entity that you want to set position
 * @param position position that you want to set for entity
 */
public static void setPosition(com.artemis.World world, Entity entity, Vector2 position) {
    Body body = getBody(world, entity);
    body.setTransform(position, body.getAngle());
}

From source file:com.dongbat.invasion.util.PhysicsUtil.java

public static void setPosition(Entity entity, Vector2 position) {
    Body body = getBody(entity);
    body.setTransform(position, body.getAngle());
}

From source file:com.gmail.emersonmx.asteroids.system.PhysicSystem.java

License:Open Source License

private void processInputEntity(Entity entity, float deltaTime) {
    MovementComponent motion = motionMapper.get(entity);
    PhysicBodyComponent bodyComponent = physicBodyMapper.get(entity);

    Body body = bodyComponent.body;
    body.applyForce(motion.velocity, body.getWorldCenter(), true);
    motion.velocity.setZero();//from  w ww .java 2s. com

    body.setTransform(body.getPosition(), motion.direction.angleRad());
}

From source file:com.laex.cg2d.render.impl.ScreenManagerImpl.java

License:Open Source License

@Override
public Body switchAnimation(final String id, final String animationName) {
    Body switchedBody = null;
    Array<Body> bodies = new Array<Body>();
    world.getBodies(bodies);/*from   w  ww. j  a  v  a  2  s  .c om*/

    for (Body bod : bodies) {
        CGShape shape = (CGShape) bod.getUserData();

        if (id.equals(shape.getId())) {
            try {
                Vector2 positionToCopy = new Vector2(bod.getTransform().getPosition());
                float rotation = bod.getTransform().getRotation();

                world.destroyBody(bod);
                entityManager.removeEntity(shape);

                switchedBody = entityManager.createEntity(shape, animationName);

                if (switchedBody != null) {
                    switchedBody.setTransform(positionToCopy, rotation);
                }

            } catch (IOException e) {
                AppExceptionUtil.handle(e);
            }
        }

    }

    return switchedBody;
}

From source file:com.sertaogames.cactus2d.components.TileMapPhysics.java

License:Open Source License

private void createBodyFromObject(Vector2 pos, Vector2 size) {
    Body rigidbody;
    PolygonShape groundPoly = new PolygonShape();

    size.mul(Cactus2DApplication.INV_PHYSICS_SCALE * 0.5f);
    groundPoly.setAsBox(size.x, size.y);

    BodyDef groundBodyDef = new BodyDef();
    groundBodyDef.fixedRotation = true;/* ww w .j a v  a2  s  .com*/
    groundBodyDef.type = BodyType.StaticBody;
    rigidbody = gameObject.world.createBody(groundBodyDef);
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = groundPoly;
    fixtureDef.filter.groupIndex = 0;
    fixtureDef.filter.categoryBits = 0x4;
    rigidbody.createFixture(fixtureDef);
    groundPoly.dispose();

    pos.add(transform.getPosition());
    pos.mul(Cactus2DApplication.INV_PHYSICS_SCALE);
    rigidbody.setTransform(pos, transform.getAngle());
    rigidbody.setUserData(gameObject);
    bodies.add(rigidbody);
}

From source file:com.sertaogames.cactus2d.components.TileMapPhysics.java

License:Open Source License

private void createBodyFromBlocks(int begin, int last, int i) {
    Vector2 tileSize = new Vector2(tm.tilemap.tileWidth, tm.tilemap.tileHeight);

    Body rigidbody;
    PolygonShape groundPoly = new PolygonShape();
    int width = last - begin + 1;
    float height = tileSize.y;
    width *= tileSize.x;// w  w  w.  j  a v  a2  s  .  c o  m
    // System.out.println("i: "+i+" width: "+width);
    Vector2 temp = new Vector2(width, tileSize.y);
    temp.mul(Cactus2DApplication.INV_PHYSICS_SCALE * 0.5f);
    groundPoly.setAsBox(temp.x, temp.y);

    BodyDef groundBodyDef = new BodyDef();
    groundBodyDef.fixedRotation = true;
    groundBodyDef.type = BodyType.StaticBody;
    rigidbody = gameObject.world.createBody(groundBodyDef);
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = groundPoly;
    fixtureDef.filter.groupIndex = 0;
    fixtureDef.filter.categoryBits = 0x4;
    rigidbody.createFixture(fixtureDef);
    groundPoly.dispose();

    temp.set(
            new Vector2(begin * tileSize.x + width / 2, (tm.tilemap.height - i) * tileSize.y - tileSize.y / 2));
    temp.add(transform.getPosition());
    temp.mul(Cactus2DApplication.INV_PHYSICS_SCALE);
    rigidbody.setTransform(temp, transform.getAngle());
    rigidbody.setUserData(gameObject);
    bodies.add(rigidbody);
}

From source file:com.sportsboards2d.BaseBoard.java

License:Open Source License

@Override
public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final ITouchArea pTouchArea,
        final float pTouchAreaLocalX, final float pTouchAreaLocalY) {

    PlayerSprite sprite = null;//from  w  ww. j a  v  a2 s .  co  m

    float[] Xs = null;
    float[] Ys = null;

    float moveX, moveY, angleDeg, diff;
    moveX = 0;
    moveY = 0;

    Line arrowLineMain = null;

    if ((pTouchArea) instanceof RefSprite) {

        switch (pSceneTouchEvent.getAction()) {

        case TouchEvent.ACTION_DOWN:

            this.mRef.setScale(2.0f);
            this.mRef.setmGrabbed(true);

            return true;

        case TouchEvent.ACTION_MOVE:

            if (this.mRef.ismGrabbed()) {
                this.mRef.setPosition(pSceneTouchEvent.getX() - PLAYER_OFFSET,
                        pSceneTouchEvent.getY() - PLAYER_OFFSET);
            }
            return true;

        case TouchEvent.ACTION_UP:

            if (this.mRef.ismGrabbed()) {
                this.mRef.setmGrabbed(false);
                this.mRef.setScale(1.0f);
            }
            return true;
        }
    } else if ((pTouchArea) instanceof BallSprite) {

        switch (pSceneTouchEvent.getAction()) {

        case TouchEvent.ACTION_DOWN:

            this.mBall.setScale(2.0f);
            this.mBall.setmGrabbed(true);

            return true;

        case TouchEvent.ACTION_MOVE:

            if (this.mBall.ismGrabbed()) {
                this.mBall.setPosition(pSceneTouchEvent.getX() - PLAYER_OFFSET,
                        pSceneTouchEvent.getY() - PLAYER_OFFSET);
            }
            return true;

        case TouchEvent.ACTION_UP:

            if (this.mBall.ismGrabbed()) {
                this.mBall.setmGrabbed(false);
                this.mBall.setScale(1.0f);
            }
            return true;
        }
    } else if ((pTouchArea) instanceof PlayerSprite) {

        mHoldDetector.onTouchEvent(pSceneTouchEvent);
        sprite = (PlayerSprite) pTouchArea;
        selectedPlayer = sprite;
        Body body = this.mPhysicsWorld.getPhysicsConnectorManager().findBodyByShape(sprite);

        if (sprite.getPlayer().getpTeam().equalsIgnoreCase("red")) {
            LineFactory.setColor(config.rTeamLineColor);
        } else if (sprite.getPlayer().getpTeam().equalsIgnoreCase("blue")) {
            LineFactory.setColor(config.bTeamLineColor);
        }

        switch (pSceneTouchEvent.getAction()) {
        case TouchEvent.ACTION_DOWN:

            sprite.setScale(2.0f);
            sprite.setmGrabbed(true);

            if (config.playerInfoDisplayToggle && config.playerInfoDisplayWhenMode == 0) {
                sprite.displayInfo(true);
            }
            path.clear();
            sprite.setStartX(sprite.getX() + PLAYER_OFFSET);
            sprite.setStartY(sprite.getY() + PLAYER_OFFSET);

            left = LineFactory.createLine(0, 0, 0, 0, 8);
            right = LineFactory.createLine(0, 0, 0, 0, 8);

            this.mMainScene.getChild(LINE_LAYER).attachChild(left);
            this.mMainScene.getChild(LINE_LAYER).attachChild(right);

            Log.d(TAG, "spriteY: " + sprite.getY());
            Log.d(TAG, "spriteX: " + sprite.getX());

            path.add(new Coordinates(sprite.getX(), sprite.getY()));
            lines.add(left);
            lines.add(right);

            return true;

        case TouchEvent.ACTION_MOVE:

            if (sprite.ismGrabbed()) {

                diff = MathUtils.distance(sprite.getStartX(), sprite.getStartY(), pSceneTouchEvent.getX(),
                        pSceneTouchEvent.getY());

                if (diff > 25) {

                    sprite.setPosition(pSceneTouchEvent.getX(), pSceneTouchEvent.getY());
                    body.setTransform(new Vector2(pSceneTouchEvent.getX() / 32, pSceneTouchEvent.getY() / 32),
                            0);

                    moveX = sprite.getX();
                    moveY = sprite.getY();

                    path.add(new Coordinates(sprite.getX() - PLAYER_OFFSET, sprite.getY() - PLAYER_OFFSET));

                    if (config.lineEnabled) {// if drawing lines is enabled

                        float relativeX = 2 * (MathUtils.bringToBounds(0, sprite.getWidth(), pTouchAreaLocalX)
                                / sprite.getWidth() - 0.5f);
                        float relativeY = 2 * (MathUtils.bringToBounds(0, sprite.getHeight(), pTouchAreaLocalY)
                                / sprite.getHeight() - 0.5f);

                        angleDeg = MathUtils.radToDeg((float) Math.atan2(-relativeX, relativeY));

                        left.setPosition(moveX, moveY, moveX - 20, moveY - 20);
                        right.setPosition(moveX, moveY, moveX + 20, moveY - 20);

                        left.setRotation(angleDeg);
                        right.setRotation(angleDeg);

                        arrowLineMain = LineFactory.createLine(sprite.getStartX(), sprite.getStartY(), moveX,
                                moveY, 8);
                        this.mMainScene.getChild(LINE_LAYER).attachChild(arrowLineMain);
                        this.lines.add(arrowLineMain);
                        sprite.setStartX(moveX);
                        sprite.setStartY(moveY);
                    }
                }
            }
            return true;

        case TouchEvent.ACTION_UP:

            if (sprite.ismGrabbed()) {
                sprite.setmGrabbed(false);
                sprite.setScale(1.0f);

                if (config.playerInfoDisplayToggle && config.playerInfoDisplayWhenMode == 0) {
                    sprite.displayInfo(false);
                }

                if (recording) {
                    if (path.size() > 1) {

                        Xs = new float[path.size()];
                        Ys = new float[path.size()];

                        for (int i = path.size() - 1; i >= 0; i--) {
                            Xs[i] = path.get(i).getX();
                            Ys[i] = path.get(i).getY();
                        }

                        final PathModifier.Path path1 = new PathModifier.Path(Xs, Ys);
                        pathList.add(new SpritePath(selectedPlayer, path1));
                        Log.d(TAG, "after record: " + Xs[0] + " " + Ys[0]);
                        sprite.setPosition(Xs[0], Ys[0]);
                        Xs[0] = Xs[0] + PLAYER_OFFSET;
                        Ys[0] = Ys[0] + PLAYER_OFFSET;
                        body.setTransform(new Vector2(Xs[0] / 32, Ys[0] / 32), 0);
                    }
                }

            }
            return true;
        }
    }
    /*
     * else if((pTouchArea) instanceof ButtonSprite){
     * 
     * int buttonPushed = buttons.indexOf(pTouchArea);
     * 
     * switch(buttonPushed){
     * 
     * case Constants.PLAY_BUTTON: for(SpritePath path:pathList){
     * PathModifier path1 = new PathModifier(1.0f, path.getPath());
     * path.getSprite().registerEntityModifier(path1); }
     * 
     * return true;
     * 
     * case Constants.STOP_BUTTON: recording = false; pathList.clear();
     * return true;
     * 
     * case Constants.PAUSE_BUTTON:
     * 
     * return true;
     * 
     * case Constants.RECORD_BUTTON: recording = true; return true;
     * 
     * case Constants.REWIND_BUTTON: return true; }
     * 
     * 
     * }
     */
    return false;
}

From source file:com.strategames.engine.gameobject.types.Icecube.java

License:Open Source License

@Override
protected void setupBody(Body body) {
    //      Gdx.app.debug("Icecube", "0");

    body.setTransform(body.getPosition(), getRotation() * MathUtils.degreesToRadians);

    int size = this.parts.size();
    for (int i = 0; i < size; i++) {
        Part part = this.parts.get(i);
        String name = part.getName();
        loader.attachFixture(body, name, i, fixtureDef);
    }//from   w w  w  .  j a  v a  2s. c o  m

    body.setSleepingAllowed(false);
}

From source file:com.tnf.ptm.entities.planet.PlanetManager.java

License:Apache License

private boolean recoverObj(PtmObject obj, float toNp, float npMinH) {
    if (npMinH < toNp) {
        return false;
    }/*  www.java  2 s.co  m*/
    if (!(obj instanceof PtmShip)) {
        return false;
    }
    PtmShip ship = (PtmShip) obj;
    Hull hull = ship.getHull();
    if (hull.config.getType() == HullConfig.Type.STATION) {
        return false;
    }
    float fh = myNearestPlanet.getFullHeight();
    Vector2 npPos = myNearestPlanet.getPos();
    Vector2 toShip = PtmMath.distVec(npPos, ship.getPosition());
    float len = toShip.len();
    if (len == 0) {
        toShip.set(0, fh);
    } else {
        toShip.scl(fh / len);
    }
    toShip.add(npPos);
    Body body = hull.getBody();
    body.setTransform(toShip, 0);
    body.setLinearVelocity(Vector2.Zero);
    PtmMath.free(toShip);
    return true;
}