-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRigidBody.java
More file actions
154 lines (128 loc) · 3.98 KB
/
RigidBody.java
File metadata and controls
154 lines (128 loc) · 3.98 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Abstract class which represents a physics object that is affected by forces
*
* @author Austin
* @version 0
*/
public abstract class RigidBody extends Actor {
protected Position position = new Position();
protected Velocity velocity = new Velocity();
protected Acceleration acceleration = new Acceleration();
protected double mass;
protected Force force;
protected double dt = 0.1;
protected double time = 0;
protected boolean paused = true;
protected boolean forceVisible = false;
/**
* Constructor for RigidBody
*
* @param x The x-position of the object (-left and +right) in m
* @param y The y-position of the object (+up and -down) in m
* @param mass The mass of the object in kg
*/
public RigidBody(double x, double y, double mass) {
this.mass = mass;
this.position = new Position(x, y);
}
/**
* Default behaviour for any physics object
*/
public void act() {
// If not paused, update the position and time
if (!paused) {
updatePosition();
time += dt;
}
setLocation((int) position.getX(), getWorld().getHeight() - (int) position.getY());
createImage();
// Draw free body diagram if required
if (forceVisible) {
drawForce();
}
}
/**
* All children must create their image
*/
protected abstract void createImage();
/**
* Updates the position of the object with the forces acting on it
*/
protected void updatePosition() {
acceleration = force.getAcceleration(mass);
velocity.updateWithAcceleration(acceleration, dt);
position.updateWithVelocity(velocity, dt);
}
/**
* Adds the object to the world (reversing y values to be +down)
*
* @param world The world to add the object to
*/
public void addToWorld(World world) {
world.addObject(this, (int) position.getX(), world.getHeight() - (int) position.getY());
}
/**
* Draws a line in relation to the object
*
* @param length The length of the line in pixels
* @param angle The angle at which the line extends in degrees (0 to right)
* @param colour The colour of the line
*/
public void drawLine(double length, double angle, Color colour) {
angle = Math.toRadians(angle);
int x1 = getX();
int y1 = getY();
int x2 = x1 + (int) (length * Math.cos(angle));
int y2 = y1 - (int) (length * Math.sin(angle));
GreenfootImage image = getWorld().getBackground();
image.setColor(colour);
image.drawLine(x1, y1, x2, y2);
getWorld().setBackground(image);
}
/**
* Draws the nett force acting on the object.
* Can be overriden to show individual forces.
*/
public void drawForce() {
double angle = force.getAngle();
double length = force.getMagnitude() * 5;
drawLine(length, Math.toDegrees(angle), Color.RED);
}
/**
* Shows the forces in a free body diagram
*/
public void showForces() {
forceVisible = true;
}
/**
* Stops showing the forces
*/
public void hideForces() {
forceVisible = false;
}
/**
* Stop updating the position
*/
public void pause() { paused = true; }
/**
* Resume updating the position
*/
public void resume() { paused = false ; }
// Getters
public double getTime() {
return time;
}
public double getMass() {
return mass;
}
public Position getPosition() {
return position;
}
public Velocity getVelocity() {
return velocity;
}
public Acceleration getAcceleration() {
return acceleration;
}
}