Collision Detection Help

10 posts

Flag Post

Ok, so i get the basics of collision detection with the whole, _mc.hitTest() stuff but I cant find a tutorial that shows the different ways it is used. Im am trying to create a “MoonLander” kind of game. Pretty much you fly around with a scrolling map and there is gravity and everything. But the one thing I dont know how to do is to create a “base” for the ship to start and land on. I want it to be a platform where the ship will start on and not be able to fall when its on it, but then still be able to take off and such. I also want to be able to land on it, but only if the ship is within a certain range of _rotation and if the _rotation is too much or too little then the ship crashes instead. Thanks in advanced!!

Flag Post

lots and lots of issues here that can be solved by getting some books, I recommend Flash Game Programming for Dummies by Andy Harris since It actually goes over the basic construction of a MoonLander type game.

Hit test is tricky because it looks for actual graphic vs. graphic collisions and runs a function whn that colission is true, but in my experience(which is still somewhat limited) it does some funny things you might not expect.

What I would do it make a “launchPad” movie clip and a “landingSite” movie clip along with the assumed “lander” movie clip. Put in all your necessary controls, making sure you also increment the angle of the lander (lander._rotation…. I think? hehe…) with the left/right keys, thand then have something that looks like this for the landing checks:

if(lander.x <= landingSite.x – landingSite.height/2 – lander.height/2){ //don’t forget that the Stage dimensions are in what would regularly be determined as quadrant IV on a graph

if((lander._rotation >= 380) &&

(lander._rotation <= 10)){

lander.dx = 0;

lander.y = landingSite.x – landingSite.height/2 – lander.height/2;





}//end landing check

but play with it, hitTest is good for shooters and whatnot but for something like a Moon Lander sort of game, it may be better to do it manually.

Flag Post

ok, thanks alot, if any has a fast way to explain how to do it manually with lines and that kind of thing then that would be great!!


Flag Post

I’m not exactly sure what you’re asking there but my previous post should sum it up, of course, all the values are up to you, you just have to remember how a computer thinks, be very precise and you’ll get some good code going.

If you need explanation on concepts or statements in AS then lemme know which ones they are specifically you need help with and I’ll do my best.

Flag Post

i mean, like how do u do it maunally like you said up above…

Flag Post

just like i said, instead of using hitTest, you manually put in variables for the position of your sprites and have the game calculate their positions relative to eachother in your onEnterFrame function. So every frame it’s checking to see where your landing pad and your lander are and when they get to a certain place relative to one another, it triggers an event, or function.

Flag Post

A really good way to do a moon lander game is to use a heightmap instead of trying to do a hit test. A heightmap is just an array of y-values which define the “height” of the terrain at each point. You can’t do concave surfaces with a standard heightmap, but that should be ok for a moon lander game.

Basically it works by defining a dx value for the heightmap, which just means the difference in x position between each entry in the array. So if I picked a dx of 100 and my heightmap was {10, 20, 30, 40, 50} then I would have a terrain that was 400 pixels long and went “up” by 40 pixels. You have to decide your own dx based upon what you want for the game, and the lower the dx the more detailed you can make the terrain features.

You then use this heightmap for two different things. First is drawing the actual terrain, where you use the flash drawing API in a loop to physically draw the terrain. You know the difference in x for each point on the heightmap so the code would simply have to iterate through each entry in the heightmap, drawing to the correct height and your dx + the x position of the last point.

Then for the collision detection you’ll have to check the position of the lander and compare it to the heightmap array. If you have a height entry for every pixel on the screen, you can just check to see if the lander.y < heightmap[lander.x], if the value of the lander’s y position is less then the height of the terrain at the landers x position, then it is below the terrain and has collided.

This isn’t the only way to do moon lander, but it’s probably what I would do. Hope that helps!

Flag Post

LOL, after reading fucrate’s post, I’m so tempted to bust out a simple moon lander game… sooo tempted.

Flag Post

ok i get it now, thx a lot fucrate

Flag Post

Heh, reading through it again I’m remembering that the lander.y < heightmap[lander.x] thing will return true if the lander is above the terrain because flash increases in the y direction as you go down :X