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 /
  • Help Room /
avatar image
2
Question by AndrewRyan · Jun 15, 2016 at 07:15 PM · 2dmathcirclevectorscalculations

How to calculate vector perpendicular to a direction vector?

Hello,

I am trying to get the direction of the vector perpendicular to the direction vector of 2 points(A,B) illustrated below. I understand a cross product requires a reference vector like transform.right to work, but I get weird behavior when the direction(dir) vector is equal to the reference vector transform.right: the rigidbody2d I am moving with it just wobbles back and forth.

P.S. using the rigidbodies transform.right would be good, but it's always rotating, so that can't work.

Any physics-related suggestions are appreciated!

     void Rotate(Rigidbody2D rb)
     {
         Vector3 dir = rb.transform.position - transform.position;
         dir.Normalize();
         Vector3 side = Vector3.Cross(dir, -transform.right);
         Vector3 cross = Vector3.Cross(dir, side);
         rb.AddForce(cross.normalized * speed);
     }

alt text

untitled-1.png (33.1 kB)
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
6

Answer by Winterblood · Jun 15, 2016 at 07:46 PM

If it's in 2D you can just swap the components and negate one:

 blueVector.x = redVector.y;
 blueVector.y = -redVector.x;

(You'll probably then want to normalise it so it's a consistent magnitude)

FWIW, the cross product will give you a vector perpendicular to both the input vectors, so it's useless in 2D.

[EDIT] BTW, if you keep adding that tangential velocity it will spiral rapidly outwards, even with damping. I'm guessing you might want it to orbit, in which case you'll need a "gravity" force to stop it flying off.

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 AndrewRyan · Jun 15, 2016 at 08:57 PM 0
Share

Thanks, WinterBlood. Are you effectively rotating the vector by doing that- i'm not sure? If not, I managed to fix it by using Quaternion.AngleAxis to rotate the direction vector 90 degrees.

avatar image NoseKills · Jun 15, 2016 at 09:47 PM 0
Share

That's the same as cross product basically.

the cross product will give you a vector perpendicular to both the input vectors, so it's useless in 2D.

Awww. you were so close :) this "problem" is in fact the solution.

Cross product works perfectly for 2D if you just use Vector3.forward (or 'back') as the reference vector (it can never have the same direction as your other 2D vector so you don't have that problem).

The 2 vectors you input to get a cross product define the plane to which the result is a normal for. If the 2 input vectors are the same or one of them is zero, they don't define a plane so there is no normal either.

P.S. I don't want to encourage anyone to prematurely optimize working code, but cross product is probably 20 times faster than quaternions for this performance wise

avatar image
2

Answer by unity_ioeaHQCQ0I7G2A · Sep 03, 2019 at 05:56 AM

In 2D: direction vector = (x,y) => normal vector = (-y,x) or (y,-x)

In 3D: direction vector = (x,y,z) => normal vector (in plane y, y stay) = (-z,y,x) or (z,y,-x)

Comment
Add comment · 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

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

88 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 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 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

Draw simple 2D shapes like circles, rectangles, paths etc... 0 Answers

Best way to detect mouse inside radius of player? 0 Answers

Need help with calculating time required to pass a set distance. 0 Answers

How do you draw 2D circles and primitives 5 Answers

Angle between two vectors 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