/* * NearestPointOnCurve : * Compute the parameter value of the point on a Bezier * curve segment closest to some arbtitrary, user-input point. * Return the point on the curve at that parameter value. * Geom::Point P; The user-supplied point Geom::Point *V; Control points of cubic Bezier */ double NearestPointOnCurve(Geom::Point P, Geom::Point *V) { double t_candidate[W_DEGREE]; /* Possible roots */ /* Convert problem to 5th-degree Bezier form */ Geom::Point *w = ConvertToBezierForm(P, V); /* Find all possible roots of 5th-degree equation */ int n_solutions = FindRoots(w, W_DEGREE, t_candidate, 0); std::free((char *)w); /* Check distance to end of the curve, where t = 1 */ double dist = SquaredLength(P - V[DEGREE]); double t = 1.0; /* Find distances for candidate points */ for (int i = 0; i < n_solutions; i++) { Geom::Point p = Bez(V, DEGREE, t_candidate[i], NULL, NULL); double new_dist = SquaredLength(P - p); if (new_dist < dist) { dist = new_dist; t = t_candidate[i]; } } /* Return the parameter value t */ return t; }
bool PathPlanner::doNodesIntersectCircle(vec2 curPos, double BoundingRadius) const { for (int i = 0; i < pWorldInfo->iNumPathNodes; i++) { //Determine distance from node to curPos double dist = SquaredLength(pWorldInfo->pPathNodes[i].vPos - curPos); if (pow((pWorldInfo->pPathNodes[i].vPos.x - curPos.x), 2) + pow((pWorldInfo->pPathNodes[i].vPos.y - curPos.y), 2) < pow(BoundingRadius, 2)) { return true; } } //Return false if no nodes are found within the radius return false; }
float GetPriority() { const MemoryRecord *pTarget = GetClient()->GetTargetingSystem()->GetCurrentTargetRecord(); if ( !pTarget ) return 0.f; if ( mShootBarrel.IsValid() ) return 1.f; Vector3f vTargetPos = pTarget->PredictPosition( 1.f ); static float fSplashRadius = 256.f; SensoryMemory *sensory = GetClient()->GetSensoryMemory(); // Check for exploding barrels near my target. MemoryRecords records; Vector3List recordpos; FilterAllType filter( GetClient(), AiState::SensoryMemory::EntAny, records ); filter.MemorySpan( Utils::SecondsToMilliseconds( 7.f ) ); filter.AddGroup( ENT_GRP_PROP_EXPLODE ); //filter.AddClass( ENT_CLASS_EXPLODING_BARREL ); sensory->QueryMemory( filter ); sensory->GetRecordInfo( records, &recordpos, NULL ); for ( uint32_t i = 0; i < recordpos.size(); ++i ) { if ( SquaredLength( recordpos[ i ], vTargetPos ) < Mathf::Sqr( fSplashRadius ) ) { MemoryRecord *pRec = sensory->GetMemoryRecord( records[ i ] ); if ( pRec ) { mShootBarrel = pRec->GetEntity(); return 1.f; } } } return 0.f; }
f32 Length(const f32* a) { return sqrtf(SquaredLength(a)); }
inline T Length () const { return ooge::math::Sqrt(SquaredLength()); }
f32 Vector3::Length() const { return sqrt(SquaredLength()); }
int checkCollide(glm::vec3 N,glm::vec3 p,glm::vec3 vel, glm::vec3 ta, glm::vec3 tb, glm::vec3 tc, glm::vec3 &result, float &distance){ // Figure out two times, t0 and t1. // By finding the signed distance to the plane at two places. // We want to know if a) it intersects the plane of the triangle // b) if it is within the bounds of the triangle (ta/tb/tc) // c) or if it's on the edges / points of the triangles. // This function returns 1 if it does intersect, or 0 otherwise. // get interval of intersection float t0, t1; bool embedded = false; // calc distance float distToPlane = SignedDistance(N,p,ta); float nDvel = glm::dot(N,vel); if (nDvel < 0.0f){ if (fabs(distToPlane) >= 1.0f){ return 0; } else { embedded = true; t0 = 0.0; t1 = 1.0; } } else{ t0 = (-1.0-distToPlane)/nDvel; t1 = (1.0 - distToPlane)/nDvel; if (t0 > t1){ float temp = t1; t1 = t0; t0 = temp; } if (t0>1.0f || t1< 0.0f){ return 0; } if (t0<0.0) t0 = 0.0; if (t1<0.0) t1 = 0.0; if (t0>1.0) t0 = 1.0; if (t1>1.0) t1 = 1.0; } glm::vec3 colPoint; bool foundCol = false; float t = 1.0; glm::vec3 planeIntersect = (p-N + t0*vel); if (!embedded){ if(checkPointInTriangle(planeIntersect, ta, tb, tc)){ foundCol = true; t = t0; colPoint = planeIntersect; distance = t*glm::length(vel); result = colPoint; return 1; } return 0; } /// VERY IMPORTANT. This is where it checks for intersection in the area of the triangle. else { if(checkPointInTriangle(planeIntersect, ta, tb, tc)){ foundCol = true; t = t0; colPoint = planeIntersect; distance = t*glm::length(vel); result = colPoint; return 1; } } if (foundCol ==false){ glm::vec3 base = p; float velSquared = SquaredLength(vel); float a,b,c; float newT; a = velSquared; // for ta b = 2.0f*glm::dot(vel,base-ta); c = SquaredLength(ta - base) -1.0; if (getLowestRoot(a,b,c,t, &newT)){ t = newT; foundCol = true; colPoint = ta; distance = t*glm::length(vel); result = colPoint; return 1; } // for tb b = 2.0f*glm::dot(vel,base-tb); c = SquaredLength(tb - base) -1.0; if (getLowestRoot(a,b,c,t, &newT)){ t = newT; foundCol = true; colPoint = tb; distance = t*glm::length(vel); result = colPoint; return 1; } // for tc b = 2.0f*glm::dot(vel,base-tc); c = SquaredLength((tc - base)) -1.0; if (getLowestRoot(a,b,c,t, &newT)){ t = newT; foundCol = true; colPoint = tc; distance = t*glm::length(vel); result = colPoint; return 1; } // now edges // ta -> tb glm::vec3 edge = tb-ta; glm::vec3 baseToVertex = ta - base; float edgeSquared = SquaredLength(edge); float edgeDotVel = glm::dot(edge, vel); float edgeDotBaseToVert = glm::dot(edge, baseToVertex); a = edgeSquared*-velSquared + edgeDotVel*edgeDotVel; b = edgeSquared*(2*glm::dot(vel,baseToVertex))-2.0*edgeDotVel*edgeDotBaseToVert; c = edgeSquared*(1-SquaredLength(baseToVertex))+edgeDotBaseToVert*edgeDotBaseToVert; if (getLowestRoot(a,b,c,t,&newT)){ float f = (edgeDotVel*newT - edgeDotBaseToVert)/edgeSquared; if(f>=0.0 && f<=1.0){ t = newT; foundCol = true; colPoint = ta + f*edge; distance = t*glm::length(vel); result = colPoint; return 1; } } // tb -> tc edge = tc-tb; baseToVertex = tb - base; edgeSquared = SquaredLength(edge); edgeDotVel = glm::dot(edge, vel); edgeDotBaseToVert = glm::dot(edge,baseToVertex); a = edgeSquared*-velSquared + edgeDotVel*edgeDotVel; b = edgeSquared*(2*glm::dot(vel,baseToVertex))-2.0*edgeDotVel*edgeDotBaseToVert; c = edgeSquared*(1-SquaredLength(baseToVertex))+edgeDotBaseToVert*edgeDotBaseToVert; if (getLowestRoot(a,b,c,t,&newT)){ float f = (edgeDotVel*newT - edgeDotBaseToVert)/edgeSquared; if(f>=0.0 && f<=1.0){ t = newT; foundCol = true; colPoint =tb + f*edge; distance = t*glm::length(vel); result = colPoint; return 1; } } // tc -> ta edge = ta-tc; baseToVertex = tc - base; edgeSquared = SquaredLength(edge); edgeDotVel = glm::dot(edge, vel); edgeDotBaseToVert = glm::dot(edge,baseToVertex); a = edgeSquared*-velSquared + edgeDotVel*edgeDotVel; b = edgeSquared*(2*glm::dot(vel,baseToVertex))-2.0*edgeDotVel*edgeDotBaseToVert; c = edgeSquared*(1-SquaredLength(baseToVertex))+edgeDotBaseToVert*edgeDotBaseToVert; if (getLowestRoot(a,b,c,t,&newT)){ float f = (edgeDotVel*newT - edgeDotBaseToVert)/edgeSquared; if(f>=0.0 && f<=1.0){ t = newT; foundCol = true; colPoint =tc + f*edge; distance = t*glm::length(vel); result = colPoint; return 1; } } } distance = t*glm::length(vel); return 0; }