# Axis-aligned bounding boxes with VEX- collision detection

## Recommended Posts

Hi guys.  I found some code on this game dev site

``````function intersect(a, b) {
return (a.minX <= b.maxX && a.maxX >= b.minX) &&
(a.minY <= b.maxY && a.maxY >= b.minY) &&
(a.minZ <= b.maxZ && a.maxZ >= b.minZ);
}``````

`I need to recreate this in VEX for packed primitive bboxes.  It doesn't have to be this method. I just need a really fast efficient way to detect a collision between moving bounding boxes and returning a 0 or a 1.  Its in a scene thats going to be using the bboxes of a package containing many instances of trees, getting copied onto points on 2 colliding surfaces, hence the need to want to keep it packed for efficiency.  Not sure how to fit this into vex.  Any advice would be great`

`Cheers`

##### Share on other sites

you can use the getbbox vex function.

##### Share on other sites

here is an example, and HIP file:

```vector amin,amax;
getbbox(0,amin,amax);
vector bmin,bmax;
getbbox(1,bmin,bmax);

int intersect_(vector amin,amax,bmin,bmax) {
return(amin.x <= bmax.x && amax.x >= bmin.x &&
amin.y <= bmax.y && amax.y >= bmin.y &&
amin.z <= bmax.z && amax.z >= bmin.z);
}
i@intersect_=intersect_(amin,amax,bmin,bmax);
```

intersect is a reserved VEX function name, so I used intersect_ instead

Edited by bunker

##### Share on other sites

Also, for object aligned boxes, I would generally loop over the objects, just use the Box SOP and then in VEX, do a intersect along the Normal (z), Up (y) vector and crossproduct(N, up) for the x axis, to get the scales.

## Create an account

Register a new account