Skip to main content
gamedev.net
🔒 Locked

Implicit Spring

Started by xdotdt Apr 8, 2005 at 1:56 AM 5 replies 1.2k views
Original Post
xdotdt
xdotdt
Thanks in advance. I'm studying about implicit integration method. I can understand how it works, however, I failed to implement it. I've coded for only one spring for two pointmasses. But it failed. Just only an identity jacobian matrix is calculated. Anyone has an example or code segments for implicit spring? Here's my code, what's wrong with it?

ComputeJacobian()
{
	Vector3f oD = m_poPM1->GetPoint() - m_poPM2->GetPoint();
	Matrix3f oDDT( 
		oD.x*oD.x, oD.x*oD.y, oD.x*oD.z,
		oD.y*oD.x, oD.y*oD.y, oD.y*oD.z,
		oD.z*oD.x, oD.z*oD.y, oD.z*oD.z );
	float fLengthInv = oD.Length();
	if( !IsZero( fLengthInv ) )
		fLengthInv = 1.0f/fLengthInv;
	oDDT = oDDT * fLengthInv * fLengthInv;
	m_oJX = m_fSpringConst * ( oDDT + ( Matrix3f::IDENTITY - oDDT )*( 1.0f - m_fOrgLength*fLengthInv ) );
	m_oJV = Matrix3f::IDENTITY * m_fDampingConst;
}


Update( float fDT )
{
	// -------------------------------------------------------------------------
	// implicit integration( backward euler method )
	// -------------------------------------------------------------------------
	ComputeJacobian();

	Matrix3f M = Matrix3f::IDENTITY * m_poPM1->GetMass();
	Matrix3f A = M - fDT*m_oJV - fDT*fDT*m_oJX;
	m_poPM1->SetForce( m_poPM1->GetForce() + fDT*m_oJX*m_poPM1->GetVelocity() );
	Vector3f b = fDT * m_poPM1->GetForce();

	if( A.MakeInverse() == false )	// not invertible
		return;

	Vector3f oDV = A * b;
	Vector3f oDX = fDT * ( m_poPM1->GetVelocity() + oDV );

	m_poPM1->SetVelocity( m_poPM1->GetVelocity() + oDV );
	m_poPM1->SetPoint( m_poPM1->GetPoint() + oDX );
	m_poPM2->SetVelocity( m_poPM2->GetVelocity() - oDV );
	m_poPM2->SetPoint( m_poPM2->GetPoint() - oDX );
}
kju:b
Eelco
Eelco
i dont think it matters a whole lot, but shouldnt this be:

float fLengthInv = oD.Length();
if( !IsZero( fLengthInv ) )
fLengthInv = 1.0f/fLengthInv;
oDDT = oDDT * fLengthInv * fLengthInv;

this:

oDDT = oDDT / oD.Dot(oD);

you wont get a divide by zero anyway, and youre wasting precious precision with all that unneeded math.
xdotdt
xdotdt
Do you mean that a round error is accumulated by not dividing by it directly?
Is that error really big enough to be careful about it?
Hmm, I'll take care. However as you mentioned, it is not the main problem.
Anyway thank you for reply.
kju:b
Eelco
Eelco
is this an implementation of:

http://www.rawhed.com/uttumuttu/temp/implicit.doc

?

i dont know if this page is correct, but i suppose it is.

your code isnt too readable imho though, so atm i dont have the time to check if your implementation matches the link.
grhodes_at_work
grhodes_at_work
Eelco, do you know what file format that implicit.doc is? It doesn't seem to be Microsoft Word, and it isn't readable with a simple text editor...
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
Eelco
Eelco
Quote:
Original post by grhodes_at_work
Eelco, do you know what file format that implicit.doc is? It doesn't seem to be Microsoft Word, and it isn't readable with a simple text editor...

no i dont have a clue.

i couldnt open it myself first, i gave up on it, then i accidentaly clicked google's html version button, and voila, there it was :)

i dont know anymore how i found it, but you should be able to get it to pop up on google using the link.
xdotdt
xdotdt
Yeah, I'm reading it.
As you said, I got it through google's preview, that is, html version.
I hope that Mikko will see this.
kju:b

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.