Results 1 to 2 of 2
  1. #1
    garnaout is offline Member
    Join Date
    Sep 2011
    Posts
    17
    Rep Power
    0

    Default Writing an acceleration function for my animation in Java

    I use jMonkey animation (java based) and I've been struggling for a while to write an accurate linear acceleration function with constant acceleration (which I thought should be simple)

    below is my code.

    the issue I am having is that the deceleration is never accurate, it's always off by like 0.2-0.7 and sicne the unit is in meter, I am in trouble..

    anyone can help?

    Java Code:
    public class AccelerateTrack extends AbstractCinematicEvent {
     
        private static final Logger log = Logger.getLogger(AccelerateTrack.class.getName());
        private Vector3f startPosition;
        private Vector3f endPosition;
        private Spatial spatial;
        private String spatialName = "";
        private double value = 0;
        private double maxSpeed;
        private double currentSpeed;
        private double acceleration;
        private double previousSpeed;
        float epsilon = 0.001f;  // used to compare floats accurately
        float animationSpeed;
        double prevDisp;
     
        // create an instance of the Simpleapplication inside Jme3Cinematics
        private Jme3Cinematics myApp = Jme3Cinematics.getApp();
     
        // reads the asset manager initialized in Jme3Cinematics
        private AssetManager assetManager = myApp.getAssetManager();
     
        public AccelerateTrack(Spatial spatial, Vector3f endPosition) {
            this.endPosition = endPosition;
            this.spatial = spatial;
            spatialName = spatial.getName();
        }
     
        public AccelerateTrack(Spatial spatial, Vector3f endPosition, double initialDuration,
                double acceleration, double previousSpeed, double maxSpeed,
                LoopMode loopMode)
        {
            super((float) initialDuration, loopMode);
            this.endPosition = endPosition;
            this.spatial = spatial;
            this.acceleration = acceleration;
            this.maxSpeed = maxSpeed;
            this.previousSpeed = previousSpeed;
            this.spatialName = spatial.getName();
            currentSpeed = previousSpeed;
        }
     
        @Override
        public void initEvent(Application app, Cinematic cinematic) {
            super.initEvent(app, cinematic);
            if (spatial == null) {
     
                spatial = cinematic.getScene().getChild(spatialName);
                if (spatial == null) {
                } else {
                    log.log(Level.WARNING, "spatial {0} not found in the scene", spatialName);
                }
            }
        }
     
        @Override
        public void onPause() {
            // do nothing
        }
     
        @Override
        public void onPlay() {
            if (playState != playState.Paused) {
                startPosition = spatial.getWorldTranslation().clone();
            }
            if (initialDuration == 0 && spatial != null) {
     
                spatial.setLocalTranslation(endPosition);
            }
     
        }
     
        @Override
        public void onStop() {
            value = 0;
     
        }
     
        // note that reverse here is done via acceleration, then deceleration
        // and NOT deceleration than acceleration (like the sim)
        // tpf : time elapsed since the last frame (not constant)
        // time: timer since the beginning of the animation event (0) until its duration
        @Override
        public void onUpdate(float tpf) {
     
            // to be synchronized with setTime()
            if (spatial != null)
            {
                // the animation speed (e.g. 2x)
                animationSpeed = myApp.getAnimationSpeed();
     
                // if positive acceleration
                if(acceleration > epsilon)
                {
                    currentSpeed = (time * acceleration) * animationSpeed; 
     
                    //System.out.println("Accelerated Speed: " +currentSpeed);
                    //System.out.println("Acceleration time: " +time);
                    //System.out.println("Animation Speed: "+myApp.getAnimationSpeed());
     
                    // limit speed to a max speed
                    if( currentSpeed > maxSpeed *  animationSpeed )
                    {
                        currentSpeed = maxSpeed ;
                    }
     
                }
     
                // if negative acceleration (or deceleration)
                else if(acceleration < -epsilon)
                {
                    //current speed = maxSpeed + (-acceleration speed so far) or deceleration
                    currentSpeed = (previousSpeed * animationSpeed) + ( time * (acceleration) * animationSpeed);
                    //currentSpeed = (maxSpeed * animationSpeed) + ( time * (acceleration) * animationSpeed);   
     
                    //System.out.println("Reduced Speed: "+currentSpeed);
                    //System.out.println("Deceleration Time: "+time);
     
                    // assuring vehicle stops completely when 0 speed is reached
                    if( currentSpeed < -epsilon )
                    {
                        currentSpeed = 0;
                    }
                }
     
                // TODO: might not be needed - implemented for vehicle going in reverse bug
                else if( currentSpeed < -epsilon )
                {
                    currentSpeed = 0;
                    //System.out.println("FORCE STOP SPATIAL!");
                }
     
                System.out.println("Final SPEED: "+currentSpeed);
                System.out.println(">>>>>>>>> Spatial Position is >>>>>>>>>>>>>: " +spatial.getLocalTranslation());
                System.out.println(">>>>>>>>> Spatial Destination is >>>>>>>>>>>>>: " +endPosition);
                System.out.println(">>>>>>>>> Spatial Start Position is >>>>>>>>>>>>>: " +startPosition);
            }
     
            // the exact current position is x(t) = x(0) + v(0)*t + 1/2(at^2)
            // which is the integral of the current speed - previousPosition
            if (spatial != null) {
     
                Vector3f dir = endPosition.subtract( startPosition );
                dir.normalize();
                //double disp = currentSpeed * time * 0.5f;
                //Vector3f velocity = dir.mult((float) (disp - prevDisp));
                //prevDisp = disp;
                Vector3f velocity = dir.mult( (float) (currentSpeed  * tpf + acceleration *tpf * tpf * 0.5f) ); // this is a position
                Vector3f pos = spatial.getLocalTranslation();
                spatial.setLocalTranslation( pos.add(velocity) );
     
            }
        }
     
        @Override
        public void write(JmeExporter ex) throws IOException {
            super.write(ex);
            OutputCapsule oc = ex.getCapsule(this);
            oc.write(spatialName, "spatialName", "");
            oc.write(endPosition, "endPosition", null);
        }
     
        @Override
        public void read(JmeImporter im) throws IOException {
            super.read(im);
            InputCapsule ic = im.getCapsule(this);
            spatialName = ic.readString("spatialName", "");
            endPosition = (Vector3f) ic.readSavable("endPosition", null);
        }
     
    }

  2. #2
    garnaout is offline Member
    Join Date
    Sep 2011
    Posts
    17
    Rep Power
    0

    Default Re: Writing an acceleration function for my animation in Java

    More clarification:

    * tpf : time elapsed since the last frame (not constant)
    * time: timer since the beginning of the animation event (0) until its duration (built in jMonkey)

    I think the issue is that tpf and time are not consistent. These are the only variables that are NOT constant therefore I am blaming them for the inaccuracy

Similar Threads

  1. 2D graphics acceleration
    By matiasmorant in forum Advanced Java
    Replies: 8
    Last Post: 01-18-2012, 01:06 AM
  2. Java Sprite animation
    By mrhid6 in forum Java Gaming
    Replies: 5
    Last Post: 10-14-2011, 03:26 AM
  3. slowing the animation of LogoAnimator.java
    By ladygagads in forum New To Java
    Replies: 2
    Last Post: 08-26-2011, 11:02 AM
  4. Replies: 0
    Last Post: 08-23-2010, 02:06 PM
  5. Can we animation in Java Swing?
    By Riaz Ali in forum AWT / Swing
    Replies: 5
    Last Post: 05-06-2010, 03:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •