x


Rotate Point A on Sphere to Point B on Sphere

Imagine a globe (sphere). I have Point A (Miami FL) and Point B (Portland OR). I would like to rotate the globe so that:

  1. The position of the globe and main camera do not move
  2. Point A ends up where Point B was
  3. The globe rotates along the an axis that runs through the center of the sphere and is normal to the plane created by Point A, Point B, and the center of the sphere.

I have been using Quaternion.FromToRotation but I have not been able to come up with a good solution.

Any suggestions?

Thanks, Mike

Edit:

Vector3 pointA;
Vector3 pointB;
GameObject sphere;

//What should I put here?

sphere.transform.rotation = resultRotation
more ▼

asked Jul 09, 2010 at 07:43 PM

Mike Miller gravatar image

Mike Miller
4 11 10 15

(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

I hope i understand it right. If so i had a similar problem yesterday.

I used this:

Quaternion rotation = Quaternion.LookRotation(targetVector);
Vector3 point = new Vector(x,y,z);
point = rotation  * point ;

Also instead of using a Vector you rotate it should work if you use the rotation on a transform.rotation of a GameObject.

Edit:

So i played around a little bit. I added one sphere in the middle of my scene, and two cubes.

    // the cubes
    public GameObject pointA;
    public GameObject pointB;
    // the sphere
    public GameObject globe;

    Quaternion rotationA;
    Quaternion rotationB;

void Start()
{
     // get the start rotation of the points
     rotationA = Quaternion.LookRotation(pointA.transform.position);
     rotationB = Quaternion.LookRotation(pointB.transform.position);
}

void Update()
{
    // here the sphere rotate from point a to b
    // to make the cubes follow the sphere, you have to calculat there position, 
    // or put the gameobjects from the cbes inside the sphere
    globe.transform.rotation = Quaternion.Slerp(rotationA, rotationB, Time.time * 0.1f);    
}

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

was the function which helped me, it worked for me but im not sure if this is the best way to do it

more ▼

answered Jul 09, 2010 at 07:49 PM

Case23 gravatar image

Case23
471 48 38 50

I am not quite sure how to implement your suggestion. The output seems to be a point and I think I am looking for a rotation.

Jul 09, 2010 at 08:06 PM Mike Miller
(comments are locked)
10|3000 characters needed characters left

I think you are on the right track with FromToRotation.

for the following to work your sphere would have to be at global (0,0,0)

Quaternion rot = Quaternion.FromToRotation (pointA, pointB);
sphere.transform.rotation *= rot; //Multiply rotations to get the resultant rotation.

if your sphere is not at global (0,0,0) you need to make sure that pointA and pointB relative to the sphere coord system and then use the following:

Quaternion rot = Quaternion.FromToRotation (pointA, pointB);
sphere.transform.localRotation *= rot; //Multiply rotations to get the resultant rotation.
more ▼

answered Jul 09, 2010 at 11:11 PM

cncguy gravatar image

cncguy
1.1k 57 45 59

Thank you. This worked.

Jul 13, 2010 at 09:42 PM Mike Miller

I posted the full code I used below.

Jul 13, 2010 at 09:52 PM Mike Miller
(comments are locked)
10|3000 characters needed characters left

cnc guy helped answer my question. This is the code I ended up with. It allows a user to grab a point on a sphere and rotate the sphere from that point.

using UnityEngine; 
using System.Collections; 

public class EarthTouchMovement : MonoBehaviour { 


    bool hasGrabbedPoint = false;
    Vector3 grabbedPoint;

   void Update () 
   { 
      if (Input.GetMouseButton(0)) 
      {         
        if(!hasGrabbedPoint)
        {
            hasGrabbedPoint = true;                                             
            grabbedPoint = getTouchedPoint();
        }
        else
        {
            Vector3 targetPoint = getTouchedPoint();
            Quaternion rot = Quaternion.FromToRotation (grabbedPoint, targetPoint);
            transform.localRotation *= rot;             
        }
      }
      else
        hasGrabbedPoint = false;      
   }

   Vector3 getTouchedPoint()
   {
        RaycastHit hit;
        Physics.Raycast (Camera.main.ScreenPointToRay(Input.mousePosition), out hit);

        return transform.InverseTransformPoint(hit.point);
   }
}
more ▼

answered Jul 13, 2010 at 09:46 PM

Mike Miller gravatar image

Mike Miller
4 11 10 15

(comments are locked)
10|3000 characters needed characters left

Is there a simple way to modify this solution to constrain the globe to stay right side up? Meaning, I want to keep the North Pole at the top instead of it ending up getting spun on its side. I guess it would spin on it's Y and X axis but not Z. Thanks.

more ▼

answered Oct 11, 2013 at 02:28 PM

TotallyAtRandom gravatar image

TotallyAtRandom
0

Oct 11, 2013 at 03:33 PM Marsupilami
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3509
x329

asked: Jul 09, 2010 at 07:43 PM

Seen: 6025 times

Last Updated: Oct 11, 2013 at 03:33 PM