- Home /
Is it possible to clamp a Vector magnitude upwards (i.e. clamp to minLength)?
Hi everyone,
Ok, here is the scenario: I am generating "ripples" at mouse click points in order to exert force on an object and move it around. I want to set it up so that if the ripple is generated close to the object (small radius when the ripple hits the object), more force is applied, and if the ripple hits the object close to the ripple's maximum outer radius, only a small force is applied.
The way I'm trying to do it is to generate a vector from the ripple's center to the object's center, generate a copy of that vector (to determine the correct direction) with the maximum magnitude possible, subtract the original vector from the maximum vector, and apply the difference. That way, if the ripple center and object are close together, the resulting vector will be large, and a large force applied, and if the ripple center and object are far apart, the resulting vector will be small, and a small force applied.
However, Vector2.ClampMagnitude
only clamps down if the magnitude is greater than the maximum specified, and my original vectors will always be smaller than that. So I need a way to clamp the magnitude upwards to a minimum.
Here's my code so far:
function OnTriggerEnter (bubble : Collider) {
Debug.Log("Hit " + bubble.gameObject.name);
var bubbleVect : Vector2 = bubble.transform.position;
var thisVect : Vector2 = transform.position;
var originalVect : Vector2 = bubbleVect - thisVect;
// ideally the next line would create a vector in the exact direction
// of originalVect but with magnitude maxVectLength
var maxVect : Vector2 = Vector2.ClampMagnitude(originalVect, maxVectLength);
var push : Vector2 = maxVect - originalVect;
bubble.SendMessage("RippleMove", push);
}
Any suggestions?
ETA:
Thanks for the answers. In the end I went with something along the lines of Peter G's answer:
var originalVect : Vector2 = bubbleVect - thisVect; // find the direction
var maxVect : Vector2 = Vector2.Scale(originalVect, Vector2(1000, 1000)); // create a copy that is outrageously huge, big enough to have a magnitude // greater than maxVectLength even if the originalVect is tiny, like 0.1
maxVect = Vector2.ClampMagnitude(maxVect, maxVectLength); // clamp the huge vector down to what I want
var push : Vector2 = maxVect - originalVect; // calculate the difference
Answer by Peter G · Jan 14, 2011 at 11:03 PM
I don't really understand how your doing this in a trigger, but as far as the Vector math goes, can you just normalize the Vector then scale it to the right length?
var originalVect : Vector2 = bubbleVect - thisVect; //I assume you are finding the direction.
var maxVect : Vector2 = originalVect.normalized; //Create a normalized copy of your original Vector.
maxVect * maxVectLength; //Scale it to the max possible length, I don't know how you are determining //this though.
var push = maxVect - originalVect; //This should give you the max minus the original.
And I would like to point out you are casting to a Vector2 implicitly when you get bubbleVect and thisVect even though you will lose data, its fine in javascript, but anyone who needs this in C# will need a cast those fields i.e. Vector2 thisVect = (Vector2)transform.position;
Thanks Peter. I ended up going with something slightly different. When I tried your solution, it seemed to work well, until I noticed that in cases where the ripple was hitting near it's maximum radius (and the resulting force vector should be very small), the resulting force vector was actually negative of what it should be. It was small, but the bubble object moved TOWARDS the center of the ripple rather than away from it. I don't know why.
Oh, btw - I am setting maxVectLength manually, that is how I am controlling what I want the maximum force possible to be.
Answer by Bunny83 · Jan 14, 2011 at 11:44 PM
It's pure mathematics ;)
I would work in 1D (only one dimention) just with the length, because that's the only information needed to calculate the min and max.
For best flexibility i would define 4 constants:
var minRadius : float = 1.0f;
var maxRadius : float = 10.0f;
var minForce : float = 10.0f;
var maxForce : float = 0.0f;
with these do the following:
var originalVect : Vector2 = bubbleVect - thisVect;
var vLength : float = originalVect.Length;
// That's the normalized distance within min and max radius. var nRange : float = Mathf.Clamp01((vLength-minRadius) / (maxRadius-minRadius));
var PushForce : float = nRange*(maxForce-minForce) + minForce;
var PushVector : Vector2 = originalVect.normalized * PushForce;
minForce is the force at minRadius or lower and maxForce is the force at maxRadius or greater
ps. haven't been checked for syntax errors since I'm a C# user in the first place ;)
Thanks Bunny... I realize you are giving me exactly what I asked for (a way to clamp up), but it's taking me a bit to wrap my head around what exactly is going on there. I found another solution that is easy and works well enough for what I need it to do.
never$$anonymous$$d ;) if you're happy and found a solution that's great. $$anonymous$$aybe the other day somebody needs something similar and he can find this one.
Your answer
![](https://koobas.hobune.stream/wayback/20220613095515im_/https://answers.unity.com/themes/thub/images/avi.jpg)