x/y 越近,x/y 速度越大
我正在使用画布在 JavaScript 中编写一个相当简单的脚本。它绘制一个中心节点,将所有周围的节点拉向它。这很好用,但是我需要每个节点相互排斥。
我将通过增加每个节点彼此远离的速度来做到这一点,因此最终它们应该趋于平稳并最终看起来像一朵花。它需要足够的力量来阻止它们互相碰撞或沉入中心节点而不飞向远处。
我只是不知道他们越接近我如何才能获得更高的数字。
因此,如果两个节点彼此相距 10px,则它们的 x 速度之一会增加 5。但如果它们彼此相距 1000 像素,那么它几乎不会增加其中一个节点的力。
有谁知道我可以用一个数学方程来解决这种问题,或者也许可以推动正确的方向?
TL;DR: 根据两个 x 值的接近程度,我需要增加一个节点的 x 速度,以便它们分开但最终趋于平稳。这只是我无法破解的数学问题,我已经完成了几乎所有的 JavaScript,包括速度的实现。
谢谢,抱歉有点啰嗦。
I am writing a fairly simple script in JavaScript using the canvas. It draws a central node which pulls all of the surrounding nodes towards it. This works great, however I need each node to repel each other.
I am going to do this by increasing each nodes velocity away from each other so eventually they should level out and end up looking something like a flower. It needs to be enough force to stop them from hitting each other or sinking into the center node without flying off into the distance.
I just can not work out how I can have a higher number the closer they get.
So if two nodes where 10px away from each other it would add 5 in force to one of their x velocities. But if they where 1000px away from each other then it would add almost nothing to the force of one of the nodes.
Does anyone know of a mathematical equation I can use to work this kind of thing out, or maybe a nudge in the right direction?
TL;DR: Depending on how close two x values are, I need to increment the x velocity of one node so they move apart but eventually level out. It is just the maths I can not crack, I have pretty much all of the JavaScript done, including the implementation of velocity.
Thanks, and sorry it is a bit wordy.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你只需要一个反比(或平方反比)关系:
或者:
你可以根据你想要的实际值来确定
k
,例如,在第一种情况下,如果你想要一个增量为5距离为 10,您可以设置k = 增量 * 距离 = 50
。You just need an inverse (or inverse square) relationship:
or:
You can determine
k
based on the actual values you want, for example, in the first case, if you wanted an increment of 5 for a distance of 10, you would setk = increment * distance = 50
.研究控制点电荷的方程,让速度基于每个“电荷”根据其接近度感受到的“力”。
Look into the equations governing electrical point charges, have the velocity be based on the "force" each "charge" would feel based on its proximity.