0

I'm making a simple game. I have one stationary rectangle and one moving only on the y axis. It looks like this.
The right car is moving down. The two cars have a width of 40 and a height of 80.

When I check collision like this it doesn't work properly.

if (dist(other_car.x, other_car.y, main_car.pos.x, main_car.pos.y) < 40) {
  console.log("colliding");
}

How do I make it more accurate?

Federico Grandi
  • 6,785
  • 5
  • 30
  • 50

1 Answers1

1

Right now you're doing circle-circle collision detection. You want to be doing rectangle-rectangle collision detection.

Shameless self-promotion: here is a tutorial on collision detection. It's written for Processing, but all of the ideas apply to P5.js as well.

Googling "rectangle-rectangle collision detection" will also return a ton of results. Good luck!

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107