javascript - particle brownian motion with directions -
i'm trying use brownian motion create group of random moving particles. http://jsfiddle.net/j75em/16/
so far i've got particles moving randomly i'm not sure how set forward direction make more natural.
i've tried use change in x , y axis calculate rotation using atan, can see uncommenting rotate doesn't seem perform well.
is right approach type of movement? thanks;
this pretty neat!
you sort of going right way should use atan2 function. removes need 0 checks.
the atan2 function gives angle anticlockwise positive x vector
(1, 0) --->
the bees 90 degrees off starting angle must subtract 90 degrees direction. (depending on way round dy , dx calculation, might need add)
you find direction changes rapidly, consider limiting next change set of changes cause angle change below threshold. make movement little smoother.
i go generating angle between -pi/8 , pi/8 radians, , random length. using polar coordinates. add new random polar offset x , y position
newx = currentx + (randomlength * cos(randomangle + currentangle)) , newy = currenty + (randomlength * sin(randomangle + currentangle))
if work angles can more natural effects if want bees stay within area, can force bias towards center of area closer , closer edge.
update:
so i've taken closer look. trouble expect .rotate set rotation when adds rotation
there 2 options fixing this.
rotate difference between previous , current angle
set rotation using .transform method
you can see solution 2 in action here http://jsfiddle.net/c5a2a/
Comments
Post a Comment