10 Replies - 1912 Views - Last Post: 29 May 2011 - 02:45 AM Rate Topic: -----

#1 Hiram  Icon User is offline

  • D.I.C Head

Reputation: 69
  • View blog
  • Posts: 203
  • Joined: 02-June 09

Bounding Box collision detection...

Posted 28 May 2011 - 03:02 AM

Hi guys, I'm playing around with collision detection at the moment. The whole concept is pretty simple and I'm having no trouble with it, however all the references I've found online recommend something to me that is frankly, stupid.

When you're building the box, it's pretty simple; just go over your mesh and find the minimum and maximum x, y and z coordinates. However all the tutorials I've found recommend setting the intial minimum and maximum values to something excessively small, and excessively large. So the big question is, why?

I don't understand why you'd set the intial values to, say, -999999 and +999999. Is there something I'm not seeing?

Is This A Good Question/Topic? 0
  • +

Replies To: Bounding Box collision detection...

#2 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,173
  • Joined: 14-February 08

Re: Bounding Box collision detection...

Posted 28 May 2011 - 05:46 AM

Post the code please, this sounds like it's just the value to kick off the collision algorithm.
Was This Post Helpful? 0
  • +
  • -

#3 Hiram  Icon User is offline

  • D.I.C Head

Reputation: 69
  • View blog
  • Posts: 203
  • Joined: 02-June 09

Re: Bounding Box collision detection...

Posted 28 May 2011 - 05:52 AM

Yeah sure. This particular article is quite large, so here's a snippet:

// If we want to calculate min and max values we should first initialize them with something we consider extreme and not like to occur 
void initbox(BoundingBox *B)/>
{
b->min.x = 100000;
b->min.y = 100000;
b->min.z = 100000;

b->max.x = -100000;
b->max.y = -100000;
b->max.z = -100000;
}



Original can be viewed here.
Was This Post Helpful? 0
  • +
  • -

#4 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,173
  • Joined: 14-February 08

Re: Bounding Box collision detection...

Posted 28 May 2011 - 05:56 AM

Quote

// If we want to calculate min and max values we should first initialize them with something we consider extreme and not like to occur.


This explains it for you. Sometimes an algorithm needs to have an initial comparison to kick the algorithm off, these values are just so high/low so that they always satisfy a condition that starts off the algorithm. Do you know what I mean?
Was This Post Helpful? 1
  • +
  • -

#5 Hiram  Icon User is offline

  • D.I.C Head

Reputation: 69
  • View blog
  • Posts: 203
  • Joined: 02-June 09

Re: Bounding Box collision detection...

Posted 28 May 2011 - 06:04 AM

I think I understand you. So, basically, the algorithm may not detect a collision initially if you they were set to less extreme values? So the first time would guarantee a "collision" and make whatever changes were required, e.g. recalculating the min and max points?

The main reason I'm confused about it is that I envisioned when I create my bounding box, I'd set the min and max points straight away, to what they actually are in the object's mesh. But all of my games have static mesh at the moment, so the min and max points never change, so I guess it makes sense to do it this way in context with my game?

Thanks heaps for the help!
Was This Post Helpful? 0
  • +
  • -

#6 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,173
  • Joined: 14-February 08

Re: Bounding Box collision detection...

Posted 28 May 2011 - 06:20 AM

GLuint index = model->triangles[object->triangles[i]].vindices[j];
GLfloat x = model->vertices[index*3 + 0];
GLfloat y = model->vertices[index*3 + 1];
GLfloat z = model->vertices[index*3 + 2];

if (box->min.x>x) box->min.x = x;
if (box->min.y>y) box->min.y = y;
if (box->min.z>z) box->min.z = z;

if (box->max.x<x) box->max.x = x;
if (box->max.y<y) box->max.y = y;
if (box->max.z<z) box->max.z = z;



Take a look at how the min and max are calculated, first it's going to check whether the currently set min value is more than the current vertex position values, of course you want this to be true to start with so that the min value gets set to the vertex position. Then the algorithm can continue to calculate the rest of the values.

If your min and max values are not going to change then just create a constructor that can set these values and then skip the calculation part.
Was This Post Helpful? 1
  • +
  • -

#7 sparkart  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 113
  • View blog
  • Posts: 688
  • Joined: 16-February 09

Re: Bounding Box collision detection...

Posted 28 May 2011 - 07:28 AM

It looks like the code in the article dynamically creates the bounding box for you.
Was This Post Helpful? 1
  • +
  • -

#8 Hiram  Icon User is offline

  • D.I.C Head

Reputation: 69
  • View blog
  • Posts: 203
  • Joined: 02-June 09

Re: Bounding Box collision detection...

Posted 28 May 2011 - 07:55 AM

Ah, that makes a lot more sense now! Seeing as my min and max won't change I can stick with my original plan of setting them in the constructor. Thanks heaps guys.
Was This Post Helpful? 0
  • +
  • -

#9 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Bounding Box collision detection...

Posted 28 May 2011 - 10:22 AM

View PostHiram, on 28 May 2011 - 11:02 AM, said:

...

When you're building the box, it's pretty simple; just go over your mesh and find the minimum and maximum x, y and z coordinates. However all the tutorials I've found recommend setting the intial minimum and maximum values to something excessively small, and excessively large. So the big question is, why?

I don't understand why you'd set the intial values to, say, -999999 and +999999. Is there something I'm not seeing?

The reason is because all algorithms are defined within a certain range of values that you must specify. Also, in game programming because all algorithms are usually very expensive to execute to some level, it is important to strictly control a range in which an algorithm can be implemented in order to help avoid performance drops.
Was This Post Helpful? 1
  • +
  • -

#10 Hiram  Icon User is offline

  • D.I.C Head

Reputation: 69
  • View blog
  • Posts: 203
  • Joined: 02-June 09

Re: Bounding Box collision detection...

Posted 28 May 2011 - 06:35 PM

Ah, that makes sense too! I enjoy working in an area where performance matters a lot, as strange as that sounds.
Was This Post Helpful? 0
  • +
  • -

#11 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Bounding Box collision detection...

Posted 29 May 2011 - 02:45 AM

View PostHiram, on 29 May 2011 - 02:35 AM, said:

Ah, that makes sense too! I enjoy working in an area where performance matters a lot, as strange as that sounds.

Nope, not strange at all! That is a vital quality that all game programmers should possess. Hold on to it, it will make you a very creative programmer. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1