0

我正在做一个小项目,并且很难让转向算法在具有 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()
4

1 回答 1

1

尝试修改这个:

var qstart = new THREE.Quaternion();
    var qend = new THREE.Quaternion();
    var m  = new THREE.Matrix4();

    qstart.setFromRotationMatrix( MOVING_OBJECT.matrixWorld );
    qend.setFromRotationMatrix( m.lookAt( TARGET.position, MOVING_OBJECT.position, new THREE.Vector3( 0, 1, 0 ) ) );  

    MOVING_OBJECT.rotation.setEulerFromQuaternion( qstart.slerpSelf(qend, 0.015 ) ); 
于 2012-10-09T13:00:39.557 回答