Sign in to follow this  
phil67rpg

collision detection

Recommended Posts

phil67rpg    443
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.
[code]
#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);

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

Share this post


Link to post
Share on other sites
alvaro    21246
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'.

Share this post


Link to post
Share on other sites
phil67rpg    443
I made some changes to my code.
[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);

};
[/code]

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

Share this post


Link to post
Share on other sites
alvaro    21246
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.

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