views:

142

answers:

3

Hi

I am trying to make a simple collision detection class for a soccer game. Here is the code:

int Collision(int x1, int y1, int radius1, int x2, int y2, int radius2)  
{  
    int dx = x2 - x1;  
    int dy = y2 - y1;  
    int radii = radius1 + radius2;  
    if ((dx*dy)+(dy*dy)< radii * radii)  
    {  
        return true;  
    }  
    else  
    {  
        return false;  
    }  
}  

The problem is with the code returning true or false. Visual Studio says it cannot implicitly convert bool to int, and I understand that, but how can I fix it? Thanks for any help.

+1  A: 

Define your function like this:

bool Collision(int x1, int y1, int radius1, int x2, int y2, int radius2)

Now you can return true or false. If you keep int then you need to return an integer value such as 0 and 1 but this doesn't express the function intent.

You could also shorten your code a bit:

bool Collision(int x1, int y1, int radius1, int x2, int y2, int radius2)  
{  
    int dx = x2 - x1;  
    int dy = y2 - y1;  
    int radii = radius1 + radius2;  
    return ((dx * dy) + (dy * dy) < radii * radii);
}
Darin Dimitrov
Thanks man, I didn't know it was so simple. It works.
NeoHaxxor
@NeoHaxxor - simple, yes, but do you understand _why_ it works?
Oded
Any reason for the downvote? Please leave a comment when downvoting.
Darin Dimitrov
why you want to change the return type of function , you can use something like thisint result = (dx*dy)+(dy*dy)< radii * radii ? 1:0and retrun result ;
saurabh
@saurab, because when a function has to determine whether two points collide it should return a `boolean` value. It expresses much more clearly its intent to the consumer.
Darin Dimitrov
see my comments , why are you changing the return type , you can use something a<b?1:0 , why are you suggesting to change the return type itself
saurabh
@Darin : what about if he uses this function 1000 places
saurabh
@saurabh, because it is more appropriate in this case. There's nothing preventing you from using a function that returns a bool value in 1000 places.
Darin Dimitrov
@Darin : can you do something like this int i = GetResult() and GetResult is returning true or false without using casting
saurabh
@saurabh, no you can't, that's what the OP was trying to do. See the compiler error he is getting.
Darin Dimitrov
i think if you can change something without changing a lot of code , you should prefer that alternative until and otherwise your design is not having some very serious problem.
saurabh
so that was my point , changing a return type can break your code a lot of places in this case , so use something which does not break code.
saurabh
@saurabh, the code's already broken and there cannot be any consumers of this method simply because this method doesn't even compile. So I really don't understand your point.
Darin Dimitrov
+3  A: 

If you need to return a true/false variable, you should change your first line to this:

bool Collision(int x1, int y1, int radius1, int x2, int y2, int radius2) {
Tommy
+1  A: 

Don't forget to fix your algorithm. ((dx*dy)+(dy*dy)< radii * radii) should be: ((dx*dx)+(dy*dy)< radii * radii)

Just when you think: Whew! I fixed that int/bool thing,, you get a bunch of false positives.

Steve H