Example #1
0
 /* (non-Javadoc)
  * @see org.dyn4j.geometry.Convex#getFarthestPoint(org.dyn4j.geometry.Vector, org.dyn4j.geometry.Transform)
  */
 @Override
 public Vector2 getFarthestPoint(Vector2 n, Transform transform) {
   // transform the normal into local space
   Vector2 localn = transform.getInverseTransformedR(n);
   Vector2 point = new Vector2();
   // set the farthest point to the first one
   point.set(this.vertices[0]);
   // prime the projection amount
   double max = localn.dot(this.vertices[0]);
   // loop through the rest of the vertices to find a further point along the axis
   int size = this.vertices.length;
   for (int i = 1; i < size; i++) {
     // get the current vertex
     Vector2 v = this.vertices[i];
     // project the vertex onto the axis
     double projection = localn.dot(v);
     // check to see if the projection is greater than the last
     if (projection > max) {
       // otherwise this point is the farthest so far so clear the array and add it
       point.set(v);
       // set the new maximum
       max = projection;
     }
   }
   // transform the point into world space
   transform.transform(point);
   return point;
 }
Example #2
0
  /* (non-Javadoc)
   * @see org.dyn4j.geometry.Convex#getFarthestFeature(org.dyn4j.geometry.Vector, org.dyn4j.geometry.Transform)
   */
  @Override
  public Edge getFarthestFeature(Vector2 n, Transform transform) {
    // transform the normal into local space
    Vector2 localn = transform.getInverseTransformedR(n);
    Vector2 maximum = new Vector2();
    double max = -Double.MAX_VALUE;
    int index = 0;
    // find the vertex on the polygon that is further along on the penetration axis
    int count = this.vertices.length;
    for (int i = 0; i < count; i++) {
      // get the current vertex
      Vector2 v = this.vertices[i];
      // get the scalar projection of v onto axis
      double projection = localn.dot(v);
      // keep the maximum projection point
      if (projection > max) {
        // set the max point
        maximum.set(v);
        // set the new maximum
        max = projection;
        // save the index
        index = i;
      }
    }

    // once we have the point of maximum
    // see which edge is most perpendicular
    int l = index + 1 == count ? 0 : index + 1;
    int r = index - 1 < 0 ? count - 1 : index - 1;
    Vector2 leftN = this.normals[index == 0 ? count - 1 : index - 1];
    Vector2 rightN = this.normals[index];
    // create the maximum point for the feature (transform the maximum into world space)
    transform.transform(maximum);
    Vertex vm = new Vertex(maximum, index);
    // is the left or right edge more perpendicular?
    if (leftN.dot(localn) < rightN.dot(localn)) {
      Vector2 left = transform.getTransformed(this.vertices[l]);
      Vertex vl = new Vertex(left, l);
      // make sure the edge is the right winding
      return new Edge(vm, vl, vm, maximum.to(left), index + 1);
    } else {
      Vector2 right = transform.getTransformed(this.vertices[r]);
      Vertex vr = new Vertex(right, r);
      // make sure the edge is the right winding
      return new Edge(vr, vm, vm, right.to(maximum), index);
    }
  }