Hitbox around a triangle?

Is there an accurate way to create a hitbox (for collision detection) around a triangle?

1 Like

Not sure of the application, but pseudo code:

if((objectXcoordinates >= (triangleCoordinates + bufferSizeCoordinates)){ 
  doSomething(); 
}

Hello @anon94203042,

I think what is usually done to check if a point is within a triangle is to compute the area of the triangle and compare it with the sum of the area of the 3 triangles formed with the new point.

If this is equal, the point is inside the triangle otherwise the point is outside.

Here’s a quick and dirty demo:

Triangle t = new Triangle(200, 600, 400, 200, 600, 600);

void setup() {
  size(800, 800);
  background(20);
}


void draw() {
  background(20);
  
  if (t.isInside(mouseX, mouseY)) {
    t.setBackgroundColor(50, 240, 20, 40);
  } else {
    t.setBackgroundColor(255, 255, 255, 40);
  }
  
  t.draw();
}


class Triangle {
  PVector a, b, c;
  color backgroundColor;
  
  Triangle(float x1, float y1, float x2, float y2, float x3, float y3) {
    a = new PVector(x1, y1);
    b = new PVector(x2, y2);
    c = new PVector(x3, y3);
    backgroundColor = color(255, 255, 255, 40);
  }
  
  boolean isInside(float x, float y) {
    PVector p = new PVector(x, y);
    
    float area_1 = b.copy().sub(a).cross(p.copy().sub(a)).mag();
    float area_2 = c.copy().sub(b).cross(p.copy().sub(b)).mag();
    float area_3 = a.copy().sub(c).cross(p.copy().sub(c)).mag();
    float area_tot = b.copy().sub(a).cross(c.copy().sub(a)).mag();
    
    if (area_1 + area_2 + area_3 - area_tot == 0) {
      return true;
    }
    
    return false; 
  }
  
  void setBackgroundColor(float r, float g, float b, float o) {
    backgroundColor = color(r, g, b, o);
  }
  
  void draw() {
    fill(backgroundColor);
    stroke(255);
    strokeWeight(2);
    beginShape();
    vertex(a.x, a.y);
    vertex(b.x, b.y);
    vertex(c.x, c.y);
    endShape(CLOSE);
    
    fill(255);
    noStroke();
    ellipse(a.x, a.y, 8, 8);
    ellipse(b.x, b.y, 8, 8);
    ellipse(c.x, c.y, 8, 8);
  }
}
1 Like

You can use a line detection algorithm. Triangle has 3 sides, cast 3 rays from the mouse to the midpoint of the lines, if you have any intersections then you are outside of the triangle else you are inside.

You can use this method to see if a point is inside a triangle

/**
 * Determine if the point pX/pY is inside triangle defined by triangle ABC whose
 * vertices are given by [ax,ay] [bx,by] [cx,cy]
 * @return true if the point is inside
 */
public boolean isInsideTriangle(double aX, double aY, 
  double bX, double bY, 
  double cX, double cY, 
  double pX, double pY) {
  double ax, ay, bx, by, cx, cy, apx, apy, bpx, bpy, cpx, cpy;
  double cCROSSap, bCROSScp, aCROSSbp;

  ax = cX - bX;  
  ay = cY - bY;
  bx = aX - cX;  
  by = aY - cY;
  cx = bX - aX;  
  cy = bY - aY;
  apx= pX - aX;  
  apy= pY - aY;
  bpx= pX - bX;  
  bpy= pY - bY;
  cpx= pX - cX;  
  cpy= pY - cY;

  aCROSSbp = ax*bpy - ay*bpx;
  cCROSSap = cx*apy - cy*apx;
  bCROSScp = bx*cpy - by*cpx;

  return ((aCROSSbp >= 0.0f) && (bCROSScp >= 0.0f) && (cCROSSap >= 0.0f));
}
2 Likes

There is an illustrated discussion of the Triangle / Point formula here:

1 Like

Cool! I’ll check it out. Thanks