Platforming Collision Engine – Freehand Vector Terrain vs. Rectangle discussion on Kongregate

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. 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. 5 edits later… Okay, I can’t figure out textile formatting for big blocks of code so I’m just pasting it here: 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. 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. 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. 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. 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. Source.


Яндекс.Метрика Рейтинг@Mail.ru Free Web Counter
page counter
Last Modified: April 23, 2016 @ 1:12 am