Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fake a 2D sprite moving on the Z axis?

Tags:

math

graphics

I will precise my question, I have a 2D object moving in a 2D world (X,Y), and I want to fake a movement on the Z axis. So I believe the best is to play with its extent (width,height) and position a bit. But what would be the equation to determine the new extent for an object of size(w,h) and moving from 1 meter forward the camera (Z axis)? What would be the parameters of such a function?

Thanks in advance for your help.

like image 945
Silouane Avatar asked Feb 24 '23 13:02

Silouane


2 Answers

Use a projection by storing the object's true (X,Y,Z) coordinates and display from a camera K units above the plane by a 2D projection (K*X/(Z+K),K*Y/(Z+K)) where +Z moves away from the camera.

To change the height, width follow a similar pattern with (DX,DY) the true size of the sprite and (K*DX/(Z+K),K*DY/(Z+K)) the apparent (drawn) size.

To do this right you can follow the advice from this FlipCode article.

like image 88
John Alexiou Avatar answered Mar 08 '23 17:03

John Alexiou


The main parameters would be the distance to the camera and the aperture angle.

It is simple to determine the new size by new_size = size / distance. Notice that objects that have no distance would be of infinite size. To get the effect of the aperture angle you would want to include another factor f: new_size = f * size / distance Where f is the distance of unit size. Distance of unit size is the distance where the image would be drawn at its original size. Of course, this must not be zero too. By this distance you define impicitly the aperture angle.

When I talk about size I mean width and height so the formula applies on both.

I hope you can follow my explanations.

like image 22
Nobody moving away from SE Avatar answered Mar 08 '23 18:03

Nobody moving away from SE