Skip to content

Instantly share code, notes, and snippets.

@ludo6577
Created January 20, 2017 01:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ludo6577/60815377e5c82d5c7aca94ef819835f5 to your computer and use it in GitHub Desktop.
Save ludo6577/60815377e5c82d5c7aca94ef819835f5 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
public class CalibrationScript : MonoBehaviour
{
public Transform P1; //Physic
public Transform P2; //Physic
public Transform P3; //Physic
public Transform V1; //Virtual
public Transform V2; //Virtual
public Transform V3; //Virtual
// Use this for initialization
void Start () {
StartCoroutine(Calibrate());
}
IEnumerator Calibrate()
{
yield return new WaitForSeconds(1);
// 1) Remove all rotation (we will add it manually later)
transform.rotation = Quaternion.identity;
yield return new WaitForSeconds(0.5f);
// 2) translate the Chaperon in a vector that begin at the virtual point to the physical point
var p1 = P1.position;
var p2 = P2.position;
var p3 = P3.position;
var v1 = V1.position;
var v2 = V2.position;
var v3 = V3.position;
var translationVector = new Vector3(p1.x - v1.x, p1.y - v1.y, p1.z - v1.z);
transform.Translate(translationVector, Space.World);
yield return new WaitForSeconds(0.5f);
// 3) Rotate the Chaperon so v2 and p2 touch each other (p1 et v1 already touch so we use this position as the center of rotation)
// Perpendicular vector calculated by cross. See: https://docs.unity3d.com/Manual/ComputingNormalPerpendicularVector.html
var rotationVector1 = new Vector3(p1.x - p2.x, p1.y - p2.y, p1.z - p2.z);
var rotationVector2 = new Vector3(p1.x - v2.x, p1.y - v2.y, p1.z - v2.z);
// Precision problem?
var parentCenter = createParent(p1);
parentCenter.transform.rotation = Quaternion.FromToRotation(-rotationVector1, rotationVector2);
parentCenter.transform.rotation = Quaternion.FromToRotation(p1 - p2, -p1 - v2);
//StartCoroutine(TestSomething(p1, cross, angle));
//transform.RotateAround(parentCenter.transform.position, cross, angle.magnitude);
//parentCenter.transform.rotation = Quaternion.FromToRotation(p1, rotationVector1, rotationVector2);
//var cross = Vector3.Cross(rotationVector1, rotationVector2);
//var angle = Quaternion.ToEulerAngles(Quaternion.FromToRotation(rotationVector1, rotationVector2));
//transform.RotateAround(cross, angle.magnitude);
//StartCoroutine(TestSomething(p1, rotationVector1, -rotationVector2));
yield return new WaitForSeconds(1);
//yield return new WaitForSeconds(0.5f);
//var go = createParent(transform.position);
//yield return new WaitForSeconds(0.5f);
//go.transform.rotation = Quaternion.FromToRotation(rotationVector1, rotationVector2);
//yield return new WaitForSeconds(0.5f);
//removeLastParent();
//yield return new WaitForSeconds(0.5f);
}
IEnumerator TestSomething(Vector3 center, Vector3 cross, Vector3 angle)
{
var parentCenter = createParent(center);
while (true)
{
parentCenter.transform.RotateAround(cross, angle.magnitude);
//parentCenter.transform.rotation = Quaternion.FromToRotation(rotationVector1, rotationVector2);
//transform.RotateAround(parentCenter.transform.position, cross, magnitude);
yield return new WaitForSeconds(0.5f);
}
}
private GameObject parentGameObject;
private GameObject createParent(Vector3 position)
{
parentGameObject = new GameObject("Center of rotation");
parentGameObject.transform.position = position;
transform.parent = parentGameObject.transform;
return parentGameObject;
}
private void removeLastParent()
{
transform.parent = parentGameObject.transform.parent;
GameObject.Destroy(parentGameObject);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment