Jump to content
  • Advertisement
Sign in to follow this  
psiko_scweek

c++ inheritence newbie help

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

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
Advertisement
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
It appears that you're trying to pass your Block by value instead of by address (e.g. you passed "myBlock" instead of "&myBlock") when calling your obj_collision function.

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
ok thats seems to be working great!

now, if you dont mind, care to explain to me as to why that works? im really trying to learn as much as i can!

thanks again!

matt

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!