首页 > 解决方案 > 如何使用 Cannon.js 制作吸引点

问题描述

我正在使用 Three.js 和 Cannon.js。

另外,我正在尝试复制这个对象(多球那个)。基本上,球从其原点移动到压力点并停止,或围绕压力点旋转(这称为质量弹簧)。

目前我可以通过速度设置球的方向,但球不会停在压力点,距离越远,速度就越高(我需要恒定的速度)。

我在 Github 上问过这个问题,有人告诉我看看这个例子。唯一的问题是它只围绕我正在寻找如何将球移动到听起来不一样的某个点的行星旋转。任何帮助是极大的赞赏

标签: three.jscannon.js

解决方案


也许你需要一个像这样的物理模型:

friction model
m = 2 (it could be also m = 1 or m = 3 or whatever looks good)

coefficients:
cf = coefficient of friction
cs = coefficient of spring

Dynamics / update of position and velocity with time-increment dt

x_cursor = x coordinate of cursor's position
y_cursor = y coordinate of cursor's position

x  =  x  +  dt * v_x
y  =  y  +  dt * v_y
v_norm = sqrt(v_x^2 + v_y^2)^(m-1)
v_x  =  v_x  -  dt * cf * v_norm * v_x  -  dt * cs * ( x - x_cursor ) 
v_y  =  v_y  -  dt * cf * v_norm * v_y  -  dt * cs * ( y - y_cursor )

推荐阅读