0

I am making a simple 3D OpenGL game. At the moment I have four bounding walls, a random distribution of internal walls and a simple quad cube for my player.

I want to set up collision detection between the player and all of the walls. This is easy with the bounding walls as i can just check if the x or z coordinate is less than or greater than a value. The problem is with the interior walls. I have a glGenList which holds small rectangular wall segments, at the initial setup i randomly generate an array of x,z co ordinates and translate these wall segments to this position in the draw scene. I have also added a degree of rotation, either 45 or 90 which complicates the collision detection.

Could anyone assist me with how I might go about detecting collisions here. I have the co ordinates for each wall section, the size of each wall section and also the co ordinates of the player.

Would i be looking at a bounded box around the player and walls or is there a better alternative?

genpfault
  • 51,148
  • 11
  • 85
  • 139
Tom smith
  • 670
  • 2
  • 15
  • 31
  • `glGenLists`? Display Lists in 2013 (almost 2014)? Dude, those things are deprecated for ages. Don't use them. – datenwolf Nov 14 '13 at 15:45

2 Answers2

0

I think your question is largely about detecting collision with a wall at an angle, which is essentially the same as "detecting if a point matches a line", which there is an answer for how you do here:

How can I tell if a point belongs to a certain line?

(The code may be C#, but the math behind it applies in any language). You just have to replace the Y in those formulas for Z, since Y appears to not be a factor in your current design.

There has been MANY articles and even books written on how to do "good" collision detection. Part of this, of course, comes down to a "do you want very accurate or very fast code" - for "perfect" simulations, you may sacrifice speed for accuarcy. In most games, of the players body "dents" the wall just a little bit because the player has gone past the wall intersection, that's perhaps acceptable.

It is also useful to "partition the space". The common way for this is "Binary space partitioning", which is nicely described and illustrated here: http://en.wikipedia.org/wiki/Binary_space_partition

Books on game programming should cover basic principles of collision detection. There is also PLENTY of articles on the web about it, including an entry in wikipedia: http://en.wikipedia.org/wiki/Collision_detection

Community
  • 1
  • 1
Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • I will see how this could work for me. Failing that it is not necessary for walls to be angled, and i am sure it would be a lot easier with straight edged walls. – Tom smith Nov 15 '13 at 15:32
  • I've never heard of binary space partitioning being used for a 3D world. Typically for 2D games I've seen quad trees, and octal trees for 3D worlds. – aj.toulan Dec 09 '13 at 18:26
0

Short of making a rigid body physics engine, one could use point to plane distance to see if any of the cubes corner points are less than 0.0f away from the plane (I would use FLT_MIN so the points have a little radius to them). You will need to store a normalized 3d vector (vector of length 1.0f) to represent the normal of the plane. If the dot product between the vector from the center of the plane to the point and the plain normal is less than the radius you have a collision. After that, you can take the velocity (the length of the vector) of the cube, multiply it by 0.7f for some energy absorption and store this as the cubes new velocity. Then reflect the normalized velocity vector of the cube over the normal of the plane, then multiply that by the previously calculated new velocity of the cube.

If you really want to get into game physics, grab a pull from this guys github. I've used his book for a Physics for games class. There are some mistakes in the book so be sure to get all code samples from github. He goes through making a mass aggregate physics engine and a rigid body one. I would also brush up on matrices and tensors.

aj.toulan
  • 1,341
  • 1
  • 16
  • 25
  • I will have a look at your suggestions, I am only looking for basic collision detection, at this stage i don't need to take into account any reactive forces, and the complexity wouldn't justify using any physics engine – Tom smith Nov 15 '13 at 15:30
  • In that case, just use the point to plane collision. This link has a code sample at the bottom. http://geomalgorithms.com/a04-_planes.html – aj.toulan Nov 15 '13 at 15:50