简体   繁体   中英

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

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.

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.

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.

The width and height will be inversely proportional to the distance from the viewer. If they are twice as far away, the size will be half. So If your "natural" distance from the viewer is A, and the new position is at A+Z, you will want to multiply the original width and height by A/(A+Z). This also works for small negative Z values (object is closer to viewer and will appear larger).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM