-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSoftFloat.js
More file actions
86 lines (64 loc) · 1.59 KB
/
SoftFloat.js
File metadata and controls
86 lines (64 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
'use strict';
function SoftFloat() {
var ATTRACTION = 0.2;
var DAMPING = 0.5;
this.value = 0;
this.velocity = 0;
this.acceleration = 0;
this.damping = 0;
this.attraction = 0;
this.targeting = false;
this.target = 0;
if (arguments.length === 0) {
this.value = 0;
this.damping = DAMPING;
this.attraction = ATTRACTION;
} else if (arguments.length === 1) {
this.value = arguments[0];
this.damping = DAMPING;
this.attraction = ATTRACTION;
} else if (arguments.length === 3) {
this.value = arguments[0];
this.damping = arguments[1];
this.attraction = arguments[2];
}
this.set = function(v) {
this.value = v;
this.target = v;
this.targeting = false;
}
this.pin = function() {
if (arguments.length == 1) {
this.target = arguments[0];
}
this.value = this.target;
this.targeting = false;
}
this.get = function() {
return this.value;
}
this.getInt = function() {
return Math.floor(this.value);
}
this.update = function() {
if (this.targeting) {
this.acceleration += this.attraction * (this.target - this.value);
this.velocity = (this.velocity + this.acceleration) * this.damping;
this.value += this.velocity;
this.acceleration = 0;
if (Math.abs(this.velocity) > 0.00001) {
return true;
}
this.value = this.target;
this.targeting = false;
}
return false;
}
this.setTarget = function(t) {
this.targeting = true;
this.target = t;
}
this.getTarget = function() {
return this.target;
}
}