-
Notifications
You must be signed in to change notification settings - Fork 27
/
Composite.hx
56 lines (44 loc) · 1.5 KB
/
Composite.hx
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
package echo.util.verlet;
import echo.util.verlet.Constraints;
/**
* A Composite contains Dots and Constraints. It can be thought of as a "Body" in the Verlet simulation.
*/
class Composite {
public var dots:Array<Dot> = [];
public var constraints:Array<Constraint> = [];
public function new() {}
public function add_dot(?x:Float, ?y:Float) {
var dot = new Dot(x, y);
dots.push(dot);
return dot;
}
public inline function remove_dot(dot:Dot) {
return dots.remove(dot);
}
public function add_constraint(constraint:Constraint):Constraint {
constraints.push(constraint);
return constraint;
}
public inline function remove_constraint(constraint:Constraint):Bool {
return constraints.remove(constraint);
}
public inline function pin(index:Int) add_constraint(new PinConstraint(dots[index]));
public function bounds(?aabb:AABB):AABB {
var left = dots[0].x;
var top = dots[0].y;
var right = dots[0].x;
var bottom = dots[0].y;
for (i in 1...dots.length) {
if (dots[i].x < left) left = dots[i].x;
if (dots[i].y < top) top = dots[i].y;
if (dots[i].x > right) right = dots[i].x;
if (dots[i].y > bottom) bottom = dots[i].y;
}
return aabb == null ? AABB.get_from_min_max(left, top, right, bottom) : aabb.set_from_min_max(left, top, right, bottom);
}
public inline function clear() {
dots.resize(0);
constraints.resize(0);
}
public function toString() return 'Dot Group: {dots: $dots, constraints: $constraints}';
}