本文整理匯總了Java中com.badlogic.gdx.physics.box2d.BodyDef類的典型用法代碼示例。如果您正苦於以下問題:Java BodyDef類的具體用法?Java BodyDef怎麽用?Java BodyDef使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
BodyDef類屬於com.badlogic.gdx.physics.box2d包,在下文中一共展示了BodyDef類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: createBodies
import com.badlogic.gdx.physics.box2d.BodyDef; //導入依賴的package包/類
@Override public void createBodies(World world) {
this.anchorBody = Box2DFactory.createCircle(world, this.cx, this.cy, 0.05f, true);
// Joint angle is 0 when flipper is horizontal.
// The flipper needs to be slightly extended past anchorBody to rotate correctly.
float ext = (this.flipperLength > 0) ? -0.05f : +0.05f;
// Width larger than 0.12 slows rotation?
this.flipperBody = Box2DFactory.createWall(world, cx+ext, cy-0.12f, cx+flipperLength, cy+0.12f, 0f);
flipperBody.setType(BodyDef.BodyType.DynamicBody);
flipperBody.setBullet(true);
flipperBody.getFixtureList().get(0).setDensity(5.0f);
jointDef = new RevoluteJointDef();
jointDef.initialize(anchorBody, flipperBody, new Vector2(this.cx, this.cy));
jointDef.enableLimit = true;
jointDef.enableMotor = true;
// counterclockwise rotations are positive, so flip angles for flippers extending left
jointDef.lowerAngle = (this.flipperLength>0) ? this.minangle : -this.maxangle;
jointDef.upperAngle = (this.flipperLength>0) ? this.maxangle : -this.minangle;
jointDef.maxMotorTorque = 1000f;
this.joint = (RevoluteJoint)world.createJoint(jointDef);
flipperBodySet = Collections.singletonList(flipperBody);
this.setEffectiveMotorSpeed(-this.downspeed); // Force flipper to bottom when field is first created.
}
示例2: createCircle
import com.badlogic.gdx.physics.box2d.BodyDef; //導入依賴的package包/類
/** 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;
}
示例3: createWall
import com.badlogic.gdx.physics.box2d.BodyDef; //導入依賴的package包/類
/**
* 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;
}
示例4: makePolygonGround
import com.badlogic.gdx.physics.box2d.BodyDef; //導入依賴的package包/類
/**
* Creates polygon ground from the given object, at the given world.
*
* @param world The world were the ground will be.
* @param object The object used to initialize the ground.
* @return The body of the created ground.
*/
static Body makePolygonGround(World world, PolygonMapObject object) {
Polygon polygon = object.getPolygon();
// Body and Fixture variables
BodyDef bdef = new BodyDef();
FixtureDef fdef = new FixtureDef();
PolygonShape shape = new PolygonShape();
bdef.type = BodyDef.BodyType.StaticBody;
bdef.position.set(PIXEL_TO_METER * polygon.getX(), PIXEL_TO_METER * polygon.getY());
Body body = world.createBody(bdef);
float[] new_vertices = polygon.getVertices().clone();
for (int i = 0; i < new_vertices.length; i++)
new_vertices[i] *= PIXEL_TO_METER;
shape.set(new_vertices);
fdef.shape = shape;
fdef.filter.categoryBits = GROUND_BIT;
body.createFixture(fdef);
return body;
}
示例5: makeRectGround
import com.badlogic.gdx.physics.box2d.BodyDef; //導入依賴的package包/類
/**
* Creates rectangular ground from the given object, at the given world.
*
* @param world The world were the ground will be.
* @param object The object used to initialize the ground.
* @return The body of the created ground.
*/
static Body makeRectGround(World world, RectangleMapObject object) {
Rectangle rect = object.getRectangle();
// Body and Fixture variables
BodyDef bdef = new BodyDef();
FixtureDef fdef = new FixtureDef();
PolygonShape shape = new PolygonShape();
bdef.type = BodyDef.BodyType.StaticBody;
bdef.position.set(PIXEL_TO_METER * (rect.getX() + rect.getWidth() / 2), PIXEL_TO_METER * (rect.getY() + rect.getHeight() / 2));
Body body = world.createBody(bdef);
shape.setAsBox((rect.getWidth() / 2) * PIXEL_TO_METER, (rect.getHeight() / 2) * PIXEL_TO_METER);
fdef.shape = shape;
fdef.filter.categoryBits = GROUND_BIT;
body.createFixture(fdef);
return body;
}
示例6: WaterModel
import com.badlogic.gdx.physics.box2d.BodyDef; //導入依賴的package包/類
/**
* Water Model's constructor.
* Creates a water model from the given object, into the given world.
*
* @param world The world the water model will be in.
* @param rect The rectangle to create the water model with.
*/
public WaterModel(World world, Rectangle rect) {
// Body and Fixture variables
BodyDef bdef = new BodyDef();
FixtureDef fdef = new FixtureDef();
PolygonShape shape = new PolygonShape();
bdef.type = BodyDef.BodyType.StaticBody;
bdef.position.set(PIXEL_TO_METER * (rect.getX() + rect.getWidth() / 2), PIXEL_TO_METER * (rect.getY() + rect.getHeight() / 2));
this.body = world.createBody(bdef);
shape.setAsBox((rect.getWidth() / 2) * PIXEL_TO_METER, (rect.getHeight() / 2) * PIXEL_TO_METER);
fdef.shape = shape;
fdef.filter.categoryBits = FLUID_BIT;
fdef.isSensor = true;
fdef.density = 1f;
fdef.friction = 0.1f;
fdef.restitution = 0f;
body.createFixture(fdef);
body.setUserData(new BuoyancyController(world, body.getFixtureList().first()));
}
示例7: createStaticBoxBody
import com.badlogic.gdx.physics.box2d.BodyDef; //導入依賴的package包/類
/**
* Create static body from a TiledObject Refer the box2d manual to
* understand the static body
*
* @param o
* TiledObject
*/
public Body createStaticBoxBody(float x, float y, float width, float height) {
BodyDef groundBodyDef = new BodyDef();
groundBodyDef.type = BodyType.StaticBody;
// transform into box2d
x = x * WORLD_TO_BOX;
// get position-y from map
y = Gdx.graphics.getHeight() - y;
// transform into box2d
y = y * WORLD_TO_BOX;
groundBodyDef.position.set(x, y);
Body groundBody = world.createBody(groundBodyDef);
PolygonShape polygon = new PolygonShape();
((PolygonShape) polygon).setAsBox(width * WORLD_TO_BOX / 2, height
* WORLD_TO_BOX / 2);
groundBody.createFixture(polygon, 0.0f);
groundBody.setUserData("static");
return groundBody;
}
示例8: createStaticCircleBody
import com.badlogic.gdx.physics.box2d.BodyDef; //導入依賴的package包/類
/**
* Create static body from a TiledObject Refer the box2d manual to
* understand the static body
*
* @param o
* TiledObject
*/
public Body createStaticCircleBody(float x, float y, float radius) {
BodyDef groundBodyDef = new BodyDef();
groundBodyDef.type = BodyType.StaticBody;
// transform into box2d
x = x * WORLD_TO_BOX;
// get position-y of object from map
y = Gdx.graphics.getHeight() - y;
// transform into box2d
y = y * WORLD_TO_BOX;
groundBodyDef.position.set(x, y);
Body groundBody = world.createBody(groundBodyDef);
CircleShape shape = new CircleShape();
((CircleShape) shape).setRadius(radius * WORLD_TO_BOX / 2);
groundBody.createFixture(shape, 0.0f);
groundBody.setUserData("static");
return groundBody;
}
示例9: createTempBody
import com.badlogic.gdx.physics.box2d.BodyDef; //導入依賴的package包/類
public Body createTempBody(float x, float y, FixtureDef fixtureDef) {
// Dynamic Body
BodyDef bodyDef = new BodyDef();
if (box2dDebug)
bodyDef.type = BodyType.StaticBody;
else
bodyDef.type = BodyType.DynamicBody;
// transform into box2d
x = x * WORLD_TO_BOX;
y = y * WORLD_TO_BOX;
bodyDef.position.set(x, y);
Body body = world.createBody(bodyDef);
Shape shape = new CircleShape();
((CircleShape) shape).setRadius(1 * WORLD_TO_BOX);
if (fixtureDef == null)
throw new GdxRuntimeException("fixtureDef cannot be null!");
fixtureDef.shape = shape;
body.createFixture(fixtureDef);
return body;
}
示例10: createRopeTipBody
import com.badlogic.gdx.physics.box2d.BodyDef; //導入依賴的package包/類
private Body createRopeTipBody()
{
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.DynamicBody;
bodyDef.linearDamping = 0.5f;
Body body = world.createBody(bodyDef);
FixtureDef circleDef = new FixtureDef();
CircleShape circle = new CircleShape();
circle.setRadius(1.0f/PTM_RATIO);
circleDef.shape = circle;
circleDef.density = 10.0f;
// Since these tips don't have to collide with anything
// set the mask bits to zero
circleDef.filter.maskBits = 0x01; //0;
body.createFixture(circleDef);
return body;
}
示例11: createStaticBoxBody
import com.badlogic.gdx.physics.box2d.BodyDef; //導入依賴的package包/類
/**
* Create static body from a TiledObject Refer the box2d manual to
* understand the static body
*
* @param o
* TiledObject
*/
public Body createStaticBoxBody(float x, float y, float width, float height) {
BodyDef groundBodyDef = new BodyDef();
groundBodyDef.type = BodyType.StaticBody;
// transform into box2d
x = x * WORLD_TO_BOX;
// get position-y from map
y = tileMapRenderer.getMapHeightUnits() - y;
// transform into box2d
y = y * WORLD_TO_BOX;
groundBodyDef.position.set(x, y);
Body groundBody = world.createBody(groundBodyDef);
PolygonShape polygon = new PolygonShape();
((PolygonShape) polygon).setAsBox(width * WORLD_TO_BOX / 2, height
* WORLD_TO_BOX / 2);
groundBody.createFixture(polygon, 0.0f);
groundBody.setUserData("static");
return groundBody;
}
示例12: createStaticCircleBody
import com.badlogic.gdx.physics.box2d.BodyDef; //導入依賴的package包/類
/**
* Create static body from a TiledObject Refer the box2d manual to
* understand the static body
*
* @param o
* TiledObject
*/
public Body createStaticCircleBody(float x, float y, float radius) {
BodyDef groundBodyDef = new BodyDef();
groundBodyDef.type = BodyType.StaticBody;
// transform into box2d
x = x * WORLD_TO_BOX;
// get position-y of object from map
y = tileMapRenderer.getMapHeightUnits() - y;
// transform into box2d
y = y * WORLD_TO_BOX;
groundBodyDef.position.set(x, y);
Body groundBody = world.createBody(groundBodyDef);
CircleShape shape = new CircleShape();
((CircleShape) shape).setRadius(radius * WORLD_TO_BOX / 2);
groundBody.createFixture(shape, 0.0f);
groundBody.setUserData("static");
return groundBody;
}
示例13: createTempBody
import com.badlogic.gdx.physics.box2d.BodyDef; //導入依賴的package包/類
public Body createTempBody(float x, float y, FixtureDef fixtureDef) {
// Dynamic Body
BodyDef bodyDef = new BodyDef();
if (box2dDebug)
bodyDef.type = BodyType.StaticBody;
else
bodyDef.type = BodyType.DynamicBody;
// transform into box2d
x = x * WORLD_TO_BOX;
y = y * WORLD_TO_BOX;
bodyDef.position.set(x, y);
Body body = world.createBody(bodyDef);
Shape shape = new CircleShape();
((CircleShape) shape).setRadius(1 * WORLD_TO_BOX);
if (fixtureDef == null)
throw new GdxRuntimeException("fixtureDef cannot be null!");
fixtureDef.shape = shape;
body.createFixture(fixtureDef);
return body;
}
示例14: createBody
import com.badlogic.gdx.physics.box2d.BodyDef; //導入依賴的package包/類
private void createBody() {
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.DynamicBody;
// Set our body's starting position in the world
bodyDef.position.set(Gdx.input.getX() / 100f, camera.viewportHeight - Gdx.input.getY() / 100f);
// Create our body in the world using our body definition
Body body = world.createBody(bodyDef);
// Create a circle shape and set its radius to 6
PolygonShape square = new PolygonShape();
square.setAsBox(0.3f, 0.3f);
// Create a fixture definition to apply our shape to
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = square;
fixtureDef.density = 0.5f;
fixtureDef.friction = 0.5f;
fixtureDef.restitution = 0.5f;
// Create our fixture and attach it to the body
body.createFixture(fixtureDef);
square.dispose();
}
示例15: InteractiveTileObject
import com.badlogic.gdx.physics.box2d.BodyDef; //導入依賴的package包/類
public InteractiveTileObject(World world, TiledMap map, Rectangle bounds){
this.world = world;
this.map = map;
this.bounds = bounds;
BodyDef bdef = new BodyDef();
FixtureDef fdef = new FixtureDef();
PolygonShape shape = new PolygonShape();
bdef.type = BodyDef.BodyType.StaticBody;
bdef.position.set((bounds.getX() + bounds.getWidth() / 2) / NoObjectionGame.PPM, (bounds.getY() + bounds.getHeight() / 2 )/ NoObjectionGame.PPM);
body = world.createBody(bdef);
shape.setAsBox(bounds.getWidth() / 2 / NoObjectionGame.PPM, bounds.getHeight() / 2 / NoObjectionGame.PPM);
fdef.shape = shape;
fixture = body.createFixture(fdef);
}