• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Collision Detection + Classes

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

7 replies to this topic

### #1Shenaynay  Members

Posted 05 May 2012 - 11:47 AM

Hi guys. I'm making an asteroid clone using allegro following a tutorial. In the tutorial, he uses structs to make objects and puts the functions in main.cpp, but I've used classes to clean the code up a bit. He suggest using bounding box collision detection and I've followed how to do it but as I've used classes instead of structs, it doesn't work.

Here's my detection code:
void collideBullet(Bullet bullet[], int bSize, Comet comets[], int cSize)
{
for (int i = 0; i < bSize; i++)
{
if (bullet[i].live)
{
for (int j = 0; j < cSize; j++)
{
if (comets[j].live)
{
if (bullet[i].x > (comets[j].x - comets[j].boundX) &&
bullet[i].x < (comets[j].x + comets[j].boundX) &&
bullet[i].y > (comets[j].y - comets[j].boundY) &&
bullet[i].y < (comets[j].y + comets[j].boundY))
{
bullet[i].live = false;
comets[j].live = false;
}
}
}
}
}
}


Everything else works in my game, apart from the comets don't disappear. Thanks.

### #2alnite  Members

Posted 05 May 2012 - 11:56 AM

What's boundX and boundY? Are they width and height? If so, you collision should actually look like this:


if (bullet[i].x > (comets[j].x) &&
bullet[i].x < (comets[j].x + comets[j].boundX) &&
bullet[i].y > (comets[j].y) &&
bullet[i].y < (comets[j].y + comets[j].boundY))
{
bullet[i].live = false;
comets[j].live = false;
}


### #3Shenaynay  Members

Posted 05 May 2012 - 12:11 PM

boundX and boundY are the bounding box of the comet.

Edited by robbiewoods05, 05 May 2012 - 05:52 PM.

### #4Shenaynay  Members

Posted 05 May 2012 - 05:52 PM

I tried your suggestion but it didn't work :/

### #5alnite  Members

Posted 05 May 2012 - 07:34 PM

boundX and boundY are the bounding box of the comet.

Then you need the width and height of the bounding box.

if (bullet[i].x > (comets[j].boundX) &&
bullet[i].x < (comets[j].boundX + comets[j].boundWidth) &&
bullet[i].y > (comets[j].boundY) &&
bullet[i].y < (comets[j].boundY + comets[j].boundHeight))

Edited by alnite, 05 May 2012 - 07:35 PM.

### #6kruncher  Members

Posted 10 May 2012 - 10:38 AM

I do not understand the specifics of your game, but surely wouldn't circular collision be more appropriate for a comet?

I am guessing that you are using C# from the syntax (not tested, but looks right):

void collideBullet(Bullet bullet[], int bSize, Comet comets[], int cSize) {
// change to meet your needs

for (int i = 0; i < bSize; i++) {
if (bullet[i].live) {
for (int j = 0; j < cSize; j++) {
if (comets[j].live) {
// Calculate distance from origins
float distanceBetween = Math.Sqrt(
Math.Pow(bullet[i].x - comets[j].x, 2.0f) +
Math.Pow(bullet[i].y - comets[j].y, 2.0f)
);
// Remove radius of bullet and comet to get inner distance

// Touching or overlapping?
if (distanceBetween <= 0.0f) {
bullet[i].live = false;
comets[j].live = false;
}
}
}
}
}
}


Edited by kruncher, 10 May 2012 - 10:39 AM.

Rotorz Limited (http://www.rotorz.com)

Check out our latest game Munchy Bunny!
Editor Tool: Rotorz Tile System (for Unity 3D)

### #7littletray26  Members

Posted 03 July 2012 - 07:56 PM

I've followed how to do it but as I've used classes instead of structs, it doesn't work.

FTR: C++ Structs and Classes are, for the most part, identical. Therefore there is something else causing your code to fail
The majority of Internet Explorer users don't understand the concept of a browsing application, or that there are options.
They just see the big blue 'e' and think "Internet". The thought process usually does not get much deeper than that.

Worms are the weirdest and nicest creatures, and will one day prove themselves to the world.

I love the word Clicky

### #8frazchaudhry  Members

Posted 03 July 2012 - 10:42 PM

I've followed how to do it but as I've used classes instead of structs, it doesn't work.

FTR: C++ Structs and Classes are, for the most part, identical. Therefore there is something else causing your code to fail

yup structs and classes are inherently the same thing. The only difference being that the members and methods of a class are private by default and public in the case of structs. If your object does not disappear you might wanna check the part of the code responsible for drawing the object to the screen, and once the object is no longer "live" do not draw it to the screen. Maybe that's the issue can't tell without looking at the rest of the code.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.