-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHitBody.pde
85 lines (66 loc) · 2.14 KB
/
HitBody.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
enum HitBodyType{None, Boundary, Box, Bird, Pig};
abstract class HitBody
{
HitBodyType m_type;
Body m_body;
float m_w; float m_h;
boolean m_enableCollision;
HitBody(HitBodyType type)
{
m_type = type;
m_enableCollision = false;
}
void onUpdate()
{
onDisplay();
}
abstract boolean done();
boolean onIntrospect()
{
boolean d = done();
if(d) killBody();
return d;
}
abstract void onDisplay();
protected abstract void makeBody(Vec2 center, float w, float h);
void killBody()
{
box2d.destroyBody(m_body);
}
// Serveral method to calculate magnitude of impulse in collision
float averageNormalImpulse(ContactImpulse impulse)
{
float total = 0.0;
for(int i = 0; i < impulse.normalImpulses.length; i++){
total += impulse.normalImpulses[i];
}
return total / impulse.normalImpulses.length;
}
float maximumNormalImpulse(ContactImpulse impulse)
{
float max = 0.0;
for(int i = 0; i < impulse.normalImpulses.length; i++){
if(max < impulse.normalImpulses[i]) max = impulse.normalImpulses[i];
}
return max;
}
void onImpulseCollision(ContactImpulse impulse) { /* Do nothing by default */ }
//void handleCollision(Contact c, Contact prev, Contact next)
//{
// if(prev == null) return;
// WorldManifold worldManifold = new WorldManifold(); c.getWorldManifold(worldManifold);
// Collision.PointState[] state1 = new Collision.PointState[2];
// Collision.PointState[] state2 = new Collision.PointState[2];
// Manifold oldManifold = prev.getManifold();
// Collision.getPointStates(state1, state2, oldManifold, c.getManifold());
// if(state2[0] != Collision.PointState.NULL_STATE && state2[0] != Collision.PointState.REMOVE_STATE){
// Body bodyA = c.getFixtureA().getBody();
// Body bodyB = c.getFixtureB().getBody();
// Vec2 point = worldManifold.points[0];
// Vec2 vA = bodyA.getLinearVelocityFromWorldPoint(point);
// Vec2 vB = bodyB.getLinearVelocityFromWorldPoint(point);
// float approachVel = abs(Vec2.dot(vB.sub(vA), worldManifold.normal));
// impulseCollision(approachVel);
// }
//}
}