-
Notifications
You must be signed in to change notification settings - Fork 0
/
particle_string_circle_toxi.pde
104 lines (87 loc) · 1.8 KB
/
particle_string_circle_toxi.pde
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
import toxi.physics2d.constraints.*;
import toxi.physics2d.behaviors.*;
import toxi.physics2d.*;
import toxi.geom.*;
import java.util.List;
//Number of particles for string
int string_res=100;
//Number of particles for ball
int ball_res=60;
//Ball radius
int ball_radius=80;
// squared snap distance for mouse selection
float snap_dist=20*20;
VerletPhysics2D physics;
VerletParticle2D selectedparticles;
void setup()
{
size(1024,720,P3D);
initPhysics();
}
void draw()
{
// 1st update
physics.update();
// then drawing
background(224);
// draw all springs
stroke(255,0,255);
for(VerletSpring2D s : physics.springs)
{
line(s.a.x,s.a.y,s.b.x,s.b.y);
}
// show all particles
fill(0);
noStroke();
for(VerletParticle2D p : physics.particles)
{
ellipse(p.x,p.y,5,5);
}
// highlight selected particle (if there is one currently)
if(selectedparticles!=null)
{
fill(255,0,255);
ellipse(selectedparticles.x,selectedparticles.y,20,20);
}
}
void mousePressed()
{
// find particle under mouse
Vec2D mousepos=new Vec2D(mouseX,mouseY);
for(int i=1;i<physics.particles.size()-1;i++)
{
VerletParticle2D p=physics.particles.get(i);
// using distanceToSquared() is faster than distanceTo()
if(mousepos.distanceToSquared(p)<snap_dist)
{
// lock it and store for further reference
selectedparticles=p.lock();
// force quit the loop
break;
}
}
}
void mouseDragged()
{
if(selectedparticles!=null)
{
selectedparticles.set(mouseX,mouseY);
}
}
void mouseReleased()
{
// unlock the selected particle
if(selectedparticles!=null)
{
selectedparticles.unlock();
selectedparticles=null;
}
}
void keyPressed()
{
//Reset the sketch
if(key=='r')
{
initPhysics();
}
}