I basically have a rubber band you can pull back on the screen. I want it so the further you pull away, the more tension the rubber band gets. In other words, when I initially pull back the rubber band, the band will stay exactly where the mouse curser is, but as I gradually pull back, the band wont move back as much as the mouse is.
The values are between 1 and 10, 1 being no tension, and 10 being max. So when you’re near the 10 value, the further you will need to pull back the mouse to get it to the max value.
I have these variables
var currentTension : int = 1;
var maxTension : int = 10;
var actualTension : float = 1;
The currentTension is calculated by the position of the first initial click of the mouse, take away the current position - which gives the distance, capped by the maxTension.
I then want to apply the real tension effect the the actualTension.
Example:
See how the gaps between the values closer to 10 are larger? Meaning that the transition between, say 8 and 9 are larger. How would I calculate that?
Maybe use a lerp value? Vector3.Lerp(original.position, positionOfMouse.position, factor); This way you only need to figure out a good way to calculate a factor ranging from 1-0.5 maybe, witch would tell the lerp function how much it should lerp towards the mouse position in scene.
I don't know if that is what I'm looking for. I get a good result when I do this: (currentTension * currentTension) / maxTension; Although the result this give is the opposite that I want, tension is strong to begin with, then gets easier as I pull back.
One way to do this would be to use an easing function, maybe quadratic. These are good for when you have a known range that needs to follow a curve instead of a linear progression.
Another way, probably more straight forward, is to just find a formula that scales the way you want. For example N factorial might yield the result you want. This is useful when you want the progression to be able to continue to infinity.
But if you want a system where the amount of movement decreases as you increase distance, something like the following would give you some results:
// Let's assume aligned to X axis for simplicity...
float stretchWidth = (currentMousePositionIn3D.transform.position.x - startingPosition.transform.position.x) + 1; //Mouse position via ScreenToWorldPoint maybe
float tensionCoefficient = .028f; // this defines how resistant to stretch this is
float newWidth = stretchWidth * (1/(stretchWidth * Math.Pow(tensionCoefficient)));
endPoint.transform.position = new Vector3(startingPosition.transform.position.x + newWidth, startingPosition.transform.position.y, startingPosition.transform.position.z);
You could also (if you want it arbitrarily stretchable on any axis) measure all these through Vector3.magnitude and do multiplication on a unit vector if you want.
How would I apply this to my variables that I have? The currentTension is just the distance from the start of the rubber band position, and the current mouse position.
Thanks guys - I wanted to go more down the mathematical route, so I stared to play with a load of formulas. So this is a break down of how I did it:
So firstly, my range is between 0, 7 (was between 1, 10 but I changed it). 0 being minimum value, and 7 being the max. I needed to change this range so it was between 0 and 1:
var clampDistance = (distance / 7);
I can then use this formula (out of many) to make an ease type:
Maybe use a lerp value? Vector3.Lerp(original.position, positionOfMouse.position, factor); This way you only need to figure out a good way to calculate a factor ranging from 1-0.5 maybe, witch would tell the lerp function how much it should lerp towards the mouse position in scene.
– gajdotI don't know if that is what I'm looking for. I get a good result when I do this: (currentTension * currentTension) / maxTension; Although the result this give is the opposite that I want, tension is strong to begin with, then gets easier as I pull back.
– oliver-jones