Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

2D All the way! / Platformer Pixel Collisions Via Ray Casting [PlayBasic]

Author
Message
Kevin Picone
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 25th Aug 2009 02:30 Edited at: 11th Aug 2010 22:24
Platformer Pixel Collisions Via Ray Casting

This example demo's a basic platform game control system with acceleration, jumping, falling and collision. The collision is really the only interesting part of the demo (since the rest of is cut and pasted from an example you can find the PlayBasic/Projects/Tutorials/Physics folder.

In this demo, we're using an image as our collision environment, the image is a 2 colour representation of the 'hard floor' areas that a character can stand upon. When the character is moved/updated we're using the ray Casting function PixelRunLength() on this collision image to detect our position relative to the floor. Then applying some simple logic to determine what that character should do, given how far from the height it is.

Characters have 3 modes. They're either Walking, Jumping or Falling.

If the character is Walking then we need to get some idea of the terrain they're walking upon, so we run a ray down from the characters origin point until it hits the ground. If you're walking upon a slope, within this demo we're going to assume the slope never raises / drops move than the characters height in gradient. So if we're running up/down hill, then this assumption will hold us upon the ground, until the ground if too far away. In that situation, the character changes from walk mode to falling mode.

If the character is Jumping, then we don't need to check for ground collisions at all, only when it's falling.

When the character is falling, we're casting a ray down from it's current position. This time we're assuming the ground is further away then the characters height. If it is, we keep falling, if it isn't, then it's landed upon something. So we use the distance from ground to reposition the character relatively.

We could expand upon this logic further and detect situations were the character is only just above the ground perhaps. Which can happen if they try and jump up onto something and almost miss. Here, you could trigger a climbing animation or perhaps the character moves into a hanging by hands mode or something. In the demo it just forces a reposition, which can be a bit clunky..

Anyway, have fun.


Download
PlayBasic Source Codes

Pic



Attachments

Login to view attachments

Login to post a reply

Server time is: 2024-04-26 16:01:44
Your offset time is: 2024-04-26 16:01:44