Question

I'm doing some collision detection and handling for a GBA game I'm writing and I'm running into problems with putting the handling part into a function. I'm trying to change the value of a member variable (in this case Player.global_x), however passing the object into a function doesn't allow this to work. The relevant code is as follows:

main.cpp

bool CheckCollision(sprite obj1, sprite obj2){
    int obj1_top = obj1.global_y;
    int obj1_bottom = obj1.global_y + (obj1.height-1);
    int obj1_left = obj1.global_x;
    int obj1_right = obj1.global_x + (obj1.width-1);
    int obj2_top = obj2.global_y;
    int obj2_bottom = obj2.global_y + (obj2.height-1);
    int obj2_left = obj2.global_x;
    int obj2_right = obj2.global_x + (obj2.width-1);

    if(obj1_right < obj2_left){     // if obj1 is left of obj2
        return false;
    }
    if(obj1_left > obj2_right){     // if obj1 is right of obj2
        return false;
    }
    if(obj1_bottom < obj2_top){     // if obj1 is above obj2
        return false;
    }
    if(obj1_top > obj2_bottom){     // if obj1 is below obj2
        return false;
    }

    return true;
}

void HandleCollision(sprite obj1, sprite obj2){
    if(obj1.direction == RIGHT){
        Player.global_x -= (obj1.global_x + obj1.width) - obj2.global_x;    // This works but is not modular
        obj1.global_x -= (obj1.global_x + obj1.width) - obj2.global_x;      // This does not work
    }
}

void CheckAllPossibleCollisions(){
    bool collision = false;
    for(int i=0; i<(WallsVector.size()); i++){
        collision = CheckCollision(Player, WallsVector.at(i));
        if(collision==true){
            // This piece of code works, but would incur repetition of code for each different type of game object
            /*if(Player.direction == RIGHT){
                Player.global_x -= ((Player.global_x+Player.width) - WallsVector.at(i).global_x);
            }*/
            HandleCollision(Player, WallsVector.at(i));
        }
    }
}

The CheckCollision() function works fine when passing the objects in, but it doesn't work in HandleCollision() when changing a variable.

Any comments you can make to help with this would be appreciated, thanks!

Was it helpful?

Solution

You are passing the parameters by value. That means the functions get their own copies. It looks like you need to pass references:

void HandleCollision(sprite& obj1, const sprite& obj2)

Note that here, I have marked the 2nd parameter as const, because you do not modify it in the function. Whether you should pass a value or a reference in this case is only a matter of optimization. Only the first parameter needs to be a (non-const) reference.

OTHER TIPS

That's not working because you passed your object by value:

void HandleCollision(sprite obj1, sprite obj2);

Instead you should pass them by reference:

void HandleCollision(sprite& obj1, sprite& obj2);

If you define your function like you did (passing by value), the two arguments will be copied and the function will use the copies.
So, even if you change their members, these copies will be destroyed as soon as the function returns and outside you will have your original sprites unmodified.

On the other hand, if you define your function to accept references to the objects, no copies will be made and you will be able to modify them.

Also notice that when passing by reference, the fact that those objects aren't copied onto the stack, means less memory operations and better performance.

As a final note, your CheckCollision function works because you don't change the sprites but only need to read from them but you really should pass by reference in there too. In fact, you should always pass by reference when passing objects as arguments because it's faster. If you don't need to modify the object, you should pass them as a constant reference:

bool CheckCollision(const sprite& obj1, const sprite& obj2);

You can either pass by the parameter by reference or by using pointers. If there is any chance you might pass a object that has no value then pointers would actually be my go to method. It all depends on what you want to do with it though.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top