Distance to Size code?

Hi...

Just looking for a quick bit of help... probably gonna be a lot quicker to ask here than spend ages trying to brush up on my rather rusty math skills :)

So Im translating distance from an object to the objects displayed size (x and y) to build a fake 3D depth of view effect.

my current simple code is basically:

Constrain (object.size) To 4*(100-(object.distance))

which starts my object at 4x4 pixels at a distance of 100, and resizes up to a size of 400x400 pixels at a distance of 1. That works great.

But its not a true representation of how something increases/decreases in size depending on the distance. It should change a lot more slowly the further away it is, and increase faster at really close distance... so some sort of curve, as displayed in the attached image. So a use of Sine/Cosine maybe?

Im guessing someone like @Socks @Armelline or @tatiang are going to look at my relatively low level math problem with a smile or possibly disdain :) but will be able to knock out an answer in a matter of seconds... thus saving me loads of time, and educating me in the process... :)

cheers....

Comments

  • tmanntmann Member Posts: 278

    more of a log curve maybe ?

  • ArmellineArmelline Member, PRO Posts: 5,369
    edited June 2015

    I don't have time to test in creator right now, but perhaps something as simple as exp() could get the desired result?

    Edit: Or the same thing kind of thing from the other direction like @tmann says!

  • ChunkypixelsChunkypixels Member Posts: 1,114

    ok... time to look up log curves and exp() function :)

    I used to be quite good at math at school too... but sadly that was 25 years ago and most of it has disintegrated in my ageing memory...

  • SocksSocks London, UK.Member Posts: 12,822
    edited June 2015

    Here's a quick demo, move the orange distance slider at the bottom left and right, the box on the left has a non-linear relationship (the 'Desired Code') with the distance slider, the box on the right has a linear relationship (the 'Current Code').

    You really need to cover up one of the boxes with your hand to get an idea of the movement, having the other in your field of vision skews the way the one you are looking at appears to grow - both boxes grow/shrink from 0-200 pixels.

  • SocksSocks London, UK.Member Posts: 12,822

    @tmann said:
    more of a log curve maybe ?

    @Armelline said:
    I don't have time to test in creator right now, but perhaps something as simple as exp() could get the desired result?

    Edit: Or the same thing kind of thing from the other direction like tmann says!

    That works pretty well. File attached, drag the slider.

  • ChunkypixelsChunkypixels Member Posts: 1,114

    @Socks nice demo as usual

    Now to break it down and figure out what your code does :)

  • SocksSocks London, UK.Member Posts: 12,822

    All three for comparison, file attached.

    From left to right:

    sin / exp / linear

    . . . . . . . . . . . . .

  • SocksSocks London, UK.Member Posts: 12,822

    @Chunkypixels said:
    Socks nice demo as usual

    Now to break it down and figure out what your code does :)

    It just extracts a quarter of a circle (from 270° to 360°) and uses that to size the actor, so when it starts to grow it relates its size from the sin of 270° - and as it progresses through its growth the sin at first rises slowly and then gets progressively faster.

    I think @tmann and @Armelline's idea of using leg/exp is much better, and a lot more simple to adapt / adjust.

  • SocksSocks London, UK.Member Posts: 12,822

    I got them to plot out the shape of their growth paths . . .

  • SocksSocks London, UK.Member Posts: 12,822
    edited June 2015

    Actually ignore that last post (bad maths!!) This is how the progression of growth looks . . .

    sin / exp / linear

  • ArmellineArmelline Member, PRO Posts: 5,369
  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,880

    Hmmmm....

    If you are doing a physical simulation, object size is linear. The calculation is:
    size = 1/distance

    However, in a cartoons or some games, sometimes size perception is distorted to enhance an effect. In that case, you can use any nonlinear function that gives the effect you want. To me, the inverse square law is easy and gives just the right feel. (Probably because light, gravity, and electricity follow the inverse square law.) The inverse square law would be invoked like this:
    size = 1/pow(distance,2)

  • SocksSocks London, UK.Member Posts: 12,822
    edited June 2015

    @RThurman said:
    Hmmmm....

    If you are doing a physical simulation, object size is linear. // However, in a cartoons or some games, sometimes size perception is distorted to enhance an effect

    Agreed, it's linear, but the relationship between a fixed size object's distance away from you and how much of your field of view it occupies is non-linear. The obvious example being a very distant object like a 1x1 metre cube (let's say it's a mile away) moving 1 metre towards you will not appear to change size at all (the area of your field of view it occupies will only fractionally change, imperceivably so) - whereas if it were 1 metre away from you, directly at head level, it would occupy something like 1/3 of your FOV - and moving it a metre more towards you would see it fill your FOV, trippling its apparent size (area of FOV being interpreted as apparent size). So I'd argue it's a real world effect !

  • SocksSocks London, UK.Member Posts: 12,822

    @RThurman said:
    size = 1/pow(distance,2)

    That works great too, it returns the same curve as my messy manhandling of a sine function, but a lot simpler / more useable.

    sin / exp / pow

  • ArmellineArmelline Member, PRO Posts: 5,369

    Unless I'm mistaken (and I really might be right now), pow(distance,2) is just a gentler form of exp(), which is pow(x,10).

    So using pow() as @RThurman suggests is a great way of really fine-tuning the effect.

  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,880

    @Socks said:

    Agreed, it's linear, but [words....words...words]. So I'd argue it's a real world effect !

    You can't agree and disagree at the same time!

    You are arguing that a perceptual illusion (that is not real) is a "real world effect."

    But I'l agree half way and say that this perceptual illusion has a real world effect.

  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,880

    @Socks said:
    That works great too, it returns the same curve as my messy manhandling of a sine function, but a lot simpler / more useable.

    Its pretty close. And it feels right to our subjective experience. We are used to the inverse square law in the real world. For example, light and magnetism follow the inverse square law. (I think.)

    Also, the exaggeration can be enhanced simply by cubing the function (x^3). Or increasing the function by any other power you want.

  • SocksSocks London, UK.Member Posts: 12,822
    edited June 2015

    @RThurman said:
    You can't agree and disagree at the same time!

    Lol ! :smile:

    I'm not sure I explained myself very well, I mean object size is linear in that its size is not effected by its distance from the observer, but its apparent size (the area of your field of view it occupies) is non-linear with regard to distance, hope that makes better sense.

    @RThurman said:
    You are arguing that a perceptual illusion (that is not real) is a "real world effect."

    Yes ! Lol, absolutely it's a real world effect, the number of cones in your retina that are stimulated by reflected light from an object happens in a way that is non-linear with regard to an object's distance from you, the same is true of the effect on a camera's film stock or sensor (and various other real world examples) the effect is very real ! :smile:

    @RThurman said:
    But I'l agree half way and say that this perceptual illusion has a real world effect.

    When you say half way, are you able to say how this progresses to full way ? Is it in a linear fashion ? :tongue:

    tl;dr: Growing a 2D image at a linear rate is not how a real world object would appear if it were moving towards the observer.

  • SocksSocks London, UK.Member Posts: 12,822
    edited June 2015

    @RThurman said:
    I might throw the problem at C4D and see what it's got to say . . . .

    It is exponential, one obvious caveat being the focal length of the camera lens / eye - as the focal length tends toward infinity the relationship between the object's size (on your retina) becomes more linear - which sort of returns to that earlier point about a very distant object moving towards you not appearing to change size - but in real world situations (i.e. a standard set of eyes) the relationship is exponential.

  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,880

  • SocksSocks London, UK.Member Posts: 12,822

    @RThurman said:

    @Armelline said:

    :p ;)

  • ChunkypixelsChunkypixels Member Posts: 1,114

    cheers for all the input from everyone... some of it was a little above my head, but got lots of things to test and play with... so much so that I've spent most of the day playing with and learning many of the Functions people suggested...

    Heres a GIF capture showing how the code is being used.... PanzerZone 1944

    The game runs much smoother than the GIF shows, and its still a very early WIP... Ive got to tie in the overhead map positioning of the moving tanks to the fake 3D viewport, but its coming along very nicely... for a game i started yesterday as a break from my main project Im meant to be finishing... http://forums.gamesalad.com/discussion/87023/kingdoms-tri-peaks#latest

    again.. thanks for the input and help :)

Sign In or Register to comment.