passing objects to functions

So i wrote a collision detection function for a game im creating and it works as i want it to but now i'm trying to make it more general so it accepts any 2 objects and checks if there is a collision between them.

Now i understand that i'm passing a copy of these objects rather than the objects themselves to the function but i don't think that should affect it as no action is performed in the function.

I think my question can be best phrased like this:
Why does the first example work and the second not?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
for(int i = 0; i <= walllist.size(); i++){
            if((walllist[i].getxpos() <= player1.getxpos() && player1.getxpos() <= (walllist[i].getxpos() + walllist[i].wall.w)) ||
               (walllist[i].getxpos() <= player1.getxpos() + 64 && player1.getxpos() + 64 <= walllist[i].getxpos() + walllist[i].wall.w)){
                if(walllist[i].getypos() <= player1.getypos() && player1.getypos() <= (walllist[i].getypos() + walllist[i].wall.h)){
                    if( player1.getyvel() < 0 ){
                         player1.updatevel(-1000,0);
                         jump = 0;
                         player1.updatepos(-1000, walllist[i].getypos() + walllist[i].wall.h + 1);
                    }
                }
                if(walllist[i].getypos() <= player1.getypos() + 92 && player1.getypos() + 92 <= (walllist[i].getypos() + walllist[i].wall.h)){
                    if( player1.getyvel() > 0){
                         player1.updatevel(-1000,0);
                         player1.updatepos(-1000, walllist[i].getypos() - 92 - 1);
                         jump = 0;
                    }
                 }
             }
             if ((player1.getypos() <= walllist[i].getypos() &&  walllist[i].getypos() <= player1.getypos() + 92) ||
                (player1.getypos() <= walllist[i].getypos() + walllist[i].wall.h &&  walllist[i].getypos() + walllist[i].wall.h <= player1.getypos() + 92) ||
                (walllist[i].getypos() <= player1.getypos() && player1.getypos() <= (walllist[i].getypos() + walllist[i].wall.h)) ||
                (walllist[i].getypos() <= player1.getypos() + 92 && player1.getypos() + 92 <= walllist[i].getypos() + walllist[i].wall.h))
                {
                if(walllist[i].getxpos() <= player1.getxpos() && player1.getxpos() <= (walllist[i].getxpos() + walllist[i].wall.w)){
                    if( player1.getxvel() < 0 ){
                        player1.updatevel(0,-1000);
                        player1.updatepos(walllist[i].getxpos() + walllist[i].wall.w + 1, -1000);
                    }
                }
                if(walllist[i].getxpos() <= player1.getxpos() + 64 && player1.getxpos() + 64 <= (walllist[i].getxpos() + walllist[i].wall.w)){
                    if( player1.getxvel() > 0 ){
                        player1.updatevel(0,-1000);
                        player1.updatepos(walllist[i].getxpos() - 64 - 1, -1000);
                    }
                }
            }
        }



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
for(int i = 0; i <= walllist.size(); i++){
            walls a = walllist[i]; entity b = player1;

            if((a.getxpos() <= b.getxpos() && b.getxpos() <= (a.getxpos() + a.wall.w)) ||
               (a.getxpos() <= b.getxpos() + b.image.w && b.getxpos() + b.image.w <= a.getxpos() + a.wall.w)){
                if(a.getypos() <= b.getypos() && b.getypos() <= (a.getypos() + a.wall.h)){
                    if( b.getyvel() < 0 ){
                         player1.updatevel(-1000,0);
                         jump = 0;
                         player1.updatepos(-1000, walllist[i].getypos() + walllist[i].wall.h + 1);
                    }
                }
                if(a.getypos() <= b.getypos() + b.image.h && b.getypos() + b.image.h <= (a.getypos() + a.wall.h)){
                    if( b.getyvel() > 0){
                         player1.updatevel(-1000,0);
                         player1.updatepos(-1000, walllist[i].getypos() - 92 - 1);
                         jump = 0;
                    }
                 }
             }
             if ((b.getypos() <= a.getypos() &&  a.getypos() <= b.getypos() + b.image.h) ||
                (b.getypos() <= a.getypos() + a.wall.h &&  a.getypos() + a.wall.h <= b.getypos() + b.image.h) ||
                (a.getypos() <= b.getypos() && b.getypos() <= (a.getypos() + a.wall.h)) ||
                (a.getypos() <= b.getypos() + b.image.h && b.getypos() + b.image.h <= a.getypos() + a.wall.h))
                {
                if(a.getxpos() <= b.getxpos() && b.getxpos() <= (a.getxpos() + a.wall.w)){
                    if( player1.getxvel() < 0 ){
                        player1.updatevel(0,-1000);
                        player1.updatepos(walllist[i].getxpos() + walllist[i].wall.w + 1, -1000);
                    }
                }
                if(a.getxpos() <= b.getxpos() + b.image.w && b.getxpos() + b.image.w <= (a.getxpos() + a.wall.w)){
                    if( player1.getxvel() > 0 ){
                        player1.updatevel(0,-1000);
                        player1.updatepos(walllist[i].getxpos() - 64 - 1, -1000);
                    }
                }
            }
        }


The only difference is the re-labelling of the walls and entity.
Im really sorry if these code snippets are too long. Thanks for any help.





Last edited on
The code is not functionally equivalent. In the first you are checking and modifying player. In the second you are checking b and modifying player.
Thankyou very much for your reply.

Ok i was aware of this but i dont think it should make much difference as b is reset on each iterate and only one of the 4 if statements can ever be triggered at the same time. But i guess i'll look into this again a bit later.

How would you suggest i go about the whole passing objects to a function to check, do i just need to rework my function or is there some clever trick im missing??

Thanks again.
Is "walls.wall.w" an integer that could be compared to "entity.getxpos()"? In the first example you are explicitly listing an offset of 64, in the second one you seem to be trying to use the width of the image as your offset. Assuming your copy constructor is ok this could be your issue.
They're interchangeable i tried that but thanks for the suggestion.
Have you considered refactoring your conditionals into a function and passing the arguments by reference?

If only one of the cases can be triggered at a time, should you just "continue" the loop after a condition is satisfied? Or maybe use "else if" to indicate that only one option is possible?
Topic archived. No new replies allowed.