How to calculate size of object depending of its height on the screen?

I am programming a mostly 2D game on Android (with Andengine), but the player is a bouncing ball (kinda in 3D).

I wanted to make the bouncing of the ball the most realistic possible, using quadrilic equations (http://www.mathopenref.com/quadraticexplorer.html). So I know how to calculate the vertical height (z) of the ball, but I do not know, what size should the ball be, depending on its vertical height (z).

When the ball touches the ground, it should be 64px (width and height of the sprite), but if for example it is at 100px above ground, what should be the width and height of the displayed ball ? Is there an easy way to calculate it ?

Thanks

Game Dev noob.

-

Remember one thing: Perspective projection ==== division by distance from camera. Let's say, that camera is at 0, so distance is Z coordinate of your object.

So your ball has X and Y coordinates equal to zero, it only changes the Z coordinate and you want to compute the size depending on Z. Let's say, your object is sliding between Z=100 and Z=10. First, you should find an absolute diameter d (size at Z=1)

for Z = 100, you decided, that size should be 64, so d/100 = 64, thus d = 6400

for Z = 10 : size = 6400/10 = 640

for Z = 200 : size = 6400/200 = 32

for Z = infinity : size = 6400/infinity = 0

Note, that larger Z (far away) makes size smaller and vice versa. If Z is 0 (disc on camera), size is infinite. If Z is negative, size is negative (or "behind the camera").

-
Good answer and explanation. – API-Beast Feb 17 '13 at 21:18
Remember to insert an if-statement or similar so you don't actually divide by Z if it's 0. Otherwise your face will melt. – HumanCatfood Feb 18 '13 at 14:18

well there is ofc if you go into 3D calculations; but it sounds too me that try and error is enough. just set 2 different scales for 2 different height and interpolate. (eg. height: 0 = scale 64px; height 100 = scale 128) in case "kinda 3D" is isometric you don't need to scale it actually (but a shadow scaling on the ground might help)

-
I was hoping there was something I didn't know that could help me calculate perfection, but I guess ill have to try – xtrimsky Feb 17 '13 at 19:46

I am making an assumption that you are viewing this ball from above for answering this question. Essentially, the size of the ball at a specific height depends on the viewing frustrum of your projection, and camera height relative to the ground(assuming once again that your view is pointing straight down the axis that you are using for 'vertical'). Using a perspective projection Matrix with a given view angle and aspect ratio, you should be able to transform a unit length vector (0,1,0) at ground location to get your base height, then whilst the game is playing, transform that same unit length vector at the current height of the ball (lets say 0,1,100) and the pixel size of the ball should be 64px * (length of transformed unit vector at 100height / length of transformed unit vector at 0 height).

Personally I would make sure your ball texture/sprite is higher resolution than 64px and use that for the sprite at the maximum height, and then scale it down to whatever size is required for height 0. You can then provide mip/maps for the sprite which should make your ball look great at any height.

-