-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFluidBody.pde
56 lines (45 loc) · 1.61 KB
/
FluidBody.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
class FluidBody extends Component implements PhysicsObserver{
private float xSize;
private float ySize;
Collider collider;
float density;
float frictionCoeff;
Map<Camera, Integer> cameras = new HashMap();//Camera, Layer
boolean isCulled;
FluidBody(GameObject _gameObject, float _density, float _frictionCoeff){
super(_gameObject, FluidBody.class);
GameEngine.addToPhysicsList(this);
xSize = gameObject.transform.size.x;
ySize = gameObject.transform.size.y;
density = _density;
frictionCoeff = _frictionCoeff;
collider = gameObject.getComponent(Collider.class);
if (collider == null){
print("\nFluidBody does not possess a collider!\n");
}
}
boolean isHit(){ if (collider != null) return collider.hasCollided(); else return false; }
void update(){
HashMap<ICollidable, CollisionInfo> collidedWith = collider.getCollidedWith();
if (collidedWith.size() > 0){
for (Map.Entry<ICollidable, CollisionInfo> entry : collidedWith.entrySet()) {
ICollidable other = entry.getKey();
if (other.getPhysicsBody() != null){
other.getPhysicsBody().applyForce(getDragForce(other.getPhysicsBody()));
}
}
}
}
PVector getDragForce(PhysicsBody mover){
float velocityMagnitude = mover.velocity.mag();
if (mover.collider == null || !mover.collider.collidedAgainstSolid){
float dragMagnitude = this.density * this.frictionCoeff * velocityMagnitude;
PVector drag = mover.velocity.get();
drag.normalize();
drag.mult(-mover.surfaceArea);
drag.mult(dragMagnitude);
return drag;
}
return PVector.zero();
}
}