Sign in to follow this  
psiko_scweek

c++ inheritence newbie help

Recommended Posts

Alright... im trying to learn C++ and implement collision detection into my game. im learning c++ so please be easy on me. what i have is an Object Class
[SOURCE]
class Object 
{ 
protected: 
int x, y; 
int direction,speed;
int graphicwidth;
int graphicheight;
int height, width;
int solid;
SDL_Surface *graphic;

public: 
Object(); 
~Object();
void step(); 
void draw(); 
void setGraphicHeight(int tempHeigt);
void setGraphicWidth(int tempWidth);
int getGraphicHeight();
int getGraphicWidth();
void setSolid(int tempSolid);
void setX(int tempX);
void setY(int tempY);
int getX();
int getY();
void setGraphic(SDL_Surface *graphic);
};
[/SOURCE]
and I set up an inheritence for it for another object. to handle the input
[SOURCE]
class Blocks : public Object {
public:
void input();
};
[/SOURCE]
but when I try to call my collison code by...
[SOURCE]
//collision detection
int obj_collision(Object *object1, Object *object2)
{
int obj_top1, obj_top2;
int obj_bot1, obj_bot2;
int obj_rigt1, obj_rigt2;
int obj_left1, obj_left2;

obj_left1 = object1->getX();
obj_left2 = object2->getX();

obj_top1 = object1->getY();
obj_top2 = object2->getY();

obj_rigt1 = object1->getX() + object1->getGraphicWidth();
obj_rigt2 = object2->getX() + object2->getGraphicWidth();

obj_bot1 = object1->getY() + object1->getGraphicHeight();
obj_bot2 = object2->getY() + object2->getGraphicHeight();

return 0;
}
[/SOURCE]
but it doesnt work...what did i do wrong? lol again please be easy on me im learning.

Share this post


Link to post
Share on other sites
After a quick check, I didn't see anything obviously incorrect in your code. Well, except that you used pointers instead of references without checking if they are NULL, which is a Bad ThingTM.

Other than that, my crystal ball is out for repairs, so I'm afraid I will not be able to guess what you mean by but it doesnt work.

Share this post


Link to post
Share on other sites
My first response on reading your post was "Define doesn't work." Posting a bunch of code and saying "Why doesn't this work?" is not a good way to ask a question.

However, I looked over your code and the obvious thing that struck me was that your collision detection function doesn't even attempt to detect collision. It sets a bunch of variables to the coordinates of the objects and then returns zero. That is always going to say "no collision." You need to do a rectangle intersection test between the two objects' rectangles.

Share this post


Link to post
Share on other sites
lol alright sorry!

it doesnt compile...it gives me errors when i try to compile

Cannot Convert 'Blocks' to 'Object*' for arugment '1' to 'int object_collision(object*, object*)'

lol sorry for that.

anymore help would be great.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by psiko_scweek
lol alright sorry!

it doesnt compile...it gives me errors when i try to compile

Cannot Convert 'Blocks' to 'Object*' for arugment '1' to 'int object_collision(object*, object*)'

lol sorry for that.

anymore help would be great.


If that is the error, pass a Blocks pointer instead of an instance.

Share this post


Link to post
Share on other sites
ok thanks for the advice.

here is a lil bit more of my code, maybe that helps..



/////////// CREATE THE OBJECTS ///////////
Blocks Ball;
Object Background;
Object Wall[10];



and when i try to pass the function

obj_collision(Ball,Wall);

is where I get the error message...

do i try and change

Ball to &Ball?

Share this post


Link to post
Share on other sites
Yes. Changing Ball to &Ball is necessary (or at least the easiest way of doing it) when you need to pass a pointer for a parameter and all you have is non-references.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
obj_collision(&Ball,Wall);
will work, but I do not know if you understand why it works (ie the Wall part seeing as you are having problem with pointers ?). It will test the ball against the first wall.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Sure.
Object Wall[10]; is an array of ten objects with indexs from zero to nine.
An array name is a pointer to the first element contained in that array, so if its the first wall you want to check then
obj_collision(&Ball,Wall); does the job.

If you want to check any of the other walls then you have two options
obj_collision(&Ball,Wall+index);
or
obj_collision(&Ball,& Wall[index]);

The first one uses the fact I just mentioned (array name == pointer) and adds the sizeof(Wall) to the pointer to get the next pointer. where as the second version uses the index to retreive the instance and then takes the address of it.

Share this post


Link to post
Share on other sites
You shouldn't be. It's not required to make a destructor virtual (as far as compilation goes).

A destructor should be virtual if you intend for it to ever be called through a pointer to a base object that is actually a derived object (i.e., you delete a "Base*" that is currently pointing to a "Derived" instance).

If you can guarantee that you don't need runtime dispatch of the destructor, the destructor doesn't have to be virtual. You can argue that its good practice or not, but it is not required to compile.

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