Hello and thanks in advance for your help
Here is what i have:
- A 2D Plan where multiple objects are moving randomly.
- Each object has a position(x,y), and an integer direction (0 to 360).
Here is what i want to achieve:
- When 2 objects become close (let say distance < 20 pixel ), i want to make their direction to change so they become more far.
- When they become little bit far ( distance 40 to 60 pixel ), i want to change their direction so they become more close.
- Between 20 and 40 they follow each other ( but this part i did it easily )
- if distance > 60 px they just ignore each other (this part is already ok as well )
I experience some difficulties for point 1 and 2:
- To compare both angles ( it's ok in simple cases but if one object angle is 359 and the other is 2, i can't find a good way to calculate that their actual direction is close, althought 359 - 2 = 357.
- To apply a small variation to both objects so they look to influencate each other. Just to notice: if both object's direction are extreamly different they can ignore each other.
for instance, currently i am doing that when objects are too close ( distance < 20 ): self is the current object and b is another object of same type.
self.SetDirection( self.direction - ( b.direction - self.direction ));
And when objects are between 20-60px
self.SetDirection( direction + ( b.direction - self.direction ));
the setdirection method (i know it's ugly, and probably wrong ):
function TBoid.toAngle360(angle: double): double;
begin
if angle < 0
then angle := 360 - ( abs(round(angle)) mod 360 )
else if angle > 360
then angle := round(angle) mod 360;
result := angle;
end;
I would be very thankfull if anyone could help me, give some any explanation or point at my mistakes. I could not find any suitable answer on this site or in google, and i'm weak with trigonometric calculations, angles, radians and all that...
Thanks