Re: [Plib-users] <<Code review>> ssgEntity::isectTest ( sgSphere *s, sgMat4 m, int testNeeded )
Brought to you by:
sjbaker
From: Steve B. <sjb...@ai...> - 2000-02-29 06:23:22
|
Rahul Choudhury wrote: > sgVec3 center_vec ; > float sum_radii = s->getRadius() + tmp.getRadius() ; > sgSubVec3 ( center_vec, s->getCenter(), > tmp.getCenter() ) ; > > if ( center_vec[0] > sum_radii || > center_vec[1] > sum_radii || > center_vec[2] > sum_radii ) { > return SSG_OUTSIDE ; > } > > I think the last line should be modified to > take absolute values of center_vec[0], > center_vec[1] and center_vec[2] Yep - I think you are right about that one. > Next thing ssg code does is taking squares > of each of the radius, adding them and > compares them with the square of the > distances between the centers > > Or in other words, it says > if d^2 >= radiousOne^2 + radiousTwo^2 > then the given sphere lies outside the > entity. (here d is the distance > between the two centers of spheres, > radiousOne is the radius of the > entity sphere and radiousTwo is the > radius of second sphere). > > In stead I think we should say > if d^2 >= (radiousOne + radiousTwo)^2 > or d >= (radiousOne + radiousTwo) > then the given sphere lies outside the > entity. I think I could save time by saying: d^2 >= (r1+r2)^2 ...but in order to use: d >= r1+r2 ...I'd have to compute d using a sqrt(). Since a sqrt is MUCH slower than a multiply (or even the TWO multiplies I actually did) - it's more efficient to compare the squares of the ranges than the ranges themselves. However, if I compute (r1+r2)^2, and that test fails then I'd have to compute r1^2 and r2^2 later. It's a fine line as to which would be more efficient - and it depends mainly on the statistics of sphere-to-sphere contacts. Good catch on the first one though - the code would never have broken - but it will be faster with your change. -- Steve Baker http://web2.airmail.net/sjbaker1 sjb...@ai... (home) http://www.woodsoup.org/~sbaker sj...@ht... (work) |