• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 634
  • Last Modified:

Rotate vector using angular velocity to match another.

I have a unit vector which I want to be able calculate the angular velocity it would need to rotate it so that it eventually faces the same direction as another unit vector.
0
codevomit
Asked:
codevomit
  • 6
  • 2
  • 2
  • +1
2 Solutions
 
TommySzalapskiCommented:
What info do you have? You just need to calculate the angle between them (using arccos(v1.x*v2.x+v1.y*v2.y)) and some intuition.
0
 
ozoCommented:
wouldn't any angular velocity work, if you allow it to rotate for a sufficient amount of time?
0
 
phoffricCommented:
For the game, given the time from start to finish, won't you want to accelerate and decelerate smootly for realism? In between the accelerations, you could rotate at constant velocity.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
codevomitAuthor Commented:
@ozo and @TommySzalapski, that would work for 2D but not for 3D, I assumed that 3D would be implied when asking this question. My bad for not stating that.
0
 
ozoCommented:
In 3D, any angular velocity vector perpendicular to the plane of the unit vectors should work.
0
 
codevomitAuthor Commented:
That sounds more like it, I'll try that out tomorrow and get back to you.
0
 
codevomitAuthor Commented:
To get the angular velocity along a plane, I was thinking of using a cross product of the two vectors in a quaternion and rotating a vector (1,1,1) with it to give me angular movement in the appropriate direction, would that work?
0
 
TommySzalapskiCommented:
I'm not sure what you mean by rotating "with it", but the cross product is a good tool. The vector that you get as a result of the cross product will be orthogonal (perpendicular) to both, so you want to use the cross product vector as the axis of rotation.
0
 
codevomitAuthor Commented:
"With it" means rotating a vector using a quaternion, pretty much what you and ozo have described. Anyway I've knocked something together in Unity that should do it, it doesn't work but I think I may of spotted my error, with assigning bot vect1 and vect2 as (1,1,1).

var rb : Transform;

function Update () {
       var vect1 : Vector3 =  Vector3.one;
       var vect2 : Vector3 =  Vector3.one;
       var c : Vector3;
       var rot: Quaternion;

       vect1 = transform.rotation * vect1;
       vect2 = rb.rotation * vect2;

       c = Vector3.Cross(vect1, vect2);

       rot.x = c.x;
       rot.y = c.y;
       rot.z = c.z;
       rot.w = Mathf.Acos(Vector3.Dot(vect1, vect2));

       vect1 = rot * vect1;

       vect1 *=400; // So we can see it;
  
       Debug.DrawLine (transform.position , transform.position+vect1, Color.red); // Should be pointing in the direction of the other transformation
 }

Open in new window

0
 
codevomitAuthor Commented:
This does what I need it to do

var rb : Transform;

function Update () {
       var vect1 : Vector3 =  Vector3.right;
       var vect2 : Vector3 =  Vector3.right;
	   var vect3 : Vector3;
       var c : Vector3;
       var rot: Quaternion;

       vect1 = transform.rotation * vect1;
       vect2 = rb.rotation * vect2;

       c = Vector3.Cross(vect1, vect2);

       rot.x = c.x;
       rot.y = c.y;
       rot.z = c.z;
       rot.w = Mathf.Acos(Vector3.Dot(vect1, vect2));

       vect3 = rot * Vector3.one;

	   vect1 *=10; // So we can see it;
       vect2 *=10; // So we can see it;
      // vect3 *=400; // So we can see it;
	   c *= 10;
	   vect3 *=2;
	   rigidbody.AddRelativeTorque(vect3);
	   
	   vect3 *=400; // So we can see it;
  
       Debug.DrawLine (transform.position , transform.position+vect3, Color.red); 
       Debug.DrawLine (transform.position , transform.position+vect1, Color.green); 
       Debug.DrawLine (rb.position , rb.position+vect2, Color.green); 
       Debug.DrawLine (transform.position , transform.position+c, Color.white); 
 }

Open in new window

0
 
codevomitAuthor Commented:
The answer gave me a good direction but was not complete, my last comment should help fill in the blanks if anybody else is looking at this.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

  • 6
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now