Collision Detection + Classes

Started by
6 comments, last by frazchaudhry 11 years, 9 months ago
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.live)
{
for (int j = 0; j < cSize; j++)
{
if (comets[j].live)
{
if (bullet.x > (comets[j].x - comets[j].boundX) &&
bullet.x < (comets[j].x + comets[j].boundX) &&
bullet.y > (comets[j].y - comets[j].boundY) &&
bullet.y < (comets[j].y + comets[j].boundY))
{
bullet.live = false;
comets[j].live = false;
}
}
}
}
}
}


Everything else works in my game, apart from the comets don't disappear. Thanks.
Advertisement
What's boundX and boundY? Are they width and height? If so, you collision should actually look like this:



if (bullet.x > (comets[j].x) &&
bullet.x < (comets[j].x + comets[j].boundX) &&
bullet.y > (comets[j].y) &&
bullet.y < (comets[j].y + comets[j].boundY))
{
bullet.live = false;
comets[j].live = false;
}
boundX and boundY are the bounding box of the comet.
I tried your suggestion but it didn't work :/

boundX and boundY are the bounding box of the comet.


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

if (bullet.x > (comets[j].boundX) &&
bullet.x < (comets[j].boundX + comets[j].boundWidth) &&
bullet.y > (comets[j].boundY) &&
bullet.y < (comets[j].boundY + comets[j].boundHeight))
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]
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.live) {
for (int j = 0; j < cSize; j++) {
if (comets[j].live) {
// Calculate distance from origins
float distanceBetween = Math.Sqrt(
Math.Pow(bullet.x - comets[j].x, 2.0f) +
Math.Pow(bullet.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.live = false;
comets[j].live = false;
}
}
}
}
}
}
[/font]
Rotorz Limited (http://www.rotorz.com)

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

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

[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.


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.

This topic is closed to new replies.

Advertisement