3

This has been giving me a headache for a couple of days so I have finally accepted defeat and am going to ask here.

My game consists of cubes of varying sizes which have to be moved around in a small area. I have the cube movement working perfectly, the only thing I can't seem to get my head around is collision detection.

The ground is completely flat so I don't need to worry about the Y-Axis, the coordinates of the cube are relative to the centre of the cube at ground level. I should probably also add that each level is made up of multiple rectangular ground planes which are positioned adjacent to each other.

Any suggestions as to how this could be done (pseudo code is fine) with minimal cpu usage are welcome. Thanks very much.

  • 1
    OpenGL ES just handles drawing. Nothing else. You didn't specify clearly if this was a 2D or 3D game. Found this over on a board with more relavancy :http://gamedev.stackexchange.com/questions/5906/collision-resolution – Morrison Chang Aug 05 '14 at 17:14
  • Sorry, its 3d but the objects do not move from 0 on the Y axis at any point. – user3866355 Aug 05 '14 at 17:22
  • This question appears to be off-topic because it belongs to GameDev.SE – Kromster Aug 06 '14 at 04:49

1 Answers1

4

Collision detection is very simple with things like cubes or spheres.

you can use either sphere to sphere detection which is basically drawing an imaginary sphere around the cubes and then checking if distance between the two centre points of the spheres is less than the sphere radius. like so:-

float x, y, z;
x = SmallModel->GetX() - LargeModel->GetX();    
y = SmallModel->GetY() - LargeModel->GetY();    
z = SmallModel->GetZ() - LargeModel->GetZ();    

float collisionDist = sqrt( x*x + y*y + z*z );  

if (collisionDist < sphereRadius)
{
    // Collision occurred…
}

or you can use bounding boxes which is more suited here as the above is not as accurate where as bounding boxes will be exact since you are using cubes and they are axis aligned.

The process here is also fairly simple: there is a collision if the min and max of each cube lies within each other. I.e. collision if:

Cube1XMin > Cube2XMin && Cube1XMax < Cube2XMax &&
Cube1YMin > Cube2YMin && Cube1YMax < Cube2YMax &&
Cube1ZMin > Cube2ZMin && Cube1ZMax < Cube2ZMax

so you will need to declare 6 variables for each cube as above and these min and max values are found by first knowing the size of the cube and secondly knowing the position of the cubes.

So if a cube is 10 * 10 * 10 and positioned at 100,0,100

then xmin = 95 ymin = -5 zmin = 95 xmax = 105 ymax = 5 zmax = 105

find the values for the other cubes and run the above equation and you should find out if you have a collision.

For handling a collision you need to move the cubes back to the frame before and then maybe have them bounce back, i think it is possible to get stuck inside each other if you do not move them back by a frame first.

To save on CPU usage you can probably send the calculations to the GPU using shaders but thats a different topic

Rob85
  • 1,719
  • 1
  • 23
  • 46