Reputation: 804
So i'm making a snowboarding game with SDL, and I have a function in the obstacle class that checks for collision. When the obstacle calls this function, if the player collided with it I went to set the collidedObject of the player class to the object it collided with. The code looks like this:
void Obstacle::checkCollision()
{
// Check for collision
// If player collided
player.collidedObject = theObjectThatCalledThisFunction;
}
But i don't know how to get the object that called the function. Any help? I didn't really know what to search, and what i did try searching wasn't much help. Thanks.
Upvotes: 0
Views: 38
Reputation: 7961
Just a thought but it would make more sense for you to checkCollision somewhere outside the obstacle class. Something like:
bool Obstacle::checkCollision(Player &player) {
//collision test here
return collisionResult;
}
PlayerSystem::collisionTest() {
foreach(Obstacle : ObstaclesCloseBy) {
if(obstacle->checkCollision(player)
player.rollLikeABarrelDownTheSlope(); //or whatever
}
}
Upvotes: 0
Reputation: 23058
this
points to the object calling the member function. Hence, if player.collidedObject
is of type Obstacle
, then you could write
player.collidedObject = *this;
Upvotes: 1