Sign in to follow this  
Robbie Woods

Collision Detection + Classes

Recommended Posts

Robbie Woods    137
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:
[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;
}
}
}
}
}
}
[/CODE]

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

Share this post


Link to post
Share on other sites
alnite    3436
What's boundX and boundY? Are they width and height? If so, you collision should actually look like this:

[CODE]

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;
}
[/CODE]

Share this post


Link to post
Share on other sites
alnite    3436
[quote name='Shenaynay' timestamp='1336241503' post='4937645']
boundX and boundY are the bounding box of the comet.
[/quote]

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

[CODE] 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))[/CODE] Edited by alnite

Share this post


Link to post
Share on other sites
kruncher    354
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):

[font=courier new,courier,monospace][CODE]
void collideBullet(Bullet bullet[], int bSize, Comet comets[], int cSize) {
// change to meet your needs
float bulletRadius = 3.0f;

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
distanceBetween -= bulletRadius + comets[j].radius;

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

Share this post


Link to post
Share on other sites
littletray26    284
[quote name='Shenaynay' timestamp='1336240037' post='4937638']
I've followed how to do it but as I've used classes instead of structs, it doesn't work.
[/quote]

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

Share this post


Link to post
Share on other sites
frazchaudhry    1220
[quote name='littletray26' timestamp='1341366997' post='4955505']
[quote name='Shenaynay' timestamp='1336240037' post='4937638']
I've followed how to do it but as I've used classes instead of structs, it doesn't work.
[/quote]

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

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this