Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm trying to put together 2D game on Flash ActionScript. Game objects positions are updated by physics library and I'm trying to control them applying forces and updating these forces every frame.

Right now I'm pondering how to control a helicopter. I'm using concept of throttle and target height. To make heli float on constant height, I'm applying force of

F0 = -m * gy

which negates gravity for it. To make it move up or down, target height is used - it is increased with up key and decreased with down key, then the heli should ascend or descend to it. The problem is transitional process - right now the heli misses target height and then oscillates around it.

In short: how to control body with forces to make it move from point A to point B and stop there? Here's how I'm controlling it now:

    public function update(time:int):void
    {
        lastUpdate = time;
        var deltaY:Number = targetHeight - body.graphic.y;
        throttleMark.y = targetHeight;
        //force for stable floating
        var antigrav:Number = -body.gmass * body.space.gravityy;
        //made-up damping factor
        //   (in fact, too small, but higher scale make heli move very slowly)
        var damping:Number = -body.vy * body.vy * body.vy * 0.5;
        var attraction:Number = deltaY * 1000.0;
        var totalForce:Number = antigrav + attraction + damping;
        body.applyRelativeForce(_strafe, totalForce, 0.0, 0.0);
    }
share|improve this question

2 Answers 2

up vote 5 down vote accepted

You could consider running a simple PID controller in your game. It's simple to implement in a function like the one you have already.

By tweaking the parameters (or computing them, there are many techniques) you should be able to get a pretty neat behaviour.

Be sure to run your physics loop with a fixed time step though, as this is necessary to achieve stable regulation when implemented with discrete/digital logic (as oppossed to analog circuits....)

However, some non-linear daming that Lunin suggests might be an option. That would allow the chopper to "soft-snap" to the target height. It depends on how "physics-y" you want your game to behave. :)

share|improve this answer
    
Thanks for direction! I implemented PID scheme and it works for me. It took time to find stable coefficients, but now heli 'snaps' in place without much overshoot. –  alxx May 11 '11 at 7:40
1  
PIDs are like magical unicorns. Hard to tame, but once they are hey you've got a magical unicorn! –  tenpn May 11 '11 at 8:06

Consider increasing your dampening as you get closer to the target height or making a boolean switch for a fixed increase once you've reached the target and until another height is set. That would allow you to have faster movement without sacrificing your ability to stop or requiring much change in code.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.