-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCircleCollider.pde
115 lines (90 loc) · 3.75 KB
/
CircleCollider.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
class CircleCollider extends Collider{
float radius;
CollisionInfo currentCollisionInfo = new CollisionInfo();
CircleCollider(GameObject _gameObject, float _radius, boolean _isSolid){
super(_gameObject, _isSolid);
colliderType = ColliderType.CIRCLE;
radius = _radius;
}
PVector[] getVertices(){
PVector[] vertices = new PVector[(int)Math.ceil(TWO_PI / 0.1f)];
int count = 0;
for (float a = 0; a < TWO_PI; a += 0.1) {
vertices[count++] = new PVector(cos(a) * radius, sin(a) * radius);
}
return vertices;
}
PVector getSize() { return new PVector(radius * 2, radius * 2); }
void resolveCollision(ICollidable other){
if (isSolid && physicsBody != null){
if (other.isSolid()){
if (other.getPhysicsBody() != null){
physicsBody.elasticCollision(other, GameEngine.getCollisionInfo(this, other));
}
else{
physicsBody.worldCollision(other, GameEngine.getCollisionInfo(this, other));
}
}
}
}
public void setSize(PVector _size){
radius = _size.x;
}
void manageCollision(ICollidable against){
PVector center = gameObject.transform.getPosition();
center.add(new PVector(radius, radius));
if (against.getColliderType() == ColliderType.SQUARE){
CollisionInfo collisionInfo = CollisionHelper.squareCircleCollision(this, against);
if (collisionInfo != null){
if (against.isSolid())
collidedAgainstSolid = true;
isHit = true;
against.setCollided(true);
currentCollisionInfo.collisionPoint = collisionInfo.collisionPoint;
currentCollisionInfo.surfaceNormal = collisionInfo.surfaceNormal;
GameEngine.addCollision(this, against, collisionInfo);
collidedWith.put(against, collisionInfo);
against.addCollidedWith(this, collisionInfo);
}
return;
} else if (against.getColliderType() == ColliderType.CIRCLE){
CollisionInfo collisionInfo = CollisionHelper.circleCircleCollision(this, against);
if (collisionInfo != null){
if (against.isSolid())
collidedAgainstSolid = true;
isHit = true;
against.setCollided(true);
currentCollisionInfo.collisionPoint = collisionInfo.collisionPoint;
currentCollisionInfo.surfaceNormal = collisionInfo.surfaceNormal;
GameEngine.addCollision(this, against, collisionInfo);
collidedWith.put(against, collisionInfo);
against.addCollidedWith(this, collisionInfo);
}
return;
} else if (against.getColliderType() == ColliderType.TRIANGLE){
PVector position = getPosition();
CollisionInfo collisionInfo = CollisionHelper.getPolyCircleIntersection(against, this);
if (collisionInfo != null){
if (against.isSolid())
collidedAgainstSolid = true;
isHit = true;
against.setCollided(true);
currentCollisionInfo.collisionPoint = collisionInfo.collisionPoint;
currentCollisionInfo.surfaceNormal = collisionInfo.surfaceNormal;
GameEngine.addCollision(this, against, collisionInfo);
collidedWith.put(against, collisionInfo);
against.addCollidedWith(this, collisionInfo);
}
return;
}
return;
}
void display(Camera camera){
PVector viewPos = PVector.add(gameObject.transform.getPosition(), new PVector(radius, radius));
viewPos.sub(camera.getPosition());
fill(255);
circle(viewPos.x, viewPos.y, radius * 2);
if (currentCollisionInfo != null && currentCollisionInfo.collisionPoint != null)
circle(PVector.sub(currentCollisionInfo.collisionPoint, camera.getPosition()).x, PVector.sub(currentCollisionInfo.collisionPoint, camera.getPosition()).y, radius);
}
}