VelocityNode.js 1.2 KB
Newer Older
S
SUNAG 已提交
1 2 3 4
/**
 * @author sunag / http://www.sunag.com.br/
 */

S
SUNAG 已提交
5
THREE.VelocityNode = function( target, params ) {
S
SUNAG 已提交
6

S
SUNAG 已提交
7
	THREE.Vector3Node.call( this );
S
SUNAG 已提交
8

S
SUNAG 已提交
9
	this.requestUpdate = true;
S
SUNAG 已提交
10

S
SUNAG 已提交
11
	this.target = target;
S
SUNAG 已提交
12

S
SUNAG 已提交
13 14 15
	this.position = this.target.position.clone();
	this.velocity = new THREE.Vector3();
	this.moment = new THREE.Vector3();
S
SUNAG 已提交
16

S
SUNAG 已提交
17
	this.params = params || {};
S
SUNAG 已提交
18

S
SUNAG 已提交
19 20
};

S
SUNAG 已提交
21 22
THREE.VelocityNode.prototype = Object.create( THREE.Vector3Node.prototype );
THREE.VelocityNode.prototype.constructor = THREE.VelocityNode;
S
SUNAG 已提交
23

S
sunag 已提交
24
THREE.VelocityNode.prototype.updateFrame = function( delta ) {
S
SUNAG 已提交
25

S
SUNAG 已提交
26 27
	this.velocity.subVectors( this.target.position, this.position );
	this.position.copy( this.target.position );
S
SUNAG 已提交
28 29 30

	switch ( this.params.type ) {

S
SUNAG 已提交
31
		case "elastic":
S
SUNAG 已提交
32

S
SUNAG 已提交
33
			delta *= this.params.fps || 60;
S
SUNAG 已提交
34

S
SUNAG 已提交
35 36
			var spring = Math.pow( this.params.spring, delta );
			var friction = Math.pow( this.params.friction, delta );
S
SUNAG 已提交
37

S
SUNAG 已提交
38 39 40 41 42 43 44 45 46 47 48
			// spring
			this.moment.x += this.velocity.x * spring;
			this.moment.y += this.velocity.y * spring;
			this.moment.z += this.velocity.z * spring;

			// friction
			this.moment.x *= friction;
			this.moment.y *= friction;
			this.moment.z *= friction;

			this.value.copy( this.moment );
S
SUNAG 已提交
49

S
SUNAG 已提交
50
			break;
S
SUNAG 已提交
51

S
SUNAG 已提交
52
		default:
S
SUNAG 已提交
53

S
SUNAG 已提交
54
			this.value.copy( this.velocity );
S
SUNAG 已提交
55

S
SUNAG 已提交
56 57
			break;
	}
S
SUNAG 已提交
58 59

};