Should my feeler rays be the same length in all directions or proportionally longer in the direction of the agent's velocity?
I currently extend all the rays, regardless of their direction, based on the agent's velocity:
/// <summary>
/// Steers the agent to avoid moving obstacles lying in its path
/// </summary>
/// <returns></returns>
private Vector3 ObstacleAvoidance()
{
List<RayCastResult> results = new List<RayCastResult>();
Vector3 steeringForce = new Vector3();
// Feeler length is proportional to the agent's velocity
float speed = agent.Velocity.Length();
float ooMaxSpeed = 1f / agent.MaxSpeed;
float feelerLength = speed * ooMaxSpeed * minFeelerLength;
feelerLength += minFeelerLength;
// For each one of the agent's feelers
for (int i = 0; i < numFeelers; ++i)
{
// Clear the list in preparation for the new ray cast
results.Clear();
if (game.Space.RayCast(feelers, feelerLength, results))
{
float earliestTime = float.MaxValue;
EntityCollidable entityHit = null;
int index = 0;
int numHit = results.Count;
for (int ii = 0; ii < numHit; ++ii)
{
EntityCollidable e = results[ii].HitObject as EntityCollidable;
// Ignore non EntityCollidables
// Ignore feeler collision with the agent
// Keep the earliest time of collision
if (e != null && e.Entity.Tag != agent && results[ii].HitData.T < earliestTime)
{
earliestTime = results[ii].HitData.T;
entityHit = e;
index = ii;
}
}
if (entityHit == null)
{
// Nothing to avoid
continue;
}
else
{
// [2.5D]
Vector3 offset = agent.Position - results[index].HitData.Location;
// 'PerpendicularComponent' method
float projection = Vector3.Dot(offset, agent.Forward);
Vector3 avoidance = offset - (agent.Forward * projection);
avoidance.Normalize();
avoidance *= agent.MaxSpeed;
avoidance += agent.Forward * agent.MaxSpeed * 0.75f;
avoidance -= agent.Velocity;
steeringForce += avoidance;
}
}
}
return steeringForce;
}
However, this produces violent shaking of the agent and not smooth obstacle avoidance:
Video of shaking behaviour
Should I take the dot product of the ray direction and the direction of the velocity and only extend by a multiple of the dot product? (Values less than 0 would be 0)