I am trying to "fake 3D" in a computer game. Think of a road on a flat landscape, and that the objects start to appear somewhere in the distant. As they get closer, they look bigger, and eventually they grow in size very fast. (The same effect, another example: Think of a comet moving close to planet Earth. At first, it seems if it just becomes a tad bigger, but eventually it grows insanely fast until it collides with Earth.)

In this case, the objects move slightly downwards as they seem to get nearer (as in the road example). I'm thinking that when the object first appears, it's close to 0 pixels in width and height. As it moves towards the player (i.e., it goes downwards some pixels), it becomes closer to hundred percent of its true size. (Again, at first it grows slowly, and when it is rather close to the player, it grows very quickly.)

I think I will need to solve this using logarithmic/exponential calculations, and there are several threads on that. What I would really want to do however, is to send in three values to a LogaritmicGrowth method:

  • the starting Y point
  • the Y point at which the object should appear at 100%
  • the Y point where the object is at this very moment.

Thus, what I would like to get in return is the scaling factor for the object in question. So if it's halfway between the starting point and the ending point, then perhaps 0.3 (or so) should be returned, meaning that if the true size of the object is 100 pixels with and 50 pixels height, it should be drawn at 30 pixels with and 16.7 pixels width.

I can write the method inputs and outputs myself, but need help with the formula. (I tried asking this question on Stackoverflow, but was told to ask on this forum instead.)

Also, on Stackoverflow, I got this reply, but I don't think it's really what I am after (and if it is, then I can't understand it):

Think about a point P which is D distance in front of you, which has a height Y (from your line of observation). Your screen is d distance in front of you. The intersection point of the light from P on the screen is p, which makes a height y on screen.

Then, by considering the similar triangles, one can show that:

y = (Y/D) d

Thanks for any input!

  • $\begingroup$ You might want to look into using perspective to display 3D objects in 2D frames. $\endgroup$ – hardmath Mar 12 '18 at 23:35

If you want to use an exponential function, then to determine the size of the object you should probably use an equation like this:
let $S$ be the max size of the object
let $m$ be the starting position of the object (the horizon)
let $M$ be the final position of the object (when it leaves the screen)
let $x$ be the object's current position
The current size of the object should be given as $$y = S^\frac{x-m}{M-m}$$You will probably need to play around with multipliers to increase/decrease the rate of change so that it looks realistic, but this will be a start.


Thanks both!

Actually I came to think of exponential curves are created when taking the x to the power of something. In case someone else needs a reply to this question, here's what I did (in C#):

    /// <summary>
    ///  Method that enlargens the kind of object sent in
    /// </summary>
    public void ExponentialGrowth2(string name, float startY, float endY)
        float totalDistance = endY - startY;
        float currentY = 0;

        for (int i = 0; i < Bodies.Bodylist.Count; i++)
            if (Bodies.Bodylist[i].Name.StartsWith(name)) //looks for all bodies of this type
                currentY = Bodies.Bodylist[i].PosY;
                float distance = currentY - startY + (float)Bodies.Bodylist[i].circle.Height;
                float fraction = distance / totalDistance; //such as 0.8

                Bodies.Bodylist[i].circle.Width = Bodies.Bodylist[i].OriginalWidth * Math.Pow(fraction, 3);
                Bodies.Bodylist[i].circle.Height = Bodies.Bodylist[i].OriginalHeight * Math.Pow(fraction, 3);


In other words, the width and height of the object becomes the (distance/totalDistance)^3 * the object's width. I could have used ^2 or so as well, it's just that ^3 gave a great visual effect.

Thanks again!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.