# How could I implement 3D player collision with rotation in LWJGL?

I have a problem with my current collision implementation.

Currently for player collision, I just use an AABB where I check if another AABB is in the way of the player, as shown in this code. (The code below is a sample of checking for collisions in the Z axis)

for (int z = (int) (this.position.getZ()); z > this.position.getZ() - moveSpeed - boundingBoxDepth; z--)
{
// The maximum Z you can get.
int maxZ = (int) (this.position.getZ() - moveSpeed - boundingBoxDepth) + 1;
AxisAlignedBoundingBox aabb = WarmupWeekend.getInstance().currentLevel.getAxisAlignedBoundingBoxAt(new Vector3f(this.position.getX(), this.position.getY(), z));
AxisAlignedBoundingBox potentialCameraBB = new AxisAlignedBoundingBox(this, "collider", new Vector3f(this.position.getX(), this.position.getY(), z), boundingBoxWidth, boundingBoxHeight, boundingBoxDepth);
if (aabb != null)
{
if (potentialCameraBB.colliding(aabb) && aabb.COLLIDER_TYPE.equalsIgnoreCase("collider"))
{
break;
}
else if (!potentialCameraBB.colliding(aabb) && z == maxZ)
{
if (this.grounded)
{
playFootstep();
}
this.position.z -= moveSpeed;
break;
}
}
else if (z == maxZ)
{
if (this.grounded)
{
playFootstep();
}
this.position.z -= moveSpeed;
break;
}
}


Now, when I tried to implement rotation to this method, everything broke. I'm wondering how I could implement rotation to this block (and as all other checks in each axis are the same) and others. Thanks in advance.

• I'd use JBullet physics library. jbullet.advel.cz – Sri Harsha Chilakapati Sep 12 '14 at 15:10
• @SriHarshaChilakapati JBullet is old, and to be honest, pretty horrible. I would not recommend JBullet. – joehot200 Mar 19 '15 at 18:25
• I've bountied this question. I am hoping to get a detailed answer on this. – joehot200 Mar 22 '15 at 9:21

AABBs (Axis-Aligned Bounding Boxes) are (as the name suggests) without rotation. What you need are OBBs (Oriented Bounding Boxes), there are many tutorials on the web but collisions in 3D space are always either not good enough ore a pain in the back.

Wikipedia provides a nice set of formulas ;) http://en.wikipedia.org/wiki/Bounding_volume#Basic_intersection_checks

• I would really appreciate a more detailed answer than this. This is something that I too am very interested in, and while I am well beyond bounding boxes, I still would be very interested in how all these bounding boxes would be created and used. – joehot200 Mar 19 '15 at 18:27
• @joehot200 You basically iterate through every vertex of your mesh/model and take the outermost ones in each direction. These define the size of your bounding box. Then you can use them to detect if two objects could collide (if the bounding boxes overlap) and then use a more complicated (and way mor precise) method to finally determine if the two objects really collide. For OBBs you also need to rotate the boxes using vector math (complicated) but they are not much more useful than AABBs when they are just used to detect eventual collision. For efficiency/precision use a sphere/ellipsoid. – mtronics Mar 25 '15 at 14:25
• @mtronics i think you are trying to refer to SAT (Separating Axis Theorem) when you say "take the outermost ones in each direction". – Tinfoilboy Mar 25 '15 at 21:45

The way I solved this a long time back (I forgot about this question entirely until now), was that I just basically checked if the boxes intersected, because the rotation was just for a first person camera. So it only would have effected the viewport and movement code, the actual collision was just checking for an intersection. Basically, it was just me over thinking things. If you actually NEED rotation in collisions, you might be able to resize the box every time it rotates on the X axis.

For Example

public void rotateX()
{
rotation.x += Mouse.getDX();
collider.size.x = baseColliderSizeX + (float) Math.toRadians(rotation.x);
}


Though I have no clue if that really works.

Also, the way I was trying to do it, was by doing continuous collision detection, which would check every possible position for a collision. But that isn't usually needed, as you can just bump the player/entity back to a point where they aren't going through the old object. If you really need continuous collision detection, I would really have not a clue what to do, because it would have to be based off of the players current direction.