The penny has dropped on how to create a 3D object in Flash; something that has evaded my understanding for too long. I’m going to attempt to explain it here, to give other people a shot at understanding it, and to help me nail it once and for all!
Virtual 3D to Actual 2D
To present a 3D object in Flash, we have to convert a hypothetical three dimensional location (x, y, z) into an actual two dimensional location on the screen (x, y). To do this we have to use a formula which scales the x and y coordinates of an object relative to a centre point.
Let’s use an analogy to describe this formula, as I find that’s the easiest way to learn (and to teach).
We are Flash
Consider for a moment that our field of vision represents a 2D coordinate system with the origin (0,0) set at whatever we’re focusing on. Anything to the left of that focus point is in the negative x region, anything above is in the negative y region, anything to the right is positive x and anything below is positive y.
House in the country
Imagine walking down a drive to a house in the country. A chimney at the top left corner of the house starts off near the centre of our vision because the house is quite far away, but gradually moves upwards and toward the left of our vision as we move closer. Likewise a pond on the right hand side of the house starts off fairly close to our central point of focus, and gradually moves further right and down, until we’ve walked past it.
The x and y values associated with the chimney and the pond are being scaled in relation to our centre point of focus (the front door); anything in a negative region will become more negative, and vice versa.
This scaling is achieved by using a combination of the Z value (how close we are to the house) and our field of view.
There is no Field of View in Flash!?
In mapping 3D to a 2D screen and giving the effect of perspective, we scale the amount of impact the Z position of the object has on its x and y coordinates. We do it using the field of view value as a modifier because it’s our real-world field of view that determines the amount of perspective distortion that we see with our eyes.
However, there is no such thing as field of view in Flash, so we set up an artificial field of view value which can be adjusted to achieve the right balance of perspective distortion for the 3D scene.
If the field of view is really small (such as 1), a Z value of 100 would have a great impact on the scale of the x and y values, creating massive visual distortion. Conversely, setting an extremely high field of view will diminish the significant effect of scaling that the Z value has on the x and y, creating a flattened image with next to no perspective at all.
————————————————————————————————–
This is most easily understood by thinking that the field of view diminishes the significance of the Z value; the higher the field of view, the less impact the Z value has in scaling the x and y values.
————————————————————————————————–
A standard value for a natural looking scene is around 300 using the formula described below.
2D to 3D Formula
scaleRatio = fieldOfView/(fieldOfView + Z);
This is the formula, but let’s break it down so that we understand it
//=====[ with a low field of view ]======//
scaleRatio = 10/(10+100)
scaleRatio = 0.09, creating massive distortion when applied to x and y
//=====[ with a high field of view ]=====//
scaleRatio = 10000/(10000+100)
scaleRatio = 0.9, which is very close to 1, creating minor distortion
————————————————————————————————–
Code:
// a natural-ish value for fieldOfView, play around with it
var fieldOfView = 300;
// calculate scaleRatio using a stored Z value for an object
var scaleRatio= fieldOfView/(fieldOfView + zPos3D);
// apply the scaling on the x and y in relation to the centrePoint
var xPos2D = (centrePointX + xPos3D) * scaleRatio;
var yPos2D = (centrePointY + yPos3D) * scaleRatio;
// position the object in 2D space
mc.x = xPos2D;
mc.y = yPos2D;
————————————————————————————————–
Of course, you still have to move these 3D coordinate values around, updating the 2D values on each frame, but this represents the building blocks of rendering 3D in Flash!
Woohoo… Thanks to Seb, Alan and Kirupa.com 😀
3 Comments
Rockytastic
Yo man. You seem pretty cool.
That being said, I must disagree with your analysis of 3d space! Particularly with the mention of distortion.
I’m sad to say I can’t show you a counterexample, but I have programmed a 3d engine before. If you program your engine realistically, taking all the x, y and z positions of objects into account and translating them into angles relative to a single line stemming from the perspective of the player, you get automatic distortion! How obvious that distortion may be is dependent on the focal length you set (the wider the more distorted), but it always exists if the engine itself is portraying 3d accurately.
I never got into warping the actual objects though, just moving their positions and giving them atmospheric fog as they went far away. Since then I’ve learned to work that kind of stuff, but it was back when I was still just figuring out a lot of flash, so the engine ended up being kinda confusing to develop with, which meant I wasn’t going to make any games with it. It’s too bad, I was so excited about having a 3d engine when no one else seemed to be bothering with it. . . . I still hope to make another one with better design qualities and actually use it for something. . . . It’s still a relatively unpracticed operation in the flash scene.
Anyway, I didn’t mean to show up and try to tell you what’s what, but figured we’re after some of the same stuff so I’d let you know some of my own discoveries. I’ve been into flash for a while, but just recently got serious about taking part in the gaming scene. I’d like to see a lot of things change.
Please, check out my new blog at http://www.sokay.net/blog
I’d love to hear what you think! I’m afraid it might come off as kind of pretentious sometimes, but I’d really like to offer a new kind of respect for games and their development. I don’t like how we’re being viewed, even by some of our own kind.
James @ FuturLab
hello!
I’m just a n00b to 3D myself, but the illusion of 3D in flash is this easy to achieve, so that’s good enough for me :p
I’ll check out your blog for sure!
Thanks for the comment,
J
Rockytastic
Oh, I just remembered! I did make a game engine that operates similarly to your 3d. I didn’t want to spend time going all the way with the 3d aspect, but I wanted to make a “silent scope” kind of sniper game so I used the same “z linked to y” shortcut.
I still like the ideas I came up with for it, but I never built it into a game! Maybe I’ll do that. . . .