Created
January 20, 2017 01:24
-
-
Save ludo6577/60815377e5c82d5c7aca94ef819835f5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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