我正在做一个小项目,并且很难让转向算法在具有 Object3D 定位的 ThreeJS 中正常工作。我目前似乎遇到的主要问题是角速度相当不受控制,一切都旋转得太快了。我希望能够限制这一点。我提供了代码的简化示例。我想我快到了,我看了一些四元数和 Matrix4,但无法弄清楚。以前有人遇到过这个问题吗?谢谢!
class Movement extends THREE.Object3D
constructor: ->
super
@velocity = new THREE.Vector3()
@speed = 4
@ease = 7
@hault = 2
steer: (mode, target) ->
if @speed is 0
@velocity.set 0, 0, 0
return true
# velocity vector
switch mode
when 'arrive'
fromPosition = @position
toPosition = target
when 'flee'
fromPosition = target
toPosition = @position
@velocity.sub toPosition, fromPosition
# calculate magnitude
magnitude = @velocity.length()
# hault
if magnitude < @hault
return true
# limit angle of rotation
# angle = Math.acos fromPosition.dot(toPosition) / fromPosition.length() / toPosition.length()
# axis = new THREE.Vector3().cross fromPosition, toPosition .normalize()
# speed limit
if magnitude > @speed
@velocity.divideScalar magnitude / @speed
# easing
if magnitude < @ease and mode is 'arrive'
@velocity.multiplyScalar magnitude / (@ease + @hault / 2)
# adjust for time
@velocity.multiplyScalar GLOBAL.clock.getDelta()
# update position and rotation
@position.addSelf @velocity
@rotation.y = Math.atan2 -@velocity.z, @velocity.x
@rotation.z = Math.asin @velocity.y / @velocity.length()