Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

200
Views
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/p5@1.4.1/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!

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!