Path: chuka.playstation.co.uk!news From: "Chris Wallace" Newsgroups: scee.yaroze.freetalk.english Subject: HELP! Year 5 Maths.... Date: Tue, 21 Feb 2006 23:47:27 -0000 Organization: PlayStation Net Yaroze (SCEE) Lines: 87 Message-ID: NNTP-Posting-Host: cpc1-with3-3-0-cust45.bagu.cable.ntl.com X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2180 X-RFC2646: Format=Flowed; Original X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 Ok, I'm trying to make my cars avoid objects that nasty people leave in the middle of the track.... Of course the first step to avoiding objects is knowing that they are there, so I thought to myself: I have a target location and a current location, if I draw a line between the two I can test it with a collision sphere around objects and if they meet then I can deal with it.. Sounded good to me anyway - to make it a little more effiicient I check first what the closest object is and then only check with that object, but I have tried it with testing all objects and I get the same result. Essentually, the cars seem to know what they are closest too, and as I am drawing to screen all collision boundarys (currently spheres) and the targeting lines I can see that they are intersecting.... but the code doesn't seem to notice. Below is the code.... if anyone can see any glaring errors it would be a big help!!! --------- int closest=0; double dist=100000.0; for(int i=0;inum_objects;i++) { //calculate distance between this car and object: double xdiff = (x - track->Objects[i]->x); double zdiff = (z - track->Objects[i]->z); double mag2 = (xdiff*xdiff)+(zdiff*zdiff); double mag = sqrt(mag2); if(mag < dist) { dist = mag; closest = i; } } // we now know the closest object and its distance from us //move the target away from the close object, depending on HOW close the object is //we know which object is close, closest, so //check if it is in our way double x1 = x; double y1 = 10; double z1 = z; double x2 = targetx; double y2 = 10; double z2 = targetz; double x3 = track->Objects[closest]->x; double y3 = 10; double z3 = track->Objects[closest]->z; double r = track->Objects[closest]->radius; double a = square(x2 - x1) + square(y2 - y1) + square(z2 - z1); double b = 2* ( (x2 - x1)*(x1 - x3) + (y2 - y1)*(y1 - y3) + (z2 - z1)*(z1 - z3) ); double c = square(x3) + square(y3) + square(z3) + square(x1) + square(y1) + square(z1) - 2* ( x3*x1 + y3*y1 + z3*z1 ) - square(r); double answer = square(b) - (4 * a * c); if(answer >= 0) { printf("%i : %f - EEEEEEK!!!!!!\n",closest,answer); }