コード例 #1
0
ファイル: Entity.java プロジェクト: kmeaw/ultraworm
  public final boolean isTouching(ReadableRectangle rect) {
    if (isRound() && getRadius() == 0) {
      return false;
    }

    if (!isRound() && getBounds(BOUNDS).isEmpty()) {
      return false;
    }

    if (isRound()) {
      return rectRoundCollisionCheck(mapX, mapY, getRadius(), rect);
    } else {
      return BOUNDS.intersects(rect);
    }
  }
コード例 #2
0
ファイル: Entity.java プロジェクト: kmeaw/ultraworm
  /**
   * Collision detection.
   *
   * @param dest The entity to check for collision with.
   * @return true if this entity is touching the destination entity; note that an entity can never
   *     be touching itself
   */
  public final boolean isTouching(Entity dest) {
    if (dest == this) {
      return false;
    }

    if (isRound() && getRadius() == 0) {
      return false;
    }

    if (dest.isRound() && dest.getRadius() == 0) {
      return false;
    }

    if (!isRound() && getBounds(BOUNDS).isEmpty()) {
      return false;
    }

    if (!dest.isRound() && dest.getBounds(TEMP).isEmpty()) {
      return false;
    }

    // At this point, BOUNDS and TEMP hold bounding rectangles, which we might
    // use if it's a rect-rect collision

    if (isRound() && dest.isRound()) {
      // Round-Round collision check
      float dx = dest.mapX + dest.getCollisionX() - (this.mapX + this.getCollisionX());
      float dy = dest.mapY + dest.getCollisionY() - (this.mapY + this.getCollisionY());
      dx *= dx;
      dy *= dy;

      return Math.sqrt(dx + dy) < getRadius() + dest.getRadius();
    } else if (isRound() && !dest.isRound()) {
      // Round-Rect collison check
      return rectRoundCollisionCheck(
          mapX + getCollisionX(), mapY + getCollisionY(), getRadius(), TEMP);
    } else if (!isRound() && dest.isRound()) {
      // Rect-round collision check
      return rectRoundCollisionCheck(
          dest.mapX + dest.getCollisionX(),
          dest.mapY + dest.getCollisionY(),
          dest.getRadius(),
          BOUNDS);
    } else {
      // Rect-rect collision check
      return BOUNDS.intersects(TEMP);
    }
  }