Jump to content
  • Advertisement
Sign in to follow this  

collision detection

This topic is 2807 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

// 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
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!