Java Code Examples for com.badlogic.gdx.physics.box2d.Body#setType()

The following examples show how to use com.badlogic.gdx.physics.box2d.Body#setType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Box2DFactory.java    From homescreenarcade with GNU General Public License v3.0 6 votes vote down vote up
/** Creates a circle object with the given position and radius. Resitution defaults to 0.6. */
public static Body createCircle(World world, float x, float y, float radius, boolean isStatic) {
    CircleShape sd = new CircleShape();
    sd.setRadius(radius);

    FixtureDef fdef = new FixtureDef();
    fdef.shape = sd;
    fdef.density = 1.0f;
    fdef.friction = 0.3f;
    fdef.restitution = 0.6f;

    BodyDef bd = new BodyDef();
    bd.allowSleep = true;
    bd.position.set(x, y);
    Body body = world.createBody(bd);
    body.createFixture(fdef);
    if (isStatic) {
        body.setType(BodyDef.BodyType.StaticBody);
    }
    else {
        body.setType(BodyDef.BodyType.DynamicBody);
    }
    return body;
}
 
Example 2
Source File: Box2DFactory.java    From homescreenarcade with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Creates a wall by constructing a rectangle whose corners are (xmin,ymin) and (xmax,ymax),
 * and rotating the box counterclockwise through the given angle, with specified restitution.
 */
public static Body createWall(World world, float xmin, float ymin, float xmax, float ymax,
        float angle, float restitution) {
    float cx = (xmin + xmax) / 2;
    float cy = (ymin + ymax) / 2;
    float hx = Math.abs((xmax - xmin) / 2);
    float hy = Math.abs((ymax - ymin) / 2);
    PolygonShape wallshape = new PolygonShape();
    // Don't set the angle here; instead call setTransform on the body below. This allows future
    // calls to setTransform to adjust the rotation as expected.
    wallshape.setAsBox(hx, hy, new Vector2(0f, 0f), 0f);

    FixtureDef fdef = new FixtureDef();
    fdef.shape = wallshape;
    fdef.density = 1.0f;
    if (restitution>0) fdef.restitution = restitution;

    BodyDef bd = new BodyDef();
    bd.position.set(cx, cy);
    Body wall = world.createBody(bd);
    wall.createFixture(fdef);
    wall.setType(BodyDef.BodyType.StaticBody);
    wall.setTransform(cx, cy, angle);
    return wall;
}
 
Example 3
Source File: Box2DSynchronizerPre.java    From riiablo with Apache License 2.0 5 votes vote down vote up
@Override
protected void process(int entityId) {
  Body body = mBox2DBody.get(entityId).body;
  Vector2 velocity = mVelocity.get(entityId).velocity;
  body.setLinearVelocity(velocity);

  // FIXME: This is a temp fix to prevent shoving NPCs -- need to explore Contact Filters
  if (velocity.isZero()) {
    body.setType(BodyDef.BodyType.StaticBody);
  } else {
    body.setType(BodyDef.BodyType.DynamicBody);
  }
}
 
Example 4
Source File: Box2DFactory.java    From uracer-kotd with Apache License 2.0 4 votes vote down vote up
/** Creates a wall by constructing a rectangle whose corners are (xmin,ymin) and (xmax,ymax), and rotating the box
 * counterclockwise through the given angle, with specified restitution. */
public static Body createWall (World world, float xmin, float ymin, float xmax, float ymax, float angle, float restitution) {
	float cx = (xmin + xmax) / 2;
	float cy = (ymin + ymax) / 2;
	float hx = (xmax - xmin) / 2;
	float hy = (ymax - ymin) / 2;
	if (hx < 0) {
		hx = -hx;
	}

	if (hy < 0) {
		hy = -hy;
	}

	PolygonShape wallshape = new PolygonShape();
	wallshape.setAsBox(hx, hy, new Vector2(0f, 0f), angle);

	FixtureDef fdef = new FixtureDef();
	fdef.shape = wallshape;
	fdef.density = 1.0f;
	fdef.friction = 0.02f;

	fdef.filter.groupIndex = CollisionFilters.GroupTrackWalls;
	fdef.filter.categoryBits = CollisionFilters.CategoryTrackWalls;
	fdef.filter.maskBits = CollisionFilters.MaskWalls;

	if (Config.Debug.TraverseWalls) {
		fdef.filter.groupIndex = CollisionFilters.GroupNoCollisions;
	}

	if (restitution > 0) {
		fdef.restitution = restitution;
	}

	BodyDef bd = new BodyDef();
	bd.position.set(cx, cy);
	Body wall = world.createBody(bd);
	wall.createFixture(fdef);
	wall.setType(BodyDef.BodyType.StaticBody);
	return wall;
}