Example #1
0
  /** Tests the getAxes method. */
  @Test
  public void getAxes() {
    Vector2[] vertices =
        new Vector2[] {new Vector2(0.0, 1.0), new Vector2(-1.0, -1.0), new Vector2(1.0, -1.0)};
    Polygon p = new Polygon(vertices);
    Transform t = new Transform();

    Vector2[] axes = p.getAxes(null, t);
    TestCase.assertNotNull(axes);
    TestCase.assertEquals(3, axes.length);

    // test passing some focal points
    Vector2 pt = new Vector2(-3.0, 2.0);
    axes = p.getAxes(new Vector2[] {pt}, t);
    TestCase.assertEquals(4, axes.length);

    // make sure the axes are perpendicular to the edges
    Vector2 ab = p.vertices[0].to(p.vertices[1]);
    Vector2 bc = p.vertices[1].to(p.vertices[2]);
    Vector2 ca = p.vertices[2].to(p.vertices[0]);

    TestCase.assertEquals(0.000, ab.dot(axes[0]), 1.0e-3);
    TestCase.assertEquals(0.000, bc.dot(axes[1]), 1.0e-3);
    TestCase.assertEquals(0.000, ca.dot(axes[2]), 1.0e-3);

    // make sure that the focal axes are correct
    TestCase.assertEquals(0.000, p.vertices[0].to(pt).cross(axes[3]), 1.0e-3);
  }
Example #2
0
  /**
   * Tests the createAABB method.
   *
   * @since 3.1.0
   */
  @Test
  public void createAABB() {
    Vector2[] vertices =
        new Vector2[] {new Vector2(0.0, 1.0), new Vector2(-1.0, -1.0), new Vector2(1.0, -1.0)};
    Polygon p = new Polygon(vertices);

    AABB aabb = p.createAABB(Transform.IDENTITY);
    TestCase.assertEquals(-1.0, aabb.getMinX(), 1.0e-3);
    TestCase.assertEquals(-1.0, aabb.getMinY(), 1.0e-3);
    TestCase.assertEquals(1.0, aabb.getMaxX(), 1.0e-3);
    TestCase.assertEquals(1.0, aabb.getMaxY(), 1.0e-3);

    // try using the default method
    AABB aabb2 = p.createAABB();
    TestCase.assertEquals(aabb.getMinX(), aabb2.getMinX());
    TestCase.assertEquals(aabb.getMinY(), aabb2.getMinY());
    TestCase.assertEquals(aabb.getMaxX(), aabb2.getMaxX());
    TestCase.assertEquals(aabb.getMaxY(), aabb2.getMaxY());

    Transform tx = new Transform();
    tx.rotate(Math.toRadians(30.0));
    tx.translate(1.0, 2.0);
    aabb = p.createAABB(tx);
    TestCase.assertEquals(0.500, aabb.getMinX(), 1.0e-3);
    TestCase.assertEquals(0.634, aabb.getMinY(), 1.0e-3);
    TestCase.assertEquals(2.366, aabb.getMaxX(), 1.0e-3);
    TestCase.assertEquals(2.866, aabb.getMaxY(), 1.0e-3);
  }
Example #3
0
  /** Tests the farthest methods. */
  @Test
  public void getFarthest() {
    Vector2[] vertices =
        new Vector2[] {new Vector2(0.0, 1.0), new Vector2(-1.0, -1.0), new Vector2(1.0, -1.0)};
    Polygon p = new Polygon(vertices);
    Transform t = new Transform();
    Vector2 y = new Vector2(0.0, -1.0);

    Edge f = p.getFarthestFeature(y, t);
    // should always get an edge
    TestCase.assertTrue(f.isEdge());
    TestCase.assertEquals(-1.000, f.max.point.x, 1.0e-3);
    TestCase.assertEquals(-1.000, f.max.point.y, 1.0e-3);
    TestCase.assertEquals(-1.000, f.vertex1.point.x, 1.0e-3);
    TestCase.assertEquals(-1.000, f.vertex1.point.y, 1.0e-3);
    TestCase.assertEquals(1.000, f.vertex2.point.x, 1.0e-3);
    TestCase.assertEquals(-1.000, f.vertex2.point.y, 1.0e-3);

    Vector2 pt = p.getFarthestPoint(y, t);

    TestCase.assertEquals(-1.000, pt.x, 1.0e-3);
    TestCase.assertEquals(-1.000, pt.y, 1.0e-3);

    // rotating about the origin
    t.rotate(Math.toRadians(90), 0, 0);

    pt = p.getFarthestPoint(y, t);

    TestCase.assertEquals(1.000, pt.x, 1.0e-3);
    TestCase.assertEquals(-1.000, pt.y, 1.0e-3);
  }
Example #4
0
 /** Tests the getFoci method. */
 @Test
 public void getFoci() {
   Vector2[] vertices =
       new Vector2[] {new Vector2(0.0, 1.0), new Vector2(-1.0, -1.0), new Vector2(1.0, -1.0)};
   Polygon p = new Polygon(vertices);
   Transform t = new Transform();
   // should return none
   Vector2[] foci = p.getFoci(t);
   TestCase.assertNull(foci);
 }
Example #5
0
    /**
     * Draws the body.
     *
     * <p>Only coded for polygons.
     *
     * @param gl the OpenGL graphics context
     */
    public void render(GL2 gl) {
      // save the original transform
      gl.glPushMatrix();

      // transform the coordinate system from world coordinates to local coordinates
      gl.glTranslated(this.transform.getTranslationX(), this.transform.getTranslationY(), 0.0);
      // rotate about the z-axis
      gl.glRotated(Math.toDegrees(this.transform.getRotation()), 0.0, 0.0, 1.0);

      // loop over all the body fixtures for this body
      for (BodyFixture fixture : this.fixtures) {
        // get the shape on the fixture
        Convex convex = fixture.getShape();
        // check the shape type
        if (convex instanceof Polygon) {
          // since Triangle, Rectangle, and Polygon are all of
          // type Polygon in addition to their main type
          Polygon p = (Polygon) convex;

          // set the color
          gl.glColor4fv(this.color, 0);

          // fill the shape
          gl.glBegin(GL2.GL_POLYGON);
          for (Vector2 v : p.getVertices()) {
            gl.glVertex3d(v.x, v.y, 0.0);
          }
          gl.glEnd();

          // set the color
          gl.glColor4f(this.color[0] * 0.8f, this.color[1] * 0.8f, this.color[2] * 0.8f, 1.0f);

          // draw the shape
          gl.glBegin(GL.GL_LINE_LOOP);
          for (Vector2 v : p.getVertices()) {
            gl.glVertex3d(v.x, v.y, 0.0);
          }
          gl.glEnd();
        }
        // circles and other curved shapes require a little more work, so to keep
        // this example short we only include polygon shapes; see the RenderUtilities
        // in the Sandbox application
      }

      // set the original transform
      gl.glPopMatrix();
    }
Example #6
0
  /** Tests the translate methods. */
  @Test
  public void translate() {
    Vector2[] vertices =
        new Vector2[] {new Vector2(0.0, 1.0), new Vector2(-1.0, -1.0), new Vector2(1.0, -1.0)};
    Polygon p = new Polygon(vertices);

    p.translate(1.0, -0.5);

    TestCase.assertEquals(1.000, p.vertices[0].x, 1.0e-3);
    TestCase.assertEquals(0.500, p.vertices[0].y, 1.0e-3);

    TestCase.assertEquals(0.000, p.vertices[1].x, 1.0e-3);
    TestCase.assertEquals(-1.500, p.vertices[1].y, 1.0e-3);

    TestCase.assertEquals(2.000, p.vertices[2].x, 1.0e-3);
    TestCase.assertEquals(-1.500, p.vertices[2].y, 1.0e-3);
  }
Example #7
0
  /** Tests the rotate methods. */
  @Test
  public void rotate() {
    Vector2[] vertices =
        new Vector2[] {new Vector2(0.0, 1.0), new Vector2(-1.0, -1.0), new Vector2(1.0, -1.0)};
    Polygon p = new Polygon(vertices);

    // should move the points
    p.rotate(Math.toRadians(90), 0, 0);

    TestCase.assertEquals(-1.000, p.vertices[0].x, 1.0e-3);
    TestCase.assertEquals(0.000, p.vertices[0].y, 1.0e-3);

    TestCase.assertEquals(1.000, p.vertices[1].x, 1.0e-3);
    TestCase.assertEquals(-1.000, p.vertices[1].y, 1.0e-3);

    TestCase.assertEquals(1.000, p.vertices[2].x, 1.0e-3);
    TestCase.assertEquals(1.000, p.vertices[2].y, 1.0e-3);
  }
Example #8
0
  /** Tests the contains method. */
  @Test
  public void contains() {
    Vector2[] vertices =
        new Vector2[] {new Vector2(0.0, 1.0), new Vector2(-1.0, 0.0), new Vector2(1.0, 0.0)};
    Polygon p = new Polygon(vertices);

    Transform t = new Transform();
    Vector2 pt = new Vector2(2.0, 4.0);

    // shouldn't be in the polygon
    TestCase.assertTrue(!p.contains(pt, t));

    // move the polygon a bit
    t.translate(2.0, 3.5);

    // should be in the polygon
    TestCase.assertTrue(p.contains(pt, t));

    t.translate(0.0, -0.5);

    // should be on a vertex
    TestCase.assertTrue(p.contains(pt, t));
  }
Example #9
0
  /** Tests the project method. */
  @Test
  public void project() {
    Vector2[] vertices =
        new Vector2[] {new Vector2(0.0, 1.0), new Vector2(-1.0, 0.0), new Vector2(1.0, 0.0)};
    Polygon p = new Polygon(vertices);
    Transform t = new Transform();
    Vector2 x = new Vector2(1.0, 0.0);
    Vector2 y = new Vector2(0.0, 1.0);

    t.translate(1.0, 0.5);

    Interval i = p.project(x, t);

    TestCase.assertEquals(0.000, i.min, 1.0e-3);
    TestCase.assertEquals(2.000, i.max, 1.0e-3);

    // rotating about the center
    t.rotate(Math.toRadians(90), 1.0, 0.5);

    i = p.project(y, t);

    TestCase.assertEquals(-0.500, i.min, 1.0e-3);
    TestCase.assertEquals(1.500, i.max, 1.0e-3);
  }