|
From: Mart K. <ma...@ke...> - 2007-10-25 14:51:38
|
Hello kC (and others), Op Thursday 25 October 2007 14:42:23 schreef kC: > thanks mart for the information. the reason i need to determine which cars > are in view is because i need to highlight those cars that are in view. You can make an estimate guess about if a car is visible of not. If a car if next to you, it is certain that it isn't draw (if isn't in front and it isn't in the mirror). So you can reduce the number of highlighted cars by looking to the angle of the car in respect to the direction of the car. Also, if the car is 2 km in front and not straight before your car, it also is very likely that it isn't drawn. However, these kind of restriction is hard to get perfect. > how do i determine the distance away, and the angle of the cars? i'm quite > lost in torcs. i know i can use car->sx, car->sy to determine the locations > of the cars with respect to the track coordinates, but as i need to > highlight them (using circles) from a cam perspective, how do i translate > the x,y,z cam coordinates? The coordinates of the track are enough to give a distance and an angle. Let car be the (tCarElt*) of the focused car and ocar the (tCarElt*) of the car you want to get information about. You can find out the position of car with: car->_pos_X, car->_pos_Y and car->_pos_Z. This also for ocar. The distance between two points in the three-dimentional real space (of course it isn't a real space, but it is close enough), can be calculated by substracting the two vectors from each other and then take the norm. So, in practice: dist = sqrt(sqr(car->_pos_X - ocar->_pos_X)+sqr(car->_pos_Y - ocar->_pos_Y)+sqr(car->_pos_Z - ocar->_pos_Z)); sqr is defined as #define sqr(r) (r)*(r). You can leave out the sqrt; then you get the square of the distance. For the angle, you just have to project one of the vectors to make a two-dimentional vector space. Then can easily be done by ignoring the Z-value. Then you consider the line though car (car->_pos_X and car->_pos_Y) and ocar. Now you make a second line which is perpendicular to that line (you can do that by rotating the line 1/2*pi radials around the z-axis. Then you translate the line such that it goes through ocar. Now make it a plane by adding the z-axis. Your information should now be printed in that plane (it has the maximal viewing angle). Of course, you must take care of that you draw on that part of the plane that is close to the car (and not below the track). The place of the camera can slightly change from the position of the car. To get the real camera position, you can read in the code how that position is calculated. It proberbly depends on the xml-file of the car type. > thanks once again for helping a mate out! Regards, Mart |