public static boolean isPointInside(
      Polygon polygon, float x, float y, boolean toleranceOnOutside) {
    float verts[] = polygon.getTransformedVertices();

    boolean inside = false;

    float oldX = verts[verts.length - 2];
    float oldY = verts[verts.length - 1];

    float oldSqDist = Vector2.dst2(oldX, oldY, x, y);

    for (int i = 0; i < verts.length; i += 2) {
      float newX = verts[i];
      float newY = verts[i + 1];
      float newSqDist = Vector2.dst2(newX, newY, x, y);

      if (oldSqDist
              + newSqDist
              + 2.0f * Math.sqrt(oldSqDist * newSqDist)
              - Vector2.dst2(newX, newY, oldX, oldY)
          < TOLERANCE_IS_POINT_INSIDE) return toleranceOnOutside;

      float leftX = newX;
      float leftY = newY;
      float rightX = oldX;
      float rightY = oldY;

      if (newX > oldX) {
        leftX = oldX;
        leftY = oldY;
        rightX = newX;
        rightY = newY;
      }

      if (leftX < x
          && x <= rightX
          && (y - leftY) * (rightX - leftX) < (rightY - leftY) * (x - leftX)) inside = !inside;

      oldX = newX;
      oldY = newY;
      oldSqDist = newSqDist;
    }

    return inside;
  }
 public void rayCast(RayCastCallback callback, Vector2 point1, Vector2 point2) {
   if (point1.dst2(point2) <= ZERO) {
     Gdx.app.log("World.rayCast", "Called with zero distance");
     return;
   }
   callback.reset();
   callback.result.start.set(toBox(tmp1.set(point1)));
   callback.result.end.set(toBox(tmp2.set(point2)));
   world.rayCast(callback, tmp1, tmp2);
   callback.result.scl(toWorldScale());
 }
Exemple #3
0
  private void setClosestStartPosition(final Vector2 position) {
    Gdx.app.debug(
        TAG,
        "setClosestStartPosition INPUT: ("
            + position.x
            + ","
            + position.y
            + ") "
            + currentMapType.toString());

    // Get last known position on this map
    playerStartPositionRect.set(0, 0);
    closestPlayerStartPosition.set(0, 0);
    float shortestDistance = 0f;

    // Go through all player start positions and choose closest to last known position
    for (MapObject object : spawnsLayer.getObjects()) {
      String objectName = object.getName();

      if (objectName == null || objectName.isEmpty()) {
        continue;
      }

      if (objectName.equalsIgnoreCase(PLAYER_START)) {
        ((RectangleMapObject) object).getRectangle().getPosition(playerStartPositionRect);
        float distance = position.dst2(playerStartPositionRect);

        Gdx.app.debug(TAG, "DISTANCE: " + distance + " for " + currentMapType.toString());

        if (distance < shortestDistance || shortestDistance == 0) {
          closestPlayerStartPosition.set(playerStartPositionRect);
          shortestDistance = distance;
          Gdx.app.debug(
              TAG,
              "closest START is: ("
                  + closestPlayerStartPosition.x
                  + ","
                  + closestPlayerStartPosition.y
                  + ") "
                  + currentMapType.toString());
        }
      }
    }
    playerStart = closestPlayerStartPosition.cpy();
  }
 public boolean checkCollision(Entity e) {
   if (collidable
       && e.collidable
       && position.dst2(e.getPosition()) < MathUtils.sqr(radius + e.getRadius())) return true;
   return false;
 }