• 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 Dot45 · Mar 05, 2012 at 01:16 PM · rotationmeshangle

Rotate point around a point with angle

I want to rotate vertex point (blue point) to the new position around the second position with an angle between first and second position in order to make mesh looks better.

how can i do that?? i use

 private Vector3 MoveAroundPointWithAngle(Vector3 wantedP, Vector3 aroundP, float angle)    
 {
         
   float newX = Mathf.Cos(angle)*(wantedP.x-aroundP.x) - Mathf.Sin(angle) * (wantedP.y-aroundP.y) + aroundP.y;

   float newY = Mathf.Sin(angle)*(wantedP.x-aroundP.x) - Mathf.Cos(angle) * (wantedP.y-aroundP.y) + aroundP.y;

   float newZ = wantedP.z;
 
   return new Vector3(wantedP.x+newX,wantedP.y+newY,newZ);
 }

the new point i want vertex point to move is unknown

Vector3 wantedP is original vertex point and Vector3 aroundP is the second position.

alt text

125alt text125

Comment
Add comment · Show 1
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 Dot45 · Mar 05, 2012 at 01:25 PM 0
Share

the function i made doesn't work well, the result is very weird.

4 Replies

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

Answer by Dot45 · Mar 14, 2012 at 10:17 AM

ok , with my friend help, i can make it correctly now.

Vector3 oldDirection is (second position - first position) and angle as degree

"Rotate vector around a point with angle"


 private Vector3 RotateVector2D(Vector3 oldDirection, float angle)   
 {
         float newX = Mathf.Cos(angle*Mathf.Deg2Rad) * (oldDirection.x) - Mathf.Sin(angle*Mathf.Deg2Rad) * (oldDirection.y);   
         float newY = Mathf.Sin(angle*Mathf.Deg2Rad) * (oldDirection.x) + Mathf.Cos(angle*Mathf.Deg2Rad) * (oldDirection.y);        
         float newZ = oldDirection.z;    
         return new Vector3(newX, newY, newZ);   
 }
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
avatar image
0

Answer by jakovd · Mar 05, 2012 at 03:01 PM

Quaternions are the way to go when you need something done that includes vectors and rotations. Check em out in script reference:

http://unity3d.com/support/documentation/ScriptReference/Quaternion.html

You may find it difficult to understand how quaternions work internally. Just use their methods straight-forward, don't bother yourself with details. :)

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
avatar image
0

Answer by Owen-Reynolds · Mar 05, 2012 at 03:31 PM

You've got a flipped sign in the equation. The standard rotation matrix is:

 cos -sin
 sin  cos

You flipped the bottom right to a negative. But, jakovd's advice is good -- often easier, and a just a tiny bit CPU slower, to say Quaternion R = Quaternion.LookRotation(wantP-aroundP); and then use it: p2=R*p2;, which "applies" the rotation to a point.

Comment
Add comment · Show 6 · 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 Dot45 · Mar 06, 2012 at 02:43 AM 0
Share

i cannot multiply Quaternion and Vector3

ps. sorry for stupid question but i'm ready suck for $$anonymous$$athematics.

avatar image Dot45 · Mar 06, 2012 at 02:50 AM 0
Share

i use

private Vector3 $$anonymous$$oveAroundPointWithAngle(Vector3 wantedP, Vector3 aroundP, float angle)

 {
        Quaternion r = Quaternion.LookRotation(wantedP-aroundP);
        Vector3 newP = wantedP * r;
        return newP;

 }

it said that "*" cannot be applied with Vector3 and Quanternion

avatar image Dot45 · Mar 06, 2012 at 03:29 AM 0
Share

i think there is $$anonymous$$understanding

the new point i want vertex point to move is unknown

Vector3 wantedP is original vertex point and Vector3 aroundP is the second position.

avatar image Dot45 · Mar 06, 2012 at 03:35 AM 0
Share

this is the result, very strange

http://img141.imageshack.us/img141/9591/ice2l.jpg

avatar image Owen-Reynolds · Mar 06, 2012 at 04:22 AM 0
Share

Just look up Quaternion in the script reference. You have to put the quaternion first: Q*V3 but not V3*Q.

$$anonymous$$y example of using Quats is if you know where the orange dots are, and want to use that angle to rotate the blue ones.

Show more comments
avatar image
0

Answer by jakovd · Mar 06, 2012 at 08:10 AM

@sakamotomiwa could you explain why are you trying to rotate these vertices through the script? Maybe you are facing the problem that would be much easier to solve using some 3D modeling tool... I have no clue what is it that you are showing us with your pictures, mate :)

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 Owen-Reynolds · Mar 06, 2012 at 02:18 PM 1
Share

It looks like a line-renderer mesh. He's got a series of points (the orange dots) and is trying to figure out where the corners of the created mesh (blue dots) should go.

The problem with the built-in line-renderer is the quads always tilt to face the camera, which is no good for something like a river. His approach does work (I've seen finished versions.)

avatar image Dot45 · Mar 07, 2012 at 03:20 PM 0
Share

the mesh that i created will follow mouse position like line-renderer but the different is i created mesh from scratch. Now mesh only go forward not follow go up or down direction. the line mesh acts as slider plane so the character can slide along the path that player created.

I have used box prefab created it and rotate with the angle between 2 positions it works but its collider is not smooth and continually so i change to this method in order to get seamless collider which connect together

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Limit rotation angle on Z axis while slerp 1 Answer

Natural rotation of sun and moon with 2 axis 2 Answers

How to rotate an object to a specific angle with angularVelocity? 0 Answers

How to rotate a mesh collider? 0 Answers

Problems caused by non-unique Euler angle solutions 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