Example usage for com.badlogic.gdx.physics.box2d PolygonShape setAsBox

List of usage examples for com.badlogic.gdx.physics.box2d PolygonShape setAsBox

Introduction

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

Prototype

public void setAsBox(float hx, float hy, Vector2 center, float angle) 

Source Link

Document

Build vertices to represent an oriented box.

Usage

From source file:com.esotericsoftware.spine.Box2DExample.java

License:Open Source License

public void create() {
    batch = new SpriteBatch();
    renderer = new ShapeRenderer();
    skeletonRenderer = new SkeletonRenderer();
    skeletonRenderer.setPremultipliedAlpha(true);

    atlas = new TextureAtlas(Gdx.files.internal("spineboy/spineboy.atlas"));

    // This loader creates Box2dAttachments instead of RegionAttachments for an easy way to keep
    // track of the Box2D body for each attachment.
    AtlasAttachmentLoader atlasLoader = new AtlasAttachmentLoader(atlas) {
        public RegionAttachment newRegionAttachment(Skin skin, String name, String path) {
            Box2dAttachment attachment = new Box2dAttachment(name);
            AtlasRegion region = atlas.findRegion(attachment.getName());
            if (region == null)
                throw new RuntimeException("Region not found in atlas: " + attachment);
            attachment.setRegion(region);
            return attachment;
        }/*from   w w w. j  a va 2s  .c  o  m*/
    };
    SkeletonJson json = new SkeletonJson(atlasLoader);
    json.setScale(0.6f * 0.05f);
    SkeletonData skeletonData = json.readSkeletonData(Gdx.files.internal("spineboy/spineboy.json"));
    animation = skeletonData.findAnimation("walk");

    skeleton = new Skeleton(skeletonData);
    skeleton.x = -32;
    skeleton.y = 1;
    skeleton.updateWorldTransform();

    // See Box2DTest in libgdx for more detailed information about Box2D setup.
    camera = new OrthographicCamera(48, 32);
    camera.position.set(0, 16, 0);
    box2dRenderer = new Box2DDebugRenderer();
    createWorld();

    // Create a body for each attachment. Note it is probably better to create just a few bodies rather than one for each
    // region attachment, but this is just an example.
    for (Slot slot : skeleton.getSlots()) {
        if (!(slot.getAttachment() instanceof Box2dAttachment))
            continue;
        Box2dAttachment attachment = (Box2dAttachment) slot.getAttachment();

        PolygonShape boxPoly = new PolygonShape();
        boxPoly.setAsBox(attachment.getWidth() / 2 * attachment.getScaleX(),
                attachment.getHeight() / 2 * attachment.getScaleY(),
                vector.set(attachment.getX(), attachment.getY()), attachment.getRotation() * MathUtils.degRad);

        BodyDef boxBodyDef = new BodyDef();
        boxBodyDef.type = BodyType.StaticBody;
        attachment.body = world.createBody(boxBodyDef);
        attachment.body.createFixture(boxPoly, 1);

        boxPoly.dispose();
    }
}

From source file:com.github.unluckyninja.defenseofhuman.model.entity.Hook.java

License:Open Source License

public Hook(GameWorld gameWorld, Player.HookShooter shooter, Vector2 direction, float launchSpeed) {
    this.gameWorld = gameWorld;
    this.shooter = shooter;

    Vector2 dir = direction.cpy();

    BodyDef bodyDef = new BodyDef();
    bodyDef.position.set(shooter.getPlayer().getLaunchPosition());
    bodyDef.angle = (float) Math.toRadians(dir.angle());
    bodyDef.bullet = true;//from   ww w .j  a  va  2  s  .  c om
    bodyDef.type = BodyDef.BodyType.DynamicBody;
    bodyDef.linearVelocity.set(dir.nor().scl(launchSpeed));

    body = shooter.getPlayer().getWorld().createBody(bodyDef);

    PolygonShape shape = new PolygonShape();
    shape.setAsBox(0.15f, 0.1f, new Vector2(0.15f, 0), 0);

    body.createFixture(shape, 0.01f).setSensor(true);

    shape.dispose();

    body.setUserData(this);

    // maybe we can move all the definitions to fields?
}

From source file:com.hatstick.fireman.physics.Box2DPhysicsWorld.java

License:Apache License

private Fixture createPhysicsObject(Rectangle bounds, ModelType type, Integer id) {

    UserData userData = new UserData(numEnterPoints, type, null);
    CircleShape objectPoly = new CircleShape();
    objectPoly.setRadius(bounds.width / 2);

    BodyDef enemyBodyDef = new BodyDef();
    enemyBodyDef.type = BodyType.DynamicBody;
    enemyBodyDef.position.x = bounds.x;//  ww  w .ja  v a2s  .c om
    enemyBodyDef.position.y = bounds.y;
    Body objectBody = world.createBody(enemyBodyDef);
    FixtureDef objectFixtureDef = new FixtureDef();
    objectFixtureDef.shape = objectPoly;

    //   objectBody.setUserData(userData);
    objectFixtureDef.restitution = .025f;
    Fixture fixture = objectBody.createFixture(objectFixtureDef);
    fixture.setUserData(userData);
    objectBody.setLinearDamping(2f);
    objectBody.setGravityScale(.4f);
    objectBody.setFixedRotation(true);
    objectPoly.dispose();
    numEnterPoints++;

    //add a sensor on the bottom to check if touching ground (for jumping)
    PolygonShape polygonShape = new PolygonShape();
    polygonShape.setAsBox(bounds.width / 3, bounds.height / 8, new Vector2(0, -bounds.height / 2), 0);
    objectFixtureDef = new FixtureDef();
    objectFixtureDef.shape = polygonShape;
    objectFixtureDef.isSensor = true;
    Fixture footSensorFixture = objectBody.createFixture(objectFixtureDef);
    footSensorFixture.setUserData(new UserData(numEnterPoints, ModelType.FOOT_SENSOR, id));

    //add a sensor on left side to check if touching wall (for grappling)
    PolygonShape polygonShape2 = new PolygonShape();
    polygonShape2.setAsBox(bounds.width / 8, bounds.height / 3, new Vector2(-bounds.width / 2, 0), 0);
    objectFixtureDef = new FixtureDef();
    objectFixtureDef.shape = polygonShape2;
    objectFixtureDef.isSensor = true;
    Fixture leftSideSensorFixture = objectBody.createFixture(objectFixtureDef);
    leftSideSensorFixture.setUserData(new UserData(numEnterPoints, ModelType.LEFT_SIDE_SENSOR, id));

    //add a sensor on right side to check if touching wall (for grappling)
    polygonShape2.setAsBox(bounds.width / 8, bounds.height / 3, new Vector2(bounds.width / 2, 0), 0);
    objectFixtureDef = new FixtureDef();
    objectFixtureDef.shape = polygonShape2;
    objectFixtureDef.isSensor = true;
    Fixture rightSideSensorFixture = objectBody.createFixture(objectFixtureDef);
    rightSideSensorFixture.setUserData(new UserData(numEnterPoints, ModelType.RIGHT_SIDE_SENSOR, id));

    return fixture;
}

From source file:com.laex.MyGdxGame.java

License:Open Source License

/**
 * Creates the bodies./*from  w w w .  ja v a2s .c  o m*/
 */
private void createBodies() {
    // this part of box2d code is taken from Tumbler test from Box2D Source
    {
        BodyDef bodyDef = new BodyDef();
        ground = world.createBody(bodyDef);
    }

    {
        BodyDef bodyDef = new BodyDef();
        bodyDef.type = BodyType.DynamicBody;
        bodyDef.allowSleep = true;
        bodyDef.position.set(0.0f, 0.0f);
        body = world.createBody(bodyDef);

        PolygonShape shape = new PolygonShape();

        shape.setAsBox(0.5f, 10.0f, new Vector2(5.0f, 0.0f), 0.0f);
        body.createFixture(shape, 5.0f);

        shape.setAsBox(0.5f, 10.0f, new Vector2(-5.0f, 0.0f), 0.0f);
        body.createFixture(shape, 5.0f);

        shape.setAsBox(10.0f, 0.5f, new Vector2(0.0f, 5.0f), 0.0f);
        body.createFixture(shape, 5.0f);

        shape.setAsBox(10.0f, 0.5f, new Vector2(0.0f, -5.0f), 0.0f);
        body.createFixture(shape, 5.0f);

        RevoluteJointDef rjd = new RevoluteJointDef();
        rjd.bodyA = ground;
        rjd.bodyB = body;
        rjd.localAnchorA.set(0.0f, 10.0f);
        rjd.localAnchorB.set(0.0f, 0.0f);
        rjd.referenceAngle = 0.0f;
        rjd.motorSpeed = 0.05f * MathUtils.PI;
        rjd.maxMotorTorque = 1e8f;
        rjd.enableMotor = true;
        joint = world.createJoint(rjd);

    }

}

From source file:com.me.mygdxgame.WallActor.java

License:Apache License

private void boundsToM(Rectangle boundsPix, PolygonShape rectangleShape) {
    Vector2 halfSizeM = new Vector2(), centerM = new Vector2();
    boundsPix.getPosition(centerM);// ww  w  .j  av a  2  s  .c  om
    centerM.div(gdxTest.getPixelsPerMeter());
    boundsPix.getPosition(centerM);
    centerM.div(gdxTest.getPixelsPerMeter());

    rectangleShape.setAsBox(halfSizeM.x, halfSizeM.y, centerM, 0f);
}

From source file:com.mygdx.entities.DynamicEntities.enemies.En_Goober.java

public En_Goober(Vector2 pos) {
    super(pos, 30f * RATIO, 30f * RATIO);
    fd.restitution = 0.2f;/*from www .java  2 s .  co m*/

    float sscale = 0.38f * RATIO;

    moveSprite = new ImageSprite("goober_move", true);
    moveSprite.sprite.setScale(sscale);
    isprite = moveSprite;

    prepSprite = new ImageSprite("goober_prep", true);
    prepSprite.sprite.setScale(sscale);

    attackSprite = new ImageSprite("goober-attack2", false);
    attackSprite.sprite.setScale(sscale);

    bodyDamageSprite = new ImageSprite("goober-dmg", false);
    bodyDamageSprite.sprite.setScale(sscale);

    prepTime = 1.0f;
    attTime = 0.3f;
    recovTime = 3f;

    attackFC = new FrameCounter_Attack(prepTime, attTime, recovTime);

    //attack sensors
    attFd1 = new FixtureDef();
    PolygonShape vertBox = new PolygonShape();
    vertBox.setAsBox(width * 0.4f / PPM, (75f / PPM) * RATIO, new Vector2(0, (75f / PPM) * RATIO), 0);
    attFd1.shape = vertBox;
    attFd1.isSensor = true;
    attFd1.filter.categoryBits = BIT_EN;
    attFd1.filter.maskBits = BIT_PLAYER;

    attFd2 = new FixtureDef();
    PolygonShape horzBox = new PolygonShape();
    horzBox.setAsBox((75f / PPM) * RATIO, width * 0.4f / PPM, new Vector2((75f / PPM) * RATIO, 0), 0);
    attFd2.shape = horzBox;
    attFd2.isSensor = true;
    attFd2.filter.categoryBits = BIT_EN;
    attFd2.filter.maskBits = BIT_PLAYER;

    attFd3 = new FixtureDef();
    PolygonShape vertBoxSouth = new PolygonShape();
    vertBoxSouth.setAsBox(width * 0.4f / PPM, (75f / PPM) * RATIO, new Vector2(0, -(75f / PPM) * RATIO), 0);
    attFd3.shape = vertBoxSouth;
    attFd3.isSensor = true;
    attFd3.filter.categoryBits = BIT_EN;
    attFd3.filter.maskBits = BIT_PLAYER;

    attFd4 = new FixtureDef();
    PolygonShape horzBoxWest = new PolygonShape();
    horzBoxWest.setAsBox((75f / PPM) * RATIO, width * 0.4f / PPM, new Vector2(-(75f / PPM) * RATIO, 0), 0);
    attFd4.shape = horzBoxWest;
    attFd4.isSensor = true;
    attFd4.filter.categoryBits = BIT_EN;
    attFd4.filter.maskBits = BIT_PLAYER;

    //ai
    moveToSB = new Arrive<Vector2>(this, EnvironmentManager.player).setTimeToTarget(0.01f)
            .setArrivalTolerance(2f).setDecelerationRadius(10);

    wanderSB = new Wander<Vector2>(this).setFaceEnabled(false).setAlignTolerance(0.001f)
            .setDecelerationRadius(5).setTimeToTarget(0.1f).setWanderOffset(90).setWanderOrientation(10)
            .setWanderRadius(40f).setWanderRate(MathUtils.PI2 * 4);

    seekWanderSB = new Seek<Vector2>(this, seekWanderTarget);

    //ai
    this.maxLinearSpeed = 150f;
    this.maxLinearAcceleration = 500f;
    this.maxAngularSpeed = 30f;
    this.maxAngularAcceleration = 5f;

}

From source file:com.nebula2d.components.BoundingBox.java

License:Open Source License

@Override
protected Shape getShape(Transform transform) {
    PolygonShape shape = new PolygonShape();
    shape.setAsBox(extents.halfw, extents.halfh, transform.getPosition(), transform.getRotation());
    return shape;
}

From source file:com.siondream.core.physics.MapBodyManager.java

License:Open Source License

private Shape getRectangle(RectangleMapObject rectangleObject) {
    Rectangle rectangle = rectangleObject.getRectangle();
    PolygonShape polygon = new PolygonShape();
    Vector2 size = new Vector2((rectangle.x + rectangle.width * 0.5f) / units,
            (rectangle.y + rectangle.height * 0.5f) / units);
    polygon.setAsBox(rectangle.width * 0.5f / units, rectangle.height * 0.5f / units, size, 0.0f);
    return polygon;
}

From source file:com.siondream.core.physics.PhysicsLoader.java

License:Apache License

private Shape loadShape(JsonValue root) {
    Shape shape = null;/*  w w w.  j a va2  s .c om*/
    JsonValue shapeValue = root.get("shape");

    if (shapeValue == null) {
        return shape;
    }

    String type = shapeValue.getString("type");

    float x = shapeValue.getFloat("centerX", 0.0f);
    float y = shapeValue.getFloat("centerY", 0.0f);

    if (type.equals("circle")) {
        logger.info("loading cicle shape");
        CircleShape circle = new CircleShape();
        circle.setPosition(new Vector2(x, y));
        circle.setRadius(shapeValue.getFloat("radius", 1.0f));
        shape = circle;
    } else if (type.equals("polygon")) {
        logger.info("loading polygon shape");
        PolygonShape polygon = new PolygonShape();
        polygon.setAsBox(shapeValue.getFloat("width", 1.0f), shapeValue.getFloat("height", 1.0f),
                new Vector2(x, y), 0.0f);
        shape = polygon;
    } else {
        logger.error("shape unknown " + type);
    }

    return shape;
}

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

License:Open Source License

@Override
protected void setupBody(Body body) {
    PolygonShape box = new PolygonShape();
    box.setAsBox(super.halfWidth, super.halfHeight, new Vector2(super.halfWidth, super.halfHeight), 0f);
    body.createFixture(box, 0.0f);/*  w  w  w  .  ja va 2s  .  c o m*/
    box.dispose();
}