Sign in to follow this  

collision detection

Recommended Posts

// Object-to-object bounding-box collision detector:
short int Sprite_Collide(sprite_ptr object1, sprite_ptr 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);



I found this code on this web site.I just dont understand what "sprite_ptr object1" is doing.

Share this post

Link to post
Share on other sites
Do you understand the concept of pointers?

object1 is just a pointer to a sprite class, which looks like it has the members x,y,width, and height.

It uses this pointer to get the dimensions of a sprite, and then it checks if it overlaps with another sprite.

What about this do you find particularly confusing?

Share this post

Link to post
Share on other sites
It's the equivalent of the dot operator for a pointer.


class Box {
int x;
int y;
void DoSomething();


Box b;
b.x = 5;
b.y = 10;
b.DoSomething(); //does something with b

Box* ptr_to_b = &b;

int temp;
temp = ptr_to_b->x; //temp = 5;
temp = ptr_to_b->y; //temp = 10;
ptr_to_b->DoSomething(); //also does something with b


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