Spring-loaded mechanism

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Spring-loaded mechanism

Postby misho » Fri Dec 21, 2018 6:00 pm

Hi,

I need to simulate the interaction of two bodies when they are connected by a clamped and compressed spring, and then the clamp is let go. Basically, I need to instantaneously apply a separation force to one of the bodies, so that it suddenly "flies away" from the other body. What is the proper way of accomplishing this? I considered using these among others, but I'm not sure what's the best way...

NewtonBodySetForce()
NewtonBodyAddForce()
NewtonBodyAddImpulse()
NewtonSetVelocity()
misho
 
Posts: 525
Joined: Tue May 04, 2010 10:13 am

Re: Spring-loaded mechanism

Postby Julio Jerez » Fri Dec 21, 2018 6:29 pm

NewtonBodyAddImpulse()
Julio Jerez
Moderator
Moderator
 
Posts: 10954
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Spring-loaded mechanism

Postby misho » Fri Dec 21, 2018 7:42 pm

Beautiful, thanks!
misho
 
Posts: 525
Joined: Tue May 04, 2010 10:13 am

Re: Spring-loaded mechanism

Postby misho » Fri Jan 04, 2019 5:31 pm

This is probably not a bug, but when I add an add an impulse, it seems to be cumulative on the already-existing body velocity. For example, if the body is not moving, it behaves as expected. But if the body is moving, the effect of this impulse will greatly vary on the velocity vector of the object.

I guess I'll have to calculate for the "corrected" impulse vector...
misho
 
Posts: 525
Joined: Tue May 04, 2010 10:13 am

Re: Spring-loaded mechanism

Postby Julio Jerez » Fri Jan 04, 2019 5:48 pm

that's why is called add impulse.
if is override the body velocity will be total wrong. didn't you see this:
https://www.youtube.com/watch?v=BLuI118nhzc
Julio Jerez
Moderator
Moderator
 
Posts: 10954
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Spring-loaded mechanism

Postby misho » Fri Jan 04, 2019 8:46 pm

Julio Jerez wrote:that's why is called add impulse.
if is override the body velocity will be total wrong. didn't you see this:
https://www.youtube.com/watch?v=BLuI118nhzc


LOL yes indeed, but I thought it was going to be relative to the body motion :mrgreen:
misho
 
Posts: 525
Joined: Tue May 04, 2010 10:13 am

Re: Spring-loaded mechanism

Postby Julio Jerez » Fri Jan 04, 2019 8:57 pm

yikes, my mistake sorry, I gave you the wrong function the function you want is this:
Code: Select all
void NewtonBodyApplyImpulsePair (const NewtonBody* const body, dFloat* const linearImpulse, dFloat* const angularImpulse, dFloat timestep);
the implementation is this.
Code: Select all
void dgBody::ApplyImpulsePair (const dgVector& linearImpulseIn, const dgVector& angularImpulseIn, dgFloat32 timestep)
{
   m_impulseForce += linearImpulseIn.Scale(1.0f / timestep);
   m_impulseTorque += angularImpulseIn.Scale(1.0f / timestep);

   m_sleeping   = false;
   m_equilibrium = false;
   Unfreeze ();
}


The function I gave you is one that I wrote many year ago for game play. Basically that funtion do a lot of baby sitting, it calculate the impulse required for a point on a body to have a desired velocity.
back them that hwo game physics work, by as you can see that stuff have a way to come back and hunt you.
I should probably delete it, and move the implementation to the utility demos.
sorry about that.
Julio Jerez
Moderator
Moderator
 
Posts: 10954
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Spring-loaded mechanism

Postby misho » Sat Jan 05, 2019 6:27 pm

Thanks Julio!

Unfortunately that function didn't work for me at all. Given the same setup, when I use these two functions:

Code: Select all
   if (vLength(LinearImpulse))
//      NewtonBodyApplyImpulsePair(body, &(LinearImpulse.m_x), &(AngularImpulse.m_x), timediff);
      NewtonBodyAddImpulse(body, &(LinearImpulse.m_x), &(bodyMatrix.m_posit.m_x), timediff);


Only the original (NewtonBodyAddImpulse()) method gives me result I expected (when object is NOT moving). There is no movement whatsoever from NewtonBodyApplyImpulsePair()

The result I expect is, when I call either of these functions, the body "pops" away in the amount and direction I specified in LinearImpulse vector. I get that result only in the original NewtonBodyAddImpulse() function.

Is there anything I'm doing wrong?
misho
 
Posts: 525
Joined: Tue May 04, 2010 10:13 am

Re: Spring-loaded mechanism

Postby Julio Jerez » Sat Jan 05, 2019 7:36 pm

misho wrote:The result I expect is, when I call either of these functions, the body "pops" away in the amount and direction I specified in LinearImpulse


but that is exactly whe tha function does. you can see the code.

Code: Select all
void dgBody::ApplyImpulsePair (const dgVector& linearImpulseIn, const dgVector& angularImpulseIn, dgFloat32 timestep)
{
   m_impulseForce += linearImpulseIn.Scale(1.0f / timestep);
   m_impulseTorque += angularImpulseIn.Scale(1.0f / timestep);

   m_sleeping   = false;
   m_equilibrium = false;
   Unfreeze ();
}


set a break point, and see that is accumulat the impulse on the force and wake the body.
In teh next update the relative velocity between the two bodies will be
impulseForce / efectiveMass
Julio Jerez
Moderator
Moderator
 
Posts: 10954
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Spring-loaded mechanism

Postby misho » Sat Jan 05, 2019 7:42 pm

Julio Jerez wrote:In teh next update the relative velocity between the two bodies will be
impulseForce / efectiveMass


Sorry - do I have to apply this to both bodies?
misho
 
Posts: 525
Joined: Tue May 04, 2010 10:13 am

Re: Spring-loaded mechanism

Postby Julio Jerez » Sat Jan 05, 2019 8:23 pm

Not you do not have to apply to both bodies.
If for example the two bodies are of equal mass, them the other body should respond with a velocity equal both opposite. Both if the other body is more massive, the it move very little, and it is the body you apply the impulse that moves.
This is the classical text book high school problem of that astronaut in space that lost the thruster and only have a rentch, he throw s the rentch in the opposite direction of the line between him and the craft, then he gain some velocity toward the craft.

The impulse will affect the other body yes, it is the law.
Julio Jerez
Moderator
Moderator
 
Posts: 10954
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Spring-loaded mechanism

Postby Julio Jerez » Sat Jan 05, 2019 8:37 pm

The above explation only works if the two bodies connected by a bilateral joint, if is a contact only them only the body move, since contact can't assert repulsive forces.

How are you doing this?
Julio Jerez
Moderator
Moderator
 
Posts: 10954
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Spring-loaded mechanism

Postby misho » Sat Jan 05, 2019 8:47 pm

Julio Jerez wrote:The above explation only works if the two bodies connected by a bilateral joint, if is a contact only them only the body move, since contact can't assert repulsive forces.

How are you doing this?


I have a "hard" hinge (of type dCustomHinge) that connects 2 bodies. When I destroy the hinge (jettison rocket panel) I want a smaller body (panel) to "spring" off and away. So - I destroy the hinge and set an impulse vector on the smaller body.

In my ApplyForceAndTorqueCallback() I check for this vector (as in my code I posted) and use NewtonBodyAddImpulse() (which works) or NewtonBodyApplyImpulsePair() (which does not)
misho
 
Posts: 525
Joined: Tue May 04, 2010 10:13 am

Re: Spring-loaded mechanism

Postby Julio Jerez » Sat Jan 05, 2019 9:02 pm

misho wrote:[NewtonBodyApplyImpulsePair()

Is the correct function.
In the sandbox take any demo.
Create a body, with any velocity, an call that function to apply some impulse, the trace the velocity in any of the call backs, if there isn't a big the velocity should be added the velocity at creation time.
Julio Jerez
Moderator
Moderator
 
Posts: 10954
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Spring-loaded mechanism

Postby misho » Sat Jan 05, 2019 9:08 pm

Julio Jerez wrote:In the sandbox take any demo.
Create a body, with any velocity, an call that function to apply some impulse, the trace the velocity in any of the call backs, if there isn't a big the velocity should be added the velocity at creation time.


Ok, I will investigate with the sandbox demo.

Also, now that I have your ear :mrgreen: , I've been trying to ask you a question about collisions:

here

Can you please take look at it and let me know your thoughts?

Thanks!
misho
 
Posts: 525
Joined: Tue May 04, 2010 10:13 am

Next

Return to General Discussion

Who is online

Users browsing this forum: No registered users and 2 guests