2D camera perspective projection from 3D coordinates -- HOW?
- by Jack
I am developing a camera for a 2D game with a top-down view that has depth.
It's almost a 3D camera. Basically, every object has a Z even though it is in 2D, and similarly to parallax layers their position, scale and rotation speed vary based on their Z.
I guess this would be a perspective projection.
But I am having trouble converting the objects' 3D coordinates into the 2D space of the screen so that everything has correct perspective and scale. I never learned matrices though I did dig the topic a bit today. I tried without using matrices thanks to this article but every attempt gave awkward results.
I'm using ActionScript 3 and Flash 11+ (Starling), where the screen coordinates work like this:
Left-handed coordinates system illustration
I can explain further what I did if you want to help me sort out what's wrong, or you can directly tell me how you would do it properly. In case you prefer the former, read on.
These are images showing the formulas I used:
upload.wikimedia.org/math/1/c/8/1c89722619b756d05adb4ea38ee6f62b.png
upload.wikimedia.org/math/d/4/0/d4069770c68cb8f1aa4b5cfc57e81bc3.png
(Sorry new users can't post images, but both are from the wikipedia article linked above, section "Perspective projection". That's where you'll find what all variables mean, too)
The long formula is greatly simplified because I believe a normal top-down 2D camera has no X/Y/Z rotation values (correct ?). Then it becomes d = a - c. Still, I can't get it to work.
Maybe you could explain what numbers I should put in a(xyz), c(xyz), theta(xyz), and particularly, e(xyz) ? I don't quite get how e is different than c in my case.
c.z is also an issue to me. If the Z of the camera's target object is 0, should the camera's Z be something like -600 ? ( = focal length of 600)
Whatever I do, it's wrong.
I only got it to work when I used arbitrary calculations that "looked" right, like most cameras with parallax layers seem to do, but that's fake! ;)
If I want objects to travel between Z layers I might as well do it right. :)
Thanks a lot for your help!