//----------------------------------------------------------------// bool USSurface2D::GetRayHit ( ZLVec2D& loc, ZLVec2D& ray, float& time ) { float d; d = ray.Dot ( this->mNorm ); if ( d == 0.0f ) return false; // ray is parallel time = ( loc.Dot ( this->mNorm ) + this->mDist ) / -d; return true; }
//----------------------------------------------------------------// bool USSurface2D::GetHit ( ZLVec2D& sphereLoc, ZLVec2D& move, SurfaceHit2D& hit ) { // The usual stuff... ZLVec2D unitMove = move; unitMove.Norm (); if ( unitMove.Dot ( this->mNorm ) >= -0.001f ) return false; if ( ZLDist::PointToPlane2D ( sphereLoc, *this ) <= 0.0f ) return false; // Get the point of first contact on the polygon... ZLVec2D pofcop = this->mNorm; pofcop.Reverse (); pofcop.Add ( sphereLoc ); this->ClampPoint ( pofcop ); // Send a ray from the point on the surface to intersect the circle. // The ray is the inverse of the move vec. ZLVec2D inverseMove = move; inverseMove.Reverse (); float t0, t1; u32 sectType; sectType = ZLSect::VecToCircle ( t0, t1, pofcop, inverseMove, sphereLoc, 1.0f ); // Bail if the point will not intersect the sphere. if ( sectType == ZLSect::SECT_PARALLEL ) return false; if ( t0 >= hit.mTime ) return false; // Bail if the point will graze the sphere. if ( sectType == ZLSect::SECT_TANGENT ) return false; // Bail if the point will stay outside of the sphere. if (( t0 > 1.0f ) || ( t1 < 0.0f )) return false; // OMG! We hit something! float time = t0; hit.mTime = time; inverseMove.Scale ( t0 ); hit.mPoint = pofcop; hit.mPoint.Add ( inverseMove ); hit.mNorm = sphereLoc; hit.mNorm.Sub ( hit.mPoint ); hit.mNorm.Norm (); if ( unitMove.Dot ( hit.mNorm ) >= -0.001f ) return false; return true; }
//----------------------------------------------------------------// bool USSurface2D::GetTouch ( ZLVec2D& sphereLoc, ZLSurfaceTouch2D& touch ) { // The usual stuff... float dist = ZLDist::PointToPlane2D ( sphereLoc, *this ); if ( dist <= 0.0f ) return false; if ( dist > 1.001f ) return false; // Get the point of first contact on the polygon... ZLVec2D pofcop = this->mNorm; pofcop.Reverse (); pofcop.Add ( sphereLoc ); this->ClampPoint ( pofcop ); ZLVec2D ray = pofcop; ray.Sub ( sphereLoc ); dist = ray.NormSafe (); if ( dist >= touch.mDist ) return false; float dot = ray.Dot ( touch.mFinger ); if ( dot < 0.85f ) return false; // we have a touch... touch.mDist = dist; touch.mPoint = pofcop; touch.mTouch = true; touch.mHit = !touch.mPrevTouch; //touch.mNorm = this->GetNorm (); return true; }
//----------------------------------------------------------------// bool USSurface2D::GetRayHit ( ZLVec2D& loc, ZLVec2D& ray, float pad, float& time ) { float d; d = ray.Dot ( this->mNorm ); if ( d == 0.0f ) return false; time = ( loc.Dot ( this->mNorm ) + this->mDist ) / -d; // now get the point of impact ZLVec2D sect = ray; sect.Scale ( time ); sect.Add ( loc ); float dot = this->mTangent.Dot ( sect ); if ( dot < ( this->mP0 - pad )) return false; if ( dot > ( this->mP1 + pad )) return false; return true; }