-
Notifications
You must be signed in to change notification settings - Fork 0
/
CircleCollider.java
75 lines (61 loc) · 2.26 KB
/
CircleCollider.java
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
import java.awt.*;
public class CircleCollider extends Collider
{
int radius;
// create a new circle collider
public CircleCollider(double x, double y, int radius)
{
this.x = x;
this.y = y;
this.radius = radius;
}
// draw the box collider on the screen
public void draw(Graphics g)
{
g.setColor(Color.BLUE);
g.drawOval(getIntX() - radius, getIntY() - radius, radius * 2, radius * 2);
g.fillOval(getIntX() - 2, getIntY() - 2, 4, 4);
}
// returns whether or not a point exists inside the collider bounds
public boolean intersect(double x, double y)
{
return PointMath.distance(this.x, this.y, x, y) <= radius;
}
// this means a node has collided with the collider, so now we have to rebound it,
// basically bouncing it off of the surface of this collider
@Override
public void rebound(Node node)
{
if(listener != null)
{
listener.onCollisionTriggered(this, node);
return;
}
// get the direction the node is coming from
double dirX = node.x - x;
double dirY = node.y - y;
// get the velocityu of the node
double velX = node.x - node.oldX;
double velY = node.y - node.oldY;
// get the magnitude of the velocity
double velocityMagnitude = PointMath.magnitude(velX, velY);
// get the magnitude of the direction (which in this case is just the distance to the node)
double magnitude = PointMath.magnitude(dirX, dirY);
// normalize the x and y of the distance to the node, which gives us a direction vector
double normalizedX = dirX / magnitude;
double normalizedY = dirY / magnitude;
// move the node to the edge of the collider based on the direction vector
double pushedX = normalizedX * radius;
double pushedY = normalizedY * radius;
// add force to the node by setting the old position further towards the center of
// the collider depending on the node's current velocity. this essentially inverts the
// velocity of the node (while taking some for collision dampening)
double pushedOldX = normalizedX * (radius - velocityMagnitude * Simulation.COLLISION_DAMP);
double pushedOldY = normalizedY * (radius - velocityMagnitude * Simulation.COLLISION_DAMP);
// apply the new positions
node.x = x + pushedX;
node.y = y + pushedY;
node.oldX = x + pushedOldX;
node.oldY = y + pushedOldY;
}
}