Java Code Examples for org.jbox2d.common.Vec2#set()

The following examples show how to use org.jbox2d.common.Vec2#set() . 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: Distance.java    From jbox2d with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * this returns pooled objects. don't keep or modify them
 * 
 * @return
 */
public void getClosestPoint(final Vec2 out) {
  switch (m_count) {
    case 0:
      assert (false);
      out.setZero();
      return;
    case 1:
      out.set(m_v1.w);
      return;
    case 2:
      case22.set(m_v2.w).mulLocal(m_v2.a);
      case2.set(m_v1.w).mulLocal(m_v1.a).addLocal(case22);
      out.set(case2);
      return;
    case 3:
      out.setZero();
      return;
    default:
      assert (false);
      out.setZero();
      return;
  }
}
 
Example 2
Source File: DynamicTreeTest.java    From jbox2d with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void MoveAABB(AABB aabb) {
	Vec2 d = new Vec2();
	d.x = MathUtils.randomFloat(rand, -0.5f, 0.5f);
	d.y = MathUtils.randomFloat(rand, -0.5f, 0.5f);
	// d.x = 2.0f;
	// d.y = 0.0f;
	aabb.lowerBound.addLocal(d);
	aabb.upperBound.addLocal(d);

	Vec2 c0 = aabb.lowerBound.add(aabb.upperBound).mulLocal(.5f);
	Vec2 min = new Vec2();
	min.set(-worldExtent, 0.0f);
	Vec2 max = new Vec2();
	max.set(worldExtent, 2.0f * worldExtent);
	Vec2 c = MathUtils.clamp(c0, min, max);

	aabb.lowerBound.addLocal(c.sub(c0));
	aabb.upperBound.addLocal(c.sub(c0));
}
 
Example 3
Source File: TestbedTest.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public boolean reportParticle(int index) {
  Vec2 p = world.getParticlePositionBuffer()[index];
  if (shape.testPoint(xf, p)) {
    Vec2 v = world.getParticleVelocityBuffer()[index];
    v.set(velocity);
  }
  return true;
}
 
Example 4
Source File: PolygonShape.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public final void computeCentroidToOut(final Vec2[] vs, final int count, final Vec2 out) {
  assert (count >= 3);

  out.set(0.0f, 0.0f);
  float area = 0.0f;

  // pRef is the reference point for forming triangles.
  // It's location doesn't change the result (except for rounding error).
  final Vec2 pRef = pool1;
  pRef.setZero();

  final Vec2 e1 = pool2;
  final Vec2 e2 = pool3;

  final float inv3 = 1.0f / 3.0f;

  for (int i = 0; i < count; ++i) {
    // Triangle vertices.
    final Vec2 p1 = pRef;
    final Vec2 p2 = vs[i];
    final Vec2 p3 = i + 1 < count ? vs[i + 1] : vs[0];

    e1.set(p2).subLocal(p1);
    e2.set(p3).subLocal(p1);

    final float D = Vec2.cross(e1, e2);

    final float triangleArea = 0.5f * D;
    area += triangleArea;

    // Area weighted centroid
    e1.set(p1).addLocal(p2).addLocal(p3).mulLocal(triangleArea * inv3);
    out.addLocal(e1);
  }

  // Centroid
  assert (area > Settings.EPSILON);
  out.mulLocal(1.0f / area);
}
 
Example 5
Source File: DistanceTest.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void initTest(boolean argDeserialized) {
	
	input.transformA = new Transform();
	input.transformB = new Transform();
	{
		m_transformA = new Transform();
		m_transformA.setIdentity();
		m_transformA.p.set(0.0f, -0.2f);
		m_polygonA = new PolygonShape();
		m_polygonA.setAsBox(10.0f, 0.2f);
	}
	
	{
		m_positionB = new Vec2();
		m_positionB.set(12.017401f, 0.13678508f);
		m_angleB = -0.0109265f;
		
		m_transformB = new Transform();
		m_transformB.set(m_positionB, m_angleB);
		
		m_polygonB = new PolygonShape();
		m_polygonB.setAsBox(2.0f, 0.1f);
	}
	for (int i = 0; i < v.length; i++) {
		v[i] = new Vec2();
	}
}
 
Example 6
Source File: DynamicTreeTest.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void GetRandomAABB(AABB aabb) {
	Vec2 w = new Vec2();
	w.set(2.0f * m_proxyExtent, 2.0f * m_proxyExtent);
	// aabb.lowerBound.x = -m_proxyExtent;
	// aabb.lowerBound.y = -m_proxyExtent + worldExtent;
	aabb.lowerBound.x = MathUtils.randomFloat(rand, -worldExtent,
			worldExtent);
	aabb.lowerBound.y = MathUtils.randomFloat(rand, 0.0f,
			2.0f * worldExtent);
	aabb.upperBound.set(aabb.lowerBound).addLocal(w);
}
 
Example 7
Source File: StackTest.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public float op(Vec2 argVec) {
  argVec.set(MathUtils.randomFloat(-100, 100), MathUtils.randomFloat(-100, 100));
  argVec.mulLocal(3.2f);
  float s = argVec.length();
  argVec.normalize();
  return s;
}
 
Example 8
Source File: PoolingPerf.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public float op(Vec2 argVec) {
  argVec.set(MathUtils.randomFloat(-100, 100), MathUtils.randomFloat(-100, 100));
  argVec.mulLocal(3.2f);
  float s = argVec.length();
  argVec.normalize();
  return s;
}
 
Example 9
Source File: MouseJoint.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
  public void solveVelocityConstraints(final SolverData data) {

    Vec2 vB = data.velocities[m_indexB].v;
    float wB = data.velocities[m_indexB].w;

    // Cdot = v + cross(w, r)
    final Vec2 Cdot = pool.popVec2();
    Vec2.crossToOutUnsafe(wB, m_rB, Cdot);
    Cdot.addLocal(vB);

    final Vec2 impulse = pool.popVec2();
    final Vec2 temp = pool.popVec2();

    temp.set(m_impulse).mulLocal(m_gamma).addLocal(m_C).addLocal(Cdot).negateLocal();
    Mat22.mulToOutUnsafe(m_mass, temp, impulse);

    Vec2 oldImpulse = temp;
    oldImpulse.set(m_impulse);
    m_impulse.addLocal(impulse);
    float maxImpulse = data.step.dt * m_maxForce;
    if (m_impulse.lengthSquared() > maxImpulse * maxImpulse) {
      m_impulse.mulLocal(maxImpulse / m_impulse.length());
    }
    impulse.set(m_impulse).subLocal(oldImpulse);

    vB.x += m_invMassB * impulse.x;
    vB.y += m_invMassB * impulse.y;
    wB += m_invIB * Vec2.cross(m_rB, impulse);

//    data.velocities[m_indexB].v.set(vB);
    data.velocities[m_indexB].w = wB;
    
    pool.pushVec2(3);
  }
 
Example 10
Source File: Distance.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void getWitnessPoints(Vec2 pA, Vec2 pB) {
  switch (m_count) {
    case 0:
      assert (false);
      break;

    case 1:
      pA.set(m_v1.wA);
      pB.set(m_v1.wB);
      break;

    case 2:
      case2.set(m_v1.wA).mulLocal(m_v1.a);
      pA.set(m_v2.wA).mulLocal(m_v2.a).addLocal(case2);
      // m_v1.a * m_v1.wA + m_v2.a * m_v2.wA;
      // *pB = m_v1.a * m_v1.wB + m_v2.a * m_v2.wB;
      case2.set(m_v1.wB).mulLocal(m_v1.a);
      pB.set(m_v2.wB).mulLocal(m_v2.a).addLocal(case2);

      break;

    case 3:
      pA.set(m_v1.wA).mulLocal(m_v1.a);
      case3.set(m_v2.wA).mulLocal(m_v2.a);
      case33.set(m_v3.wA).mulLocal(m_v3.a);
      pA.addLocal(case3).addLocal(case33);
      pB.set(pA);
      // *pA = m_v1.a * m_v1.wA + m_v2.a * m_v2.wA + m_v3.a * m_v3.wA;
      // *pB = *pA;
      break;

    default:
      assert (false);
      break;
  }
}
 
Example 11
Source File: WeldJoint.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
  public boolean solvePositionConstraints(final SolverData data) {
    Vec2 cA = data.positions[m_indexA].c;
    float aA = data.positions[m_indexA].a;
    Vec2 cB = data.positions[m_indexB].c;
    float aB = data.positions[m_indexB].a;
    final Rot qA = pool.popRot();
    final Rot qB = pool.popRot();
    final Vec2 temp = pool.popVec2();
    final Vec2 rA = pool.popVec2();
    final Vec2 rB = pool.popVec2();

    qA.set(aA);
    qB.set(aB);

    float mA = m_invMassA, mB = m_invMassB;
    float iA = m_invIA, iB = m_invIB;

    Rot.mulToOutUnsafe(qA, temp.set(m_localAnchorA).subLocal(m_localCenterA), rA);
    Rot.mulToOutUnsafe(qB, temp.set(m_localAnchorB).subLocal(m_localCenterB), rB);
    float positionError, angularError;

    final Mat33 K = pool.popMat33();
    final Vec2 C1 = pool.popVec2();
    final Vec2 P = pool.popVec2();

    K.ex.x = mA + mB + rA.y * rA.y * iA + rB.y * rB.y * iB;
    K.ey.x = -rA.y * rA.x * iA - rB.y * rB.x * iB;
    K.ez.x = -rA.y * iA - rB.y * iB;
    K.ex.y = K.ey.x;
    K.ey.y = mA + mB + rA.x * rA.x * iA + rB.x * rB.x * iB;
    K.ez.y = rA.x * iA + rB.x * iB;
    K.ex.z = K.ez.x;
    K.ey.z = K.ez.y;
    K.ez.z = iA + iB;
    if (m_frequencyHz > 0.0f) {
      C1.set(cB).addLocal(rB).subLocal(cA).subLocal(rA);

      positionError = C1.length();
      angularError = 0.0f;

      K.solve22ToOut(C1, P);
      P.negateLocal();

      cA.x -= mA * P.x;
      cA.y -= mA * P.y;
      aA -= iA * Vec2.cross(rA, P);

      cB.x += mB * P.x;
      cB.y += mB * P.y;
      aB += iB * Vec2.cross(rB, P);
    } else {
      C1.set(cB).addLocal(rB).subLocal(cA).subLocal(rA);
      float C2 = aB - aA - m_referenceAngle;

      positionError = C1.length();
      angularError = MathUtils.abs(C2);

      final Vec3 C = pool.popVec3();
      final Vec3 impulse = pool.popVec3();
      C.set(C1.x, C1.y, C2);

      K.solve33ToOut(C, impulse);
      impulse.negateLocal();
      P.set(impulse.x, impulse.y);

      cA.x -= mA * P.x;
      cA.y -= mA * P.y;
      aA -= iA * (Vec2.cross(rA, P) + impulse.z);

      cB.x += mB * P.x;
      cB.y += mB * P.y;
      aB += iB * (Vec2.cross(rB, P) + impulse.z);
      pool.pushVec3(2);
    }

//    data.positions[m_indexA].c.set(cA);
    data.positions[m_indexA].a = aA;
//    data.positions[m_indexB].c.set(cB);
    data.positions[m_indexB].a = aB;

    pool.pushVec2(5);
    pool.pushRot(2);
    pool.pushMat33(1);

    return positionError <= Settings.linearSlop && angularError <= Settings.angularSlop;
  }
 
Example 12
Source File: MouseJoint.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public void getAnchorA(Vec2 argOut) {
  argOut.set(m_targetA);
}
 
Example 13
Source File: WeldJoint.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public void getReactionForce(float inv_dt, Vec2 argOut) {
  argOut.set(m_impulse.x, m_impulse.y);
  argOut.mulLocal(inv_dt);
}
 
Example 14
Source File: FrictionJoint.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
   * @see org.jbox2d.dynamics.joints.Joint#initVelocityConstraints(org.jbox2d.dynamics.TimeStep)
   */
  @Override
  public void initVelocityConstraints(final SolverData data) {
    m_indexA = m_bodyA.m_islandIndex;
    m_indexB = m_bodyB.m_islandIndex;
    m_localCenterA.set(m_bodyA.m_sweep.localCenter);
    m_localCenterB.set(m_bodyB.m_sweep.localCenter);
    m_invMassA = m_bodyA.m_invMass;
    m_invMassB = m_bodyB.m_invMass;
    m_invIA = m_bodyA.m_invI;
    m_invIB = m_bodyB.m_invI;

    float aA = data.positions[m_indexA].a;
    Vec2 vA = data.velocities[m_indexA].v;
    float wA = data.velocities[m_indexA].w;

    float aB = data.positions[m_indexB].a;
    Vec2 vB = data.velocities[m_indexB].v;
    float wB = data.velocities[m_indexB].w;


    final Vec2 temp = pool.popVec2();
    final Rot qA = pool.popRot();
    final Rot qB = pool.popRot();

    qA.set(aA);
    qB.set(aB);

    // Compute the effective mass matrix.
    Rot.mulToOutUnsafe(qA, temp.set(m_localAnchorA).subLocal(m_localCenterA), m_rA);
    Rot.mulToOutUnsafe(qB, temp.set(m_localAnchorB).subLocal(m_localCenterB), m_rB);

    // J = [-I -r1_skew I r2_skew]
    // [ 0 -1 0 1]
    // r_skew = [-ry; rx]

    // Matlab
    // K = [ mA+r1y^2*iA+mB+r2y^2*iB, -r1y*iA*r1x-r2y*iB*r2x, -r1y*iA-r2y*iB]
    // [ -r1y*iA*r1x-r2y*iB*r2x, mA+r1x^2*iA+mB+r2x^2*iB, r1x*iA+r2x*iB]
    // [ -r1y*iA-r2y*iB, r1x*iA+r2x*iB, iA+iB]

    float mA = m_invMassA, mB = m_invMassB;
    float iA = m_invIA, iB = m_invIB;

    final Mat22 K = pool.popMat22();
    K.ex.x = mA + mB + iA * m_rA.y * m_rA.y + iB * m_rB.y * m_rB.y;
    K.ex.y = -iA * m_rA.x * m_rA.y - iB * m_rB.x * m_rB.y;
    K.ey.x = K.ex.y;
    K.ey.y = mA + mB + iA * m_rA.x * m_rA.x + iB * m_rB.x * m_rB.x;

    K.invertToOut(m_linearMass);

    m_angularMass = iA + iB;
    if (m_angularMass > 0.0f) {
      m_angularMass = 1.0f / m_angularMass;
    }

    if (data.step.warmStarting) {
      // Scale impulses to support a variable time step.
      m_linearImpulse.mulLocal(data.step.dtRatio);
      m_angularImpulse *= data.step.dtRatio;

      final Vec2 P = pool.popVec2();
      P.set(m_linearImpulse);

      temp.set(P).mulLocal(mA);
      vA.subLocal(temp);
      wA -= iA * (Vec2.cross(m_rA, P) + m_angularImpulse);

      temp.set(P).mulLocal(mB);
      vB.addLocal(temp);
      wB += iB * (Vec2.cross(m_rB, P) + m_angularImpulse);

      pool.pushVec2(1);
    } else {
      m_linearImpulse.setZero();
      m_angularImpulse = 0.0f;
    }
//    data.velocities[m_indexA].v.set(vA);
    if( data.velocities[m_indexA].w != wA) {
      assert(data.velocities[m_indexA].w != wA);
    }
    data.velocities[m_indexA].w = wA;
//    data.velocities[m_indexB].v.set(vB);
    data.velocities[m_indexB].w = wB;

    pool.pushRot(2);
    pool.pushVec2(1);
    pool.pushMat22(1);
  }
 
Example 15
Source File: Body.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Set the mass properties to override the mass properties of the fixtures. Note that this changes
 * the center of mass position. Note that creating or destroying fixtures can also alter the mass.
 * This function has no effect if the body isn't dynamic.
 * 
 * @param massData the mass properties.
 */
public final void setMassData(MassData massData) {
  // TODO_ERIN adjust linear velocity and torque to account for movement of center.
  assert (m_world.isLocked() == false);
  if (m_world.isLocked() == true) {
    return;
  }

  if (m_type != BodyType.DYNAMIC) {
    return;
  }

  m_invMass = 0.0f;
  m_I = 0.0f;
  m_invI = 0.0f;

  m_mass = massData.mass;
  if (m_mass <= 0.0f) {
    m_mass = 1f;
  }

  m_invMass = 1.0f / m_mass;

  if (massData.I > 0.0f && (m_flags & e_fixedRotationFlag) == 0) {
    m_I = massData.I - m_mass * Vec2.dot(massData.center, massData.center);
    assert (m_I > 0.0f);
    m_invI = 1.0f / m_I;
  }

  final Vec2 oldCenter = m_world.getPool().popVec2();
  // Move center of mass.
  oldCenter.set(m_sweep.c);
  m_sweep.localCenter.set(massData.center);
  // m_sweep.c0 = m_sweep.c = Mul(m_xf, m_sweep.localCenter);
  Transform.mulToOutUnsafe(m_xf, m_sweep.localCenter, m_sweep.c0);
  m_sweep.c.set(m_sweep.c0);

  // Update center of mass velocity.
  // m_linearVelocity += Cross(m_angularVelocity, m_sweep.c - oldCenter);
  final Vec2 temp = m_world.getPool().popVec2();
  temp.set(m_sweep.c).subLocal(oldCenter);
  Vec2.crossToOut(m_angularVelocity, temp, temp);
  m_linearVelocity.addLocal(temp);

  m_world.getPool().pushVec2(2);
}
 
Example 16
Source File: MotorJoint.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Get the target linear offset, in frame A, in meters.
 */
public void getLinearOffset(Vec2 out) {
  out.set(m_linearOffset);
}
 
Example 17
Source File: MotorJoint.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public void getAnchorB(Vec2 out) {
  out.set(m_bodyB.getPosition());
}
 
Example 18
Source File: MotorJoint.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public void getAnchorA(Vec2 out) {
  out.set(m_bodyA.getPosition());
}
 
Example 19
Source File: DebugDraw.java    From jbox2d with BSD 2-Clause "Simplified" License 2 votes vote down vote up
/**
 * takes the screen coordinates and puts the corresponding world coordinates in argWorld.
 * 
 * @param screenX
 * @param screenY
 * @param argWorld
 */
public void getScreenToWorldToOut(float screenX, float screenY, Vec2 argWorld) {
  argWorld.set(screenX, screenY);
  viewportTransform.getScreenToWorld(argWorld, argWorld);
}
 
Example 20
Source File: DebugDraw.java    From jbox2d with BSD 2-Clause "Simplified" License 2 votes vote down vote up
/**
 * Takes the world coordinates and puts the corresponding screen coordinates in argScreen.
 * 
 * @param worldX
 * @param worldY
 * @param argScreen
 */
public void getWorldToScreenToOut(float worldX, float worldY, Vec2 argScreen) {
  argScreen.set(worldX, worldY);
  viewportTransform.getWorldToScreen(argScreen, argScreen);
}