





 
 




 
 


hi everyone,
don't know where to post this exactly.
want to do some crowd simulation externally and have this prob.
every dude has property X,Y, SPEED and DIRECTION.
direction ranging from 0..1
they are update like
x = x + speed * sin(direction * 2*pi)
y = y + speed * cos(direction * 2*pi)
now i want people be attracted by others and move in their direction
so i'd need something to get the direction vector back into the direction
variable
i calc the delta, say dx and dy, between the dude and the dude attracted to,
and normalized to 1
but i cannot figure the final step
newdir = ....
maybe something with arcsin/arccos?
it should be just the reverse of the equation above.
it tried newdir = (arcsin(dx)+arccos(dy))/(2*pi) and stuff like this but not
with very satisfactory results.
thanks for help in advance. i know there are some true mathematicians here
;)
Post a reply to this message


 
 




 
 


atan2() is the function you are looking for.

 Warp
Post a reply to this message


 
 




 
 


You'd probably need to use atan(dy/dx). however the usage of atan is a
little problematic since dx is not necessarily unequal zero and it's
difficult to get exact angles since e.g. atan(1) could be both 45° or 225°
(which is the opposite direction).
I recommend using one vector for the direction and speed (with the speed
being the length of the direction vector) and one for the location. So
instead of
x = x + speed * sin(direction * 2*pi)
y = y + speed * cos(direction * 2*pi)
you'd have
Position = Position+Direction ( with Direction = vnormalize(<dx,dy>)*Speed )
if you need to access the former x and y components use
x=Position.x
y=Position.y
Hope that helps!
Regards Roman
Post a reply to this message


 
 




 
 


> I recommend using one vector for the direction and speed
yeah, skipped to that, makes it much easier thanks
Post a reply to this message


 
 




 
 


Roman Reiner <lim### [at] gmxde> wrote:
> You'd probably need to use atan(dy/dx). however the usage of atan is a
> little problematic since dx is not necessarily unequal zero and it's
> difficult to get exact angles since e.g. atan(1) could be both 45° or 225°
> (which is the opposite direction).
That's exactly why atan2() exists.

 Warp
Post a reply to this message


 
 




 
 


> That's exactly why atan2() exists.
>
cool, didnt notice that func.
it's even in the delphi math lib
thanks alotnow i changed the code anyway to direction vectors but the
next time
Post a reply to this message


 
 




 
 


Warp <war### [at] tagpovrayorg> wrote:
> That's exactly why atan2() exists.
>  Warp
Didn't know that either. Thanks for the tip!
Regards Roman
Post a reply to this message


 
 




 