< Zurück | Inhalt | Weiter >

From StretchBehavior.java

public void processStimulus( java.util.Enumeration criteria )

{

//update the positions of the vertices—regardless of criteria float elongation = 0;

float force_spring = 0; float force_mass = 0; float force_sum = 0; float timeFactor = 0.1f; float accel_sum = 0;


//loop over every vertex and calculate its new position

//based on the sum of forces due to acceleration and the spring for( int n = 0; n <m_CoordinateArray.length; n+=3 )

{

m_Vector.x = m_CoordinateArray[n]; m_Vector.y = m_CoordinateArray[n+1]; m_Vector.z = m_CoordinateArray[n+2];


//use squared lengths, as sqrt is costly

elongation = m_LengthArray[n/3] − m_Vector.lengthSquared();


//Fspring = k*Le

force_spring = m_kSpringConstant * elongation;

force_mass = m_AccelerationArray[n/3] * m_MassArray[n/3];


//calculate resultant force

force_sum = force_mass + force_spring;


//a = F/m

m_AccelerationArray[n/3] = (force_sum / m_MassArray[n/3]) * m_kAccelerationLossFactor;

accel_sum += m_AccelerationArray[n/3]; m_Vector.normalize();

//apply a portion of the acceleration as change

//in coordinate based on the normalized vector

//from the origin to the vertex m_CoordinateArray[n] +=

m_Vector.x * timeFactor * m_AccelerationArray[n/3]; m_CoordinateArray[n+1] +=

m_Vector.y * timeFactor * m_AccelerationArray[n/3]; m_CoordinateArray[n+2] +=

m_Vector.z * timeFactor * m_AccelerationArray[n/3];

}


//assign the new coordinates m_GeometryArray.setCoordinates( 0, m_CoordinateArray );


while( criteria.hasMoreElements() )

{

WakeupCriterion wakeUp =

(WakeupCriterion) criteria.nextElement();


//if a key was pressed increase the acceleration at the vertices

//a little to upset the equilibrium

if( wakeUp instanceof WakeupOnAWTEvent )

{

for( int n = 0; n <m_AccelerationArray.length; n++ ) m_AccelerationArray[n] += 0.3f;

}

else

{

//otherwise, print the average acceleration System.out.print( "Average acceleration:\t"

+ accel_sum/m_AccelerationArray.length + "\n" );

}

}


//assign the next WakeUpCondition, so we are notified again

wakeupOn( m_WakeupCondition );

}


image


After pressing a key has disturbed the equilibrium of the model, it can take a considerable length of time to return to equilibrium. In figure 13.4 the model took over 500 frames to stabilize.



image


Figure 13.4 The StretchBehavior causes the Sphere to oscillate in size. By plotting the average vertex acceleration, you can see that the model took in excess of 500 frames to stabilize. The parameters used were Spring Constant 0.8, Acceleration Loss Factor 0.98, and Vertex Mass 50 + 2.5 (average)