-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulator.java
More file actions
92 lines (76 loc) · 1.59 KB
/
Simulator.java
File metadata and controls
92 lines (76 loc) · 1.59 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
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class Simulator
{
private List<Sphere> sphereList;
private List<Spring> springList;
private double x, y;
private Grid grid;
private Vector gravity;
public Simulator(List<Sphere> sphereList, List<Spring> springList, int x, int y)
{
this.sphereList = sphereList;
this.springList = springList;
this.x = x;
this.y = y;
grid = new Grid(x, y, 10);
gravity = new Vector(0, 10);
}
public void step(double dt, double restitution)
{
ListIterator<Spring> iter = springList.listIterator();
while(iter.hasNext())
{
Spring current = iter.next();
if(current.remove)
{
iter.remove();
}
}
for(Spring spr : springList)
{
spr.applySpringForce();
}
for(Sphere s : sphereList)
{
if(s.mass <= 0.0)
{
continue;//fixed object
}
Vector.add(s.force, s.force, gravity);
Vector.mul_num(s.force, s.force, dt);
Vector.add(s.vel, s.vel, s.force);
Vector.muladd_num(s.pos, s.vel, dt);
s.force.reset();
}
//for(int i = 0; i < 10; i++)
//{
// for(Spring spr : springList)
// {
// spr.iterateToConstraints();
// }
//}
handleCollisions(restitution);
}
private void handleCollisions(double restitution)
{
grid.fill(sphereList);
for(Sphere a : sphereList)
{
List<ArrayList<Sphere>> list = a.list;
for(ArrayList<Sphere> cellList : list)
{
for(Sphere b : cellList)
{
if(a != b)
{
Physics.collide(a, b, restitution);
}
}
}
Physics.collideWithWall(a, restitution, x, y);
grid.clear(a);
}
}
}