# Bounding Box and Collision plane?

This topic is 2940 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi guys!
This is a problem that has plagued me for a while (specifically due to my use of different techniques and the fact that googles results for "AABB" or "Bounding Box" present little documentation on exactly how to do this).

My problem is not to do with the actual collision detection, but more to do with how I detect which plane of the box I am colliding/intersecting with.

At the moment, I have made two different methods for testing the basic collision detection. The first being the following which I obtained from a friend, which performs a traditional bounding box collision check:
private boolean checkCollision(float object1x, float object1y, float object1z, float object2x, float object2y, float object2z, float hyp) { //Check bounding box || could use pythag if((object1x<object2x+hyp && object1x>object2x-hyp)) { if((object1y<object2y+hyp && object1y>object2y-hyp)) { if(object1z<object2z+hyp && object1z>object2z-hyp) { return true; } } } else return false; }

The second is basically a 3D Pythagorus function which returns the distance between the player and the 'box'.

Now, my problem comes with the fact that I need to prevent the player from moving in a certain direction (x, y, z) upon colliding with a box, at the moment I'm using an overly complex and inefficient method of running this pythag function for each face of the box, and then using the results to determine which direction to 'lock' (preventing the player from moving any further in that direction).

However, this is extremely buggy as you could imagine, especially when it comes to collisions with the corners of the box, where 'sliding' becomes impossible. If for example you were to hit the centre of the plane, or anywhere directly on the face, sliding works, but coming up to the corner the collision code begins to become buggy and clunky.

So my question is, does anyone have some example code, or (preferably) an algorithm/example/description of how some of you more advanced and experienced developers deal with checking which side of the box the player is colliding with, and using this to lock the players movement in that direction?

Thank you for reading this wall of text!
Matt.

P.S. I realise there are alternative methods such as ray-tracing etc, but at the moment I'm really just looking for a way to just get basic Bounding Box collision detection working, or any form of collision detection at all that works, nothing really that overly complex.

P. P. S. By AABB what I really mean is just Bounding Boxes, my current plans do not involve any sort of rotation occurring.

##### Share on other sites
If I understand correctly you are colliding an AABB with another AABB.

Take your velocity and divide it into the 3 components: velocity.X, velocity.Y, velocity.Z

Then test each component individually.

So move the AABB along the x-axis only using velocity.X, and test for intersection. If intersection, move the AABB back along the x-axis by the amount that it is overlapping the other AABB and set the velocity.X to zero; since a collision was found along the x-axis.

Then do the same for Y and Z.

Sliding will happen naturally as you shut of the velocity for the axis that collided.

That's probably the easiest way, but as a disclaimer: I don't have a lot of experience with collision detection.

##### Share on other sites

If I understand correctly you are colliding an AABB with another AABB.

Take your velocity and divide it into the 3 components: velocity.X, velocity.Y, velocity.Z

Then test each component individually.

So move the AABB along the x-axis only using velocity.X, and test for intersection. If intersection, move the AABB back along the x-axis by the amount that it is overlapping the other AABB and set the velocity.X to zero; since a collision was found along the x-axis.

Then do the same for Y and Z.

Sliding will happen naturally as you shut of the velocity for the axis that collided.

That's probably the easiest way, but as a disclaimer: I don't have a lot of experience with collision detection.

At the moment, I'm basically directly modifying the players x, y and z co-ordinates by adding 10 when a direction key is pressed, so by velocity I assume you mean the 10? (Bit of a noob question, but I just need to check, my terminology isn't that good!).
Secondly, how exactly would I obtain the 'amount' of intersection? My current implementation of a bounding box is based on the centre of the cube, and not the min/max. Would I basically get the difference between the (centre X + (box width / 2)) - (position of player + player box width) and subtract that from the centre X - (box width / 2)?
Finally, would this method work with negative co-ordinates?

Thanks,
Matt.

(BTW, sorry about the noob questions, I'm quite tired right now!)

##### Share on other sites
Hi,
I've finally gotten the method mentioned above implemented, and it works very well.

However, I still have a problem when it comes to collisions with the corners of the box. An example being violent 'jerking' of the camera as the player is pushed back away from the corner and then moves forward again. This is the only problem, face-player collisions work perfectly and I have no jittering there.

Heres the code I use to check collisions (at the moment I haven't got full AABBs implemented, but this code should still work nonetheless):

 public float getIntersect(float intersect1, float intersect1dist, float intersect2, float intersect2dist) { if (intersect1 > intersect2) return ((intersect1 + intersect1dist) - (intersect2 + intersect2dist)); else return -((intersect2 - intersect2dist) - (intersect1 - intersect1dist)); } public Vector3f checkBoxEntityCollision(Vector3f box, Vector3f entity, Vector3f entityVelocity) { Vector3f newPosition = new Vector3f(); Vector3f.add(entity, entityVelocity, newPosition); float xIntersect = 0; float zIntersect = 0; float yIntersect = 0; if (checkCollision(box, newPosition, 2.0f)) { xIntersect = getIntersect(newPosition.x, 0.0f, box.x, 2.0f); zIntersect = getIntersect(newPosition.z, 0.0f, box.z, 2.0f); } // The if statements basically prevent a collision with the z faces when colliding with the x, and the other way around. if (zIntersect > 0.3 || zIntersect < -0.3) newPosition.x -= xIntersect; if (xIntersect > 0.3 || xIntersect < -0.3) newPosition.z -= zIntersect; return newPosition; }

Does anyone know why I get this 'jerking' movement at corners?

Also, can anyone recommend a better way of deciding which face the player is colliding with? I feel my current method is rather flawed.

##### Share on other sites
Does no-one have a solution to this?? I'm finding it very difficult to find an alternative way to handle the collision response, and I just can't seem to get corners working .

If anyone has any suggestions, please post!

Thanks,
Matt.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 10
• 18
• 13
• 9
• 9