org.jbox2d.dynamics.Body Java Examples

The following examples show how to use org.jbox2d.dynamics.Body. 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: MoBike.java    From kAndroid with Apache License 2.0 6 votes vote down vote up
private void updateTopAndBottomBounds() {
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyType.STATIC;

    PolygonShape shape = new PolygonShape();
    float hx = mappingView2Body(width);
    float hy = mappingView2Body(proportion);
    shape.setAsBox(hx, hy);

    FixtureDef def = new FixtureDef();
    def.shape = shape;
    def.density = density;
    def.friction = frictionRatio;
    def.restitution = restitutionRatio;

    bodyDef.position.set(0, -hy);
    Body topBody = world.createBody(bodyDef);
    topBody.createFixture(def);

    bodyDef.position.set(0, mappingView2Body(height) + hy);
    Body bottomBody = world.createBody(bodyDef);
    bottomBody.createFixture(def);
}
 
Example #2
Source File: WheelJoint.java    From jbox2d with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public float getJointTranslation() {
  Body b1 = m_bodyA;
  Body b2 = m_bodyB;

  Vec2 p1 = pool.popVec2();
  Vec2 p2 = pool.popVec2();
  Vec2 axis = pool.popVec2();
  b1.getWorldPointToOut(m_localAnchorA, p1);
  b2.getWorldPointToOut(m_localAnchorA, p2);
  p2.subLocal(p1);
  b1.getWorldVectorToOut(m_localXAxisA, axis);

  float translation = Vec2.dot(p2, axis);
  pool.pushVec2(3);
  return translation;
}
 
Example #3
Source File: PolyShapes.java    From jbox2d with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public boolean reportFixture(Fixture fixture) {
  if (m_count == e_maxCount) {
    return false;
  }

  Body body = fixture.getBody();
  Shape shape = fixture.getShape();

  boolean overlap = p.getCollision().testOverlap(shape, 0, m_circle, 0, body.getTransform(),
      m_transform);

  if (overlap) {
    DrawFixture(fixture);
    ++m_count;
  }

  return true;
}
 
Example #4
Source File: ConfinedTest.java    From jbox2d with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void step(TestbedSettings settings) {

	super.step(settings);

	for (Body b = getWorld().getBodyList(); b != null; b = b.getNext())
	{
		if (b.getType() != BodyType.DYNAMIC)
		{
			continue;
		}

		Vec2 p = b.getPosition();
		if (p.x <= -10.0f || 10.0f <= p.x || p.y <= 0.0f || 20.0f <= p.y)
		{
			p.x += 0.0;
		}
	}

	addTextLine("Press 'c' to create a circle");
}
 
Example #5
Source File: DominoTower.java    From jbox2d with BSD 2-Clause "Simplified" License 6 votes vote down vote up
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 = getWorld().createBody(bd);
    myBody.createFixture(fd);
  }
 
Example #6
Source File: PbSerializer.java    From jbox2d with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public SerializationResult serialize(Joint argJoint, Map<Body, Integer> argBodyIndexMap,
    Map<Joint, Integer> argJointIndexMap) {
  PbJoint.Builder builder = serializeJoint(argJoint, argBodyIndexMap, argJointIndexMap);
  if (builder == null) {
    return null;
  }
  final PbJoint joint = builder.build();
  return new SerializationResult() {
    @Override
    public void writeTo(OutputStream argOutputStream) throws IOException {
      joint.writeTo(argOutputStream);
    }

    @Override
    public Object getValue() {
      return joint;
    }
  };
}
 
Example #7
Source File: ConfinedTest.java    From jbox2d with BSD 2-Clause "Simplified" License 6 votes vote down vote up
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);
}
 
Example #8
Source File: Box2DContactListener.java    From Form-N-Fun with MIT License 6 votes vote down vote up
@Override
public void beginContact(Contact cp) {
    // TODO Auto-generated method stub
    Fixture f1 = cp.getFixtureA();
    Fixture f2 = cp.getFixtureB();

    // Get both bodies
    Body b1 = f1.getBody();
    Body b2 = f2.getBody();

    // Get objects that reference these bodies
    Object o1 = b1.getUserData();
    Object o2 = b2.getUserData();

    if ((o1.getClass() == Maze.class && o2.getClass() == Ball.class) || (o1.getClass() == Ball.class && o2.getClass() == Maze.class)) { //if ball touches maze
        balltosurfacecontact(); //play sound
    }

}
 
Example #9
Source File: JBox2DTest.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
@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);

}
 
Example #10
Source File: RayCastTest.java    From jbox2d with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public float reportFixture(Fixture fixture, Vec2 point, Vec2 normal, float fraction) {
  Body body = fixture.getBody();
  Object userData = body.getUserData();
  if (userData != null) {
    int index = (Integer) userData;
    if (index == 0) {
      // filter
      return -1f;
    }
  }

  m_hit = true;
  m_point = point;
  m_normal = normal;
  return fraction;
}
 
Example #11
Source File: RayCastTest.java    From jbox2d with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public float reportFixture(Fixture fixture, Vec2 point, Vec2 normal, float fraction) {
  Body body = fixture.getBody();
  Object userData = body.getUserData();
  if (userData != null) {
    int index = (Integer) userData;
    if (index == 0) {
      // filter
      return -1f;
    }
  }

  m_hit = true;
  m_point = point;
  m_normal = normal;
  return 0f;
}
 
Example #12
Source File: RayCastTest.java    From jbox2d with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public float reportFixture(Fixture fixture, Vec2 point, Vec2 normal, float fraction) {
  Body body = fixture.getBody();
  int index = 0;
  Object userData = body.getUserData();
  if (userData != null) {
    index = (Integer) userData;
    if (index == 0) {
      // filter
      return -1f;
    }
  }

  assert (m_count < e_maxCount);

  m_points[m_count].set(point);
  m_normals[m_count].set(normal);
  ++m_count;

  if (m_count == e_maxCount) {
    return 0f;
  }

  return 1f;
}
 
Example #13
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 #14
Source File: PbSerializer.java    From jbox2d with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public SerializationResult serialize(Body argBody) {
  PbBody.Builder builder = serializeBody(argBody);
  if (builder == null) {
    return null;
  }
  final PbBody body = builder.build();
  return new SerializationResult() {
    @Override
    public void writeTo(OutputStream argOutputStream) throws IOException {
      body.writeTo(argOutputStream);
    }

    @Override
    public Object getValue() {
      return body;
    }
  };
}
 
Example #15
Source File: TestbedTest.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public boolean reportFixture(Fixture argFixture) {
  Body body = argFixture.getBody();
  if (body.getType() == BodyType.DYNAMIC) {
    boolean inside = argFixture.testPoint(point);
    if (inside) {
      fixture = argFixture;

      return false;
    }
  }

  return true;
}
 
Example #16
Source File: WeldJointDef.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Initialize the bodies, anchors, and reference angle using a world anchor point.
 * 
 * @param bA
 * @param bB
 * @param anchor
 */
public void initialize(Body bA, Body bB, Vec2 anchor) {
  bodyA = bA;
  bodyB = bB;
  bodyA.getLocalPointToOut(anchor, localAnchorA);
  bodyB.getLocalPointToOut(anchor, localAnchorB);
  referenceAngle = bodyB.getAngle() - bodyA.getAngle();
}
 
Example #17
Source File: VerticalStack.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void processBody(Body argBody, Long argTag) {
  if (argTag == BULLET_TAG) {
    m_bullet = argBody;
    return;
  }
  super.processBody(argBody, argTag);
}
 
Example #18
Source File: CharacterCollision.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void processBody(Body argBody, Long argTag) {
  if (argTag == CHARACTER_TAG) {
    m_character = argBody;
    return;
  }
  super.processBody(argBody, argTag);
}
 
Example #19
Source File: TheoJansen.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void processBody(Body argBody, Long argTag) {
  if (argTag == CHASSIS_TAG) {
    m_chassis = argBody;
  } else if (argTag == WHEEL_TAG) {
    m_wheel = argBody;
  }
}
 
Example #20
Source File: TheoJansen.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public Long getTag(Body argBody) {
  if (argBody == m_chassis) {
    return CHASSIS_TAG;
  } else if (argBody == m_wheel) {
    return WHEEL_TAG;
  }
  return null;
}
 
Example #21
Source File: VerticalStack.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public Long getTag(Body argBody) {
  if (argBody == m_bullet) {
    return BULLET_TAG;
  }
  return super.getTag(argBody);
}
 
Example #22
Source File: ConstantVolumeJointDef.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Adds a body to the group
 * 
 * @param argBody
 */
public void addBody(Body argBody) {
  bodies.add(argBody);
  if (bodies.size() == 1) {
    bodyA = argBody;
  }
  if (bodies.size() == 2) {
    bodyB = argBody;
  }
}
 
Example #23
Source File: RevoluteJointDef.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * 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();
}
 
Example #24
Source File: BodyTypes.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void processBody(Body body, Long tag) {
  if (tag == ATTACHMENT_TAG) {
    m_attachment = body;
  } else if (tag == PLATFORM_TAG) {
    m_platform = body;
  } else {
    super.processBody(body, tag);
  }
}
 
Example #25
Source File: BodyTypes.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public Long getTag(Body body) {
  if (body == m_attachment)
    return ATTACHMENT_TAG;
  if (body == m_platform)
    return PLATFORM_TAG;
  return super.getTag(body);
}
 
Example #26
Source File: ConstantVolumeJointDef.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Adds a body and the pre-made distance joint. Should only be used for deserialization.
 */
public void addBodyAndJoint(Body argBody, DistanceJoint argJoint) {
  addBody(argBody);
  if (joints == null) {
    joints = new ArrayList<DistanceJoint>();
  }
  joints.add(argJoint);
}
 
Example #27
Source File: ApplyForce.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void processBody(Body body, Long tag) {
  if (tag == BODY_TAG) {
    m_body = body;
  }
  super.processBody(body, tag);
}
 
Example #28
Source File: ApplyForce.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public Long getTag(Body body) {
  if (body == m_body) {
    return BODY_TAG;
  }
  return super.getTag(body);
}
 
Example #29
Source File: MoBike.java    From kAndroid with Apache License 2.0 5 votes vote down vote up
private void updateLeftAndRightBounds() {
    BodyDef bodyDef = new BodyDef();
    //生成静态刚体
    bodyDef.type = BodyType.STATIC;

    //设置形状 - 多边形
    PolygonShape shape = new PolygonShape();

    //hx 1  hy 屏幕高度
    float hx = mappingView2Body(proportion);
    float hy = mappingView2Body(height);
    shape.setAsBox(hx, hy);

    //设置 系数
    FixtureDef def = new FixtureDef();
    def.shape = shape;
    def.density = density;
    def.friction = frictionRatio;
    def.restitution = restitutionRatio;

    //-1 高度
    bodyDef.position.set(-hx, hy);
    Body leftBody = world.createBody(bodyDef);
    //设置位置
    leftBody.createFixture(def);

    //这里便于理解 应该是 w + 1  h
    //而不是 w + 1 0 验证结果一样
    bodyDef.position.set(mappingView2Body(width) + hx, 0);
    Body rightBody = world.createBody(bodyDef);
    rightBody.createFixture(def);
}
 
Example #30
Source File: PulleyJointDef.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Initialize the bodies, anchors, lengths, max lengths, and ratio using the world anchors.
 */
public void initialize(Body b1, Body b2, Vec2 ga1, Vec2 ga2, Vec2 anchor1, Vec2 anchor2, float r) {
  bodyA = b1;
  bodyB = b2;
  groundAnchorA = ga1;
  groundAnchorB = ga2;
  localAnchorA = bodyA.getLocalPoint(anchor1);
  localAnchorB = bodyB.getLocalPoint(anchor2);
  Vec2 d1 = anchor1.sub(ga1);
  lengthA = d1.length();
  Vec2 d2 = anchor2.sub(ga2);
  lengthB = d2.length();
  ratio = r;
  assert (ratio > Settings.EPSILON);
}