# Perform a AABB hit test in 3D

**URL:** https://discourse.processing.org/t/perform-a-aabb-hit-test-in-3d/7319
**Category:** Coding Questions
**Created:** [January 8, 2019, 12:51pm UTC](https://discourse.processing.org/t/perform-a-aabb-hit-test-in-3d/7319 "2019-01-08T12:51:07Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![clankill3r](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/clankill3r/32/1098_2.png) [@clankill3r](https://discourse.processing.org/u/clankill3r)
#### Post date: [January 8, 2019, 12:51pm UTC](https://discourse.processing.org/t/perform-a-aabb-hit-test-in-3d/7319/1 "2019-01-08T12:51:08Z")

</div>

Is it possible to do a point hit test on a axis aligned bounding box after things like rotation are applied?

Here is one of my current attempts, but i’m not sure if i’m after something that is possible at all.

```java

int w = 200;
int h = 200;

void setup() {
  size(600, 600, P3D); 
}

void draw() {
  background(30);
  
  pushMatrix();
  translate(width/2, height/2);
  rotateY(radians(45));
  fill(255);
  rect(0, 0, w, h);
  
  PVector p1 = new PVector(modelX(0, 0, 0), modelY(0, 0, 0), modelZ(0, 0, 0));
  PVector p2 = new PVector(modelX(w, 0, 0), modelY(w, 0, 0), modelZ(w, 0, 0));
  PVector p3 = new PVector(modelX(w, h, 0), modelY(w, h, 0), modelZ(w, h, 0));
  PVector p4 = new PVector(modelX(0, h, 0), modelY(0, h, 0), modelZ(0, h, 0));
  
  PVector mouse = new PVector(modelX(mouseX-width/2, mouseY-height/2, 0), 
                              modelY(mouseX-width/2, mouseY-height/2, 0), 
                              modelZ(mouseX-width/2, mouseY-height/2, 0));
  
  PMatrix3D m = (PMatrix3D) getMatrix();
  m.invert();
  
  popMatrix();
  
  // -------------------------------------------------------------
  
  PVector c1 = m.mult(p1, null);
  PVector c2 = m.mult(p2, null);
  PVector c3 = m.mult(p3, null);
  PVector c4 = m.mult(p4, null);
  
  PVector cm = m.mult(mouse, null);
  
  pushMatrix();
  beginShape();
  translate(width*0.4, 0);
  noFill();
  stroke(255);
  vertex(c1.x, c1.y);
  vertex(c2.x, c2.y);
  vertex(c3.x, c3.y);
  vertex(c4.x, c4.y);
  endShape(CLOSE);
  
  ellipse(cm.x, cm.y, 10, 10);
  popMatrix();
  
  
}

```

---

<div class="post-metadata">

### Author: ![BennyHacker](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bennyhacker/32/2969_2.png) [@BennyHacker](https://discourse.processing.org/u/BennyHacker)
#### Post date: [January 8, 2019, 7:59pm UTC](https://discourse.processing.org/t/perform-a-aabb-hit-test-in-3d/7319/2 "2019-01-08T19:59:53Z")

</div>

This is not a complete answer but collisions with rotated cubes can be quite difficult and require quite a bit of knowledge about trigonometry. I’d start by drawing sketches of cubes only rotating in one axis and try to find easier solutions based on that. One method I came up with was checking if the point collides with a 2D polygon made from the 4 top or bottom points, then checking to see if the collision is inside of the y range of that cube

![rotated_cube_test](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/9/96898484c860cabbc04bf95f119fb81d451397b4.png)

---

<div class="post-metadata">

### Author: ![clankill3r](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/clankill3r/32/1098_2.png) [@clankill3r](https://discourse.processing.org/u/clankill3r)
#### Post date: [January 8, 2019, 10:44pm UTC](https://discourse.processing.org/t/perform-a-aabb-hit-test-in-3d/7319/3 "2019-01-08T22:44:09Z")

</div>

Yeah in 2d I cn do that using the inverse matrix.

---

<div class="post-metadata">

### Author: ![BennyHacker](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bennyhacker/32/2969_2.png) [@BennyHacker](https://discourse.processing.org/u/BennyHacker)
#### Post date: [January 8, 2019, 11:53pm UTC](https://discourse.processing.org/t/perform-a-aabb-hit-test-in-3d/7319/4 "2019-01-08T23:53:37Z")

</div>

Did you get it working?

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [January 9, 2019, 9:42am UTC](https://discourse.processing.org/t/perform-a-aabb-hit-test-in-3d/7319/5 "2019-01-09T09:42:47Z")

</div>

It is most unlikely that you can do that because you can only get 2 coordinates with the mouse and there is no way to get the third coordinate to specify a 3D position. The best you can get is a 3D vector of the direction you are looking and use ray-tracing to see if it intersects the quad.
