new int linearProgram2(IList <Line> lines, float radius, Vector2 optVelocity, bool directionOpt, ref Vector2 result) { if (!directionOpt) { /* * Optimize direction. Note that the optimization velocity is of unit * length in this case. */ result = optVelocity * radius; } else if (Vector2.absSq(optVelocity) > Vector2.sqr(radius)) { /* Optimize closest point and outside circle. */ result = Vector2.normalize(optVelocity) * radius; } else { /* Optimize closest point and inside circle. */ result = optVelocity; } for (int i = 0; i < lines.Count; ++i) { if (Vector2.det(lines[i].direction, lines[i].point - result) > 0.0f) { /* Result does not satisfy constraint i. Compute new optimal result. */ Vector2 tempResult = result; if (!linearProgram1(lines, i, radius, optVelocity, directionOpt, ref result)) { result = tempResult; return(i); } } } return(lines.Count); }
public override void interactWith(SuperAgent agent) { SuperAgent other = agent; Vector2 relativePosition = other.position_ - position_; Vector2 relativeVelocity = velocity_ - other.velocity_; float distSq = (float)Math.Round(Vector2.absSq(relativePosition), 3); float combinedRadius = radius_ + other.radius_; float combinedRadiusSq = (float)Math.Round(Vector2.sqr(combinedRadius), 3); Vector2 w = new Vector2(); Line line = new Line(); Vector2 u; if (distSq > combinedRadiusSq) { /* No collision. */ w = relativeVelocity - (1.0f / timeHorizon_) * relativePosition; /* Vector from cutoff center to relative velocity. */ float wLengthSq = Vector2.absSq(w); Vector2 unitW = new Vector2(); float dotProduct1 = w * relativePosition; if (dotProduct1 < 0.0f && Vector2.sqr(dotProduct1) > combinedRadiusSq * wLengthSq) { /* Project on cut-off circle. */ float wLength = (float)Math.Round(Math.Sqrt(wLengthSq), 3); unitW = w / wLength; line.direction = new Vector2(unitW.y(), -unitW.x()); u = (combinedRadius * (1.0f / timeHorizon_) - wLength) * unitW; } else { /* Project on legs. */ float leg = (float)Math.Round(Math.Sqrt(distSq - combinedRadiusSq), 3); if (Vector2.det(relativePosition, w) > 0.0f) { /* Project on left leg. */ line.direction = new Vector2(relativePosition.x() * leg - relativePosition.y() * combinedRadius, relativePosition.x() * combinedRadius + relativePosition.y() * leg) / distSq; } else { /* Project on right leg. */ line.direction = -new Vector2(relativePosition.x() * leg + relativePosition.y() * combinedRadius, -relativePosition.x() * combinedRadius + relativePosition.y() * leg) / distSq; } float dotProduct2 = (float)Math.Round(relativeVelocity * line.direction, 3); u = dotProduct2 * line.direction - relativeVelocity; } } else { /* Collision. Project on cut-off circle of time timeStep. */ float invTimeStep = (float)Math.Round(1.0f / sim_.getTimeStep(), 3); /* Vector from cutoff center to relative velocity. */ w = relativeVelocity - invTimeStep * relativePosition; float wLength = (float)Math.Round(Vector2.abs(w), 3); Vector2 unitW = w / wLength; line.direction = new Vector2(unitW.y(), -unitW.x()); u = (combinedRadius * invTimeStep - wLength) * unitW; } // This is where you can choose the proportion of responsabilities that each agents takes in avoiding collision line.point = velocity_ + 0.5f * u; orcaLines_.Add(line); }
new public bool linearProgram1(IList <Line> lines, int lineNo, float radius, Vector2 optVelocity, bool directionOpt, ref Vector2 result) { float dotProduct = lines[lineNo].point * lines[lineNo].direction; float discriminant = Vector2.sqr(dotProduct) + Vector2.sqr(radius) - Vector2.absSq(lines[lineNo].point); if (discriminant < 0.0f) { /* Max speed circle fully invalidates line lineNo. */ return(false); } float sqrtDiscriminant = (float)Math.Sqrt(discriminant); float tLeft = -dotProduct - sqrtDiscriminant; float tRight = -dotProduct + sqrtDiscriminant; ; for (int i = 0; i < lineNo; ++i) { float denominator = Vector2.det(lines[lineNo].direction, lines[i].direction); float numerator = Vector2.det(lines[i].direction, lines[lineNo].point - lines[i].point); if (Math.Abs(denominator) <= RVO_EPSILON) { /* Lines lineNo and i are (almost) parallel. */ if (numerator < 0.0f) { return(false); } else { continue; } } float t = numerator / denominator; if (denominator >= 0.0f) { /* Line i bounds line lineNo on the right. */ tRight = Math.Min(tRight, t); } else { /* Line i bounds line lineNo on the left. */ tLeft = Math.Max(tLeft, t); } if (tLeft > tRight) { return(false); } } if (!directionOpt) { /* Optimize direction. */ if (optVelocity * lines[lineNo].direction > 0.0f) { /* Take right extreme. */ result = lines[lineNo].point + tRight * lines[lineNo].direction; } else { /* Take left extreme. */ result = lines[lineNo].point + tLeft * lines[lineNo].direction; } } else { /* Optimize closest point. */ float t = lines[lineNo].direction * (optVelocity - lines[lineNo].point); if (t < tLeft) { result = lines[lineNo].point + tLeft * lines[lineNo].direction; } else if (t > tRight) { result = lines[lineNo].point + tRight * lines[lineNo].direction; } else { result = lines[lineNo].point + t * lines[lineNo].direction; } } return(true); }
public override void interactWith(Obstacle obstacle) { Obstacle obstacle1 = obstacle; Obstacle obstacle2 = obstacle1.next_; Vector2 relativePosition1 = obstacle1.point_ - position_; Vector2 relativePosition2 = obstacle2.point_ - position_; /* * Check if velocity obstacle of obstacle is already taken care of by * previously constructed obstacle ORCA lines. */ bool alreadyCovered = false; for (int j = 0; j < orcaLines_.Count; ++j) { if (Vector2.det((1.0f / timeHorizonObst_) * relativePosition1 - orcaLines_[j].point, orcaLines_[j].direction) - (1.0f / timeHorizonObst_) * radius_ >= -RVO_EPSILON && Vector2.det((1.0f / timeHorizonObst_) * relativePosition2 - orcaLines_[j].point, orcaLines_[j].direction) - (1.0f / timeHorizonObst_) * radius_ >= -RVO_EPSILON) { alreadyCovered = true; break; } } if (alreadyCovered) { return; } /* Not yet covered. Check for collisions. */ float distSq1 = Vector2.absSq(relativePosition1); float distSq2 = Vector2.absSq(relativePosition2); float radiusSq = Vector2.sqr(radius_); Vector2 obstacleVector = obstacle2.point_ - obstacle1.point_; float s = (-relativePosition1 * obstacleVector) / Vector2.absSq(obstacleVector); float distSqLine = Vector2.absSq(-relativePosition1 - s * obstacleVector); Line line; if (s < 0 && distSq1 <= radiusSq) { /* Collision with left vertex. Ignore if non-convex. */ if (obstacle1.convex_) { line.point = new Vector2(0, 0); line.direction = Vector2.normalize(new Vector2(-relativePosition1.y(), relativePosition1.x())); orcaLines_.Add(line); } return; } else if (s > 1 && distSq2 <= radiusSq) { /* Collision with right vertex. Ignore if non-convex * or if it will be taken care of by neighoring obstace */ if (obstacle2.convex_ && Vector2.det(relativePosition2, obstacle2.direction_) >= 0) { line.point = new Vector2(0, 0); line.direction = Vector2.normalize(new Vector2(-relativePosition2.y(), relativePosition2.x())); orcaLines_.Add(line); } return; } else if (s >= 0 && s < 1 && distSqLine <= radiusSq) { /* Collision with obstacle segment. */ line.point = new Vector2(0, 0); line.direction = -obstacle1.direction_; orcaLines_.Add(line); return; } /* * No collision. * Compute legs. When obliquely viewed, both legs can come from a single * vertex. Legs extend cut-off line when nonconvex vertex. */ Vector2 leftLegDirection, rightLegDirection; if (s < 0 && distSqLine <= radiusSq) { /* * Obstacle viewed obliquely so that left vertex * defines velocity obstacle. */ if (!obstacle1.convex_) { /* Ignore obstacle. */ return; } obstacle2 = obstacle1; float leg1 = (float)Math.Sqrt(distSq1 - radiusSq); leftLegDirection = new Vector2(relativePosition1.x() * leg1 - relativePosition1.y() * radius_, relativePosition1.x() * radius_ + relativePosition1.y() * leg1) / distSq1; rightLegDirection = new Vector2(relativePosition1.x() * leg1 + relativePosition1.y() * radius_, -relativePosition1.x() * radius_ + relativePosition1.y() * leg1) / distSq1; } else if (s > 1 && distSqLine <= radiusSq) { /* * Obstacle viewed obliquely so that * right vertex defines velocity obstacle. */ if (!obstacle2.convex_) { /* Ignore obstacle. */ return; } obstacle1 = obstacle2; float leg2 = (float)Math.Sqrt(distSq2 - radiusSq); leftLegDirection = new Vector2(relativePosition2.x() * leg2 - relativePosition2.y() * radius_, relativePosition2.x() * radius_ + relativePosition2.y() * leg2) / distSq2; rightLegDirection = new Vector2(relativePosition2.x() * leg2 + relativePosition2.y() * radius_, -relativePosition2.x() * radius_ + relativePosition2.y() * leg2) / distSq2; } else { /* Usual situation. */ if (obstacle1.convex_) { float leg1 = (float)Math.Sqrt(distSq1 - radiusSq); leftLegDirection = new Vector2(relativePosition1.x() * leg1 - relativePosition1.y() * radius_, relativePosition1.x() * radius_ + relativePosition1.y() * leg1) / distSq1; } else { /* Left vertex non-convex; left leg extends cut-off line. */ leftLegDirection = -obstacle1.direction_; } if (obstacle2.convex_) { float leg2 = (float)Math.Sqrt(distSq2 - radiusSq); rightLegDirection = new Vector2(relativePosition2.x() * leg2 + relativePosition2.y() * radius_, -relativePosition2.x() * radius_ + relativePosition2.y() * leg2) / distSq2; } else { /* Right vertex non-convex; right leg extends cut-off line. */ rightLegDirection = obstacle1.direction_; } } /* * Legs can never point into neighboring edge when convex vertex, * take cutoff-line of neighboring edge instead. If velocity projected on * "foreign" leg, no constraint is added. */ Obstacle leftNeighbor = obstacle1.previous_; bool isLeftLegForeign = false; bool isRightLegForeign = false; if (obstacle.convex_ && Vector2.det(leftLegDirection, -leftNeighbor.direction_) >= 0.0f) { /* Left leg points into obstacle. */ leftLegDirection = -leftNeighbor.direction_; isLeftLegForeign = true; } if (obstacle2.convex_ && Vector2.det(rightLegDirection, obstacle2.direction_) <= 0.0f) { /* Right leg points into obstacle. */ rightLegDirection = obstacle2.direction_; isRightLegForeign = true; } /* Compute cut-off centers. */ Vector2 leftCutoff = (1.0f / timeHorizonObst_) * (obstacle1.point_ - position_); Vector2 rightCutoff = (1.0f / timeHorizonObst_) * (obstacle2.point_ - position_); Vector2 cutoffVec = rightCutoff - leftCutoff; /* Project current velocity on velocity obstacle. */ /* Check if current velocity is projected on cutoff circles. */ float t = (obstacle1 == obstacle2 ? 0.5f : ((velocity_ - leftCutoff) * cutoffVec) / Vector2.absSq(cutoffVec)); float tLeft = ((velocity_ - leftCutoff) * leftLegDirection); float tRight = ((velocity_ - rightCutoff) * rightLegDirection); if ((t < 0.0f && tLeft < 0.0f) || (obstacle1 == obstacle2 && tLeft < 0.0f && tRight < 0.0f)) { /* Project on left cut-off circle. */ Vector2 unitW = Vector2.normalize(velocity_ - leftCutoff); line.direction = new Vector2(unitW.y(), -unitW.x()); line.point = leftCutoff + radius_ * (1.0f / timeHorizonObst_) * unitW; orcaLines_.Add(line); return; } else if (t > 1.0f && tRight < 0.0f) { /* Project on right cut-off circle. */ Vector2 unitW = Vector2.normalize(velocity_ - rightCutoff); line.direction = new Vector2(unitW.y(), -unitW.x()); line.point = rightCutoff + radius_ * (1.0f / timeHorizonObst_) * unitW; orcaLines_.Add(line); return; } /* * Project on left leg, right leg, or cut-off line, whichever is closest * to velocity. */ float distSqCutoff = ((t <0.0f || t> 1.0f || obstacle1 == obstacle2) ? Single.PositiveInfinity : Vector2.absSq(velocity_ - (leftCutoff + t * cutoffVec))); float distSqLeft = ((tLeft < 0.0f) ? Single.PositiveInfinity : Vector2.absSq(velocity_ - (leftCutoff + tLeft * leftLegDirection))); float distSqRight = ((tRight < 0.0f) ? Single.PositiveInfinity : Vector2.absSq(velocity_ - (rightCutoff + tRight * rightLegDirection))); if (distSqCutoff <= distSqLeft && distSqCutoff <= distSqRight) { /* Project on cut-off line. */ line.direction = -obstacle1.direction_; line.point = leftCutoff + radius_ * (1.0f / timeHorizonObst_) * new Vector2(-line.direction.y(), line.direction.x()); orcaLines_.Add(line); return; } else if (distSqLeft <= distSqRight) { /* Project on left leg. */ if (isLeftLegForeign) { return; } line.direction = leftLegDirection; line.point = leftCutoff + radius_ * (1.0f / timeHorizonObst_) * new Vector2(-line.direction.y(), line.direction.x()); orcaLines_.Add(line); return; } else { /* Project on right leg. */ if (isRightLegForeign) { return; } line.direction = -rightLegDirection; line.point = rightCutoff + radius_ * (1.0f / timeHorizonObst_) * new Vector2(-line.direction.y(), line.direction.x()); orcaLines_.Add(line); return; } }
// Update is called once per frame void Update() { for (int block = 0; block < workers.Count; ++block) { doneEvents_[block].Reset(); ThreadPool.QueueUserWorkItem(workers[block].clear); } WaitHandle.WaitAll(doneEvents_); updateWorker(workers.Count); if (!reachedGoal()) { if (hum_but_.isOn && !human_prefab) { int range = agents.Count; for (int i = 0; i < range; i++) { Destroy(agents[i].gameObject); addAgent(human, agents[i].position, sim_.getDefaultRadius(), i); } human_prefab = true; } else if (!hum_but_.isOn && human_prefab) { int range = agents.Count; for (int i = 0; i < range; i++) { Destroy(agents[i].gameObject); addAgent(prefab, agents[i].position, sim_.getDefaultRadius(), i); } human_prefab = false; } setAgentsProperties(); setPreferredVelocities(); sim_.initialize_virtual_and_agents(); for (int i = 0; i < getNumAgents(); i++) { Vector2 agent_position = sim_.getAgentPosition(i); Vector2 p1 = agent_position + new Vector2(corridor_length_, 0); sim_.addVirtualAgent(0, p1); } doStep(true); /* Output the current global time. */ //print(Simulator.Instance.getGlobalTime()); if (follow_but_.isOn != follow_) { follow_ = follow_but_.isOn; for (int i = 0; i < getNumAgents(); ++i) { sim_.agents_[i].follows_ = follow_; } } if (save_but_.isOn != sim_.save) { sim_.save = save_but_.isOn; } Vector3 pos3 = Camera.main.transform.position; Camera.main.transform.position = new Vector3(pos3.x, camera_height_.value, pos3.z); int totot = getNumAgents(); for (int i = 0; i < getNumAgents(); ++i) { Vector2 position = sim_.getAgentPosition(i); agents[i].transform.position = new Vector3(position.x(), 0f, position.y()); RVO.Vector2 vector2 = sim_.getAgentVelocity(i); agents[i].rotation = Quaternion.LookRotation(new Vector3(vector2.x_, 0, vector2.y_)); if (human_prefab) { if (Vector2.absSq(sim_.getAgentVelocity(i) * 4) > 1.5f) { agents[i].GetComponent <Animator>().CrossFade("mixamo.com", 10, 1); } agents[i].GetComponent <Animator>().speed = Vector2.absSq(sim_.getAgentVelocity(i) * 4); } if (!human_prefab) { setColor(i); } } } else { for (int i = 0; i < getNumAgents(); ++i) { agents[i].transform.GetComponent <Rigidbody>().isKinematic = true; } } for (int block = 0; block < workers.Count; ++block) { doneEvents_[block].Reset(); ThreadPool.QueueUserWorkItem(workers[block].computeMedVelocity); } WaitHandle.WaitAll(doneEvents_); String tmp = ""; for (int i = 0; i < workers.Count; i++) { tmp += Vector2.abs(workers[i].vit_moy) + "\t"; } using (TextWriter tw = new StreamWriter(name, true)) { tw.WriteLine(tmp); } }
internal SuperAgent representGroup(Agent observer) { // Compute the left & right tangent points obtained for each agent of the group // First element of the pair = right tangent // Second element of the pair = left tangent IList <Pair <Vector2, Vector2> > tangents = new List <Pair <Vector2, Vector2> >(); IList <Pair <Vector2, Vector2> > radii = new List <Pair <Vector2, Vector2> >(); for (int i = 0; i < agents_.Count; i++) { tangents.Add(computeTangentsPoints(observer, agents_[i])); Pair <Vector2, Vector2> rads = new Pair <Vector2, Vector2>(); rads.First = tangents[i].First - observer.position_; rads.Second = tangents[i].Second - observer.position_; radii.Add(rads); } // Compute the group tangent points (extrema) int rightExtremumId = 0; int leftExtremumId = 0; for (int i = 1; i < tangents.Count; i++) { // Comparison if (Vector2.isOnTheLeftSide(radii[rightExtremumId].First, radii[i].First)) { rightExtremumId = i; } if (Vector2.isOnTheLeftSide(radii[i].Second, radii[leftExtremumId].Second)) { leftExtremumId = i; } } // If the tangent are taking more than 180°, cannot be considered as a group for (int i = 0; i < agents_.Count; i++) { if (Vector2.isOnTheLeftSide(radii[rightExtremumId].First, radii[i].First)) { //std::cout << "Problem representing groups : tangent angle > 180°\n"; return(new SuperAgent(null)); } if (Vector2.isOnTheLeftSide(radii[i].Second, radii[leftExtremumId].Second)) { //std::cout << "Problem representing groups : tangent angle > 180°\n"; return(new SuperAgent(null)); } } // Compute bisector Vector2 rightTangent = Vector2.rotation(radii[rightExtremumId].First, (float)Math.PI / 2); Vector2 leftTangent = Vector2.rotation(radii[leftExtremumId].Second, -(float)Math.PI / 2); Vector2 intersectionPoint = Vector2.intersectOf2Lines(tangents[rightExtremumId].First, tangents[rightExtremumId].First + rightTangent, tangents[leftExtremumId].Second, tangents[leftExtremumId].Second + leftTangent); // alpha/2 is more usefull than alpha float alpha2 = Vector2.angle(Vector2.rotation(tangents[leftExtremumId].Second - intersectionPoint, -Vector2.angle(tangents[rightExtremumId].First - intersectionPoint))) / 2; if (alpha2 < 0) { //std::cout << "Problem representing groups : angle computation\n SHALL NOT HAPPEN !!! \n"; // But if radii are different or if return(new SuperAgent(null)); } Vector2 bisector_normalize_vector = Vector2.normalize(observer.position_ - intersectionPoint); // Compute circle // The distance between the observer and the circle (along the bisector axis) float d = Single.PositiveInfinity; float a, b, c, delta, x; int constrainingAgent = 0; for (int i = 0; i < agents_.Count; i++) { Vector2 ic1 = agents_[i].position_ - intersectionPoint; a = 1 - Vector2.sqr((float)Math.Sin(alpha2)); b = 2 * (agents_[i].radius_ * (float)Math.Sin(alpha2) - ic1 * bisector_normalize_vector); c = Vector2.absSq(ic1) - Vector2.sqr(agents_[i].radius_); delta = Vector2.sqr(b) - 4 * a * c; if (delta <= 0) { if (delta < -4 * RVO_EPSILON * c) { return(new SuperAgent(null)); } else { delta = 0; } } x = (-b + (float)Math.Sqrt(delta)) / (2 * a); if (x < d) { d = x; constrainingAgent = i; } } if (d < Vector2.abs(observer.position_ - intersectionPoint) + observer.radius_ + d * Math.Sin(alpha2)) { return(new SuperAgent(null)); } SuperAgent toReturn = new SuperAgent(sim_); toReturn.position_ = intersectionPoint + bisector_normalize_vector * d; toReturn.radius_ = d * (float)Math.Sin(alpha2); toReturn.velocity_ = agents_[constrainingAgent].velocity_; return(toReturn); }