Java Code Examples for org.jbox2d.common.Settings#minParticleBufferCapacity()

The following examples show how to use org.jbox2d.common.Settings#minParticleBufferCapacity() . 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: ParticleSystem.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void addContact(int a, int b) {
    assert(a != b);
    Vec2 pa = m_positionBuffer.data[a];
    Vec2 pb = m_positionBuffer.data[b];
    float dx = pb.x - pa.x;
    float dy = pb.y - pa.y;
    float d2 = dx * dx + dy * dy;
//    assert(d2 != 0);
    if (d2 < m_squaredDiameter) {
      if (m_contactCount >= m_contactCapacity) {
        int oldCapacity = m_contactCapacity;
        int newCapacity =
            m_contactCount != 0 ? 2 * m_contactCount : Settings.minParticleBufferCapacity;
        m_contactBuffer =
            BufferUtils.reallocateBuffer(ParticleContact.class, m_contactBuffer, oldCapacity,
                newCapacity);
        m_contactCapacity = newCapacity;
      }
      float invD = d2 != 0 ? MathUtils.sqrt(1 / d2) : Float.MAX_VALUE;
      ParticleContact contact = m_contactBuffer[m_contactCount];
      contact.indexA = a;
      contact.indexB = b;
      contact.flags = m_flagsBuffer.data[a] | m_flagsBuffer.data[b];
      contact.weight = 1 - d2 * invD * m_inverseDiameter;
      contact.normal.x = invD * dx;
      contact.normal.y = invD * dy;
      m_contactCount++;
    }
  }
 
Example 2
Source File: ParticleSystem.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public int createParticle(ParticleDef def) {
    if (m_count >= m_internalAllocatedCapacity) {
      int capacity = m_count != 0 ? 2 * m_count : Settings.minParticleBufferCapacity;
      capacity = limitCapacity(capacity, m_maxCount);
      capacity = limitCapacity(capacity, m_flagsBuffer.userSuppliedCapacity);
      capacity = limitCapacity(capacity, m_positionBuffer.userSuppliedCapacity);
      capacity = limitCapacity(capacity, m_velocityBuffer.userSuppliedCapacity);
      capacity = limitCapacity(capacity, m_colorBuffer.userSuppliedCapacity);
      capacity = limitCapacity(capacity, m_userDataBuffer.userSuppliedCapacity);
      if (m_internalAllocatedCapacity < capacity) {
        m_flagsBuffer.data =
            reallocateBuffer(m_flagsBuffer, m_internalAllocatedCapacity, capacity, false);
        m_positionBuffer.data =
            reallocateBuffer(m_positionBuffer, m_internalAllocatedCapacity, capacity, false);
        m_velocityBuffer.data =
            reallocateBuffer(m_velocityBuffer, m_internalAllocatedCapacity, capacity, false);
        m_accumulationBuffer =
            BufferUtils.reallocateBuffer(m_accumulationBuffer, 0, m_internalAllocatedCapacity,
                capacity, false);
        m_accumulation2Buffer =
            BufferUtils.reallocateBuffer(Vec2.class, m_accumulation2Buffer, 0,
                m_internalAllocatedCapacity, capacity, true);
        m_depthBuffer =
            BufferUtils.reallocateBuffer(m_depthBuffer, 0, m_internalAllocatedCapacity, capacity,
                true);
        m_colorBuffer.data =
            reallocateBuffer(m_colorBuffer, m_internalAllocatedCapacity, capacity, true);
        m_groupBuffer =
            BufferUtils.reallocateBuffer(ParticleGroup.class, m_groupBuffer, 0,
                m_internalAllocatedCapacity, capacity, false);
        m_userDataBuffer.data =
            reallocateBuffer(m_userDataBuffer, m_internalAllocatedCapacity, capacity, true);
        m_internalAllocatedCapacity = capacity;
      }
    }
    if (m_count >= m_internalAllocatedCapacity) {
      return Settings.invalidParticleIndex;
    }
    int index = m_count++;
    m_flagsBuffer.data[index] = def.flags;
    m_positionBuffer.data[index].set(def.position);
//    assertNotSamePosition();
    m_velocityBuffer.data[index].set(def.velocity);
    m_groupBuffer[index] = null;
    if (m_depthBuffer != null) {
      m_depthBuffer[index] = 0;
    }
    if (m_colorBuffer.data != null || def.color != null) {
      m_colorBuffer.data = requestParticleBuffer(m_colorBuffer.dataClass, m_colorBuffer.data);
      m_colorBuffer.data[index].set(def.color);
    }
    if (m_userDataBuffer.data != null || def.userData != null) {
      m_userDataBuffer.data =
          requestParticleBuffer(m_userDataBuffer.dataClass, m_userDataBuffer.data);
      m_userDataBuffer.data[index] = def.userData;
    }
    if (m_proxyCount >= m_proxyCapacity) {
      int oldCapacity = m_proxyCapacity;
      int newCapacity = m_proxyCount != 0 ? 2 * m_proxyCount : Settings.minParticleBufferCapacity;
      m_proxyBuffer =
          BufferUtils.reallocateBuffer(Proxy.class, m_proxyBuffer, oldCapacity, newCapacity);
      m_proxyCapacity = newCapacity;
    }
    m_proxyBuffer[m_proxyCount++].index = index;
    return index;
  }
 
Example 3
Source File: ParticleSystem.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void joinParticleGroups(ParticleGroup groupA, ParticleGroup groupB) {
  assert (groupA != groupB);
  RotateBuffer(groupB.m_firstIndex, groupB.m_lastIndex, m_count);
  assert (groupB.m_lastIndex == m_count);
  RotateBuffer(groupA.m_firstIndex, groupA.m_lastIndex, groupB.m_firstIndex);
  assert (groupA.m_lastIndex == groupB.m_firstIndex);

  int particleFlags = 0;
  for (int i = groupA.m_firstIndex; i < groupB.m_lastIndex; i++) {
    particleFlags |= m_flagsBuffer.data[i];
  }

  updateContacts(true);
  if ((particleFlags & k_pairFlags) != 0) {
    for (int k = 0; k < m_contactCount; k++) {
      final ParticleContact contact = m_contactBuffer[k];
      int a = contact.indexA;
      int b = contact.indexB;
      if (a > b) {
        int temp = a;
        a = b;
        b = temp;
      }
      if (groupA.m_firstIndex <= a && a < groupA.m_lastIndex && groupB.m_firstIndex <= b
          && b < groupB.m_lastIndex) {
        if (m_pairCount >= m_pairCapacity) {
          int oldCapacity = m_pairCapacity;
          int newCapacity =
              m_pairCount != 0 ? 2 * m_pairCount : Settings.minParticleBufferCapacity;
          m_pairBuffer =
              BufferUtils.reallocateBuffer(Pair.class, m_pairBuffer, oldCapacity, newCapacity);
          m_pairCapacity = newCapacity;
        }
        Pair pair = m_pairBuffer[m_pairCount];
        pair.indexA = a;
        pair.indexB = b;
        pair.flags = contact.flags;
        pair.strength = MathUtils.min(groupA.m_strength, groupB.m_strength);
        pair.distance = MathUtils.distance(m_positionBuffer.data[a], m_positionBuffer.data[b]);
        m_pairCount++;
      }
    }
  }
  if ((particleFlags & k_triadFlags) != 0) {
    VoronoiDiagram diagram = new VoronoiDiagram(groupB.m_lastIndex - groupA.m_firstIndex);
    for (int i = groupA.m_firstIndex; i < groupB.m_lastIndex; i++) {
      if ((m_flagsBuffer.data[i] & ParticleType.b2_zombieParticle) == 0) {
        diagram.addGenerator(m_positionBuffer.data[i], i);
      }
    }
    diagram.generate(getParticleStride() / 2);
    JoinParticleGroupsCallback callback = new JoinParticleGroupsCallback();
    callback.system = this;
    callback.groupA = groupA;
    callback.groupB = groupB;
    diagram.getNodes(callback);
  }

  for (int i = groupB.m_firstIndex; i < groupB.m_lastIndex; i++) {
    m_groupBuffer[i] = groupA;
  }
  int groupFlags = groupA.m_groupFlags | groupB.m_groupFlags;
  groupA.m_groupFlags = groupFlags;
  groupA.m_lastIndex = groupB.m_lastIndex;
  groupB.m_firstIndex = groupB.m_lastIndex;
  destroyParticleGroup(groupB);

  if ((groupFlags & ParticleGroupType.b2_solidParticleGroup) != 0) {
    computeDepthForGroup(groupA);
  }
}
 
Example 4
Source File: ParticleSystem.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void callback(int a, int b, int c) {
  final Vec2 pa = system.m_positionBuffer.data[a];
  final Vec2 pb = system.m_positionBuffer.data[b];
  final Vec2 pc = system.m_positionBuffer.data[c];
  final float dabx = pa.x - pb.x;
  final float daby = pa.y - pb.y;
  final float dbcx = pb.x - pc.x;
  final float dbcy = pb.y - pc.y;
  final float dcax = pc.x - pa.x;
  final float dcay = pc.y - pa.y;
  float maxDistanceSquared = Settings.maxTriadDistanceSquared * system.m_squaredDiameter;
  if (dabx * dabx + daby * daby < maxDistanceSquared
      && dbcx * dbcx + dbcy * dbcy < maxDistanceSquared
      && dcax * dcax + dcay * dcay < maxDistanceSquared) {
    if (system.m_triadCount >= system.m_triadCapacity) {
      int oldCapacity = system.m_triadCapacity;
      int newCapacity =
          system.m_triadCount != 0
              ? 2 * system.m_triadCount
              : Settings.minParticleBufferCapacity;
      system.m_triadBuffer =
          BufferUtils.reallocateBuffer(Triad.class, system.m_triadBuffer, oldCapacity,
              newCapacity);
      system.m_triadCapacity = newCapacity;
    }
    Triad triad = system.m_triadBuffer[system.m_triadCount];
    triad.indexA = a;
    triad.indexB = b;
    triad.indexC = c;
    triad.flags =
        system.m_flagsBuffer.data[a] | system.m_flagsBuffer.data[b]
            | system.m_flagsBuffer.data[c];
    triad.strength = def.strength;
    final float midPointx = (float) 1 / 3 * (pa.x + pb.x + pc.x);
    final float midPointy = (float) 1 / 3 * (pa.y + pb.y + pc.y);
    triad.pa.x = pa.x - midPointx;
    triad.pa.y = pa.y - midPointy;
    triad.pb.x = pb.x - midPointx;
    triad.pb.y = pb.y - midPointy;
    triad.pc.x = pc.x - midPointx;
    triad.pc.y = pc.y - midPointy;
    triad.ka = -(dcax * dabx + dcay * daby);
    triad.kb = -(dabx * dbcx + daby * dbcy);
    triad.kc = -(dbcx * dcax + dbcy * dcay);
    triad.s = Vec2.cross(pa, pb) + Vec2.cross(pb, pc) + Vec2.cross(pc, pa);
    system.m_triadCount++;
  }
}
 
Example 5
Source File: ParticleSystem.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void callback(int a, int b, int c) {
  // Create a triad if it will contain particles from both groups.
  int countA =
      ((a < groupB.m_firstIndex) ? 1 : 0) + ((b < groupB.m_firstIndex) ? 1 : 0)
          + ((c < groupB.m_firstIndex) ? 1 : 0);
  if (countA > 0 && countA < 3) {
    int af = system.m_flagsBuffer.data[a];
    int bf = system.m_flagsBuffer.data[b];
    int cf = system.m_flagsBuffer.data[c];
    if ((af & bf & cf & k_triadFlags) != 0) {
      final Vec2 pa = system.m_positionBuffer.data[a];
      final Vec2 pb = system.m_positionBuffer.data[b];
      final Vec2 pc = system.m_positionBuffer.data[c];
      final float dabx = pa.x - pb.x;
      final float daby = pa.y - pb.y;
      final float dbcx = pb.x - pc.x;
      final float dbcy = pb.y - pc.y;
      final float dcax = pc.x - pa.x;
      final float dcay = pc.y - pa.y;
      float maxDistanceSquared = Settings.maxTriadDistanceSquared * system.m_squaredDiameter;
      if (dabx * dabx + daby * daby < maxDistanceSquared
          && dbcx * dbcx + dbcy * dbcy < maxDistanceSquared
          && dcax * dcax + dcay * dcay < maxDistanceSquared) {
        if (system.m_triadCount >= system.m_triadCapacity) {
          int oldCapacity = system.m_triadCapacity;
          int newCapacity =
              system.m_triadCount != 0
                  ? 2 * system.m_triadCount
                  : Settings.minParticleBufferCapacity;
          system.m_triadBuffer =
              BufferUtils.reallocateBuffer(Triad.class, system.m_triadBuffer, oldCapacity,
                  newCapacity);
          system.m_triadCapacity = newCapacity;
        }
        Triad triad = system.m_triadBuffer[system.m_triadCount];
        triad.indexA = a;
        triad.indexB = b;
        triad.indexC = c;
        triad.flags = af | bf | cf;
        triad.strength = MathUtils.min(groupA.m_strength, groupB.m_strength);
        final float midPointx = (float) 1 / 3 * (pa.x + pb.x + pc.x);
        final float midPointy = (float) 1 / 3 * (pa.y + pb.y + pc.y);
        triad.pa.x = pa.x - midPointx;
        triad.pa.y = pa.y - midPointy;
        triad.pb.x = pb.x - midPointx;
        triad.pb.y = pb.y - midPointy;
        triad.pc.x = pc.x - midPointx;
        triad.pc.y = pc.y - midPointy;
        triad.ka = -(dcax * dabx + dcay * daby);
        triad.kb = -(dabx * dbcx + daby * dbcy);
        triad.kc = -(dbcx * dcax + dbcy * dcay);
        triad.s = Vec2.cross(pa, pb) + Vec2.cross(pb, pc) + Vec2.cross(pc, pa);
        system.m_triadCount++;
      }
    }
  }
}
 
Example 6
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;
}