Hello and thanks in advance for your help
Here is what i have:
Here is what i want to achieve:
I experience some difficulties for point 1 and 2:
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
More reliable approach is to minimize using angles.
Instead of angle use velocity components vx
and vy
(note that vx=v*cos(direction)
, similar for vy
and sin
).
To compare direction similarity, calculate dot product of direction vectors (normalized by their lengths)
dot = (vx1 * vx2 + vy1 * vy2) / (len1*len2)
where
len1 = Hypot(vx1,vy1)
Dot product has value 1 for collinear vectors, close to 1 for close directions, is 0 for perpendicular vectors, is 1 for anticollinear vectors (note this is equivalent of calculation of angle difference cosine).
To make mutual influence, apply something like gravity or Coulomb force - object makes a force (hence acceleration) onto another object. So velocity components change.
For example, you have almost collinear vectors with components vx, vy
. Find normal vector N
nx = -vy
ny = vx
and add N*coeff
to one velocity and -N*coeff
to another velocity to make them divergent or convergent (depends on the sign of coeff
). Value of coeff
calculate correspondingly to distance between objects.