# Uneven terrain collision detection

**URL:** <https://forum.kirupa.com/t/uneven-terrain-collision-detection/263405>\
**Category:** programming\
**Created:** [June 15, 2008, 7:23pm UTC](https://forum.kirupa.com/t/uneven-terrain-collision-detection/263405 "2008-06-15T19:23:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![steve\_b](https://avatars.discourse-cdn.com/v4/letter/s/ee7513/32.png) [@steve\_b](https://forum.kirupa.com/u/steve_b)\
**Post date:** [June 15, 2008, 7:23pm UTC](https://forum.kirupa.com/t/uneven-terrain-collision-detection/263405/1 "2008-06-15T19:23:05Z")

</div>

I making a lander style game where you fly around and save penguins (dont ask!) and then you fly your helicopter to a landing pad. There is along way to go, but the collision detection wont work and im out of ideas! Here is the problem:

[Here is the swf](http://www.stevenboulton.com/lander_v1c.swf)

Ive used [this](http://flash-creations.com/notes/astb_bounceCollide.php) as the basis of the hit collection, a sentry/perimeter setup. There is a perimeter set around the helicopter, and one set around the ground. If they two hit each other, then some code moves them apart. It works really well. But sometimes, the helicopter gets stuck inside the the ground (see swf). Here is the code that does the testing:

```php
ground_collision = function(l, o) {
    
    var c=0;
    var xs=0;
    var ys=0;
    var diff=0;

    for (i in l.perim) {
            
        l.perim.localToGlobal(p={x:l.perim*._x,y:l.perim*._y})

        if(o.hitTest(p.x, p.y, true)){

                    _root.fuel -= 5;
                    _root.points -= 1;
                    xMovement *= -1;
                    gravity *= -1;
        }

        
        ys += p.y;
        xs += p.x;
        c++;
    }
}

```

As you can see, it simply reverses the gravity and xmovement so that the helicopter goes away from the ground. If it moves slowly, this works brilliantly. However, if you hit it with any speed, the helicopter goes right through. I know why this is, I just cant fix it ☹

Can anyone suggest a better method?

---

<div class="post-metadata">

**Author:** ![andrew2110](https://avatars.discourse-cdn.com/v4/letter/a/59ef9b/32.png) [@andrew2110](https://forum.kirupa.com/u/andrew2110)\
**Post date:** [June 16, 2008, 3:11pm UTC](https://forum.kirupa.com/t/uneven-terrain-collision-detection/263405/2 "2008-06-16T15:11:53Z")

</div>

I made a platform game as a final year project at uni… The way I did it was, calculate the ySpeed, and then:

```auto

groundHit=false;
for i=Math.round(yPosition);i<=yPosition+Math.round(ySpeed);i++
if(ground_collision(xPosition,i))
{
yPosition=i;
groundHit=true;
break;
}
if(!groundHit)
yPosition=yPosition+ySpeed;

```

No idea if an approach like this is applicable to flash at all? (my game was written with DirectX and C++). Another approach could be to simply stop ground collisions from happening if the sprite is heading upwards?
