1
\$\begingroup\$

Following the answer to my question here, the way I apply forces on an entity, is each entity has a Map of forces (Vectors) and their IDs. Every update step, the acceleration vector is set to the sum of all the forces. Forces can be removed by using their unique ID.

In psuedocode:

class Monster extends MovingEntity{
    Vector position, acceleration, velocity;
    Map<String, Vector> forces;
    // .. stuff omitted

    public void update(){
        acceleration = new Vector(0,0); // zeroing the accleration
        foreach force in forces{
            acceleration.add(force);
        }
        velocity.add(acceleration);
        position.add(velocity);
    }

    public void applyForce(Vector force, String id){
        forces.put(id, force);
    }

    public void removeForce(String id){
        forces.remove(id);
    }
}

This works fine, but here is my dilemma:

Currently, after adding a force I have to remember to remove it. For example, psuedocode to demonstrate the way player input is handled:

if(playerInput.leftPressed()) player.applyForce(new Vector(2,0), "left");
// applyForce() internally deletes the previous force with the same name.
else player.removeForce("left");
// have to remember to remove the force. sometimes a little complicated

This can also be annoying e.g. when using steering behaviors. An AI agent may want to Seek somewhere, which applies a force on itself. But when it's done Seeking, it must remove the force.

I'm wondering if it would be a good idea to empty the forces Map at the end of each update step. In the update() method from before:

    public void update(){
        acceleration = new Vector(0,0);
        foreach force in forces acceleration.add(force);
        velocity.add(acceleration);
        position.add(velocity);
        forces.empty(); // emptying the forces list
    }

Returning to the steering behaviors example, this way an agent doesn't need to remove the Seek force when it's done seeking. Each update step, as long as it calls applyForce(..), it will Seek. But the first update step that it doesn't apply a force, there will be no force. All forces are deleted automatically. And since in games forces are constantly deleted and replaced with new ones, I think this seems natural.


What is your opinion? Reset all forces each update step? Or have entities and their environment manually remove forces when it's needed?

\$\endgroup\$
2
\$\begingroup\$

You could make a distinction between forces and impulses, and use one or the other depending on your needs

A force would stay applied as long as it's not removed (e.g. gravity), while an impulse would only be applied the frame it's sent (e.g. steering impulses).

You would then remove all impulses after each physics update, but keep the forces around.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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