-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhysicsEngine.java
More file actions
93 lines (79 loc) · 2.68 KB
/
PhysicsEngine.java
File metadata and controls
93 lines (79 loc) · 2.68 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
import java.awt.Color;
/**
* Abstract class for a general purpose physics simulation engine. This class
* handles running a physics simulation EXCEPT FOR computing the forces on
* all the {@link Body} objects. Sub-classes just need to implement the
* {@link #computeForces() computForces} method.
*
* @author Sean Stern
* @version 1.0
*/
public abstract class PhysicsEngine{
private double dt;
private Color bgColor;
protected double uniRad;
protected Body[] bodies;
/**
* Constructs a {@link PhysicsEngine}
*
* @param bodies an array of {@link Body} objects; this represents
* all the bodies in the universe
* @param timeDelta the quantum of time over which the engine applies
* forces; similar in concept to a frame rate
* @param universeRadius the radius of the universe being simulated; the
* radius is the minimum distance from the center to an edge of the
* universe
* @param bgRed determines the r-value of the background; should be 0-255
* @param bgBlue determines the b-value of the background; should be 0-255
* @param bgGreen determines the g-value of the background; should be 0-255
*/
public PhysicsEngine(Body[] bodies, double timeDelta,
double universeRadius,
int bgRed, int bgBlue, int bgGreen){
this.dt = timeDelta;
this.uniRad = universeRadius;
this.bodies = bodies;
this.bgColor = new Color(bgRed, bgBlue, bgGreen);
}
/**
* Runs the simluation.
*/
public void run(){
setUpDrawingCanvas();
while(true){
drawData();
computeForces();
computePositions();
}
}
/**
* Scale the drawing window and enable efficient animation.
*/
private void setUpDrawingCanvas(){
// TODO: Scale the drawing window so that x and y axes between
// −radius and +radius
// TODO: Enable double buffering for efficient animation
}
/**
* Draws the data as a 2D visualization for each time step of the
*/
private void drawData(){
// TODO: Clear the canvas
// TODO: Draw each body on the offscreen canvas
// TODO: Copy the offscreen canvs to the onscreen canvas
// TODO: Wait for a short amount of time
}
/**
* For each body, computes the new position of the body over a single
* quantum of time.
*/
private void computePositions(){
// TODO: Move each body
}
/**
* For each body, computes the sum of all the foces acting on that body.
* Different physics simulations use different algorithms to compute
* the sume of all the forces--some are more efficient than others.
*/
protected abstract void computeForces();
}