Spring Joint with Hooke's law

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Spring Joint with Hooke's law

Postby AntonSynytsia » Wed Dec 14, 2016 7:59 am

Hi, Julio,

The legacy way of creating spring joints was by using the function NewtonUserJointCalculateSpringDamperAcceleration. But it seems like the stiffness and damp parameters don't relate to Hooke's law, where stiffness is supposed to be the spring constant in kg/s^2 and damp is supposed to be the damping coefficient in kg/s. So, I utilized the Hooke's law manually:
Code: Select all
// Code for 6th row
dFloat spring_constant(40.0); // in kg/s^2
dFloat damping_coefficient(10.0); // in kg/s
// Calc current position
const dVector& p0(matrix0.m_posit);
dFloat cur_pos((p0 - matrix1.m_posit).DotProduct3(matrix1.m_right)); // in m
dVector p1(matrix1.m_posit + matrix1.m_right.Scale(cur_pos));
// Calc Current velocity
veloc0(0.0f);
veloc1(0.0f);
NewtonBodyGetVelocity(child_body, &veloc0[0]);
if (parent_body)
   NewtonBodyGetVelocity(parent_body, &veloc1[0]);
dFloat cur_vel((veloc0 - veloc1).DotProduct3(matrix1.m_right)); // in m/s
// Calculate force using the Hookes law
dFloat force(-spring_constant * cur_pos - cur_vel * damping_coefficient);
// Add linear row
NewtonUserJointAddLinearRow(joint, &p0[0], &p1[0], &matrix1.m_right[0]);
NewtonUserJointSetRowAcceleration(joint, force);
NewtonUserJointSetRowMinimumFriction(joint, -dAbs(force));
NewtonUserJointSetRowMaximumFriction(joint, dAbs(force));
NewtonUserJointSetRowStiffness(joint, 1.0f);

Knowing the gravity and the connected mass, the final height can be obtained from the function:
-kx - bv = ma
where
k is the spring constant,
x is the current position,
b is damping coefficient,
v is the velocity (zero at the final height),
m is the connected mass and all other mass on top,
a is acceleration (g in this case).
So, x=(ma+bv)/k => x=mg/k

To test it I compared the final height of the connected mass to the expected height and it was right.
But I'm wondering if passing force into the NewtonUserJointSetRowAcceleration is the right way. Ideally, I should pass the acceleration (in m/s^2) rather than force. Should I divide it by the mass of the connected objects or is this the right way?

Anton
AntonSynytsia
 
Posts: 193
Joined: Sat Dec 28, 2013 6:36 pm

Re: Spring Joint with Hooke's law

Postby Julio Jerez » Wed Dec 14, 2016 10:00 am

they do, but the units that you are using are not the correct

in acceleration unit spring is in [1/s^2]
and damper is in [1/s]

if you want to convert the k from a given K hook value, for a linear force you multiply by the inverse of the tow masses attached by the joint.

after I am done with what I am doing now I will write a better spring damper interface that takes more familiar values.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Spring Joint with Hooke's law

Postby AntonSynytsia » Wed Dec 14, 2016 11:52 am

Okay, got it. Thanks!
AntonSynytsia
 
Posts: 193
Joined: Sat Dec 28, 2013 6:36 pm


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 18 guests