本文整理汇总了Java中org.jbox2d.dynamics.World.createBody方法的典型用法代码示例。如果您正苦于以下问题:Java World.createBody方法的具体用法?Java World.createBody怎么用?Java World.createBody使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.jbox2d.dynamics.World
的用法示例。
在下文中一共展示了World.createBody方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testNewWorld
import org.jbox2d.dynamics.World; //导入方法依赖的package包/类
@Test
public void testNewWorld() {
World world = new World(new Vec2(0, -9.8f));
BodyDef axisDef = new BodyDef();
axisDef.type = BodyType.STATIC;
axisDef.position = new Vec2(3, 3);
Body axis = world.createBody(axisDef);
CircleShape axisShape = new CircleShape();
axisShape.setRadius(0.02f);
axisShape.m_p.set(0, 0);
//FixtureDef axisFixture = new FixtureDef();
//axisFixture.shape = axisShape;
//axis.createFixture(axisFixture);
}
示例2: makeDomino
import org.jbox2d.dynamics.World; //导入方法依赖的package包/类
public void makeDomino(float x, float y, boolean horizontal, World world) {
PolygonShape sd = new PolygonShape();
sd.setAsBox(.5f*dwidth, .5f*dheight);
FixtureDef fd = new FixtureDef();
fd.shape = sd;
fd.density = ddensity;
BodyDef bd = new BodyDef();
bd.type = BodyType.DYNAMIC;
fd.friction = dfriction;
fd.restitution = 0.65f;
bd.position = new Vec2(x, y);
bd.angle = horizontal? (float)(Math.PI/2.0):0f;
Body myBody = world.createBody(bd);
myBody.createFixture(fd);
}
示例3: createCollisions
import org.jbox2d.dynamics.World; //导入方法依赖的package包/类
/**
* Cria as colis�es.
* @param world Objeto que representa o mundo do jogo.
* @param collisions V�rtices dos pol�gonos de colis�o.
*/
private void createCollisions(World world, List<List<Vec2>> collisions) {
for (List<Vec2> vertex : collisions) {
BodyDef bd = new BodyDef();
Body b = world.createBody(bd);
for (int i = 0; i < vertex.size() - 1; ++i) {
PolygonDef sd = new PolygonDef();
sd.friction = LevelConfiguration.GROUND_FRICTION;
Utils.createStrokeRect(vertex.get(i), vertex.get(i + 1), LevelConfiguration.STROKE_RADIUS, b, sd);
}
//Adiciona o v�rtice a user data para posterior colis�o.
b.setUserData(vertex);
b.setMassFromShapes();
}
}
示例4: createCheckpoint
import org.jbox2d.dynamics.World; //导入方法依赖的package包/类
/**
* Cria um checkpoint do jogo.
* @param world Objeto que representa o mundo do jogo.
* @param position Posi��o do checkpoint, em unidades do mundo.
* @param position Objeto de estado do jogo.
* @return Corpo do checkpoint.
*/
public static Body createCheckpoint(World world, Vec2 position, LevelState state) {
BodyDef def = new BodyDef();
def.position.set(position);
Body checkpoint = world.createBody(def);
PolygonDef pd = new PolygonDef();
pd.isSensor = true; //Todo checkpoint � um sensor.
pd.userData = "checkpoint";
pd.setAsBox(CHECKPOINT_SIZE.x, CHECKPOINT_SIZE.y);
checkpoint.createShape(pd);
checkpoint.setMassFromShapes();
checkpoint.setUserData(state);
return checkpoint;
}
示例5: initPhysicsBody
import org.jbox2d.dynamics.World; //导入方法依赖的package包/类
@Override Body initPhysicsBody(World world, float x, float y, float angle) {
FixtureDef fixtureDef = new FixtureDef();
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.STATIC;
bodyDef.position = new Vec2(0, 0);
Body body = world.createBody(bodyDef);
// height of the portal contact box
float boxHeight = getHeight() / 12f;
float boxWidth = getWidth() * 0.75f;
PolygonShape polygonShape = new PolygonShape();
Vec2[] polygon = new Vec2[4];
polygon[0] = new Vec2(-boxWidth/2f, getHeight()/2f - boxHeight);
polygon[1] = new Vec2(boxWidth/2f, getHeight()/2f - boxHeight);
polygon[2] = new Vec2(boxWidth/2f, getHeight()/2f);
polygon[3] = new Vec2(-boxWidth/2f, getHeight()/2f);
polygonShape.set(polygon, polygon.length);
fixtureDef.shape = polygonShape;
fixtureDef.friction = 0.1f;
fixtureDef.restitution = 0.8f;
body.createFixture(fixtureDef);
body.setTransform(new Vec2(x, y), angle);
return body;
}
示例6: initPhysicsBody
import org.jbox2d.dynamics.World; //导入方法依赖的package包/类
@Override Body initPhysicsBody(World world, float x, float y, float angle) {
FixtureDef fixtureDef = new FixtureDef();
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.STATIC;
bodyDef.position = new Vec2(0, 0);
Body body = world.createBody(bodyDef);
PolygonShape polygonShape = new PolygonShape();
Vec2[] polygon = new Vec2[4];
polygon[0] = new Vec2(-getWidth()/2f, -getHeight()/2f + getTopOffset());
polygon[1] = new Vec2(getWidth()/2f, -getHeight()/2f + getTopOffset());
polygon[2] = new Vec2(getWidth()/2f, getHeight()/2f);
polygon[3] = new Vec2(-getWidth()/2f, getHeight()/2f);
polygonShape.set(polygon, polygon.length);
fixtureDef.shape = polygonShape;
fixtureDef.friction = 0.1f;
fixtureDef.restitution = 0.8f;
body.createFixture(fixtureDef);
body.setTransform(new Vec2(x, y), angle);
return body;
}
示例7: initPhysicsBody
import org.jbox2d.dynamics.World; //导入方法依赖的package包/类
@Override
Body initPhysicsBody(World world, float x, float y, float angle) {
FixtureDef fixtureDef = new FixtureDef();
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.DYNAMIC;
bodyDef.position = new Vec2(0, 0);
Body body = world.createBody(bodyDef);
CircleShape circleShape = new CircleShape();
circleShape.m_radius = getRadius();
fixtureDef.shape = circleShape;
fixtureDef.density = 0.4f;
fixtureDef.friction = 0.1f;
fixtureDef.restitution = 0.35f;
circleShape.m_p.set(0, 0);
body.createFixture(fixtureDef);
body.setLinearDamping(0.2f);
body.setTransform(new Vec2(x, y), angle);
return body;
}
示例8: initPhysicsBody
import org.jbox2d.dynamics.World; //导入方法依赖的package包/类
@Override
Body initPhysicsBody(World world, float x, float y, float angle) {
FixtureDef fixtureDef = new FixtureDef();
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.STATIC;
bodyDef.position = new Vec2(0, 0);
Body body = world.createBody(bodyDef);
PolygonShape polygonShape = new PolygonShape();
Vec2[] polygon = new Vec2[3];
polygon[0] = new Vec2(getWidth()/2f, -getHeight()/2f + getTopOffset());
polygon[1] = new Vec2(getWidth()/2f, getHeight()/2f);
polygon[2] = new Vec2(-getWidth()/2f, getHeight()/2f);
polygonShape.set(polygon, polygon.length);
fixtureDef.shape = polygonShape;
fixtureDef.friction = 0.1f;
fixtureDef.restitution = 0.9f;
body.createFixture(fixtureDef);
body.setTransform(new Vec2(x, y), angle);
return body;
}
示例9: initPhysicsBody
import org.jbox2d.dynamics.World; //导入方法依赖的package包/类
Body initPhysicsBody(World world, float x, float y, float angle) {
FixtureDef fixtureDef = new FixtureDef();
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.DYNAMIC;
bodyDef.position = new Vec2(0, 0);
Body body = world.createBody(bodyDef);
PolygonShape polygonShape = new PolygonShape();
Vec2[] polygon = new Vec2[4];
polygon[0] = new Vec2(-getWidth() / 2f, -getHeight() / 2f + getTopOffset());
polygon[1] = new Vec2(getWidth() / 2f, -getHeight() / 2f + getTopOffset());
polygon[2] = new Vec2(getWidth() / 2f, polygon[0].y + getSpringBoxHeight());
polygon[3] = new Vec2(-getWidth() / 2f, polygon[1].y + getSpringBoxHeight());
polygonShape.set(polygon, polygon.length);
fixtureDef.shape = polygonShape;
fixtureDef.friction = 0.1f;
fixtureDef.restitution = 1.4f;
body.createFixture(fixtureDef);
body.setTransform(new Vec2(x, y), angle);
return body;
}
示例10: initPhysicsBody
import org.jbox2d.dynamics.World; //导入方法依赖的package包/类
@Override
Body initPhysicsBody(World world, float x, float y, float angle) {
FixtureDef fixtureDef = new FixtureDef();
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.STATIC;
bodyDef.position = new Vec2(0, 0);
Body body = world.createBody(bodyDef);
PolygonShape polygonShape = new PolygonShape();
Vec2[] polygon = new Vec2[4];
polygon[0] = new Vec2(-getWidth()/2f, -getHeight()/2f + getTopOffset());
polygon[1] = new Vec2(getWidth()/2f, -getHeight()/2f + getTopOffset());
polygon[2] = new Vec2(getWidth()/2f, getHeight()/2f);
polygon[3] = new Vec2(-getWidth()/2f, getHeight()/2f);
polygonShape.set(polygon, polygon.length);
fixtureDef.shape = polygonShape;
fixtureDef.friction = 1.0f;
fixtureDef.restitution = 0.3f;
body.createFixture(fixtureDef);
body.setTransform(new Vec2(x, y), angle);
return body;
}
示例11: initPhysicsBody
import org.jbox2d.dynamics.World; //导入方法依赖的package包/类
@Override
Body initPhysicsBody(World world, float x, float y, float angle) {
FixtureDef fixtureDef = new FixtureDef();
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.STATIC;
bodyDef.position = new Vec2(0, 0);
Body body = world.createBody(bodyDef);
PolygonShape polygonShape = new PolygonShape();
Vec2[] polygon = new Vec2[3];
polygon[0] = new Vec2(-getWidth()/2f, -getHeight()/2f + getTopOffset());
polygon[1] = new Vec2(getWidth()/2f, getHeight()/2f);
polygon[2] = new Vec2(-getWidth()/2f, getHeight()/2f);
polygonShape.set(polygon, polygon.length);
fixtureDef.shape = polygonShape;
fixtureDef.friction = 0.1f;
fixtureDef.restitution = 0.9f;
body.createFixture(fixtureDef);
body.setTransform(new Vec2(x, y), angle);
return body;
}
示例12: initPhysicsBody
import org.jbox2d.dynamics.World; //导入方法依赖的package包/类
@Override
Body initPhysicsBody(World world, float x, float y, float angle) {
FixtureDef fixtureDef = new FixtureDef();
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.STATIC;
bodyDef.position = new Vec2(0, 0);
Body body = world.createBody(bodyDef);
// height of the portal contact box
float boxHeight = getHeight() / 12f;
float boxWidth = getWidth() * 0.75f;
PolygonShape polygonShape = new PolygonShape();
Vec2[] polygon = new Vec2[4];
polygon[0] = new Vec2(-boxWidth/2f, getHeight()/2f - boxHeight);
polygon[1] = new Vec2(boxWidth/2f, getHeight()/2f - boxHeight);
polygon[2] = new Vec2(boxWidth/2f, getHeight()/2f);
polygon[3] = new Vec2(-boxWidth/2f, getHeight()/2f);
polygonShape.set(polygon, polygon.length);
fixtureDef.shape = polygonShape;
fixtureDef.friction = 0.1f;
fixtureDef.restitution = 0.8f;
body.createFixture(fixtureDef);
body.setTransform(new Vec2(x, y), angle);
return body;
}
示例13: initPhysicsBody
import org.jbox2d.dynamics.World; //导入方法依赖的package包/类
@Override
Body initPhysicsBody(World world, float x, float y, float angle) {
FixtureDef fixtureDef = new FixtureDef();
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.STATIC;
bodyDef.position = new Vec2(0, 0);
Body body = world.createBody(bodyDef);
PolygonShape polygonShape = new PolygonShape();
Vec2[] polygon = new Vec2[4];
polygon[0] = new Vec2(-getWidth()/2f, -getHeight()/2f + getTopOffset());
polygon[1] = new Vec2(getWidth()/2f, -getHeight()/2f + getTopOffset());
polygon[2] = new Vec2(getWidth()/2f, getHeight()/2f);
polygon[3] = new Vec2(-getWidth()/2f, getHeight()/2f);
polygonShape.set(polygon, polygon.length);
fixtureDef.shape = polygonShape;
fixtureDef.friction = 0.1f;
fixtureDef.restitution = 0.8f;
body.createFixture(fixtureDef);
body.setTransform(new Vec2(x, y), angle);
return body;
}
示例14: testSimpleBall
import org.jbox2d.dynamics.World; //导入方法依赖的package包/类
@Test
public void testSimpleBall() {
World world = new World(new Vec2(0, -9.8f));
float ballRadius = 0.15f;
BodyDef ballDef = new BodyDef();
ballDef.type = BodyType.DYNAMIC;
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.friction = 0.3f;
fixtureDef.restitution = 0.3f;
fixtureDef.density = 0.2f;
CircleShape shape = new CircleShape();
shape.m_radius = ballRadius;
fixtureDef.shape = shape;
int i=0;
int j=0;
float x = (j + 0.5f) * (ballRadius * 2 + 0.01f);
float y = (i + 0.5f) * (ballRadius * 2 + 0.01f);
ballDef.position.x = 3 + x;
ballDef.position.y = 3 + y;
Body theBall = world.createBody(ballDef);
theBall.createFixture(fixtureDef);
for (int k=0;k<100;k++) {
world.step(0.01f, 20, 40);
}
Vec2 thePosition = theBall.getPosition();
int theX = (int)(thePosition.x * 1000);
int theY = (int) (thePosition.y * 1000);
System.out.println("Finally ended at ");
System.out.println(theX);
System.out.println(theY);
}
示例15: Box2DAdapter
import org.jbox2d.dynamics.World; //导入方法依赖的package包/类
public Box2DAdapter( World world, final Compound<SphericalParticle> compound, final ModelViewTransform transform ) {
this.world = world;
this.compound = compound;
this.transform = transform;
//First create the body def at the right location
BodyDef bodyDef = new BodyDef() {{
Vector2D box2DPosition = transform.modelToView( compound.getPosition() );
position = new Vec2( (float) box2DPosition.getX(), (float) box2DPosition.getY() );
angle = (float) compound.getAngle();
//Have to specify the type as dynamic or it won't move
type = BodyType.DYNAMIC;
}};
body = world.createBody( bodyDef );
//Add a little bit of linear and rotational friction so the system doesn't accelerate out of control
body.setLinearDamping( 1 );
body.setAngularDamping( 1 );
//Add shapes for all of the constituents as rigid fixtures to the box2d shape
for ( int i = 0; i < compound.numberConstituents(); i++ ) {
final Constituent<SphericalParticle> constituent = compound.getConstituent( i );
//Create the shape to add to the body
final CircleShape shape = new CircleShape() {{
m_radius = (float) transform.modelToViewDeltaX( constituent.particle.radius );
//Set the position within the molecule
Vector2D boxOffset = transform.modelToViewDelta( constituent.relativePosition );
m_p.set( (float) boxOffset.getX(), (float) boxOffset.getY() );
}};
//Add the shape to the body
Fixture f = body.createFixture( shape, 1 );
//Add a little bit of bounciness to keep things moving randomly
f.setRestitution( 0.1f );
}
}