-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObject.pde
65 lines (57 loc) · 1.42 KB
/
Object.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
class Object{
Particle[] part ;
Spring[] spring ;
Object ( Particle[] p, Spring[] s){
part = p ;
spring = s ;
}
Object ( int i, int j){
part = new Particle[i] ;
spring = new Spring[j] ;
}
void add(Particle p){
for ( int i = 0 ; i<part.length ; i++){
if ( part[i] == null){
part[i] = p ;
break ;
}
}
}
void add(Spring s){
for ( int i = 0 ; i<spring.length ; i++){
if ( spring[i] == null){
spring[i] = s ;
break ;
}
}
}
void show(){
for ( Spring s : spring ){ s.show();}
for ( Particle p : part ){ p.show();}
}
void update(){
collision();
for ( Particle p : part ){ p.update();}
for ( Spring s : spring ){ s.update();}
}
void apply_force( PVector f){
for ( Particle p : part ){ p.acc.add(f);}
}
void collision(){
// calculate collision between particles
for ( int i = 0 ; i < part.length ; i++){
for ( int j = i+1 ; j < part.length ; j++){
if ( part[i].pos.x - part[j].pos.x < 20 && part[i].pos.x - part[j].pos.x > -20
&& part[i].pos.y - part[j].pos.y < 20 && part[i].pos.y - part[j].pos.y > -20
){
PVector v = part[j].pos.copy().sub(part[i].pos);
if ( v.mag()<20){
v.setMag((20-v.mag())/2);
part[i].pos.sub(v);
part[j].pos.add(v);
}
}
}
}
}
}