Writing a good acceleration function in Jmonkey

You need to put some printlns in your if branches and see what’s happening. I don’t understand from this code why speed can decrease if it is being set to 0.

I’m going to tweak your code fully to be sure we are on the same page.



I will change a few things.

[java] @Override

public void onUpdate(float tpf) {



// to be synchronized with setTime()

if (spatial != null)

{

// time = myApp.time;

// value = Math.min(time / initialDuration, 1.0f);



// acceleration using tpf, acceleration, and Cinematics speed

if(acceleration > epsilon)

{

System.out.println("Accelerated Speed: "+speed);

//System.out.println("Current Position: "+spatial.getLocalTranslation());

speed += tpf * accelerationFactor;// * myApp.getAnimationSpeed();



// if final speed exceeds max speed

if( speed > maxSpeed ) // no reason for epsilon here

{

speed = maxSpeed;

}

}



// deceleration using tpf, acceleration, and Cinematics speed

else if(acceleration < -epsilon)

{

System.out.println(&quot;Reduced Speed: &quot;+speed);

speed -= tpf * accelerationFactor;// * myApp.getAnimationSpeed();



// if speed becomes negative

if( speed < -maxSpeed) // I’m guessing that min = -max

{

speed = -maxSpeed;

}



}



else if( FastMath.abs(speed) < epsilon )

{

speed = 0;

}

}[/java]



In that code, the only way speed will change is if there is acceleration or if it is close to 0.

Yeah same issue, so the car drives forward with the output:



[java]rated Speed: 0.49331897

Accelerated Speed: 0.5066766

Accelerated Speed: 0.5199834

Accelerated Speed: 0.53352016

Accelerated Speed: 0.5465832

Accelerated Speed: 0.559967

Accelerated Speed: 0.5732984

Accelerated Speed: 0.5866006

Accelerated Speed: 0.5999279

Accelerated Speed: 0.613266

Accelerated Speed: 0.626581

Accelerated Speed: 0.6399545

Accelerated Speed: 0.65369654

Accelerated Speed: 0.66669333

Accelerated Speed: 0.6799811

Accelerated Speed: 0.6932751

Accelerated Speed: 0.7066358

Accelerated Speed: 0.71997696

Accelerated Speed: 0.73328173

Accelerated Speed: 0.7466768

Accelerated Speed: 0.7599908

Accelerated Speed: 0.77327967

Accelerated Speed: 0.7866588

Accelerated Speed: 0.7999605

Accelerated Speed: 0.81331813

Accelerated Speed: 0.8266747

Accelerated Speed: 0.8399882

Accelerated Speed: 0.8532924

Accelerated Speed: 0.86663103

Accelerated Speed: 0.879964

Accelerated Speed: 0.89333034

Accelerated Speed: 0.90662587

Accelerated Speed: 0.9199727

Accelerated Speed: 0.9333067

Accelerated Speed: 0.9465991

Accelerated Speed: 0.95997727

Accelerated Speed: 0.9733729

Accelerated Speed: 0.9866422

Accelerated Speed: 0.99994177

Accelerated Speed: 1.0132548[/java] // and output stops till reaching deceleration stage



the problem is in deceleration, when the deceleration starts u see the output:



[java]Reduced Speed: 1.3

Reduced Speed: 1.2886107

Reduced Speed: 1.2753024

Reduced Speed: 1.2619643

Reduced Speed: 1.2486309

Reduced Speed: 1.2353071

Reduced Speed: 1.2219752

Reduced Speed: 1.2086222

Reduced Speed: 1.1952528

Reduced Speed: 1.181949

Reduced Speed: 1.1686519

Reduced Speed: 1.1552774

Reduced Speed: 1.141968

Reduced Speed: 1.1286135

Reduced Speed: 1.1152507

Reduced Speed: 1.1019311

Reduced Speed: 1.0885663

Reduced Speed: 1.075301

Reduced Speed: 1.0619743

Reduced Speed: 1.0486567

Reduced Speed: 1.0353396

Reduced Speed: 1.022003

Reduced Speed: 1.0086342

Reduced Speed: 0.99532384

Reduced Speed: 0.9820165

Reduced Speed: 0.96868765

Reduced Speed: 0.9553152

Reduced Speed: 0.9419694

Reduced Speed: 0.9286354

Reduced Speed: 0.91533583

Reduced Speed: 0.902007

Reduced Speed: 0.88867605

Reduced Speed: 0.87529737

Reduced Speed: 0.8619418

Reduced Speed: 0.8486396

Reduced Speed: 0.83530664

Reduced Speed: 0.82198596

Reduced Speed: 0.80867606

Reduced Speed: 0.7952948

Reduced Speed: 0.7820096

Reduced Speed: 0.76861095

Reduced Speed: 0.7553242

Reduced Speed: 0.7420282

Reduced Speed: 0.72863925

Reduced Speed: 0.7153309

Reduced Speed: 0.7019651

Reduced Speed: 0.6886547

Reduced Speed: 0.6752925

Reduced Speed: 0.6619847

Reduced Speed: 0.6487025

Reduced Speed: 0.6352951

Reduced Speed: 0.6219914

Reduced Speed: 0.60862917

Reduced Speed: 0.5953511

Reduced Speed: 0.5819437

Reduced Speed: 0.5687118

Reduced Speed: 0.55535215

Reduced Speed: 0.5419709

Reduced Speed: 0.5287231

Reduced Speed: 0.5153696

Reduced Speed: 0.50203663

Reduced Speed: 0.48865694

Reduced Speed: 0.47535014

Reduced Speed: 0.4620074

Reduced Speed: 0.4484948

Reduced Speed: 0.43533376

Reduced Speed: 0.4220321

Reduced Speed: 0.40869963

Reduced Speed: 0.39539233

Reduced Speed: 0.38203678

Reduced Speed: 0.36867967

Reduced Speed: 0.35533848

Reduced Speed: 0.3420327

Reduced Speed: 0.3287033

Reduced Speed: 0.3153683

Reduced Speed: 0.3020625

Reduced Speed: 0.28871825

Reduced Speed: 0.27540582

Reduced Speed: 0.26205334

Reduced Speed: 0.24866852

Reduced Speed: 0.23538277

Reduced Speed: 0.22208008

Reduced Speed: 0.20877123

Reduced Speed: 0.1953977

Reduced Speed: 0.18205395

Reduced Speed: 0.16875948

Reduced Speed: 0.15542138

Reduced Speed: 0.14209816

Reduced Speed: 0.12871283

Reduced Speed: 0.1153855

Reduced Speed: 0.10201711

Reduced Speed: 0.08875343

Reduced Speed: 0.07537427

Reduced Speed: 0.062095188

Reduced Speed: 0.048730906

Reduced Speed: 0.035426676

Reduced Speed: 0.02209935

Reduced Speed: 0.008734553

Reduced Speed: -0.0

Reduced Speed: -0.0

Reduced Speed: -0.0

Reduced Speed: -0.0

Reduced Speed: -0.0

Reduced Speed: -0.0

Reduced Speed: -0.0

Reduced Speed: -0.0

Reduced Speed: -0.0

Reduced Speed: -0.0

Reduced Speed: -0.0

Reduced Speed: -0.0

Reduced Speed: -0.0

Reduced Speed: -0.0

Reduced Speed: -0.0

Reduced Speed: -0.0

Reduced Speed: -0.0

Reduced Speed: -0.0

Reduced Speed: -0.0

Reduced Speed: -0.0

Reduced Speed: -0.0

Reduced Speed: -0.0

Reduced Speed: -0.0

Reduced Speed: -0.0

Reduced Speed: -0.0

Reduced Speed: -0.0

Reduced Speed: -0.0[/java]



that goes indefinitely (shouldn’t). and the car decelerates write, stops, then the next trace event is go in reverse, and it does that fine however the output is still the same (which is weird) and the issue is that when is reaches the deceleration point to stop (or more accurately the acceleration point) it does not stop in the right destination but a little bit after (like it accelerated instead of decelerating)





Note that There’s [java]if (spatial != null) {

Vector3f dir = endPosition.subtract( startPosition );

dir.normalize();

Vector3f velocity = dir.mult( speed * tpf );

Vector3f pos = spatial.getLocalTranslation();

spatial.setLocalTranslation( pos.add(velocity) );

}[/java]



at the end but shouldn’t make any difference

can you paste in the current code you are using, printlns and all?

the one I showed:



[java] @Override

public void onUpdate(float tpf) {







// to be synchronized with setTime()

if (spatial != null)

{

// time = myApp.time;

// value = Math.min(time / initialDuration, 1.0f);



// acceleration using tpf, acceleration, and Cinematics speed

if(acceleration > epsilon)

{

System.out.println("Accelerated Speed: "+speed);

speed += tpf * accelerationFactor;// * myApp.getAnimationSpeed();



// if final speed exceeds max speed

if( speed > maxSpeed ) // no reason for epsilon here

{

speed = maxSpeed;

}

}



// deceleration using tpf, acceleration, and Cinematics speed

else if(acceleration < -epsilon)

{

System.out.println("Reduced Speed: "+speed);

speed -= tpf * accelerationFactor;// * myApp.getAnimationSpeed();



// if speed becomes negative

if( speed < -maxSpeed) // minSpeed = -maxSpeed

{

speed = -maxSpeed;

}



}



else if( FastMath.abs(speed) < 0.01 )

{

speed = 0;

}

}







if (spatial != null) {

Vector3f dir = endPosition.subtract( startPosition );

dir.normalize();

Vector3f velocity = dir.mult( speed * tpf );

Vector3f pos = spatial.getLocalTranslation();

spatial.setLocalTranslation( pos.add(velocity) );

}

}[/java]

Where are the debug printlns?

for now it’s line 13 and 26



Also It doesn’t enter the if statement at line 37 so we should take the else off

and if I do this:



[java] @Override

public void onUpdate(float tpf) {







// to be synchronized with setTime()

if (spatial != null)

{

// time = myApp.time;

// value = Math.min(time / initialDuration, 1.0f);



// acceleration using tpf, acceleration, and Cinematics speed

if(acceleration > epsilon)

{

System.out.println("Accelerated Speed: "+speed);

speed += tpf * accelerationFactor;// * myApp.getAnimationSpeed();



// if final speed exceeds max speed

if( speed > maxSpeed ) // no reason for epsilon here

{

speed = maxSpeed;

}

}



// deceleration using tpf, acceleration, and Cinematics speed

else if(acceleration < -epsilon)

{

System.out.println("Reduced Speed: "+speed);

speed -= tpf * accelerationFactor;// * myApp.getAnimationSpeed();



// if speed becomes negative

if( speed < -maxSpeed) // minSpeed = -maxSpeed

{

speed = -maxSpeed;

}



}



if( FastMath.abs(speed) < 0.01 )

{

speed = 0;

//acceleration = 0;

System.out.println("STOP SPEED");

}

}[/java]



I get the same output at accelerate, and tthe same at deceleration but an indefinite



[java]TOP SPEED

STOP SPEED

STOP SPEED

STOP SPEED

STOP SPEED

STOP SPEED

STOP SPEED

STOP SPEED

STOP SPEED

STOP SPEED

STOP SPEED

STOP SPEED

STOP SPEED

STOP SPEED

STOP SPEED

STOP SPEED

STOP SPEED

STOP SPEED

STOP SPEED

STOP SPEED

STOP SPEED

STOP SPEED

STOP SPEED

STOP SPEED

STOP SPEED

STOP SPEED

STOP SPEED

STOP SPEED

STOP SPEED

STOP SPEED

STOP SPEED

STOP SPEED

STOP SPEED

STOP SPEED

STOP SPEED

STOP SPEED

STOP SPEED

STOP SPEED

[/java]



event when the car is driving at constant speed

You were complaining the speed never gets to 0 so I added code to prevent precision problems with going to 0.



I can’t debug this for you. The code looks right so your inputs must be wrong.



Put a println at the top of the function outputting acceleration and speed… then one at the bottom. “Before:” “After:” type stuff.

Also, this is bad:

Vector3f dir = endPosition.subtract( startPosition );



Your direction is always calculated relative to your last position and is not your actual direction. So of course when you are going backwards everything gets screwed up. It will also be garbage when sitting still.



If you do not have a separate direction as part of your input (direction + acceleration) then this is an impossible problem.

I do though, I am providing the method everything including direction via a destination point, acceleration factor, duration and other variables.

The fact that you calculate direction from your last position in this method is the cause of all of your problems.

Vector3f dir = endPosition.subtract( startPosition );


can you explain to me how the direction should be calculated?

if I have the endPosition and the startPosition won't that give me the direction by subtracting the vectors?

Yes, but then when you go backwards your negative speed is making your go forward again… because direction is now 180 degrees from “forward”.



When you are “driving” you have speed and direction that is independent of your last position.

is there a dirty hack for that?



like



if(acceleration > epsilon)

dir = endPosition.subtract( startPosition );



if(acceleration <-epsilon)

dir = startPosition.subtract( endPosition );

By impossible… I didn’t mean “really hard” or “unless you use this trick”. I meant literally impossible. The problem with your above code is that as soon as you brake, you will instantly start going full speed in reverse.



If you do not have direction and acceleration separate from your position (and last position) then this is impossible, ie: cannot be done.



Presumably you do have a direction or you wouldn’t even know which direction to drive to begin with.

well assuming I can pass the method anything I want, how would I pass direction?



I have point A (current location) and point B (destination), I can calculate anything I want outside the method and pass it , so what should I pass? and in what format? I’m sorry but my geom skills are limited

When you have Point A (current location) and point B (destination) what does driving backwards even mean?



At any rate, your direction is the difference between point A and point B. Not the difference between your last position and your current position.

but I am not using the lastPosition and currentPosition, I am using currentPosition (A) and endPosition (B)



I’m sorry but I really don’t get it

ah!! endPosition is not the destination!!! its in fact the currentPosition!!!