I making a small steering simulator using the Reynolds boid algorithm. Now I want to add a wall avoidance feature. My walls are in 3D and defined using two points like that:
---------. P2
| |
P1 .---------
My agents have a velocity, a position, etc...
Could you tell me how to make avoidance with my agents?
Vector2D ReynoldsSteeringModel::repulsionFromWalls()
{
Vector2D force;
vector wallsList = walls();
Point2D pos = self()->position();
Vector2D velocity = self()->velocity();
for (unsigned i=0; i {
//TODO
}
return force;
}
Then I use all the forces returned by my boid functions and I apply it to my agent.
I just need to know how to do that with my walls.
Thanks for your help.
Answer
Let each wall exert an influence on the velocity.
Try something like using the inverse distance (or inverse squared distance) from the wall to determine the magnitude of the force that each wall "exerts", and the normal of the wall to determine the direction of the force that the wall "exerts".
So here the boid interacts with 4 walls. Since the dot product of the red vectors (boid-to-wall-center) is greater than 0 for 3 of the 4 walls, those walls won't exert a force on the boid.
Only the wall with a blue vector (negative dot product) will have a force.
The magnitude of the force should be great with the boid gets too close to the wall, and the direction of the force should be in the direction of the black arrow on the wall (pointing directly away from the wall).
If you use 1 / (t+1)
for the magnitude of the force, where t
is the distance from the wall, then the force will be really strong when gets close to 0, but fade off to nothing when t gets higher (note the axis scale in the diagram, it is not 0 when t=5, it is 0.2). (The t+1 is so that you don't get an infinite force / divide by 0 if the boid happens to enter the wall).
If you use 1/(t^2+1)
, then the force is much sharper near the wall, and falls off faster/smoother.
Experiment with it and see what you like.
No comments:
Post a Comment