Platforming Collision Engine - Freehand Vector Terrain vs. Rectangle

Subscribe to Platforming Collision Engine - Freehand Vector Terrain vs. Rectangle 5 posts

avatar for Ringer Ringer 249 posts
Flag Post

YES, THIS IS IN AS2. I’m just much faster at it.

Here’s a little platforming engine I’ve been messing with, intended to handle any shape of freely drawn terrain by using hitTest(point) against several points relative to a rectangle (the player). I’m not having any specific problems, but there’s a lot of messiness in the sprite’s behavior.

Arrow keys to move and jump. Check it out here: http://megaswf.com/s/2449612

For some reason MegaSWF can’t get dimensions right so try hitting “Play flash full screen” there.

I have the rectangle go transparent when it’s in its ‘air’ state and solid for the ‘ground’ state. So I’m not seeing anything that really BREAKS the rules here, but it behaves strangely when confronted with certain situations. Situations which aren’t that rare, but I can’t identify. For example, sometimes when the sprite should just be standing still, it jitters up and down rapidly. Sometimes (usually on a ledge) it switches every frame between its ground and air states. And sometimes it just jerks farther than it should; I know this is due to the ‘while’ loops for collision correction, but the way I have the sprite move each frame, I didn’t think it was possible for it to ‘embed’ itself in the terrain far enough to trigger them more than a few times.

So, does anyone have any ideas for how to smooth it out?

Code on Player MovieClip:

5 edits later…
Okay, I can’t figure out textile formatting for big blocks of code so I’m just pasting it here:

http://pastebin.com/4cMXv2XU

I hope it’s all pretty clear, I commented as much as I thought wasn’t obvious. Note that the only code anywhere else is concerned with moving the camera; all that matters otherwise is that the parent MC contains another child MC called “terrain.” So you can easily test this code in AS2 in your own FLA if you wish.

 
avatar for Drakim Drakim 1141 posts
Flag Post

I can’t specifically say what your issue is here (I’m not sure if I experienced the same jitters in the demo you described).

But I can say that when you use AS2, and code directly “on” the MovieClips instead of using classes, you are being your own worst enemy. There will be much less help to gain from other developers, and it will be much much harder to keep your code logical and consistent.

I won’t hassle you about the AS2 because I realize you have decided to keep it, but you should really consider moving from timeline coding to classes instead.

 
avatar for qwerber qwerber 4717 posts
Flag Post

It’s not going to work because you should not be using hitTest point. But because you are using AS2 any method that will guarantee no bugs will not run because it will lag.

If you will want to try though, analyze the pixels of the rectangular area that the character has swept across during the movement of the last frame. Use a bressenham pixel march until you hit a sold pixel, which will be the point of collision. Then analyze the solid pixels next to it to determine whether the surface is normal to the ground, and whether you can jump off of it.

 
avatar for Ringer Ringer 249 posts
Flag Post

Thanks for the advice guys

Originally posted by Drakim:

But I can say that when you use AS2, and code directly “on” the MovieClips instead of using classes, you are being your own worst enemy.

I know :(

Originally posted by qwerber:

If you will want to try though, analyze the pixels of the rectangular area that the character has swept across during the movement of the last frame. Use a bressenham pixel march until you hit a sold pixel, which will be the point of collision. Then analyze the solid pixels next to it to determine whether the surface is normal to the ground, and whether you can jump off of it.

I’m already doing something like this, although it’s pre-hoc, and it goes a few pixels at a time (I think I put an adjustable accuracy constant in there set to 3 or something).

Anyway, I bear a mistrust of hitTest rectangle because a) I don’t know its implementation, b) it only returns true or false, and sometimes I want to know about intermediate points of the character’s collision rectangle (so he won’t stand on just a 1 pixel corner if he’s on a slope), c) I thought it might be more efficient to write my own algorithm using only a few points, and d) I fear change. I suppose I’ll consider this an experiment and eventually stop making more work for myself by using a popular open-source engine – I’m not even really making a game with this yet.

If anyone here has built their own such robust engine that handles whatever shape of terrain like this, I’d be really interested to hear about it.

 
avatar for qwerber qwerber 4717 posts
Flag Post

rewrite in AS3 will bring you so many benefits it outweighs the work lost.