/
PhysicsComponent.pde
209 lines (168 loc) · 4.41 KB
/
PhysicsComponent.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/////////////////////
// PhysicsComponent
/////////////////////
class PhysicsComponent extends Component {
// Purpose of this components is to take care of low-level physics things.
PVector gravity;
PVector position;
PVector velocity;
PVector acceleration;
PVector drag;
float maxXSpeed;
float mass;
boolean isDynamic;
boolean atRest;
boolean _isTouchingFloor;
// Properties
float groundY;
BoundingBoxComponent boundingBox;
boolean hasFriction;
PhysicsComponent() {
super();
componentName = "PhysicsComponent";
position = new PVector();
velocity = new PVector();
acceleration = new PVector();
drag = new PVector();
gravity = new PVector(0, -1500);
maxXSpeed = 1;
mass = 1;
isDynamic = true;
atRest = false;
_isTouchingFloor = false;
hasFriction = false;
groundY = TILE_SIZE;
}
void awake() {
super.awake();
// TODO: fix cast
boundingBox = (BoundingBoxComponent)gameObject.getComponent("BoundingBoxComponent");
if (boundingBox == null) {
println("Could not find boundingBox component");
}
landed();
}
boolean isTouchingFloor() {
return _isTouchingFloor;
}
void setTouchingFloor(boolean b) {
_isTouchingFloor = b;
}
void setGravity(float x, float y) {
gravity.x = x;
gravity.y = y;
}
void update(float dt) {
if (isTouchingFloor() == false) {
velocity.y += gravity.y * dt;
}
velocity.add(acceleration);
//velocity.x += acceleration.x * dt;
//velocity.y += acceleration.y * 1;
// max running speed
if (velocity.x > maxXSpeed) {
velocity.x = maxXSpeed;
}
else if (velocity.x < 0.001) {
//velocity.x = 0;
}
// TODO: fix
// Only apply drag if touching floor
if (hasFriction) {
if (isTouchingFloor()) {
drag.set(-velocity.x*0.11, 0); //applyForce(drag);
velocity.x += drag.x;
}
else {
//if(velocity.x < 40){
drag.set(-velocity.x*0.005, 0); //applyForce(drag);
velocity.x += drag.x;
//}
}
}
position.x += velocity.x * dt;
position.y += velocity.y * dt;
acceleration.mult(0);
// If we went past the floor after jumping
// place us at the floor level
// TODO: FIX. Don't call getComponent per update()
boundingBox = (BoundingBoxComponent)gameObject.getComponent("BoundingBoxComponent");
if(boundingBox != null){
//
if (isTouchingFloor() == false && position.y - boundingBox.h <= groundY) {
/*
position.y = groundY + boundingBox.h;
_isTouchingFloor = true;
velocity.y = 0;
MarioControllerComponent mario = (MarioControllerComponent)gameObject.getComponent("MarioControllerComponent");
mario._isJumping = false;
*/
}
else if (isTouchingFloor()) {
position.y = groundY + boundingBox.h;
}
}
gameObject.position.set(position.x, position.y);
}
void setGroundY(float y) {
groundY = y;
}
void landed(){
dprintln("landed");
position.y = groundY + boundingBox.h;
_isTouchingFloor = true;
velocity.y = 0;
}
void setVelocityY(float y){
velocity.y = y;
}
void setVelocityX(float x){
velocity.x = x;
}
void render() {
/* debug.addString("");
debug.addString("PhysicsComponent");
debug.addString("----------------------");
debug.addString("position: " + position.x + " , " + position.y);
debug.addString("velocity: " + velocity.x + " , " + velocity.y);
debug.addString("grounded: " + isTouchingFloor);
debug.addString("at rest: " + isAtRest());
debug.addString("----------------------");*/
}
//
void setMaxXSpeed(float m) {
maxXSpeed = m;
}
void applyForce(float x, float y) {
acceleration.x += x;
acceleration.y += y;
checkIfGrounded();
}
void checkIfGrounded() {
if (acceleration.y != 0) {
_isTouchingFloor = false;
}
}
void applyForce(PVector force) {
acceleration.add(force);
checkIfGrounded();
}
PVector getVelocity() {
return velocity;
}
void setVelocity(float x, float y) {
velocity.x = x;
velocity.y = y;
}
void stop(){
velocity.set(0,0);
acceleration.set(0,0);
}
boolean isAtRest() {
return velocity.x == 0 && position.y == groundY;
//return velocity.x == 0 && velocity.y == 0;
}
void setHasFriction(boolean b) {
hasFriction = b;
}
}