Custom joints latency

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Custom joints latency

Postby doggyboy » Thu Aug 07, 2008 5:09 am

Hi,

I'm still developing an airhockey game. To move the mallet, I'm using a custom joint. Every graphics tick I update the joint with the new mouse position and let Newton calculate the necessary force to bring the mallet to the new position. Now, the problem is that there seems to be a small latency on the mallet. Is there any solution for this problem? This is how I initialize my joint:

Code: Select all
        m_physicsJoint = NewtonConstraintCreateUserJoint(m_physicsWorld, 6, PhysicsJointCallback, m_physicsMallet, NULL);
        NewtonJointSetUserData(m_physicsJoint, m_physicsMallet);
        NewtonUserJointSetRowStiffness(m_physicsJoint, 0.98f);
        NewtonUserJointSetRowAcceleration(m_physicsJoint, 1.0f);


And this is my joint callback function:

Code: Select all
   NewtonBody* body = (NewtonBody*) NewtonJointGetUserData(joint);

   dMatrix matrix;
   NewtonBodyGetMatrix(body, &matrix[0][0]);

   dVector pin0(1.0,0.0,0.0);
   dVector pin1(0.0,1.0,0.0);
   dVector pin2(0.0,0.0,1.0);

   dVector position;
   position.m_x = MainWindow::m_currentMousePosition[0];
   position.m_y = 1.3;
   position.m_z = MainWindow::m_currentMousePosition[2];

   // Update joint

   NewtonUserJointAddLinearRow(joint, &matrix.m_posit[0], &position[0], &pin0[0]);
   NewtonUserJointAddLinearRow(joint, &matrix.m_posit[0], &position[0], &pin1[0]);
   NewtonUserJointAddLinearRow(joint, &matrix.m_posit[0], &position[0], &pin2[0]);


I've already tried to speedup the process by multiplying the force vector in the force&torque callback with f.e factor k:

Code: Select all
       dVector force;
       NewtonBodyGetForce(m_physicsMallet, &force[0]);
       force *= 5;
       NewtonBodySetForce(m_physicsMallet, &force[0]);


Maybe I shouldn't do it with joints, but then I lose the advantage of collision detection when the mallet hits the puck. Does anyone have a good idea tot solve this issue?

Greetz!
doggyboy
 
Posts: 4
Joined: Wed Jun 25, 2008 3:02 pm

Re: Custom joints latency

Postby andrew11 » Fri Aug 08, 2008 1:48 am

I perform picking and dragging in my game and have found that applying spring forces myself in the force and torque callback works better than using a custom joint. You would apply a force proportional to the distance of the mouse to the mallet with some damping (F = k*x-damp*v).

However, the problem with your code is that you use NewtonUserJointSetRowStiffness and NewtonUserJointSetRowAcceleration when creating the joint, rather than in the callback. Put NewtonUserJointSetRowStiffness in the callback and remove NewtonUserJointSetRowAcceleration since that replaces the forces calculated by the engine to enforce the constraint. Try changing this first and see if it gets better. With the exact solver, the result with the custom joint may be acceptable.

- Andrew
andrew11
 
Posts: 3
Joined: Sun Sep 25, 2005 9:44 pm

Re: Custom joints latency

Postby doggyboy » Fri Aug 08, 2008 6:04 am

Hi,

I've removed the NewtonUserJointSetRowAcceleration and putted NewtonUserJointSetRowStiffness in the callback, but the result stays the none or less same. During some previous tests, I've already tried to use spring force, but that gives strange results. The mallet actually keeps bouncing around the destination point. Maybe you can explain to me how I should set the parameters from the damping term to avoid this strange behavior?

And thanks for your reply!

Greetz,
Doggyboy
doggyboy
 
Posts: 4
Joined: Wed Jun 25, 2008 3:02 pm

Re: Custom joints latency

Postby andrew11 » Fri Aug 08, 2008 5:46 pm

The way to reduce the bouncing around is by using what's called a "critically damped" spring. The critical damping coefficient is sqrt(4*mass*k). With less damping, the system is under damped and will oscillate. With more damping, the system will take longer to settle to the right position.

Calculation of the damping:
Code: Select all
float springk = 500;    // may adjust this value
float vdamping = sqrtf( 4 * mass * springk );        // critically damped

Here is pseudocode for the calculation of the force:
Code: Select all
dVector force = (destination - bodyposition) * springk - vdamping * bodyvelocity;

I hope this helps make the results better.
andrew11
 
Posts: 3
Joined: Sun Sep 25, 2005 9:44 pm


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 13 guests

cron