'Is there a way to detect collisions between the ball and the lines?
I'm currently working on a little physics simulation, and had some trouble figuring out how to get the ball to collide with the platforms. Here's my code:
var a, f = 0.95, s = 0, vx = 0, vy = 0, b = -0.6, m = 10, x = 200, y = 100;
function setup() {
createCanvas(400, 400);
a = m * 0.1;
}
function draw() {
background(100);
line(0, 100, 200, 210);
line(400, 100, 150, 320);
//friction and acceleration stuff
vx *= f;
x += vx;
vy += a;
y += vy;
//wall collisions
if (y >= height - 12) {
vy *= b;
y = height - 12;
}
if (y < 12) {
y = 12;
}
if (x > width - 12) {
vx *= b;
x = width - 12;
}
if (x < 12) {
vx *= b;
x = 12;
}
ellipse(x, y, 24);
//snapping position to mouse
if(mouseIsPressed)
{
s = (winMouseX - pwinMouseX)/4;
x = mouseX;
y = mouseY;
vx += s;
vy = 0;
}
}
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/p5.js"></script>
My main issues come from not knowing how to deal with slopes, so if anyone has an idea it would be much appreciated. Thanks!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|