Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Dylan Cristy · Jan 14, 2011 at 10:39 PM · vectormagnitude

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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;

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dylan Cristy · Jan 15, 2011 at 12:30 AM 0
Share

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.

avatar image
1

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 ;)

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dylan Cristy · Jan 15, 2011 at 12:32 AM 0
Share

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.

avatar image Bunny83 · Jan 15, 2011 at 01:30 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

[MATH] Vectors, magnitudes, and bears; Oh My! -Solved 2 Answers

Working out a reversed magnitude of a vector without affecting the angle 2 Answers

Does unity implement a way to convert an angle+magnitude representation of a vector to an x,y vector? 1 Answer

change the length of the vector 1 Answer

Good old trig and NaN 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges