-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhysicsBody.pde
176 lines (135 loc) · 4.65 KB
/
PhysicsBody.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
interface PhysicsObserver{
void update();
boolean isHit();
}
class PhysicsBody extends Component implements PhysicsObserver{
boolean isEnabled = true;
boolean isStable = false;
boolean isInAllowedPos = true;
private PVector velocity;
PVector frameVelocity = PVector.zero();
PVector allowedPos = PVector.zero();
float angularVelocity;
float angularAcceleration;
PVector acceleration;
Collider collider;
float topSpeed = 1000f;
float mass;
float surfaceArea;
boolean reboundOnScreenEdges = false;
boolean elasticCollided = false;
boolean applyGravity = true;
boolean hasCollided = false;
private List<CollisionInfo> frameCollisions = new ArrayList();
PhysicsBody (GameObject _gameObject) {
super(_gameObject, PhysicsBody.class);
GameEngine.addToPhysicsList(this);
this.velocity = new PVector (0, 0);
this.acceleration = new PVector (0, 0);
surfaceArea = 1;
this.mass = 1;
collider = gameObject.getComponent(Collider.class);
if (collider == null){
print("\nPhysicsBody does not possess a collider!\n");
}
else{
collider.addPhysicsBody(this);
}
}
void onDestroy(){}
boolean isHit(){ if (collider != null) return collider.hasCollided(); else return false; }
PVector getVelocity(){ return velocity.get(); }
void setVelocity(float x, float y){
velocity.x = x;
velocity.y = y;
}
void update () {
if (isEnabled){
if (elasticCollided){
velocity.add(frameVelocity.get());
velocity.div(collisionCount);
}
else{
frameVelocity.mult(0);
collisionCount = 0;
}
applyGravity();
velocity.add (acceleration);
PVector position = gameObject.transform.getPosition();
position.add (velocity);
gameObject.transform.setPosition(position);
acceleration.mult (0);
angularVelocity += angularAcceleration;
float angularDragCoeff = Helper.ensureRange(1 - (1 / ((mass * 100f / surfaceArea))), 0, 0.9975f);
angularVelocity = ((angularVelocity * 1000f) * angularDragCoeff) / 1000f;
if (angularVelocity < 0.001f)
angularVelocity = 0;
gameObject.transform.rotateOnAxis(angularVelocity);
angularAcceleration = 0.0;
velocity.x = Helper.ensureRange(velocity.x, -topSpeed, topSpeed);
velocity.y = Helper.ensureRange(velocity.y, -topSpeed, topSpeed);
if (!collider.collidedAgainstSolid)
elasticCollided = false;
}
frameCollisions.clear();
}
void applyGravity(){
PVector gravity = PVector.zero();
isStable = false;
if (applyGravity)
gravity = new PVector (0, GameEngine.gravityForce * mass);
if (collider.isSolid && collider.collidedAgainstSolid){
if (isInAllowedPos)
gameObject.transform.setPosition(allowedPos);
PVector speed = velocity.get();
float speedValue = speed.sqrMag();
if (speedValue > 0.1f){
boolean moving = PVector.sub(gameObject.transform.getPosition(), gameObject.transform.getPreviousPosition()).sqrMag() != 0;
if (!moving || !isInAllowedPos){
isInAllowedPos = false;
PVector push = PVector.sub(gameObject.transform.getPosition(), frameCollisions.get(0).collisionPoint);
push.normalize();
applyForce(push);
} else {
speed.mult(-1);
applyForce(speed);
}
}
else{
isStable = true;
}
}else if (!collider.collidedAgainstSolid){
applyForce(gravity);
allowedPos = gameObject.transform.getPosition();
isInAllowedPos = true;
}
}
void applyForce (PVector force) {
PVector f = force.get();
float _mass = mass;
if (_mass < 1f)
mass = 1f;
f.div (mass);
this.acceleration.add(f);
}
int collisionCount = 0;
void elasticCollision(ICollidable other, CollisionInfo collisionInfo){
PVector[] velocities = CollisionHelper.getElasticResponse(collider, other, collisionInfo);
other.setVelocity(velocities[1]);
if (velocities[0].sqrMag() > 1f){
elasticCollided = true;
}
frameVelocity.add(velocities[0]);
collisionCount++;
frameCollisions.add(collisionInfo);
}
void worldCollision(ICollidable other, CollisionInfo collisionInfo){
PVector _velocity = CollisionHelper.getWorldCollision(collider, other, collisionInfo);
if (_velocity.sqrMag() > 1f){
elasticCollided = true;
}
frameVelocity.add(_velocity);
collisionCount++;
frameCollisions.add(collisionInfo);
}
}