最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - particle brownian motion with directions - Stack Overflow

programmeradmin2浏览0评论

I'm trying to use brownian motion to create a group of random moving particles. /

So far I've got the particles moving randomly but I'm not sure how to set the forward direction to make it look more natural.

I've tried to use the change in x and y axis to calculate rotation using atan, you can see this by unmenting rotate but this doesn't seem to perform well.

Is this the right approach for this type of movement? thanks;

I'm trying to use brownian motion to create a group of random moving particles. http://jsfiddle/J75Em/16/

So far I've got the particles moving randomly but I'm not sure how to set the forward direction to make it look more natural.

I've tried to use the change in x and y axis to calculate rotation using atan, you can see this by unmenting rotate but this doesn't seem to perform well.

Is this the right approach for this type of movement? thanks;

Share Improve this question edited Mar 7, 2012 at 16:17 j08691 208k32 gold badges269 silver badges280 bronze badges asked Mar 7, 2012 at 16:13 michaelmichael 1,2025 gold badges23 silver badges35 bronze badges 2
  • If you're actually using bees I'd have just one set to be really erratic and have a swarm follow the leader. – Incognito Commented Mar 7, 2012 at 16:25
  • you can manipulate the odds a little so it is not entirely random. Like 85% random and 15% the last direction or something like that. I think this will make it go a little bit more to the forward direction – ajax333221 Commented Mar 7, 2012 at 16:27
Add a ment  | 

1 Answer 1

Reset to default 8

This is pretty neat!

You are sort of going about it the right way but you should actually use the atan2 function. This removes the need for any 0 checks.

The atan2 function gives you an angle which is anticlockwise from the positive x vector

(1, 0) --->

The bees are 90 degrees off from this starting angle so you must subtract 90 degrees from the direction. (depending on which way round you do the dy and dx calculation, you might need to add)

You could find that the direction changes rapidly, so you could consider limiting the next change to a set of changes that cause an angle change below some threshold. This will make the movement a little smoother.

I would actually go about it by generating an angle between say -pi/8 and pi/8 radians, and a random length. Essentially using polar coordinates. Then add this new random polar offset to the x and y position like

newX = currentX + (randomLength * cos(randomAngle + currentAngle)) and 
newY = currentY + (randomLength * sin(randomAngle + currentAngle))

If you work with angles you can also get more natural effects like if you want the bees to stay within a certain area, you can force a bias towards the center of the area as they get closer and closer to the edge.

Update:

So I've taken a closer look. The trouble is that you expect .rotate to set the rotation when it actually adds to the rotation

There are 2 options for fixing this.

  1. Rotate by the difference between the previous and the current angle

  2. Set the rotation using the .transform method

You can see solution 2 in action here http://jsfiddle/c5A2A/

发布评论

评论列表(0)

  1. 暂无评论