How to make an inversely proportional loop?
Hey, I'm trying to write a loop which updates within a method at 1/60 fps. Basically, im trying to find out how to write a loop that says:
If x
is increasing. decrease y
.
I tried defining two variables开发者_开发问答 for x
; x1
and x2
, x1
being the original value and x2
being the altered value.
int x1,x2,y;
x1 = x2;
if (x2 > x1){ y--;}
But this obviously cannot work because every 1/60fps x1 == x2
and therefore the argument is invalid..
Thanks,
OliverEDIT:
So I have the distance of an object, which is x
;
y
;
As the distance increases, I would like the scale of the layer to decrease.
As the distance decreases, I would like the scale of the layer to increase.The practical application of this is, the distance between two objects increases, so the scale of the layer decreases thus zooming out, keeping both objects within the layers camera.
I simply dont know how to express this prog-matically.
This takes care of X increasing and decreasing. I am assuming: integers, a 1-1 linear relationship. (pseudo code) Note, lastXValue is global or static to the context.
int y; // comes from somewhere
int lastXValue = y;
function callBack(int currentXValue) {
int delta = currentXValue - lastXValue; // Note: can be negative
lastXValue = currentXValue;
y -= delta;
}
I am also making the assumption that the loop is initiated and controlled elsewhere and this function gets called from the loop.
You'll want to use a static variable to save state in between function calls. Read up on it at http://ee.hawaii.edu/~tep/EE160/Book/chap14/subsection2.1.1.6.html.
Based on your edit:
So I have the distance of an object, which is
x
; I also have the scale of the layer, which isy
;As the distance increases, I would like the scale of the layer to decrease. As the distance decreases, I would like the scale of the layer to increase.
It sounds like what you really want is just to figure out a way to calculate y
from x
. It would likely be this:
y = CONSTANT / x;
where you choose CONSTANT
depending on the exact relationship you want the two variables to have - it's the value that y
will have when x == 1
.
精彩评论