-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjects.js
140 lines (119 loc) · 4.55 KB
/
objects.js
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
var World = function (ctx, canv, min, max, dT) {
this.ctx = ctx;
this.canv = canv;
this.min = min || new Vector(-100,-100);//TODO introduce world coordinates everywhere
this.max = max || new Vector(100,100);
this.dT = dT || 1;
this.t = 0;
this.drag = 0.002;//around 0.001 is reasonable, higher when interactive
this.objects = [];//list of all objects, updated in world.update
this.forceField = function(pos){return new Vector(0,0.05);};//()=>{return new Vector(0,0);}
this.mouseForceFactor = 0.1;
this.canvRect = this.canv.getBoundingClientRect();//FIXME Must be updated on windowsize change
this.update = () => {
ctx.clearRect(0,0,this.canv.width,this.canv.height);
this.t += this.dT;
this.objects.map(o => o.update(this));
this.ctx.beginPath();
this.ctx.lineWidth = 2;
this.objects.map(o => o.draw(this));
this.ctx.stroke();
}
this.addObject = obj => {
obj.draw(this);
this.objects.push(obj);
return obj
}
this.toWorldVector = vec => {
var ret = new Vector();
ret.x = (vec.x / this.canvRect.width)*Math.abs(this.max.x - this.min.x)+this.min.x;
ret.y = (vec.y / this.canvRect.height)*Math.abs(this.max.y - this.min.y)+this.min.x;
return ret;
}
this.toCanvasCoordinates = vec => {
var ret = new Vector();
ret.x = ((vec.x-this.min.x) / Math.abs(this.max.x - this.min.x)) * this.canvRect.width;
ret.y = ((vec.y-this.min.x) / Math.abs(this.max.y - this.min.y)) * this.canvRect.height;
return ret;
}
this.normalizeMousePosition = (e, world) => {
return new Vector(e.offsetX, e.offsetY)
}
canv.onmousedown = e => {
var v = this.normalizeMousePosition(e, world)
this.objects.map(o => {if(o.mousedown) o.mousedown(v)})
};
canv.onmousemove = e => {
var v = this.normalizeMousePosition(e, world);
this.objects.map(o => {if(o.mousemove) o.mousemove(v)})
};
canv.onmouseup = e => {
var v = this.normalizeMousePosition(e, world)
this.objects.map(o => {if(o.mouseup) o.mouseup(v)})
};
canv.ontouchstart = e => {e.preventDefault(); canv.onmousedown(e);}
canv.ontouchmove = e => {e.preventDefault(); canv.onmousemove(e);}
canv.ontouchend = e => {e.preventDefault(); canv.onmouseup(e);}
}
var Dot = function (pos, weight, fixed) {
this.pos = pos || new Vector(0,0);
this.vel = new Vector(0,0);
this.force = new Vector(0,0);
this.fixed = fixed || false;
if(weight == 0) {console.warn("Weight == 0 results in divisions by zero!");}
this.weight = weight || 1;
this.radius = Math.sqrt(this.weight/Math.PI)*20;
this.selected = false;
this.update = world => {
if(!this.fixed) {
this.force.addTo(world.forceField(this.pos))
this.vel.addTo(this.force.multiply(world.dT*1/this.weight))
this.vel.multiplyBy(1-world.drag);//Todo should k*v^2.
this.pos.addTo(this.vel.multiply(world.dT));
}
this.force = new Vector(0, 0);
}
this.draw = world => {
//ctx.moveTo(this.pos.x, this.pos.y);
//ctx.arc(this.pos.x, this.pos.y, this.radius, 0, 2*Math.PI);
}
this.mousedown = V => {
if( Math.abs(V.x - this.pos.x) <= this.radius &&
Math.abs(V.y - this.pos.y) <= this.radius )
this.selected = true;
}
this.mousemove = V => {
//if(this.selected) this.pos = V;
if( this.selected ) {
this.force.addTo(V.subtract(this.pos).multiply(world.mouseForceFactor));
}
}
this.mouseup = V => {
this.selected = false;
}
}
var Spring = function (p1, p2, length) {
this.p1 = p1 || new Dot(Vector(0,0));
this.p2 = p2 || new Dot(Vector(0,1));
this.distance = length || this.p1.pos.subtract(this.p2.pos).getLength();
this.coefficient = 1.0;
this.maxForce = 20;
this.torn = false;
this.update = world => {
if(this.torn) return;
var dir = this.p2.pos.subtract(this.p1.pos);
var gap = dir.getLength();
dir.divideBy(gap);//normalize dir
var diff = gap-this.distance
var force = Math.abs(diff)*this.coefficient
dir.multiplyBy(force*Math.sign(diff))
this.p1.force.addTo(dir);
this.p2.force.addTo(dir.multiply(-1));
if(dir.getLength() > this.maxForce) this.torn = true;
};
this.draw = world => {
if(this.torn) return;
ctx.moveTo(this.p1.pos.x, this.p1.pos.y);
ctx.lineTo(this.p2.pos.x, this.p2.pos.y);
};
}