Path: chuka.playstation.co.uk!news From: James Burns Newsgroups: scee.yaroze.freetalk.english Subject: Re: HELP! Year 5 Maths.... Date: Mon, 27 Feb 2006 14:33:51 +0000 Organization: PlayStation Net Yaroze (SCEE) Lines: 121 Message-ID: References: NNTP-Posting-Host: jamesb2.tg.scee.sony.co.uk Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.12) Gecko/20050923 Thunderbird/1.0.7 Mnenhy/0.7 In-Reply-To: Hi Chris, I've been working on something like this at work. An optimisation that may make a difference, is to remove the sqrt(), where you comparing the mag to dist, use mag2 < (dist * dist). I had done some research into the theory (its out there in plentiful supply), I also read up on some of the optimisations as I was considering my target platform as well. I then had a quick look at a link to MSDN, it had an article about Collision Detection with the following sample code: // Simple sphere test to start with BOOL bSphereTest( CObject3D *obj1, CObject3D *obj2 ) { D3DVECTOR relPos = obj1->prPosition - obj2->prPosition; float dist = relPos.x * relPos.x + relPos.y * relPos.y + relPos.z * relPos.z; float minDist = obj1->fRadius + obj2->fRadius; return dist <= minDist * minDist; } You can also calculate if the vectors will intersect over a time, so you can calculate a collision in the future. Another optimisation is to break down the area you apply the test into a grid, thereby you only do the detections relevant to the sector. Hope this interests you... Regards, James Chris Wallace wrote: > 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); > > } > >