My Ball's deacceleration is too fast: Speeds up nicely, slows down too fast.

Started by
2 comments, last by Bacterius 11 years, 6 months ago
It may seem simple enough, but I realized how tough it is to get it right.

Here's my basic logic:

[source lang="java"]//Some codes were called to update acceleration values.
public void accelerate(){
for (int i = 0; i < 2; i++){
speed += acceleration;
position += speed;
speed *= 0.1f;
}
}
//Some codes were called to render everything.[/source]
You may noticed that I multiply 0.1 to the speed values. The reason is that, due to game logic requirements, the acceleration values can easily speed up the speed values within a fraction of a second. I would like it so that the objects can move just as fast when it's accelerating, but takes a while to slow down when its deaccelerating.

I did thought about doing this:

[source lang="java"]//Some codes were called to update acceleration values.
public void accelerate(){
for (int i = 0; i < 2; i++){
tempValues += acceleration;
speed += tempValues;
position += speed;
speed *= 0.1f;
}
}
//Some codes were called to render everything.[/source]

I made the acceleration a cubic power, and if I were to convert the values down to position, it will create a rippling effect of decreasing power and lengthening the time it takes to stop itself from increasing when the ball is slowing down. I could make it like this, if I want:

[source lang="java"]//Some codes were called to update acceleration values.
public void accelerate(){
for (int i = 0; i < 2; i++){
tempValuesN += acceleration;
tempValuesN1 += tempValuesN;
//...
//...
tempValues3 += tempValues4;
tempValues2 += tempValues3;
tempValues1 += tempValues2;
tempValues += tempValues1;
speed += tempValues;
position += speed;
speed *= 0.1f;
}
}
//Some codes were called to render everything.[/source]

But that would be very unrealistic and very unreasonable. Are there any other ways to make the deacceleration work nicely, behaving like the ball in the game Teeter for Android?
Advertisement
Multiply the acceleration and velocity by your refresh rate (1.0 / 30.0 or 1.0 / 60.0), it'll be much smoother (since acceleration and velocity are defined with SI seconds, you need to take into account the number of times you update them every second). And get rid of the 0.1 multiplication, which looks like it's there as a band-aid.
[source lang="csharp"]public void accelerate(){
for (int i = 0; i < 2; i++){
speed += acceleration * (1.0 / refreshRate);
position += speed * (1.0 / refreshRate);
}
}[/source]
Look up Euler Time Integration for details, and you might have to swap around the velocity and position updates, not sure. Also deceleration is just negative acceleration, which you don't seem to be using here. It should work then.

If you need slightly slower deceleration, you can multiply your deceleration by any factor, like 0.5, and the ball will decelerate slower (because velocity will take more time to reach zero).

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

Any refresh rates are fine? I never had to set them before.

The acceleration values can easily go from negative to positive and back to negative within a very short time span, as it's directly hooked to an accelerometer embedded in the hardware system.
You need to use the physics refresh rate you are using. In fact, ideally you use the elapsed time since the last physics update, but we'll assume it is constant for simplicity. Although it depends a lot on what your values are. Are the values reported by your accelerometer instantaneous accelerations? Are their units in meters per second? This is all important if you want an accurate result.

If the acceleration is not well-behaved, you will need to use a better time integration scheme. The one you currently use (Euler, e.g. velocity+=acceleration dt, position+=velocity dt) is numerically unstable and doesn't work well at all when your acceleration changes very quickly (you need an increasingly high refresh rate to keep it accurate). You'll get weird stuff like slingshot effect (bad energy conservation), and the simulation will be wrong overall. Look into better methods like Verlet integration, or Runge-Kutta.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

This topic is closed to new replies.

Advertisement