• Create Account

## collision detection

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

5 replies to this topic

### #1phil67rpg  Members

Posted 23 October 2012 - 03:55 PM

I want to use the following collsion detection routine but i am confused about using pointers and objects.here is the code I am using.
#include <iostream>
#include <stdlib.h>
using namespace std;
class sprite_ptr
{
public:
int x;
int y;
int width;
int height;
int object1;
int object2;
};
short int Sprite_Collide(sprite_ptr* object1, sprite_ptr* object2);
int main()
{
cout << Sprite_Collide(sprite_ptr object1, sprite_ptr object2);
system("pause");
return 0;
}
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);

return(1);

};

I am stuck on this line of code
cout << Sprite_Collide(sprite_ptr object1, sprite_ptr object2);

### #2Álvaro  Members

Posted 23 October 2012 - 04:12 PM

There is quite a bit of confusion going on in that code.

Your class should not be called sprite_ptr', but something like Sprite'. The class is not a pointer, so calling it sprite_ptr' just confuses things.

Your class should not have members of integer type named object1' and object2' which you don't use.

The call to Sprite_Collide' should not have types in it. You only put type names in declarations and definitions, not calls.

Sprite_Collide' should probably not return a short int'. If you mean to indicate whether a collision happened or not, the type to return is bool'.

### #3phil67rpg  Members

Posted 23 October 2012 - 04:40 PM

I made some changes to my code.
#include <iostream>
#include <stdlib.h>
using namespace std;
class Sprite
{
public:
int x;
int y;
int width;
int height;
};
bool Sprite_Collide(Sprite object1, Sprite object2);
int main()
{
cout << Sprite_Collide();
system("pause");
return 0;
}
bool 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);

};


I am still confused about the function call
cout << Sprite_Collide();

### #4Álvaro  Members

Posted 23 October 2012 - 07:07 PM

What do you intend your code to do? To begin with, you need to define two objects of class Sprite that you can pass to Sprite_Collide'. Your prototype for the function says that it takes two Sprites as parameters, your definition says that it takes two pointers to Sprite and your call doesn't pass any parameters at all. Those three things should match. Passing Sprites by value (what your prototype says) is fine as a first attempt because it's the simplest. If you do that, you'll need to change all the arrows (->') to dots (.') because the arrow only works on pointers.

### #5phil67rpg  Members

Posted 23 October 2012 - 07:51 PM

well I solved my problem thanks to alvaro.

### #6Álvaro  Members

Posted 24 October 2012 - 04:34 AM

There's probably some more you can learn from this. Post the code and chances are I'll have a few final suggestions.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.