Immediately stop a body

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Immediately stop a body

Postby JernejL » Mon Jan 29, 2018 2:11 am

How can i calculate in forcetorque a correct force and torque to immediately stop a body from moving and rotating? I just want the body to stop immediately, and then apply new, adjusted forces to it as if it was never moving in the first place.
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1578
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: Immediately stop a body

Postby JoeJ » Mon Jan 29, 2018 4:08 am

You could reformulate that question to "How can i calculate force to get target velocity?",
which comes up again and again... Newton should provide some utility functions for this :)

Below code is thought to be applied per frame to drive body velocities to target values.
(Going from velocity x to 0 in one frame would cause to get velocity -x in the following frame. Correcting this again leads to oszillation. To avoid that i set a duration of 3 frames in the example.)

Code: Select all
float duration = timestep * 3; // duration until we hit the target

float mass, Ixx, Iyy, Izz; BodyGetMassMatrix (body, mass, Ixx, Iyy, Izz);
sMat4 matrix;      BodyGetMatrix(body, matrix);


sVec3 targetLinvel = (...);

sVec3 curLinvel;   BodyGetVelocity (body, curLinvel);
sVec3 force = (targetLinvel - curLinvel) * (mass / duration);


sVec3 targetAngvel = (...);

sVec3 curAngvel;   BodyGetAngVel (body, curAngvel);
sVec3 torque = (targetAngVel - curAngvel) / duration;
torque = matrix.Unrotate (torque);
torque[0] *= Ixx;
torque[1] *= Iyy;
torque[2] *= Izz;
torque = matrix.Rotate (torque);

User avatar
JoeJ
 
Posts: 1453
Joined: Tue Dec 21, 2010 6:18 pm

Re: Immediately stop a body

Postby JernejL » Tue Jan 30, 2018 4:28 am

This is great, just what i needed: I didn't know quite relationship between force and velocity.

Instead of your code to calculate torque i simply call NewtonBodySetOmega to stop rotations, this is slightly simplier and still works well with the calculation of new force to apply based on body's mass.

Thank you for this, i'm using this for rewriting my really lightweight character controller for my game :)
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1578
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 13 guests

cron