-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPoint.pde
144 lines (116 loc) · 3.22 KB
/
Point.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
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
/** Point
* Tracks physical information as it relates to a "Point" in space.
* Forces applied to a Point cause it to move when updated.
*/
public class Point {
// Variables
// Default
private PVector DEF_POS = new PVector(0,0,0);
private PVector DEF_VEL = new PVector(0,0,0);
private PVector DEF_ACC = new PVector(0,0,0);
private PVector DEF_FORCE = new PVector(0,0,0);
private float DEF_MASS = 1;
private float MAX_MASS = 10000;
// Instance
PVector pos = DEF_POS;
PVector vel = DEF_VEL;
PVector acc = DEF_ACC;
PVector force = DEF_FORCE;
float mass = DEF_MASS;
// Physics Methods
/** Add Force
* Adds some force vector to the net force of this Point.
* @param PVector of force
*/
void add_force(PVector in_force) {
this.force.add(in_force);
}
/** Update
* Applies accumulated force onto this Point.
*/
void update() {
// A = F/m
acc.set(PVector.div(force,mass));
// V = dA
vel.add(acc);
// P = dV
pos.add(vel);
// Set net force to 0 for next frame
force.set(0,0,0);
}
// Technical Methods
/** Clone
* Creates a new Point with the same parameters as this Point.
* @returns The newly created Point.
*/
Point clone() {
return new Point(pos,vel,acc,force,mass);
}
/** Copy
* This Point copies another Point's parameters.
* @param Some Point object
*/
void copy(Point in_point) {
init(in_point.pos,in_point.vel,in_point.acc,in_point.force,in_point.mass);
}
// Construction
// Default Constructor
Point() {
init(null,null,null,null,-1);
}
// Shorthand Constructors
Point(PVector in_pos) {
init(in_pos,null,null,null,-1);
}
Point(PVector in_pos,float in_mass) {
init(in_pos,null,null,null,in_mass);
}
// Full Constructor
Point(PVector in_pos,PVector in_vel,PVector in_acc,PVector in_force,float in_mass) {
init(in_pos,in_vel,in_acc,in_force,in_mass);
}
// Init Method
private void init(PVector in_pos,PVector in_vel,PVector in_acc,PVector in_force,float in_mass) {
set_pos(in_pos);
set_vel(in_vel);
set_acc(in_acc);
set_force(in_force);
set_mass(in_mass);
}
// Getters/Setters
// Position
PVector get_pos() {
return this.pos;
}
void set_pos(PVector in_pos) {
this.pos = (in_pos==null) ? this.pos : in_pos;
}
// Velocity
PVector get_vel() {
return this.vel;
}
void set_vel(PVector in_vel) {
this.vel = (in_vel==null) ? this.vel : in_vel;
}
// Acceleration
PVector get_acc() {
return this.acc;
}
void set_acc(PVector in_acc) {
this.acc = (in_acc==null) ? this.acc : in_acc;
}
// Force
PVector get_force() {
return this.force;
}
void set_force(PVector in_force) {
this.force = (in_force == null) ? this.force : in_force;
}
// Mass
float get_mass() {
return this.mass;
}
void set_mass(float in_mass) {
this.mass = (in_mass <= 0 || in_mass > MAX_MASS) ? this.mass : in_mass;
}
}