Home
38 圆与线碰撞检测
Learning Processing
38 圆与线碰撞检测
姜睿
姜睿
September 25, 2022
1 min

代码实现

1
2Point closest;
3Line line;
4Circle mouse;
5
6void setup() {
7// create canvas
8size(600, 600);
9// instantiate
10closest = new Point(0, 0);
11line = new Line(random(0, width), random(0, height), 
12random(0, width), random(0, height));
13mouse = new Circle(0, 0, 30);
14// drawing settings
15strokeWeight(5);
16}
17
18void draw() {
19background(255);
20
21// draw circle according to mouse
22mouse.updatePos(mouseX, mouseY);
23
24// check for collision
25boolean hit = checkCollsion(line, mouse);
26if (hit) stroke(255, 150, 0, 150);
27else stroke(6, 123, 218, 150);
28line.display();
29
30// draw the circle
31mouse.display(color(6, 123, 218, 150));
32}
33
34// check point with circle
35boolean checkCollsion(Point p, Circle c) {
36float distance = dist(p.x, p.y, c.center.x, c.center.y);
37return distance <= c.r;
38}
39
40// check line with circle
41boolean checkCollsion(Line l, Circle c) {
42// check ends
43boolean end1 = checkCollsion(l.start, c);
44boolean end2 = checkCollsion(l.end, c);
45if (end1 || end2) return true;
46
47// get dot product of the line and circle
48float dot = ( ((c.center.x - l.start.x) * (l.end.x - l.start.x)) 
49+ ((c.center.y - l.start.y) * (l.end.y - l.start.y)) ) / (l.length() * l.length());
50
51// find the closest point on the line
52closest.updatePos(l.start.x + (dot * l.x), 
53l.start.y + (dot * l.y));
54
55// is this point actually on the line segment?
56boolean onSegment = checkCollsion(l, closest);
57if (!onSegment) return false;
58
59// Test Only!
60// Projection Point
61fill(239, 81, 26);
62ellipse(closest.x, closest.y, 10, 10);
63
64// get distance to closest point
65float distance = dist(c.center.x, c.center.y, closest.x, closest.y);
66return distance <= c.r;
67}
68
69// check line with point
70boolean checkCollsion(Line l, Point p) {
71// get distance from the point to the two ends of the line
72float d1 = dist(p.x, p.y, l.start.x, l.start.y);
73float d2 = dist(p.x, p.y, l.end.x, l.end.y);
74
75// since floats are so minutely accurate, add
76// a little buffer zone that will give collision
77float buffer = 0.1;// higher # = less accurate
78
79// if the two distances are equal to the line's
80// length, the point is on the line!
81// note we use the buffer here to give a range,
82// rather than one #
83return d1 + d2 >= l.length() - buffer 
84&& d1 + d2 <= l.length() + buffer;
85}
86
1
2class Point {
3float x, y = 0;
4Point(float x, float y) {
5updatePos(x, y);
6}
7void updatePos(float x, float y) {
8this.x = x;
9this.y = y;
10}
11}
12
1
2class Line {
3// fields
4Point start, end = null;
5float x, y = 0;
6// constructor
7Line(float x1, float y1, float x2, float y2) {
8// start & end points
9start = new Point(x1, y1);
10end = new Point(x2, y2);
11// horizontal & vertical dist
12x = x2 - x1;
13y = y2 - y1;
14}
15// draw the line
16void display() {
17line(start.x, start.y, end.x, end.y);
18}
19// cal the length
20float length() {
21return dist(start.x, start.y, end.x, end.y);
22}
23}
24
1
2class Circle {
3// fields
4Point center = null;
5float r = 1, d = 2;
6// constructor
7Circle(float x, float y, float r) {
8center = new Point(x, y);
9this.r = r;
10this.d = this.r * 2;
11}
12// display the circle
13void display(color c) {
14fill(c);
15noStroke();
16ellipse(center.x, center.y, d, d);
17}
18// default display the circle
19void display() {
20display(color(255, 0, 0));
21}
22// update circle position
23void updatePos(float x, float y) {
24center.updatePos(x, y);
25}
26}
27

Tags

#game develop
姜睿

姜睿

学生

游戏设计学生

Expertise

游戏开发
平面设计

Related Posts

42 快速排序
42 快速排序
October 01, 2022
1 min

Legal Stuff

Privacy NoticeCookie PolicyTerms Of Use