user1540257
user1540257

Reputation: 11

C++ collision detection

Alright I need to do a collision test. This way seemed simplest but my compiler doesn't think so. Thanks for any replies!

C++ Code:

int sqrd(int num) { return (num*num); } //squares a number

bool checkColShip() {
  for(int x = 0;x < 10;x++) {
    double distance = 0; //use to be int tried to change it
    distance = abs( sqrt( sqrd((one.getXPos() - asteroid[x].getXPos()) +
                          sqrd((one.getYPos() - asteroid[x].getYPos()))
                   ); //include cmath
    distance -= 20;//ship radius
    distance -= 20;//asteroid radius
    if (distance<=0) return true;//collision true
  }
  //no collision
  return false;
}

Compiler error:

c:\documents and settings\all users\documents\c++ projects\learn sdl\learn sdl\main.cpp(702): error C2668: 'sqrt' : ambiguous call to overloaded function
c:\program files\microsoft visual studio 10.0\vc\include\math.h(589): could be 'long double sqrt(long double)'
c:\program files\microsoft visual studio 10.0\vc\include\math.h(541): or       'float sqrt(float)'
c:\program files\microsoft visual studio 10.0\vc\include\math.h(127): or       'double sqrt(double)'
while trying to match the argument list '(int)'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

P.S. I normally indent my code but first time posting here and it kept giving me problems.

Upvotes: 1

Views: 3048

Answers (4)

Open AI - Opting Out
Open AI - Opting Out

Reputation: 24163

For collisions you don't need to perform the square root. Just make sure the square of the distance is less than the square of the collision distance

int square(int value) {
    return value * value;
}

bool collide(Object ship, Object asteroid, int collision_distance) {
    int distance_squared = square(xdiff(ship, asteroid)) +
                           square(ydiff(ship, asteroid));
    return distance_squared < square(collision_distance);
}

The actual error you were seeing is because sqrd returns an int and sqrt doesn't take an int; there are three different versions for floating point arguments. The compiler didn't know the best choice. Of course the best choice is to avoid the problem in the first place.

Upvotes: 2

Drew Dormann
Drew Dormann

Reputation: 63912

The error is saying that there are three available sqrt() functions and there is no obvious choice when passing it an int.

You must decide if you want float or double or long double by casting the parameter.

Unrelated to your question - your code is always passing a real, non-negative number to sqrt(), so the square root will always be real and non-negative. Passing the result to abs() is unnecessary.

Upvotes: 5

ATaylor
ATaylor

Reputation: 2598

sqrt( sqrd((one.getXPos() - asteroid[x].getXPos()) is the issue here, like tugrul already stated.

But do you want to draw the square root of a number you squared seconds before in the first place? Why not just cast the difference to an int and thereby get rid of the comma?

Or wait...this is about Pythagoras, isn't it? In either case, tugrul is correct. sqrt can only work with long double or double. So make sqrd return 'double' and the error goes away.

Upvotes: 0

huseyin tugrul buyukisik
huseyin tugrul buyukisik

Reputation: 11916

sqrt needs "double" or at least a "long". But, sqrd is an int.

Upvotes: 1

Related Questions