How do you & ldquo; bouncing & rdquo; collision detection with Actionscript 3.0? (With video)


Well first off I'm surprised actionscript 3.0 doesnt have pixel-based collision detection.

Anyways I'm using a collision library found here (I'm willing to pick another library if needed)

I recorded a video to better show what I'm trying to do, in short I'm making a side scrolling game (like mario) and I want my ground (floor) to act just like ground does... when the user walks it walks on top of the ground, lol

At first I was thinking about doing a "bounce" effect where like "if user is X amount below the surface of the ground then push him up". It seems fine but the only way I found I can do it makes my user just keep jumping once it touches the ground (not in video).

So does anyone know how to get it so that when my user is walking and he goes UP hill on the ground it will also make the users char go up too?

There was an article posted today on about pixel perfect collision detection in ActionScript 3 which does the collision detection portion of your problem.

The real logic will be what to do when you detect a collision. However, have you ever noticed how most side-scrolling games use a flat walking surface?

One idea (that doesn't require collision detection) is to use a height map like idea. You can use an array (or the pixels in a texture) to determine the height of each section of ground along your surface. As your character moves through the scene you just index the position of the character into your height map.

// How many pixels each index of the heightMap contains.
// You'll probably want to use the same value as the distance
// your character moves when the move left/right key is pressed.
const SECTION_SIZE:int = 10; 

// Fill this as a huge array with all the heights and
// the size of this will be (mapHorizontalLengthInPixels / SECTION_SIZE).
// Each element will be the distance from the top of the screen to
// place the character so it looks like it is standing on the ground.
var heightMap:Array = [ /* ... */ ];

// TODO: you might want to Tween to this value so it doesn't look chunky
character.y = heightMap[character.x / SECTION_SIZE];

To use a texture instead of an array, you just put each int that would have been put into the heightMap array into a single pixel of a BitmapData object. The advantage to using a texture is you can store a huge amount of information in a small BitmapData Object and use getPixel32() to read it out. You can generate the map once, save it (like as a png) and then embed it into the SWF.

If you want to get fancy, and you probably will if you want platforms, bridges, moving objects and more ... then use a 2d physics engine. There are a few open-source projects already including APE, Box2dAS3, Fisix, and FOAM among others. The learning curve will be higher and the coding more difficult but it may pay off for you in the end. Heck, it is an experience itself to write one from scratch if your keen!