forked from rippeddeadlift/Kollisiondetektion_SSDS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCBalls1193451911828909653.autosave
133 lines (102 loc) · 3.41 KB
/
CBalls1193451911828909653.autosave
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
* Container class for a number (totalball) of Ball objects
*/
public class CBalls {
boolean mousedown = false;
Ball[] ball;
CBalls(PApplet pApp, int totalball) {
minim = new Minim(pApp);
ball = new Ball[totalball];
for (int bn=0; bn < ball.length; bn++)
ball[bn] = new Ball(minim, bn);
}
void draw()
{
// draw the balls
for (int bn=0; bn < ball.length; bn++)
ball[bn].draw();
}
void game_physics() {
for (int bn = 0; bn < ball.length; bn++) {
ball[bn].game_physics();
}
}
void detectCollisions() {
for (int i = 0; i < ball.length; i++) {
for (int j = i + 1; j < ball.length; j++) {
Ball b1 = ball[i];
Ball b2 = ball[j];
// Distanz zwischen den Bällen
// die Differenz der x & y -Koordinaten
// Formel um Distanz zu berechnen: sqrt((b2.sx-b1.sx)^2 + (b2.sy-b1.sy)^2)
float dx = (float)(b1.sx - b2.sx);
float dy = (float)(b1.sy - b2.sy);
float distance = (float)Math.sqrt(dx * dx + dy * dy);
if (distance < b1.Radius() + b2.Radius()) {
// Simple elastic collision response
float overlap =0.5f * ((b1.Radius() + b2.Radius() - distance ));
// Adjust positions to separate balls
if (overlap >0){
println("Overlap detected", overlap); // Prints "Overlap detected" with a newline
b1.sx -= overlap * dx / distance;
b1.sy -= overlap * dy / distance;
b2.sx += overlap * dx / distance;
b2.sy += overlap * dy / distance;
}
// double overlap = (ball1.radius + ball2.radius) - distance;
// if (overlap > 0) {
// // Korrektur der Positionen proportional zur Masse
// double correctionFactor = overlap / 2;
// ball1.sx -= nx * correctionFactor;
// ball1.sy -= ny * correctionFactor;
// ball2.sx += nx * correctionFactor;
// ball2.sy += ny * correctionFactor;
// }
// Einheitsvektor, zeigt die Richtung des Vektors an
float nx = dx / distance;
float ny = dy / distance;
// Relative Geschwindigkeit
float dvx = (float)(b1.vx - b2.vx);
float dvy = (float)(b1.vy - b2.vy);
// Skalarprodukt zweier Vektoren
float skalarprodukt = dvx * nx + dvy * ny;
if (skalarprodukt > 0) continue;
// Calculate the impulse scalar
float impulse = 2 * skalarprodukt / (float)(b1.MASS + b2.MASS);
// Apply the impulse to each ball's velocity
b1.vx -= impulse * b2.MASS * nx;
b1.vy -= impulse * b2.MASS * ny;
b2.vx += impulse * b1.MASS * nx;
b2.vy += impulse * b1.MASS * ny;
// b1.ballColor = color(random(255),random(255),random(255));
// b2.ballColor = color(random(255),random(255),random(255));
}
}
}
}
void stop()
{
// make sure to close all AudioPlayer objects
for (int bn=0; bn < ball.length; bn++) {
ball[bn].kick.close();
ball[bn].snare.close();
}
minim.stop();
}
/* Clicked mouse */
void Mouse ()
{
if (mouseButton == LEFT) System.out.printf("State: MOUSE_DOWN\n");
for (int bn=0; bn < ball.length; bn++) {
ball[bn].Mouse();
}
}
/* Released mouse */
void MouseUp ()
{
System.out.printf("State: MOUSE_RELEASED\n");
for (int bn=0; bn < ball.length; bn++) {
ball[bn].MouseUp();
}
}
}