com.barrybecker4.simulation.snake.SnakeUpdater Maven / Gradle / Ivy
// Copyright by Barry G. Becker, 2012. Licensed under MIT License: http://www.opensource.org/licenses/MIT
package com.barrybecker4.simulation.snake;
import com.barrybecker4.simulation.snake.geometry.SegmentUpdater;
/**
* Makes the snake animate forward one time step.
*
* @author Barry Becker
*/
public class SnakeUpdater {
private Snake snake;
/** the time since the start of the simulation */
private double time_ = 0.0;
private LocomotionParameters locomotionParams = new LocomotionParameters();
private SegmentUpdater segmentUpdater;
/**
* Constructor
*/
public SnakeUpdater() {
segmentUpdater = new SegmentUpdater();
}
/**
* steps the simulation forward in time
* if the timestep is too big inaccuracy and instability will result.
* @return the new timestep
*/
public double stepForward(Snake snake, double timeStep ) {
this.snake = snake;
updateParticleForces();
if ( locomotionParams.getUseFriction() ) {
updateFrictionalForces();
}
updateParticleAccelerations();
boolean unstable = updateParticleVelocities( timeStep );
updateParticlePositions( timeStep );
time_ += timeStep;
if ( unstable ) {
return timeStep / 2;
}
else {
return 1.0 * timeStep;
}
}
/**
* Parameters controlling the movement (i.e. locomotion)
*/
public LocomotionParameters getLocomotionParams() {
return locomotionParams;
}
/**
* update forces
*/
private void updateParticleForces() {
// apply the sinusoidal muscular contraction function to the
// left and right sides of the snake
for ( int i = 2; i