Tutorial & Videos

The perspective formula

The idea is that as something moves farther away (z increases), its scale approaches 0 and its x, y position converges on the 0, 0 of the vanishing point. Because the ratio of distance to scale is the same as the ratio of distance to convergence, you need to figure out what that ratio is for a given distance and use it in both places. The diagram in Figure 15-3 helps to explain this concept.

Figure 15-3. Perspective seen from the sideHere, you have an object off in the distance, a viewpoint we look from (the camera), and a picture plane, which we look through to see the scene. You have the distance of the object to the picture plane, which is the z value, and, you have the distance from the viewpoint to the picture plane. This last one is similar to the focal length of a camera lens, so we use the variable fl to represent it. A long focal length can be compared to a telephoto lens on a camera, with a narrow view that compresses the distance between objects. A short focal length is like a wide-angle lens, where you see a lot of the scene, but with a lot of distortion. A medium focal length approximates what the human eye sees, and uses a value for fl that’s between 200 and 300. Here’s the perspective formula:

scale = fl / (fl + z)
This usually yields a number between 0.0 and 1.0, which is your ratio for scaling and converging on the vanishing point. However, as z approaches -fl, (fl + z) approaches 0 and scale approaches infinity. This is the coding equivalent to being poked in the eye. What do you do with this scale value? Well, you can adjust the scale of the canvas context before drawing the object. For example, in the Ball class we’ve used throughout the book, there are scaleX and scaleY properties that are referenced in its draw method, like so:
context.scale(this.scaleX, this.scaleY);
Once the scale as been determined, you multiply the object’s x and y position by this factor to find its screen x and y position. Let’s look at an example where we use 250 as the focal length. If z is zero—in other words, the object is exactly on the picture plane—then the scale will be 250 / (250 + 0). This comes out to exactly 1.0. That’s your scaleX and scaleY (remember that for scale, 1.0 means 100%). Multiplying 1.0 by the object’s x and y positions gives the same numbers back as a result, so the object’s screen position is exactly equal to its x and y. Now move it out so that z is 250. That makes the scale equal to 250 / (250 + 250), or 0.5 for scaleX and
scaleY. It also moves the object’s screen position. If the object were at 200, 300 on the x and y axis, its screen position would now be 100, 150—it has moved halfway to the vanishing point. (Actually, the screen position would be in relation to the vanishing point, which you see shortly.) Move z out to 9750. This makes the scale equal to 250/10000, or 0.025 for scaleX and scaleY. The object becomes just a speck that is close to the vanishing point. Now we see how to write all of this in code.

Comments on: "The perspective formula" (4)

  1. Located your webblog through Stumbleupon. You know I will be signing up to your rss.

  2. Large enthusiast on this blog, quite a few your blog posts have truly helped me out. Looking towards updates!

  3. Huge enthusiast of your page, quite a few your writes have seriously helped me out. Looking forward to improvements!

  4. Awesome information, stylish website layout, carry on the good work

Leave a comment