Applyforce in the direction (or opposite) of the body

Posts that don't fit into other categories.
assofohdz
Posts: 25
Joined: Mon Apr 17, 2017 10:46 am

Applyforce in the direction (or opposite) of the body

Postby assofohdz » Tue Apr 18, 2017 2:08 pm

Hi

I'm a new user of Dyn4j and I really like it. I've integrated it into jMonkeyEngine using an Entity System as the main architectural key point.

First question here:

* I have a body, that I want to propel forward or backwards. The body has a direction and I want the force to be applied in that direction. All I have right now, is a key-press that lets me know that the force should be applied. I'm not sure whether to rotate, setDirection or some other means of aligning my Force with the body in question.

It's a top down 2d game, you can go forwards/backwards and rotate left or right. Right now, I have:

Code: Select all

        //Forward integration: only forces are set in Dyn4j
        for (Body b : entityBodyMap.values()) { // Dyn4j object(s) - the body(s)
            PhysicsForce p = ed.getComponent(b.bodyId, PhysicsForce.class); // Entity System data

            if (p != null) {
               
                Force f = p.getForce();         // Dyn4j object
                Torque t = p.getTorque();       // Dyn4j object

                Force fRotated = new Force(f.getForce().setDirection(Math.PI / 2 + b.getTransform().getRotation())); // Not sure here
               
                //Forces seems to work as I expect it:
                b.applyForce(fRotated);              //Apply accumulating forces on thrust
               
                //Rotation seems to work as I want it:
                b.setAngularVelocity(t.getTorque()); //We dont want accumulating forces on turning
            }
        }

        //Dyn4j essential call
        world.update(tpf);


The Force object is essentially a Vector2 v = (0,1) for forwards - or (0,-1) if going backwards.

Kind regards,
Asser

zoom
Posts: 141
Joined: Sun Mar 17, 2013 3:57 pm
Location: Stockholm, Sweden
Contact:

Re: Applyforce in the direction (or opposite) of the body

Postby zoom » Wed Apr 19, 2017 7:36 am

It boils down to:
"The body has a direction and I want the force to be applied in that direction."

In dyn4j the body has no direction, that is something your code interprets. So if you decide that "forward" starts out as positive Y your code can build up a force vector like this pseudo-code

Code: Select all

final Vector2 force = new Vector2();

if (forward) {force.add(0.0f, 1.0);}
if (backward) {force.add(0.0f, -1.0f);}
if (strafe-right) {force.add(1.0f, 0.0f);}
if (strafe-left) {force.add(-1.0f, 0.0f);}


After that you can scale the force to something suitable and finally you rotate it using the same rotation as the body has. This should result in a force vector pointing in the right direction. Maybe using

b.getTransform().transformR(force)


To orient the "forward" force before adding to the body.

assofohdz
Posts: 25
Joined: Mon Apr 17, 2017 10:46 am

Re: Applyforce in the direction (or opposite) of the body

Postby assofohdz » Wed Apr 19, 2017 7:54 am

In dyn4j the body has no direction


I think this clears it up for me. I was using getDirection (not sure where i got it), but perhaps not really understanding that there is no direction on a body, there is only a rotation.

Code: Select all

b.getTransform().transformR(force)


This is probably what I was looking for. Thanks.

zoom
Posts: 141
Joined: Sun Mar 17, 2013 3:57 pm
Location: Stockholm, Sweden
Contact:

Re: Applyforce in the direction (or opposite) of the body

Postby zoom » Wed Apr 19, 2017 9:26 am

I guess (haven't checked) but a body can be moving in only one direction, like if you push a billiard ball it will move in a direction but that is not the same as saying "the front of the car". So a moving body can have a direction but I guess you are more interested in "I want to move my car forward in the direction that is the front of the car".


Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 1 guest