本文整理汇总了Java中com.badlogic.gdx.physics.box2d.World.createBody方法的典型用法代码示例。如果您正苦于以下问题:Java World.createBody方法的具体用法?Java World.createBody怎么用?Java World.createBody使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.badlogic.gdx.physics.box2d.World
的用法示例。
在下文中一共展示了World.createBody方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createCircle
import com.badlogic.gdx.physics.box2d.World; //导入方法依赖的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;
}
示例2: createWall
import com.badlogic.gdx.physics.box2d.World; //导入方法依赖的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;
}
示例3: makePolygonGround
import com.badlogic.gdx.physics.box2d.World; //导入方法依赖的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;
}
示例4: makeRectGround
import com.badlogic.gdx.physics.box2d.World; //导入方法依赖的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;
}
示例5: WaterModel
import com.badlogic.gdx.physics.box2d.World; //导入方法依赖的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()));
}
示例6: InteractiveTileObject
import com.badlogic.gdx.physics.box2d.World; //导入方法依赖的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);
}
示例7: createSpinner
import com.badlogic.gdx.physics.box2d.World; //导入方法依赖的package包/类
public static Body createSpinner(World world) {
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.DynamicBody;
bodyDef.position.set(Constants.SPINNER_POSITIONS);
Body body = world.createBody(bodyDef);
CircleShape circle = new CircleShape();
circle.setRadius(Constants.SPINNER_SIZE);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = circle;
fixtureDef.density = Constants.SPENNER_DENSITY;
fixtureDef.friction = 0f;
fixtureDef.restitution = 0.45f;
body.createFixture(fixtureDef);
circle.dispose();
return body;
}
示例8: createTopTube
import com.badlogic.gdx.physics.box2d.World; //导入方法依赖的package包/类
public static Array<Body> createTopTube(World world, float posX) {
float posY = generateTubePosition();
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.KinematicBody;
bodyDef.position.set(posX, posY);
PolygonShape shape = new PolygonShape();
shape.setAsBox(Constants.TUBE_WIDTH / 2, Constants.TUBE_HEIGHT / 2);
Body bodyTop = world.createBody(bodyDef);
bodyTop.createFixture(shape, 0f);
bodyTop.resetMassData();
shape.dispose();
bodyTop.setLinearVelocity(Constants.TUBE_SPEED, 0.0f);
Array<Body> bodies = new Array<Body>();
bodies.add(bodyTop);
bodies.add(createBottomTube(world, posX, posY));
return bodies;
}
示例9: Box
import com.badlogic.gdx.physics.box2d.World; //导入方法依赖的package包/类
public Box(World world, MapObject object){
super(Assets.getAtlas().findRegion("boxCrate_double"));
Rectangle rect = ((RectangleMapObject) object).getRectangle();
bdef.type = BodyDef.BodyType.DynamicBody;
bdef.position.set((rect.getX()+rect.getWidth()/2)/ QuackHack.PPM, (rect.getY() + rect.getHeight()/2)/QuackHack.PPM); // I don't follow the math
body = world.createBody(bdef);
shape.setAsBox(rect.getWidth() / 2 / QuackHack.PPM, rect.getHeight() / 2 / QuackHack.PPM);
fdef.shape = shape;
fdef.friction = 0.4f;
fdef.density = 0.1f;
body.createFixture(fdef);
boxRegion = new TextureRegion(getTexture(), 0, 0, 128, 128);
setBounds(0,0, 128 / QuackHack.PPM, 128 / QuackHack.PPM);
setRegion(boxRegion);
setOrigin(getHeight()/2, getWidth()/2);
}
示例10: createBody
import com.badlogic.gdx.physics.box2d.World; //导入方法依赖的package包/类
/**
* Called once the parent room is ready to create a body
* for this {@link WorldObject}.
*
* @param world the World to create a body with
*/
void createBody(World world) {
// hold the pixel-based positions
float pxX = this.def.position.x;
float pxY = this.def.position.y;
// set to meters
this.def.position.set(pxX * OverworldController.PIXELS_TO_METERS,
pxY * OverworldController.PIXELS_TO_METERS);
this.body = world.createBody(this.def);
this.body.setUserData(this);
if (this.boundingQueue != null) {
this.boundingQueue.forEach(shape -> {
this.body.createFixture(shape, 0F);
shape.dispose();
});
this.boundingQueue = null;
}
// back to holding the pixel pos
this.def.position.set(pxX, pxY);
}
示例11: Food
import com.badlogic.gdx.physics.box2d.World; //导入方法依赖的package包/类
public Food(World world, float x, float y, int size, float toGive) {
super(x, y);
color = new Color((float) Math.random(), (float) Math.random(), (float) Math.random(), 1);
outline = Advio.instance.darker(color);
BodyDef bd = new BodyDef();
bd.type = BodyType.KinematicBody;
bd.position.set(x, y);
CircleShape shape = new CircleShape();
shape.setRadius(size);
FixtureDef fd = new FixtureDef();
fd.density = 0.1f;
fd.shape = shape;
fd.friction = 0.1f;
// fd.filter.groupIndex = Advio.GROUP_BEINGS;
body = world.createBody(bd);
body.createFixture(fd).setUserData(new UserData("food").add("toGive", toGive));
shape.dispose();
}
示例12: Block
import com.badlogic.gdx.physics.box2d.World; //导入方法依赖的package包/类
public Block(World world, float x, float f, BodyType type, boolean invisible) {
BodyDef bd = new BodyDef();
bd.type = type;
bd.position.set(x, f);
PolygonShape shape = new PolygonShape();
shape.setAsBox(size / 2, size / 2);
FixtureDef fd = new FixtureDef();
fd.density = 0.1f;
fd.shape = shape;
fd.friction = 0.1f;
fd.filter.groupIndex = Advio.GROUP_DONT_COLLIDE_WITH_PLAYER;
// fd.filter.groupIndex = Advio.GROUP_SCENERY;
body = world.createBody(bd);
body.createFixture(fd).setUserData(new UserData("block"));
this.invisible = invisible;
shape.dispose();
}
示例13: PressurePlate
import com.badlogic.gdx.physics.box2d.World; //导入方法依赖的package包/类
public PressurePlate(Level l, World world, float x, float y, float weight, Block[] blocks) {
super(x, y);
this.level = l;
BodyDef bd = new BodyDef();
bd.type = BodyType.KinematicBody;
bd.position.set(x, y);
PolygonShape shape = new PolygonShape();
shape.setAsBox(Block.size / 2, Block.size / 2);
FixtureDef fd = new FixtureDef();
fd.density = 0.0f;
fd.shape = shape;
fd.friction = 0.1f;
fd.isSensor = true;
this.blocks = new ArrayList<Block>(Arrays.asList(blocks));
body = world.createBody(bd);
body.createFixture(fd).setUserData(new UserData("plate").add("pressed", false).add("weight", weight));
shape.dispose();
this.world = world;
}
示例14: Player
import com.badlogic.gdx.physics.box2d.World; //导入方法依赖的package包/类
public Player(World world, float x, float y) {
super(x, y);
this.world = world;
this.graphicsSize = 10;
color = new Color((float) Math.random(), (float) Math.random(), (float) Math.random(), 1);
outline = Advio.instance.darker(color);
BodyDef bd = new BodyDef();
bd.type = BodyType.DynamicBody;
bd.position.set(x, y);
CircleShape shape = new CircleShape();
shape.setRadius(50);
FixtureDef fd = new FixtureDef();
fd.density = 0.0f;
fd.shape = shape;
fd.friction = 0.1f;
body = world.createBody(bd);
body.createFixture(fd).setUserData(new UserData("player"));
shape.dispose();
// size * 2.25f
}
示例15: AgarLogic
import com.badlogic.gdx.physics.box2d.World; //导入方法依赖的package包/类
public AgarLogic(Level l, World world, float x, float y) {
super(x, y);
this.level = l;
BodyDef bd = new BodyDef();
bd.type = BodyType.KinematicBody;
bd.position.set(x, y);
PolygonShape shape = new PolygonShape();
shape.setAsBox(Block.size / 2, Block.size / 2);
FixtureDef fd = new FixtureDef();
fd.density = 0.0f;
fd.shape = shape;
fd.friction = 0.1f;
fd.isSensor = true;
body = world.createBody(bd);
body.createFixture(fd).setUserData(new UserData("agariologic"));
shape.dispose();
this.world = world;
}