Jump to content
  • Advertisement
Sign in to follow this  
phil67rpg

bounding box colllision

This topic is 2778 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am testing a bounding box collision detection routine. I have been reading up on pointers, functions and classes.However I am still confused about how to pass sprite pointer objects to the SpriteCollision function.

#include <iostream>

using namespace std;

int x,y=10;
int width=64;
int height=64;

class Sprite
{
public:
int x,y;
int width,height;
};

short int Sprite_Collide(Sprite*, Sprite*);

int main()
{
Sprite object1;
Sprite object2;

cout << Sprite_Collide( object1, object2);
return 0;

}

// Object-to-object bounding-box collision detector:
short int Sprite_Collide(Sprite* object1, Sprite* object2)
{
int left1, left2;
int right1, right2;
int top1, top2;
int bottom1, bottom2;

left1 = object1->x;
left2 = object2->x;
right1 = object1->x + object1->width;
right2 = object2->x + object2->width;
top1 = object1->y;
top2 = object2->y;
bottom1 = object1->y + object1->height;
bottom2 = object2->y + object2->height;

if (bottom1 < top2) return(0);
if (top1 > bottom2) return(0);

if (right1 < left2) return(0);
if (left1 > right2) return(0);

return(1);
}


Share this post


Link to post
Share on other sites
Advertisement
If you want to use:

short int Sprite_Collide(Sprite*, Sprite*);

then call Sprite_Collide(&sprite1, &sprite2); // call with pointers


Alternatively, you can define:

short int Sprite_Collide(Sprite&, Sprite&);

and call Sprite_Collide(sprite1, sprite2); // call with references

If you do the latter, then you'll have to change the function to use references:
left1 = object1.x;
left2 = object2.x;
right1 = object1.x + object1.width;
right2 = object2.x + object2.width;

etc.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!