Java Code Examples for org.jbox2d.dynamics.Body#getMass()

The following examples show how to use org.jbox2d.dynamics.Body#getMass() . 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: TestbedTest.java    From jbox2d with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private void spawnMouseJoint(Vec2 p) {
  if (mouseJoint != null) {
    return;
  }
  queryAABB.lowerBound.set(p.x - .001f, p.y - .001f);
  queryAABB.upperBound.set(p.x + .001f, p.y + .001f);
  callback.point.set(p);
  callback.fixture = null;
  m_world.queryAABB(callback, queryAABB);

  if (callback.fixture != null) {
    Body body = callback.fixture.getBody();
    MouseJointDef def = new MouseJointDef();
    def.bodyA = groundBody;
    def.bodyB = body;
    def.collideConnected = true;
    def.target.set(p);
    def.maxForce = 1000f * body.getMass();
    mouseJoint = (MouseJoint) m_world.createJoint(def);
    body.setAwake(true);
  }
}
 
Example 2
Source File: CollisionProcessing.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void step(TestbedSettings settings) {
  super.step(settings);

  // We are going to destroy some bodies according to contact
  // points. We must buffer the bodies that should be destroyed
  // because they may belong to multiple contact points.
  HashSet<Body> nuke = new HashSet<Body>();

  // Traverse the contact results. Destroy bodies that
  // are touching heavier bodies.
  for (int i = 0; i < getPointCount(); ++i) {
    ContactPoint point = points[i];

    Body body1 = point.fixtureA.getBody();
    Body body2 = point.fixtureB.getBody();
    float mass1 = body1.getMass();
    float mass2 = body2.getMass();

    if (mass1 > 0.0f && mass2 > 0.0f) {
      if (mass2 > mass1) {
        nuke.add(body1);
      } else {
        nuke.add(body2);
      }
    }
  }

  // Sort the nuke array to group duplicates.
  // Arrays.sort(nuke);

  // Destroy the bodies, skipping duplicates.
  for (Body b : nuke) {

    if (b != getBomb()) {
      getWorld().destroyBody(b);
    }
  }
}
 
Example 3
Source File: ParticleSystem.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public boolean reportFixture(Fixture fixture) {
  if (fixture.isSensor()) {
    return true;
  }
  final Shape shape = fixture.getShape();
  Body b = fixture.getBody();
  Vec2 bp = b.getWorldCenter();
  float bm = b.getMass();
  float bI = b.getInertia() - bm * b.getLocalCenter().lengthSquared();
  float invBm = bm > 0 ? 1 / bm : 0;
  float invBI = bI > 0 ? 1 / bI : 0;
  int childCount = shape.getChildCount();
  for (int childIndex = 0; childIndex < childCount; childIndex++) {
    AABB aabb = fixture.getAABB(childIndex);
    final float aabblowerBoundx = aabb.lowerBound.x - system.m_particleDiameter;
    final float aabblowerBoundy = aabb.lowerBound.y - system.m_particleDiameter;
    final float aabbupperBoundx = aabb.upperBound.x + system.m_particleDiameter;
    final float aabbupperBoundy = aabb.upperBound.y + system.m_particleDiameter;
    int firstProxy =
        lowerBound(
            system.m_proxyBuffer,
            system.m_proxyCount,
            computeTag(system.m_inverseDiameter * aabblowerBoundx, system.m_inverseDiameter
                * aabblowerBoundy));
    int lastProxy =
        upperBound(
            system.m_proxyBuffer,
            system.m_proxyCount,
            computeTag(system.m_inverseDiameter * aabbupperBoundx, system.m_inverseDiameter
                * aabbupperBoundy));

    for (int proxy = firstProxy; proxy != lastProxy; ++proxy) {
      int a = system.m_proxyBuffer[proxy].index;
      Vec2 ap = system.m_positionBuffer.data[a];
      if (aabblowerBoundx <= ap.x && ap.x <= aabbupperBoundx && aabblowerBoundy <= ap.y
          && ap.y <= aabbupperBoundy) {
        float d;
        final Vec2 n = tempVec;
        d = fixture.computeDistance(ap, childIndex, n);
        if (d < system.m_particleDiameter) {
          float invAm =
              (system.m_flagsBuffer.data[a] & ParticleType.b2_wallParticle) != 0 ? 0 : system
                  .getParticleInvMass();
          final float rpx = ap.x - bp.x;
          final float rpy = ap.y - bp.y;
          float rpn = rpx * n.y - rpy * n.x;
          if (system.m_bodyContactCount >= system.m_bodyContactCapacity) {
            int oldCapacity = system.m_bodyContactCapacity;
            int newCapacity =
                system.m_bodyContactCount != 0
                    ? 2 * system.m_bodyContactCount
                    : Settings.minParticleBufferCapacity;
            system.m_bodyContactBuffer =
                BufferUtils.reallocateBuffer(ParticleBodyContact.class,
                    system.m_bodyContactBuffer, oldCapacity, newCapacity);
            system.m_bodyContactCapacity = newCapacity;
          }
          ParticleBodyContact contact = system.m_bodyContactBuffer[system.m_bodyContactCount];
          contact.index = a;
          contact.body = b;
          contact.weight = 1 - d * system.m_inverseDiameter;
          contact.normal.x = -n.x;
          contact.normal.y = -n.y;
          contact.mass = 1 / (invAm + invBm + invBI * rpn * rpn);
          system.m_bodyContactCount++;
        }
      }
    }
  }
  return true;
}