📄 steeringbehavior.vb
字号:
Dim WorldOffsetPos As Vector2D = Transformations.PointToWorldSpace(offset, leader.Heading(), leader.Side(), leader.Pos())
Dim ToOffset As Vector2D = WorldOffsetPos.Minus(m_pVehicle.Pos())
'//the lookahead time is propotional to the distance between the leader
'//and the pursuer; and is inversely proportional to the sum of both
'//agent's velocities
Dim LookAheadTime As Double = ToOffset.Length() / (m_pVehicle.MaxSpeed() + leader.Speed())
'//now Arrive at the predicted future position of the offset
Return Arrive(WorldOffsetPos.Plus(leader.Velocity().Mutiply(LookAheadTime)), Deceleration.fast)
End Function
'//this behavior attempts to evade a pursuer
'//----------------------------- Evade ------------------------------------
'//
'// similar to pursuit except the agent Flees from the estimated future
'// position of the pursuer
'//------------------------------------------------------------------------
Private Function Evade(ByVal pursuer As Vehicle) As Vector2D
'/* Not necessary to include the check for facing direction this time */
Dim ToPursuer As Vector2D = pursuer.Pos().Plus(m_pVehicle.Pos())
'//uncomment the following two lines to have Evade only consider pursuers
'//within a 'threat range'
Dim ThreatRange As Double = 100.0
If ToPursuer.LengthSQ() > ThreatRange * ThreatRange Then Return New Vector2D
'//the lookahead time is propotional to the distance between the pursuer
'//and the pursuer; and is inversely proportional to the sum of the
'//agents' velocities
Dim LookAheadTime As Double = ToPursuer.Length() / (m_pVehicle.MaxSpeed() + pursuer.Speed())
'//now flee away from predicted future position of the pursuer
Return Flee(pursuer.Pos().Minus(pursuer.Velocity()).Mutiply(LookAheadTime))
End Function
'//this behavior makes the agent wander about randomly
'//--------------------------- Wander -------------------------------------
'//
'// This behavior makes the agent wander about randomly
'//------------------------------------------------------------------------
Private Function Wander() As Vector2D
'//this behavior is dependent on the update rate, so this line must
'//be included when using time independent framerate.
Dim JitterThisTimeSlice As Double = m_dWanderJitter * m_pVehicle.TimeElapsed()
'//first, add a small random vector to the target's position
m_vWanderTarget.PlusEqual(New Vector2D(Utils.RandomClamped() * JitterThisTimeSlice, Utils.RandomClamped() * JitterThisTimeSlice))
'//reproject this new vector back on to a unit circle
m_vWanderTarget.Normalize()
'//increase the length of the vector to the same as the radius
'//of the wander circle
m_vWanderTarget.MutiplyEqual(m_dWanderRadius)
'//move the target into a position WanderDist in front of the agent
Dim target As Vector2D = m_vWanderTarget.Plus(New Vector2D(m_dWanderDistance, 0))
'//project the target into world space
Dim tTarget As Vector2D = Transformations.PointToWorldSpace(target, m_pVehicle.Heading(), m_pVehicle.Side(), m_pVehicle.Pos())
'//and steer towards it
Return tTarget.Minus(m_pVehicle.Pos())
End Function
'//this returns a steering force which will attempt to keep the agent
'//away from any obstacles it may encounter
'//---------------------- ObstacleAvoidance -------------------------------
'//
'// Given a vector of CObstacles, this method returns a steering force
'// that will prevent the agent colliding with the closest obstacle
'//------------------------------------------------------------------------
Private Function ObstacleAvoidance(ByVal obstacles As ArrayList) As Vector2D
'//the detection box length is proportional to the agent's velocity
m_dDBoxLength = Prm.MinDetectionBoxLength + (m_pVehicle.Speed() / m_pVehicle.MaxSpeed()) * Prm.MinDetectionBoxLength
'//tag all obstacles within range of the box for processing
m_pVehicle.World().TagObstaclesWithinViewRange(m_pVehicle, m_dDBoxLength)
'//this will keep track of the closest intersecting obstacle (CIB)
Dim ClosestIntersectingObstacle As BaseGameEntity = Nothing
'//this will be used to track the distance to the CIB
Dim DistToClosestIP As Double = Double.MaxValue
'//this will record the transformed local coordinates of the CIB
Dim LocalPosOfClosestObstacle As Vector2D
Dim curOb As BaseGameEntity
Dim i As Integer
For i = 0 To obstacles.Count - 1
curOb = obstacles(i)
'//if the obstacle has been tagged within range proceed
If curOb.IsTagged Then
'//calculate this obstacle's position in local space
Dim LocalPos As Vector2D = Transformations.PointToLocalSpace(curOb.Pos(), m_pVehicle.Heading(), m_pVehicle.Side(), m_pVehicle.Pos())
'//if the local position has a negative x value then it must lay
'//behind the agent. (in which case it can be ignored)
If (LocalPos.x >= 0) Then
'//if the distance from the x axis to the object's position is less
'//than its radius + half the width of the detection box then there
'//is a potential intersection.
Dim ExpandedRadius As Double = curOb.BRadius() + m_pVehicle.BRadius()
If Math.Abs(LocalPos.y) < ExpandedRadius Then
'//now to do a line/circle intersection test. The center of the
'//circle is represented by (cX, cY). The intersection points are
'//given by the formula x = cX +/-sqrt(r^2-cY^2) for y=0.
'//We only need to look at the smallest positive value of x because
'//that will be the closest point of intersection.
Dim cX As Double = LocalPos.x
Dim cY As Double = LocalPos.y
'//we only need to calculate the sqrt part of the above equation once
Dim SqrtPart As Double = Math.Sqrt(ExpandedRadius * ExpandedRadius - cY * cY)
Dim ip As Double = cX - SqrtPart
If (ip <= 0.0) Then
ip = cX + SqrtPart
End If
'//test to see if this is the closest so far. If it is keep a
'//record of the obstacle and its local coordinates
If (ip < DistToClosestIP) Then
DistToClosestIP = ip
ClosestIntersectingObstacle = curOb
LocalPosOfClosestObstacle = LocalPos
End If
End If
End If
End If
Next
'//if we have found an intersecting obstacle, calculate a steering
'//force away from it
Dim SteeringForce As New Vector2D
If Not ClosestIntersectingObstacle Is Nothing Then
'//the closer the agent is to an object, the stronger the
'//steering force should be
Dim multiplier As Double = 1.0 + (m_dDBoxLength - LocalPosOfClosestObstacle.x) / m_dDBoxLength
'//calculate the lateral force
SteeringForce.y = (ClosestIntersectingObstacle.BRadius() - LocalPosOfClosestObstacle.y) * multiplier
'//apply a braking force proportional to the obstacles distance from
'//the vehicle.
Dim BrakingWeight As Double = 0.2
SteeringForce.x = (ClosestIntersectingObstacle.BRadius() - LocalPosOfClosestObstacle.x) * BrakingWeight
End If
'//finally, convert the steering vector from local to world space
Return Transformations.VectorToWorldSpace(SteeringForce, m_pVehicle.Heading(), m_pVehicle.Side())
End Function
'//this returns a steering force which will keep the agent away from any
'//walls it may encounter
'//--------------------------- WallAvoidance --------------------------------
'//
'// This returns a steering force that will keep the agent away from any
'// walls it may encounter
'//------------------------------------------------------------------------
Private Function WallAvoidance(ByVal walls As ArrayList) As Vector2D
'//the feelers are contained in a std::vector, m_Feelers
CreateFeelers()
Dim DistToThisIP As Double = 0.0
Dim DistToClosestIP As Double = Double.MaxValue
'//this will hold an index into the vector of walls
Dim ClosestWall As Integer = -1
Dim SteeringForce As Vector2D
Dim point As Vector2D ' //used for storing temporary info
Dim ClosestPoint As Vector2D ' //holds the closest intersection point
'//examine each feeler in turn
Dim flr As Integer
Dim w As Integer
For flr = 0 To m_Feelers.Count - 1
'//run through each wall checking for any intersection points
For w = 0 To walls.Count - 1
If Geometry.LineIntersection2D(m_pVehicle.Pos(), m_Feelers(flr), CType(walls(w), Wall2D).From(), CType(walls(w), Wall2D).ToB, DistToThisIP, point) Then
'//is this the closest found so far? If so keep a record
If (DistToThisIP < DistToClosestIP) Then
DistToClosestIP = DistToThisIP
ClosestWall = w
ClosestPoint = point
End If
End If
Next
'//if an intersection point has been detected, calculate a force
'//that will direct the agent away
If (ClosestWall >= 0) Then
'//calculate by what distance the projected position of the agent
'//will overshoot the wall
Dim OverShoot As Vector2D = CType(m_Feelers(flr), Vector2D).Minus(ClosestPoint)
'//create a force in the direction of the wall normal, with a
'//magnitude of the overshoot
SteeringForce = CType(walls(ClosestWall), Wall2D).Normal() * OverShoot.Length()
End If
Next
Return SteeringForce
End Function
'//given a series of Vector2Ds, this method produces a force that will
'//move the agent along the waypoints in order
'//------------------------------- FollowPath -----------------------------
'//
'// Given a series of Vector2Ds, this method produces a force that will
'// move the agent along the waypoints in order. The agent uses the
'// 'Seek' behavior to move to the next waypoint - unless it is the last
'// waypoint, in which case it 'Arrives'
'//------------------------------------------------------------------------
Private Function FollowPath() As Vector2D
'//move to next target if close enough to current target (working in
'//distance squared space)
If Vector2D.Vec2DDistanceSQ(m_pPath.CurrentWaypoint(), m_pVehicle.Pos()) < m_dWaypointSeekDistSq Then
m_pPath.SetNextWaypoint()
End If
If Not m_pPath.Finished() Then
Return Seek(m_pPath.CurrentWaypoint())
Else
Return Arrive(m_pPath.CurrentWaypoint(), Deceleration.normal)
End If
End Function
'//this results in a steering force that attempts to steer the vehicle
'//to the center of the vector connecting two moving agents.
'//--------------------------- Interpose ----------------------------------
'//
'// Given two agents, this method returns a force that attempts to
'// position the vehicle between them
'//------------------------------------------------------------------------
Private Function Interpose(ByVal AgentA As Vehicle, ByVal AgentB As Vehicle) As Vector2D
'//first we need to figure out where the two agents are going to be at
'//time T in the future. This is approximated by determining the time
'//taken to reach the mid way point at the current time at at max speed.
Dim MidPoint As Vector2D = AgentA.Pos().Plus(AgentB.Pos()).Divided(2.0)
Dim TimeToReachMidPoint As Double = Vector2D.Vec2DDistance(m_pVehicle.Pos(), MidPoint) / m_pVehicle.MaxSpeed()
'//now we have T, we assume that agent A and agent B will continue on a
'//straight trajectory and extrapolate to get their future positions
Dim APos As Vector2D = AgentA.Pos().Plus(AgentA.Velocity().Mutiply(TimeToReachMidPoint))
Dim BPos As Vector2D = AgentB.Pos().Plus(AgentB.Velocity().Mutiply(TimeToReachMidPoint))
'//calculate the mid point of these predicted positions
MidPoint = APos.Plus(BPos).Divided(2.0)
'//then steer to Arrive at it
Return Arrive(MidPoint, Deceleration.fast)
End Function
'//given another agent position to hide from and a list of BaseGameEntitys this
'//method attempts to put an obstacle between itself and its opponent
Private Function Hide(ByVal hunter As Vehicle, ByVal obstacles As ArrayList) As Vector2D
Dim DistToClosest As Double = Double.MaxValue
Dim BestHidingSpot As Vector2D
Dim curOb, closest As BaseGameEntity
Dim i As Integer
For i = 0 To obstacles.Count - 1
curOb = obstacles(i)
'//calculate the position of the hiding spot for this obstacle
Dim HidingSpot As Vector2D = GetHidingPosition(curOb.Pos(), curOb.BRadius(), hunter.Pos())
'//work in distance-squared space to find the closest hiding
'//spot to the agent
Dim dist As Double = Vector2D.Vec2DDistanceSQ(HidingSpot, m_pVehicle.Pos())
If (dist < DistToClosest) Then
DistToClosest = dist
BestHidingSpot = HidingSpot
closest = curOb
End If
Next
'//if no suitable obstacles found then Evade the hunter
If (DistToClosest <> Single.MaxValue) Then
Return Evade(hunter)
End If
'//else use Arrive on the hiding spot
Return Arrive(BestHidingSpot, Deceleration.fast)
End Function
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -