Skip to main content
GameDev.net gamedev.net
🔒 Locked

collision detection

Started by phil67rpg Oct 23, 2012 at 9:55 PM 4 replies 1.6k views
Original Post
phil67rpg
phil67rpg
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);
alvaro
alvaro
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'.
phil67rpg
phil67rpg
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();
alvaro
alvaro
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.
phil67rpg
phil67rpg
well I solved my problem thanks to alvaro.
alvaro
alvaro
There's probably some more you can learn from this. Post the code and chances are I'll have a few final suggestions.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.