GML Objects decrease in scale based on y position

Warspite2

Member
I am working on a concept where I want to randomly place trees based on room width and height. So what I am trying to do is decrease the scale of the trees that are placed higher up to give the scene more depth. Bascially I am wanting to scale the trees based on their y position, towards the bottom trees will be closer so they should increase in scale where is towards the top they are further away so should scale down. Any idea on how I can accomplish this?
 

TheouAegis

Member
Will this be dynamic or static? In other words, are you going to have a view and you want the trees to scale relative to the view, or are you not using a view at all and want the trees to remain the same scale throughout the entirety of the game?

scale = m * ( ( y - topmostY ) + n) / ( ( h - topmostY ) + n)

m
is the maximum scale. Leave it out to default to a maximum scale of 1.
topmostY is either 0 (for static trees) or view_yview (GMS1) or camera_get_view_y (GMS2).
h is the height of either room_height, view_hview (GMS1) or camera_get_view_height (GMS2).
n is an arbitrary height which you use to shift the minimum scale. If n==h, then the minimum scale will be 2/4 and the median scale will be 3/4; if n==h/2, then minimum scale will be 1/3 and the median scale will be 2/3.
 

rIKmAN

Member
I posted a reply to a thread years ago which might be useful for you, it was for scaling shadows based on y position but would be the same process as scaling your trees.

You can use clamp to lock the scale instead of the 2 checks in the code in that post.
 
Last edited:

Warspite2

Member
Thanks for the help guys! Also im not using any views and it's only static trees. I tried this in a create for the trees and it works but the problem I was having was it didn't have much impact since it was using only 1/3 of the screen. The image_blend I used to darken the trees as they are placed further back.

var treedist1 = y/100;
var treedist2 = treedist1/5;
image_xscale = treedist2;
image_yscale = treedist2;
image_blend = make_color_hsv(200,40,y);
depth=-y;

I will try some of what you all suggest and I may get better results.

Edit: I just tried this in create...

var scale = y/720;
image_xscale = scale;
image_yscale = scale;

It works just as my above code although much shorter so I may just go ahead and roll with this. Thanks again!
 
Last edited:

TheouAegis

Member
Thanks for the help guys! Also im not using any views and it's only static trees. I tried this in a create for the trees and it works but the problem I was having was it didn't have much impact since it was using only 1/3 of the screen. The image_blend I used to darken the trees as they are placed further back.

var treedist1 = y/100;
var treedist2 = treedist1/5;
image_xscale = treedist2;
image_yscale = treedist2;
image_blend = make_color_hsv(200,40,y);
depth=-y;

I will try some of what you all suggest and I may get better results.

Edit: I just tried this in create...

var scale = y/720;
image_xscale = scale;
image_yscale = scale;

It works just as my above code although much shorter so I may just go ahead and roll with this. Thanks again!
What is the height of your room?
 

TheouAegis

Member
If 512 is your highest tree y, use that as the max height.

Just remember y/512 results in disappearing trees if y is too low. That's why I had the n in my formula. So y/512 results in disappearing trees at y:0, but (y+256)/(512+256) is a scale of 1/3 rather than 0.

Also, if your trees don't go all the way to the top of the room, then instead of y, it could be (y-min) where min is the lowest y occupied by a tree.
 
Top