/** * Creates the antenna utilized by WallAvoidance */ private void CreateFeelers() { m_Feelers.Clear(); //feeler pointing straight in front m_Feelers.Add(m_entity.VPos() + m_entity.Heading() * m_fWallDetectionFeelerLength); //feeler to left Vector3 temp = m_entity.Heading(); Quaternion rotate = Quaternion.Euler(0, 90.0f, 0); Vector3 temp2 = rotate * temp; m_Feelers.Add(m_entity.VPos() + temp2 * m_fWallDetectionFeelerLength / 2.0f); rotate = Quaternion.Euler(0, 45.0f, 0); temp2 = rotate * temp; //feeler to right m_Feelers.Add(m_entity.VPos() + temp2 * m_fWallDetectionFeelerLength / 2.0f); }
/** * this behavior creates a force that steers the agent towards the * evader */ private Vector3 Pursuit(AIAgent evader) { //if the evader is ahead and facing the agent then we can just seek //for the evader's current position. Vector3 ToEvader = evader.VPos() - m_entity.VPos(); float RelativeHeading = Vector3.Dot(m_entity.Heading(), evader.Heading()); if (Vector3.Dot(ToEvader, m_entity.Heading()) > 0 && (RelativeHeading < -0.95)) //acos(0.95)=18 degs { return(Seek(evader.VPos())); } //Not considered ahead so we predict where the evader will be. //the lookahead time is propotional to the distance between the evader //and the pursuer; and is inversely proportional to the sum of the //agent's velocities float LookAheadTime = ToEvader.magnitude / (m_entity.MaxSpeed() + evader.Speed()); //now seek to the predicted future position of the evader return(Seek(evader.VPos() + evader.Velocity() * LookAheadTime)); }
public void AIUpdate() { //update the time elapsed //keep a record of its old position so we can update its cell later //in this method Vector3 OldVelocity = aiAgent.Velocity(); Vector3 SteeringForce = Vector3.zero; //calculate the combined force from each steering behavior in the //vehicle's list SteeringForce = steeringBehaviour.Calculate(); //Acceleration = Force/Mass Vector3 acceleration = SteeringForce / aiAgent.Mass(); Vector3 gravityaccel = steeringBehaviour.GravityAccel(); grounded = (gravityaccel.magnitude > 0) ? true : false; aiAgent.Update(acceleration, gravityaccel, useWrapAround, Time.deltaTime); Vector3 vPos = aiAgent.VPos(); vPos.y = steeringBehaviour.GravityTruncate(vPos.y); aiAgent.SetVPos(vPos); //EnforceNonPenetrationConstraint(this, World()->Agents()); //update the vehicle's current cell if space partitioning is turned on if (isSmoothingOn()) { m_vSmoothedHeading = m_pHeadingSmoother.Update(aiAgent.Heading()); } //moveState if (steeringBehaviour.IsSteering()) { float epsillon = 0.01f; if (MathUtil.Equal(aiAgent.Velocity(), Vector3.zero, epsillon) && OldVelocity.magnitude >= epsillon) { if (onMoveComplete != null) { onMoveComplete(aiAgent, steeringBehaviour, MoveCompleteState.Reached); } } else if (gravityaccel.magnitude <= 0 && gravityaccel_old.magnitude > 0) { if (onMoveComplete != null) { onMoveComplete(aiAgent, steeringBehaviour, MoveCompleteState.Landed); } } } gameObject.transform.position = aiAgent.VPos(); if (useRotation) { Vector3 vDir = aiAgent.Heading(); if (vDir.magnitude > 0) { gameObject.transform.rotation = Quaternion.LookRotation(vDir); } } Vector3 target = gameObject.transform.position + gameObject.transform.rotation * Vector3.forward * aiAgent.BRadius(); gravityaccel_old = gravityaccel; DebugExtension.DebugCircle(target, 0.5f); }