I am working on a very simple Physics game for Android where an object bounces up and down with some gravity (0 friction, ball should always reach same height). I want it to reach the same height on all devices (density independence). My game is coded with the following for the physics, which works perfectly fine on a 540 * 960 device.
Note: My game has a max FPS of 30
physics code
//Instance variables
private final float acceleration = .1f;
private static float timePoint;
private float velocityY = 0;
//game loop code
timePoint += 1;//updates every tick
velocityY = velocityY - acceleration * timePoint;
position.y = (int) (position.y - velocityY);
if(position.y + bitMapHeight <= 0){
velocityY = 30;
timePoint = 0;
}
When the ball hits the ground (y = 0) I simply apply a velocity of 30, which makes the ball reach a desired height on the 540 * 960 device. Of course multiple devices lead to major headaches, and this formula will not suite all of them. I think the best approach would be to somehow calculate the velocity based on screen height, but I'm not really sure what the best approach is for something like this. Below are some screenshots showing whats happening.

If anyone can point me in the right direction I would be very grateful! Please let me know if any more details are required.
I had to solve a similar problem in a 2D game I was working on a while ago.
I developed the initial physics and rendering functionality on my own Android device until it behaved nicely on that particular device. I then obtained 'baseline' values for the relevant variables (eg. pixels:meter as mentioned by bjb568), to achieve the correct velocities on my baseline device.
Then, to make the game behave in the same way on different devices/screens, I simply used the baseline values scaled by the ratio of the actual screen resolution and the baseline screen resolution.
My game used a SurfaceView, so the 'actual screen resolution' was basically determined by the width and height of the surface.
I hope that helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With