org.jbox2d.dynamics.Body类的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(11.1k)|赞(0)|评价(0)|浏览(136)

本文整理了Java中org.jbox2d.dynamics.Body类的一些代码示例,展示了Body类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Body类的具体详情如下:
包路径:org.jbox2d.dynamics.Body
类名称:Body

Body介绍

[英]A rigid body. These are created via World.createBody.
[中]刚体。这些都是通过World创建的。createBody。

代码示例

代码示例来源:origin: konsoletyper/teavm

private void initAxis() {
  BodyDef axisDef = new BodyDef();
  axisDef.type = BodyType.STATIC;
  axisDef.position = new Vec2(3, 3);
  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);
}

代码示例来源:origin: libgdx/libgdx

public void initialize(Body bA, Body bB) {
  bodyA = bA;
  bodyB = bB;
  Vec2 xB = bodyB.getPosition();
  bodyA.getLocalPointToOut(xB, linearOffset);

  float angleA = bodyA.getAngle();
  float angleB = bodyB.getAngle();
  angularOffset = angleB - angleA;
 }
}

代码示例来源:origin: libgdx/libgdx

/**
 * Call this after you are done with time steps to clear the forces. You normally call this after
 * each call to Step, unless you are performing sub-steps. By default, forces will be
 * automatically cleared, so you don't need to call this function.
 * 
 * @see setAutoClearForces
 */
public void clearForces() {
 for (Body body = m_bodyList; body != null; body = body.getNext()) {
  body.m_force.setZero();
  body.m_torque = 0.0f;
 }
}

代码示例来源:origin: libgdx/libgdx

/**
  * Initialize the bodies, anchors, axis, and reference angle using the world anchor and world
  * axis.
  */
 public void initialize(Body b1, Body b2, Vec2 anchor, Vec2 axis) {
  bodyA = b1;
  bodyB = b2;
  bodyA.getLocalPointToOut(anchor, localAnchorA);
  bodyB.getLocalPointToOut(anchor, localAnchorB);
  bodyA.getLocalVectorToOut(axis, localAxisA);
  referenceAngle = bodyB.getAngle() - bodyA.getAngle();
 }
}

代码示例来源:origin: libgdx/libgdx

/**
  * Initialize the bodies, anchors, and reference angle using the world anchor.
  * 
  * @param b1
  * @param b2
  * @param anchor
  */
 public void initialize(final Body b1, final Body b2, final Vec2 anchor) {
  bodyA = b1;
  bodyB = b2;
  bodyA.getLocalPointToOut(anchor, localAnchorA);
  bodyB.getLocalPointToOut(anchor, localAnchorB);
  referenceAngle = bodyB.getAngle() - bodyA.getAngle();
 }
}

代码示例来源:origin: jbox2d/jbox2d

@Override
public synchronized void step(TestbedSettings settings) {
 super.step(settings);
 if (m_count < MAX_NUM) {
  BodyDef bd = new BodyDef();
  bd.type = BodyType.DYNAMIC;
  bd.position.set(0.0f, 10.0f);
  Body body = m_world.createBody(bd);
  PolygonShape shape = new PolygonShape();
  shape.setAsBox(0.125f, 0.125f);
  body.createFixture(shape, 1.0f);
  ++m_count;
 }
}

代码示例来源:origin: libgdx/libgdx

for (Body b = m_bodyList; b != null; b = b.getNext()) {
 xf.set(b.getTransform());
 for (Fixture f = b.getFixtureList(); f != null; f = f.getNext()) {
  if (b.isActive() == false) {
   color.set(0.5f, 0.5f, 0.3f);
   drawShape(f, xf, color, wireframe);
  } else if (b.getType() == BodyType.STATIC) {
   color.set(0.5f, 0.9f, 0.3f);
   drawShape(f, xf, color, wireframe);
  } else if (b.getType() == BodyType.KINEMATIC) {
   color.set(0.5f, 0.5f, 0.9f);
   drawShape(f, xf, color, wireframe);
  } else if (b.isAwake() == false) {
color.set(0.9f, 0.3f, 0.9f);
for (Body b = m_bodyList; b != null; b = b.getNext()) {
 if (b.isActive() == false) {
  continue;
 for (Fixture f = b.getFixtureList(); f != null; f = f.getNext()) {
  for (int i = 0; i < f.m_proxyCount; ++i) {
   FixtureProxy proxy = f.m_proxies[i];
for (Body b = m_bodyList; b != null; b = b.getNext()) {
 xf.set(b.getTransform());
 xf.p.set(b.getWorldCenter());
 m_debugDraw.drawTransform(xf);

代码示例来源:origin: jbox2d/jbox2d

private void launchBomb(Vec2 position, Vec2 velocity) {
 if (bomb != null) {
  m_world.destroyBody(bomb);
  bomb = null;
 }
 // todo optimize this
 BodyDef bd = new BodyDef();
 bd.type = BodyType.DYNAMIC;
 bd.position.set(position);
 bd.bullet = true;
 bomb = m_world.createBody(bd);
 bomb.setLinearVelocity(velocity);
 CircleShape circle = new CircleShape();
 circle.m_radius = 0.3f;
 FixtureDef fd = new FixtureDef();
 fd.shape = circle;
 fd.density = 20f;
 fd.restitution = 0;
 Vec2 minV = new Vec2(position);
 Vec2 maxV = new Vec2(position);
 minV.subLocal(new Vec2(.3f, .3f));
 maxV.addLocal(new Vec2(.3f, .3f));
 aabb.lowerBound.set(minV);
 aabb.upperBound.set(maxV);
 bomb.createFixture(fd);
}

代码示例来源:origin: jbox2d/jbox2d

getWorld().setGravity(new Vec2(0.0f, 0.0f));
 shape.set(new Vec2(-20.0f, -20.0f), new Vec2(-20.0f, 20.0f));
 ground.createFixture(sd);
 ground.createFixture(sd);
 ground.createFixture(sd);
 ground.createFixture(sd);
 PolygonShape poly1 = new PolygonShape();
 poly1.set(vertices, 3);
 PolygonShape poly2 = new PolygonShape();
 bd.angle = MathUtils.PI;
 bd.allowSleep = false;
 m_body = getWorld().createBody(bd);
 m_body.createFixture(sd1);
 m_body.createFixture(sd2);
  body.createFixture(fd);
  float I = body.getInertia();
  float mass = body.getMass();

代码示例来源:origin: jbox2d/jbox2d

void Break() {
 // Create two bodies from one.
 Body body1 = m_piece1.getBody();
 Vec2 center = body1.getWorldCenter();
 body1.destroyFixture(m_piece2);
 m_piece2 = null;
 BodyDef bd = new BodyDef();
 bd.type = BodyType.DYNAMIC;
 bd.position = body1.getPosition();
 bd.angle = body1.getAngle();
 Body body2 = getWorld().createBody(bd);
 m_piece2 = body2.createFixture(m_shape2, 1.0f);
 // Compute consistent velocities for new bodies based on
 // cached velocity.
 Vec2 center1 = body1.getWorldCenter();
 Vec2 center2 = body2.getWorldCenter();
 Vec2 velocity1 = m_velocity.add(Vec2.cross(m_angularVelocity, center1.sub(center)));
 Vec2 velocity2 = m_velocity.add(Vec2.cross(m_angularVelocity, center2.sub(center)));
 body1.setAngularVelocity(m_angularVelocity);
 body1.setLinearVelocity(velocity1);
 body2.setAngularVelocity(m_angularVelocity);
 body2.setLinearVelocity(velocity2);
}

代码示例来源:origin: konsoletyper/teavm

private void initReel() {
  BodyDef reelDef = new BodyDef();
  reelDef.type = BodyType.DYNAMIC;
  reelDef.position = new Vec2(3, 3);
  reel = world.createBody(reelDef);
  FixtureDef fixture = new FixtureDef();
  fixture.friction = 0.5f;
  fixture.restitution = 0.4f;
  fixture.density = 1;
  int parts = 30;
  for (int i = 0; i < parts; ++i) {
    PolygonShape shape = new PolygonShape();
    double angle1 = i / (double) parts * 2 * Math.PI;
    double x1 = 2.7 * Math.cos(angle1);
    double y1 = 2.7 * Math.sin(angle1);
    double angle2 = (i + 1) / (double) parts * 2 * Math.PI;
    double x2 = 2.7 * Math.cos(angle2);
    double y2 = 2.7 * Math.sin(angle2);
    double angle = (angle1 + angle2) / 2;
    double x = 0.01 * Math.cos(angle);
    double y = 0.01 * Math.sin(angle);
    shape.set(new Vec2[] { new Vec2((float) x1, (float) y1), new Vec2((float) x2, (float) y2),
        new Vec2((float) (x2 - x), (float) (y2 - y)), new Vec2((float) (x1 - x), (float) (y1 - y)) }, 4);
    fixture.shape = shape;
    reel.createFixture(fixture);
  }
}

代码示例来源:origin: jbox2d/jbox2d

CircleShape circleShape = new CircleShape();
 circleShape.m_radius = 1;
 Shape shape = circleShape;
 BodyDef bodyDef = new BodyDef();
 bodyDef.type = BodyType.DYNAMIC;
 bodyDef.position.set(-5, 0);
 bodyDef.allowSleep = false;
 pendulum = getWorld().createBody(bodyDef);
 pendulum.createFixture(shape, 1);
 BodyDef bodyDef = new BodyDef();
 bodyDef.type = BodyType.STATIC;
 ground = getWorld().createBody(bodyDef);
 jointDef.initialize(pendulum, ground, new Vec2(0, 0));
else
 jointDef.initialize(ground, pendulum, new Vec2(0, 0));
pendulum.applyAngularImpulse(10000);
getWorld().createJoint(jointDef);

代码示例来源:origin: jbox2d/jbox2d

public void createCircle()
{
  float radius = 2.0f;
  CircleShape shape = new CircleShape();
  shape.m_p.setZero();
  shape.m_radius = radius;
  FixtureDef fd = new FixtureDef();
  fd.shape = shape;
  fd.density = 1.0f;
  fd.friction = 0.0f;
  Vec2 p = new Vec2((float)Math.random(), 3.0f + (float)Math.random());
  BodyDef bd = new BodyDef();
  bd.type = BodyType.DYNAMIC;
  bd.position = p;
  //bd.allowSleep = false;
  Body body = getWorld().createBody(bd);
  body.createFixture(fd);
}

代码示例来源:origin: jbox2d/jbox2d

switch (argBody.getType()) {
 case DYNAMIC:
  builder.setType(PbBodyType.DYNAMIC);
 default:
  UnsupportedObjectException e = new UnsupportedObjectException("Unknown body type: "
    + argBody.getType(), Type.BODY);
  if (listener == null || listener.isUnsupported(e)) {
   throw e;
builder.setPosition(vecToPb(argBody.getPosition()));
builder.setAngle(argBody.getAngle());
builder.setLinearVelocity(vecToPb(argBody.getLinearVelocity()));
builder.setAngularVelocity(argBody.getAngularVelocity());
builder.setLinearDamping(argBody.getLinearDamping());
builder.setAngularDamping(argBody.getAngularDamping());
builder.setGravityScale(argBody.getGravityScale());
builder.setBullet(argBody.isBullet());
builder.setAllowSleep(argBody.isSleepingAllowed());
builder.setAwake(argBody.isAwake());
builder.setActive(argBody.isActive());
builder.setFixedRotation(argBody.isFixedRotation());

代码示例来源:origin: jbox2d/jbox2d

@Override
public void initTest(boolean deserialized) {
 {
  BodyDef bd = new BodyDef();
  Body ground = m_world.createBody(bd);
  ChainShape shape = new ChainShape();
  Vec2[] vertices =
    new Vec2[] {new Vec2(-20, 0), new Vec2(20, 0), new Vec2(20, 40), new Vec2(-20, 40)};
  shape.createLoop(vertices, 4);
  ground.createFixture(shape, 0.0f);
 }
 m_world.setParticleRadius(0.15f);
 m_world.setParticleDamping(0.2f);
 {
  PolygonShape shape = new PolygonShape();
  shape.setAsBox(8, 10, new Vec2(-12, 10.1f), 0);
  ParticleGroupDef pd = new ParticleGroupDef();
  pd.shape = shape;
  m_world.createParticleGroup(pd);
 }
}

代码示例来源:origin: konsoletyper/teavm

private void initBalls() {
  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;
      ballDef.position.x = 3 + x;
      ballDef.position.y = 3 + y;
      Body body = world.createBody(ballDef);
      body.createFixture(fixtureDef);
      body = world.createBody(ballDef);
      body.createFixture(fixtureDef);
      body = world.createBody(ballDef);
      body.createFixture(fixtureDef);
      body = world.createBody(ballDef);
      body.createFixture(fixtureDef);

代码示例来源:origin: libgdx/libgdx

@Override
public void initVelocityConstraints(final SolverData data) {
 m_indexB = m_bodyB.m_islandIndex;
 m_localCenterB.set(m_bodyB.m_sweep.localCenter);
 m_invMassB = m_bodyB.m_invMass;
 m_invIB = m_bodyB.m_invI;
 float mass = m_bodyB.getMass();
 Rot.mulToOutUnsafe(qB, temp.set(m_localAnchorB).subLocal(m_localCenterB), m_rB);

代码示例来源:origin: libgdx/libgdx

public ConstantVolumeJoint(World argWorld, ConstantVolumeJointDef def) {
 super(argWorld.getPool(), def);
 world = argWorld;
 if (def.bodies.size() <= 2) {
 for (int i = 0; i < targetLengths.length; ++i) {
  final int next = (i == targetLengths.length - 1) ? 0 : i + 1;
  float dist = bodies[i].getWorldCenter().sub(bodies[next].getWorldCenter()).length();
  targetLengths[i] = dist;
   djd.initialize(bodies[i], bodies[next], bodies[i].getWorldCenter(),
     bodies[next].getWorldCenter());
   distanceJoints[i] = (DistanceJoint) world.createJoint(djd);
  normals[i] = new Vec2();

代码示例来源:origin: mirkosertic/GameComposer

@Override
public void drawDebug(PhysicsDebugCanvas aCanvas) {
    Body theBody = physicsWorld.getBodyList();
    while (theBody != null) {
      float theRotatedAngle = theBody.getAngle();
      Vec2 theBodyPosition = theBody.getPosition();
      aCanvas.drawPosition(toPosition(theBodyPosition));
      Fixture theFixture = theBody.getFixtureList();
      while (theFixture != null) {
        Shape theShape = theFixture.getShape();
        if (theShape.getType() == ShapeType.POLYGON) {
          PolygonShape thePolyShape = (PolygonShape) theShape;
          for (int i = 1; i < thePolyShape.getVertexCount(); i++) {
            aCanvas.drawLine(
                toPosition(thePolyShape.getVertex(i - 1), theBodyPosition, theRotatedAngle),
                toPosition(thePolyShape.getVertex(i), theBodyPosition, theRotatedAngle),
                theBody.isAwake());
                  theRotatedAngle),
              toPosition(thePolyShape.getVertex(0), theBodyPosition, theRotatedAngle), theBody
              .isAwake());
      theBody = theBody.getNext();

代码示例来源:origin: libgdx/libgdx

if (seed.isAwake() == false || seed.isActive() == false) {
  continue;
 if (seed.getType() == BodyType.STATIC) {
  continue;
  assert (b.isActive() == true);
  island.add(b);
  b.setAwake(true);
  if (b.getType() == BodyType.STATIC) {
   continue;
   if (other.isActive() == false) {
    continue;
  if (b.getType() == BodyType.STATIC) {
   b.m_flags &= ~Body.e_islandFlag;
for (Body b = m_bodyList; b != null; b = b.getNext()) {
 if (b.getType() == BodyType.STATIC) {
  continue;
 b.synchronizeFixtures();

相关文章