-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathkinect_force.pde
More file actions
132 lines (108 loc) · 3.6 KB
/
kinect_force.pde
File metadata and controls
132 lines (108 loc) · 3.6 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
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
import com.cketcham.osceleton.*;
import toxi.geom.*;
import toxi.physics2d.*;
import toxi.physics2d.behaviors.*;
import controlP5.*;
int NUM_PARTICLES = 40;
int NUM_PEOPLE = 32;
float DRAG = 0.05f;
float ATTRACTION = 0.8f;
float RADIUS = 350;
ControlP5 cp5;
VerletPhysics2D physics;
AttractionBehavior[] attractors = new AttractionBehavior[NUM_PEOPLE * 2];
Vec2D[] hands = new Vec2D[NUM_PEOPLE * 2];
OSCeletonWrapper osceleton;
void setup() {
size(800, 560, P3D);
cp5 = new ControlP5(this);
cp5.addSlider("DRAG")
.setPosition(10, 500)
.setRange(0, 0.5f);
cp5.addSlider("ATTRACTION")
.setPosition(10, 520)
.setRange(0.0f, 2.0f);
cp5.addSlider("RADIUS")
.setPosition(10, 540)
.setRange(0.0f, 350.0f);
osceleton = new OSCeletonWrapper(this, 12345);
physics = new VerletPhysics2D();
physics.setDrag(DRAG);
physics.setWorldBounds(new Rect(0, 0, width, height));
}
float oldAttraction = 0.0f;
float oldDrag = 0.0f;
float oldRadius = 0.0f;
// Sets up attraction to hands
void setupHandGravity(Skeleton sk) {
// If a parameter changed, we need to remove the old settings
// so we can set new ones.
if (oldAttraction != ATTRACTION || oldRadius != RADIUS) {
oldAttraction = ATTRACTION;
oldRadius = RADIUS;
removeHandGravity(sk);
}
// If attraction hasn't been setup yet, initialize it
if (attractors[sk.getUser()] == null) {
hands[sk.getUser()] = new Vec2D(sk.get("l_hand").x, sk.get("l_hand").y);
hands[sk.getUser() + NUM_PEOPLE] = new Vec2D(sk.get("r_hand").x, sk.get("r_hand").y);
attractors[sk.getUser()] = new AttractionBehavior(hands[sk.getUser()], RADIUS, ATTRACTION);
attractors[sk.getUser() + NUM_PEOPLE] = new AttractionBehavior(hands[sk.getUser() + NUM_PEOPLE], RADIUS, ATTRACTION);
physics.addBehavior(attractors[sk.getUser()]);
physics.addBehavior(attractors[sk.getUser() + NUM_PEOPLE]);
}
// Update the attraction coordinates for the particles
else {
hands[sk.getUser()].set(sk.get("l_hand").x, sk.get("l_hand").y);
hands[sk.getUser() + NUM_PEOPLE].set(sk.get("r_hand").x, sk.get("r_hand").y);
}
}
// Removes attraction to hands
void removeHandGravity(Skeleton sk) {
if (attractors[sk.getUser()] != null) {
physics.removeBehavior(attractors[sk.getUser()]);
physics.removeBehavior(attractors[sk.getUser() + NUM_PEOPLE]);
attractors[sk.getUser()] = null;
attractors[sk.getUser() + NUM_PEOPLE] = null;
}
}
void addParticle() {
VerletParticle2D p = new VerletParticle2D(Vec2D.randomVector().scale(5).addSelf(width / 2, 0));
physics.addParticle(p);
// add a negative attraction force field around the new particle
physics.addBehavior(new AttractionBehavior(p, 20, -1.2f, 0.01f));
}
void draw() {
background(192);
// First draw all the particles
noStroke();
physics.setDrag(DRAG);
if (physics.particles.size() < NUM_PARTICLES) {
addParticle();
}
physics.update();
for (VerletParticle2D p: physics.particles) {
fill(76, 150, 130 + random(40, 80), random(100, 120));
ellipse(p.x, p.y, random(22, 28), random(22, 28));
}
fill(255);
for (Skeleton sk : osceleton.getSkeletons()) {
if (sk != null) {
if (sk.isVisible()) {
// Draw an elipse for each joint
HashMap < String, Joint > joints = sk.getJointSet();
for (String name: joints.keySet()) {
Joint j = joints.get(name);
fill(255);
ellipse(j.x, j.y, 15, 15);
}
// Setup attraction to hands
setupHandGravity(sk);
}
else {
// The skeleton is no longer visible so remove the attraction
removeHandGravity(sk);
}
}
}
}