Follow point of interest by applying torque

Posted by azymm on Game Development See other posts from Game Development or by azymm
Published on 2010-12-31T00:14:45Z Indexed on 2010/12/31 1:01 UTC
Read the original article Hit count: 674

Filed under:
|
|
|

Given a body with an orientation angle and a point of interest or targetAngle, is there an elegant solution for keeping the body oriented towards the point of interest by applying torque or impulses?

I have a naive solution working below, but the effect is pretty 'wobbly', it'll overshoot each time, slowly getting closer to the target angle - undesirable effect in my case. I'd like to find a solution that is more intelligent - that can accelerate to near the target angle then decelerate and stop right at the target angle (or within a small range).

If it helps, I'm using box2d and the body is a rectangle.

def gameloop(dt):
    targetAngle = get_target_angle()
    bodyAngle = get_body_angle()
    deltaAngle = targetAngle - bodyAngle
    if deltaAngle > PI:
        deltaAngle = targetAngle - (bodyAngle + 2.0 * PI)
    if deltaAngle < -PI:
        deltaAngle = targetAngle - (bodyAngle - 2.0 * PI)
    # multiply by 2, for stronger reaction
    deltaAngle = deltaAngle * 2.0;
    body.apply_torque(deltaAngle);

One other thing, when body has no linear velocity, the above solution works ok. But when the body has some linear velocity, the solution above causes really wonky movement. Not sure why, but would appreciate any hints as to why that might be.

© Game Development or respective owner

Related posts about physics

Related posts about math