Search code examples
game-physicslimejs

limejs collision detection


I am aware that LimeJS contains Box2d for collision detection. I am looking for some help getting started creating the detection. This is what I have:

A ball drops from the top of the screen. There are three draggable platforms below. Essentially, I need the ball to hit a platform, and then roll along the top till it reaches the edge and falls off.

This detection is essential to the game I am creating (by the way, I am a Web Developer, not a game developer, but I know JavaScript, so this was assigned to me), as it will also determine scoring (there is a target at the bottom). I feel like if I can get help on the detection and change of direction, I could make everything else happen.

Thank you.


Solution

  • I don't think it's necessary for you to get weighed down by box2d. There's a bit of a learning curve and it doesn't sound like you need it, unless you want your ball to bounce realistically off of the platforms.

    Here's a link for a very simple and easy to read collision detection function: http://www.gamedev.net/page/resources/_/technical/game-programming/collision-detection-r735

    Please note that in that example it considers the y-axis to start at the top of the screen with positive in the down direction.

    Basically, what you want to do is check if the ball is touching any of the platforms as it falls. You can use the bounding boxes of your sprites. When it touches, you need to make some sort of reaction. If the platforms are flat, then just stop the ball from moving in the y-direction. If the platforms are sloped then you're going to need to do some trig to figure out the direction of the balls movement.

    After that, you'll have to check if the ball touched the target. If it does, add to the score, play some bells and whistles and let the player start over.