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.

Hey I'm currently getting started with physics engines and I set up a body (box shaped) which represents my character.

Question: Which methods can I use to move a Nape.Body aka my char?

I tried so far to manipulate the velocity, addLocalForce / Impulse but I don't know which method is the best for moving a character.

share|improve this question

1 Answer 1

You want either velocity or applyImpulse(), as a rule.

Setting velocity is a tricky business, because it flat out overrides any existing velocity the body might have and thus reduces or cancels the effect of any other forces acting on the body (such as gravity). It would also require you to do extra calculations if you want to do things like accelerate or decelerate.

Thus, applyImpulse, which is meant to apply a momentary force. The vector you use for that impulse should have its magnitude multiplied by your timestep; otherwise, the net force will vary depending on your timestep and be harder to predict.

For character movement, you also probably want to cap the movement speed, as a continuous force will accelerate the body indefinitely for as long as it's applied until a collision or another force counteracts it. So you cap the magnitude (length) of your body's velocity after you apply the impulse. Here's a sample method you might call at onEnterFrame or whatever you're using to advance your Nape space:

// Play with these values.
private const acceleration:Number = 300;
private const maxSpeed:Number = 300;

// Helper constants so you can keep your movement direction straight.
private const LEFT:Boolean = false;
private const RIGHT:Boolean = true;

protected function move(/* direction:Number */ direction:Boolean, /* angle:Number, */ delta:Number = 1/20):void {
    var impulse:Vec2 = Vec2.weak();

    if(direction == LEFT) {
        impulse.x = -acceleration * delta;
    } else {
        impulse.x = acceleration * delta;
    }

    /* var impulse:Vec2 = Vec2.fromPolar(acceleration * delta, angle * Math.PI / 180, true); */

    this.body.applyImpulse(impulse);

    if(this.body.velocity.length > maxSpeed) {
        this.body.velocity.length = maxSpeed;
    }
}

Commented stuff is for movement in any direction instead of just left/right. We're assuming that angle is in degrees; remember that Nape works in radians.

EDIT: We're also assuming that you have a built-in way to stop, such as friction or drag. And speaking of friction, remember that your force will have to counteract it for platform movement. Read up on Nape Materials. Many developers like to set their bodies to zero friction when they start moving and set it back when the movement command ends so the body automatically slows to a stop.

private const zeroFriction:Material = new Material(0,0,0);
private const normalFriction:Material = new Material(0,1,2);

protected function onStartMoving():void {
    this.body.setShapeMaterials(zeroFriction);
}

protected function onStopMoving():void {
    this.body.setShapeMaterials(normalFriction);
}
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.