Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by Le-Capitaine · Dec 14, 2015 at 09:56 AM · vector3mathmagnitudenormalized

Squared vector divided by squared magnitude

That's almost certainly a laughably simple math question, but I don't have the vocabulary to Google it.

My understanding is that .normalized basically divides a vector by its magnitude, in turn obtained with a costly square root calculation. The page on .sqrMagnitude recommends avoiding the calculation altogether when possible, by using .sqrmagnitude with the square of whatever you're comparing your magnitude with.

I was wondering if that method applies to vectors. Will I get a normalised vector by squaring it, then dividing it by its squared magnitude? Does .normalized already do that?

Comment
Add comment · Show 4
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 mikelortega · Dec 14, 2015 at 11:14 AM 0
Share

Vector3.normalized exactly does that. Calculates the square root to get the magnitude and then divide.

Anyway, you sould not worry much between Vector3.sqr$$anonymous$$agnitude or Vector3.magnitude. Using .sqr$$anonymous$$agnitude or .normalized will only make a difference if you call them many many times every update.

avatar image Bonfire-Boy · Dec 14, 2015 at 12:08 PM 1
Share

There is no such thing as "squaring a vector".

avatar image Le-Capitaine Bonfire-Boy · Dec 15, 2015 at 10:10 AM 0
Share

Its components, I mean. As in

 v = new Vector3  (v.x * v.x, v.y * v.y, v.z * v.z);

Or

 v = Vector3.Scale (v, v);
avatar image Bonfire-Boy Le-Capitaine · Dec 15, 2015 at 11:22 AM 0
Share

Then, to be frank, you should say so (ie edit the question) rather than talking about "squaring a vector". $$anonymous$$aking up your own names for algebraic operations is not going to help you to get help!

But anyway, I'm afraid you're on a wild goose chase here. The "magnitude comparison" operation can be done without ever computing the magnitude, because you don't actually care what it is, all you care about it how it compares to some other value.

Normalisation is different. A normalised vector is the vector divided by its magnitude. Hence, you need the actual magnitude.

2 Replies

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

Answer by wibble82 · Dec 15, 2015 at 11:04 AM

Hey

The basic answer to the question:

I was wondering if that method applies to vectors. Will I get a normalised vector by squaring it, then dividing it by its squared magnitude? Does .normalized already do that?

Is no, you can't :)

The vector, v = new Vector3 (v.x v.x, v.y v.y, v.z * v.z) has no strong mathematical meaning - that's not how you multiply vectors together.

There are 2 ways to multiply vectors together:

  • The DOT PRODUCT, sometimes called an inner product, which gives a scalar (1 float) result. Vector3.Dot(a,b) = a.x*b.x + a.y*b.y + a.z*b.z. The squared magnitude of a vector is actually got by doing the dot product of a vector with itself. i.e. a.sqrMagnitude = a.x*a.x+a.y*a.y+a.z*a.z

  • The OUTER PRODUCT gives a 3x3 matrix, and is a more complex mathematical concept which I won't go into here, cos it's not too useful until you want to work out tensors or eigenvalues and stuff! :)

So:

a.sqrMagnitude = Vector3.Dot(a,a) = a.x*a.x+a.y*a.y+a.z*a.z

and

a.magntidue = Mathf.Sqrt(a.sqrMagnitude) = Mathf.Sqrt(a.x*a.x+a.y*a.y+a.z*a.z)

When people talk about avoiding the use of 'magnitude', they generally mean that if all you want to know is "is my vector less than / more than a given length", then you can do it more efficiently by looking at sqrMagnitude:

 float maxlen = 10.0f;
 if(a.magnitude < maxlen)
 {
     //something here
 }

is exactly the same as

 float maxlen = 10.0f;
 if(a.sqrMagnitude < maxlen*maxlen)
 {
     //something here
 }

Hope that clarifies it a bit :)

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 supernat · Dec 17, 2015 at 12:43 AM 0
Share

Ugh, I was hoping to never see the word eigenvalue ever again, thanks! ;) Great post though. Just don't mention vector spaces, and we'll be fine. hehe

avatar image
1

Answer by Soraphis · Dec 14, 2015 at 12:14 PM

Vector(2, 0) -> normalized = Vector(1, 0)

 Vector(a, b) -> SqrMagnitude = a*a + b*b (pythagoras)
 Vector(a, b) -> Magnitude = Sqrt(a*a + b*b) 

 Vector(a, b) -> normalized = 1/Sqrt(a*a + b*b)  * Vector(a, b) = Vector(a / Sqrt(a*a + b*b), b / Sqrt(a*a + b*b) )


if you want to check if a vectors length is ... say less then 10:

 if( v.Magnitude < 10)

is the same as:

 if( v.SqrMagnitude < 100)
Comment
Add comment · Show 3 · 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 Le-Capitaine · Dec 15, 2015 at 10:15 AM 0
Share

So, can I weasel out of the sqrt call by squaring the vector's components, then divide that by its original squared magnitude?

avatar image Bonfire-Boy Le-Capitaine · Dec 15, 2015 at 11:13 AM 1
Share

No, you can't. You can easily see this by considering a vector such as (1,2).

Squaring both elements gives you (1,4) which is a different direction to (1,2), and dividing a vector by a scalar does not change its direction. Since all you're proposing doing with these 2 vectors is dividing each of them by a scalar, there's no way that that can result in identical vectors.

avatar image supernat · Dec 17, 2015 at 12:45 AM 0
Share

Just an extra note, unrelated to the question: the Vector.normalized property computes a temporary value and returns it while the Vector.Normalize() method modifies the vector, and returns the normalized value. So use Normalize() if you need to call .normalized more than once (or assign Vector.normalized to a new variable and use it).

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

37 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Vector3 values always precalculated, or just in MonoDevelop? 1 Answer

force.normalized * magnitude 1 Answer

how to Break a vector3 line into half and Quater 2 Answers

Rotate object based on hit normal + matching camera direction 0 Answers

How to get vector X distance from point on Y angle? 4 Answers


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