0

I am trying to "fake 3D" in a game in WPF. Think of a road, and that the objects appear somewhere in the distant. As they get closer, they look bigger, and eventually they grow in size very fast.

I'm thinking that when the object appears, it's close to 0 in width and height. As it moves towards the player, it becomes closer to hundred percent of its true size.

I think I will need to solve this using logarithmic 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 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.

I can write the method inputs and outputs myself, but need help with the calculation. Thanks!

petter
  • 53
  • 9
  • IMO this is more of a math question than a programming one. –  Mar 12 '18 at 18:40
  • I would argue that there is no question here at all...([Why is "Can someone help me?" not an actual question?](https://stackoverflow.com/questions/4036812/c-sharp-lazy-loaded-automatic-properties)) – Rufus L Mar 12 '18 at 18:49

2 Answers2

1

I am not entirely sure about the use of log here. This is a simple geometry problem.

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

Axeon Thra
  • 302
  • 3
  • 12
  • Thanks but this does not do what I was looking for really. See my own answer below for the code. (Basically, what I'm doing is using an exponential growth.) – petter Mar 13 '18 at 09:41
1

Just in case someone else is looking at this question in the future, here's the correct reply (I figured it out myself):

    /// <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);

            }
        }
    }

The method could be worked on further, such as allowing randomized power-to values (say from 1.5 to 4.5). Note that the higher the exponential value, the greater the effect.

petter
  • 53
  • 9