void orbit() { angle += Time.deltaTime * orbitSpeed * speedScalar; Quat q = new Quat(angle, new Vector3(0, 1, 0)); //positionVector is used to set how far from the sun the object orbits Vector3 p = positionVector; Quat k = new Quat(1.0f, p); Quat newk = q * k * q.Inverse(); Vector3 newp = newk.GetAxis(); transform.position = newp; }
//function for applying the transformations to the object model public void transformModel() { applyMouseInputs(); Vector3[] TransformedVertices = new Vector3[ModelSpaceVertices.Length]; //set quaternions to rotate by Quat q1 = new Quat(pitchAng - lastpitchAng, new Vector3(1, 0, 0)); Quat q2 = new Quat(yawAng - lastyawAng, new Vector3(0, 1, 0)); Quat q3 = new Quat(rollAng - lastrollAng, new Vector3(0, 0, 1)); //rotate orientation quaternion by pitch yaw and roll quaternion representations; q = ((q * q2) * q1) * q3; for (int i = 0; i < TransformedVertices.Length; i++) { //rotate each vertex by the orientation from model space; Quat K = new Quat(ModelSpaceVertices[i]); Quat newK = q * K * q.Inverse(); Vector3 newP = newK.GetAxis(); Vector3 quatVert = newP; //scale each vertex by the scale multiplier; Matrix4by4 sMatrix = scale(scaleMul); Vector3 scaVert = sMatrix * quatVert; //translate the object so it will be a set distance ahead of the player; Matrix4by4 tMat = translate(new Vector3(0, 0, distanceAhead)); Vector4 ratVert = new Vector4(scaVert.x, scaVert.y, scaVert.z, 1); Vector4 scatVert = tMat * ratVert; //rotate the object around the origin(camera), so it is always in front of the camera; Matrix4by4 yawMatrix = yawRot(g); Matrix4by4 pitchMatrix = pitchRot(y); Matrix4by4 pyMatrix = yawMatrix * pitchMatrix; Vector3 scarotVert = pyMatrix * scatVert; //translate the object so it is correctly positioned in the world; Matrix4by4 tMatrix = translate(new Vector3(pos.x, 0, pos.z)); Vector4 tVert = new Vector4(scarotVert.x, scarotVert.y, scarotVert.z, 1); Vector4 finalVert = tMatrix * tVert; TransformedVertices[i] = finalVert; } //set mesh to the transformed vertices MeshFilter MF = GetComponent <MeshFilter>(); MF.mesh.vertices = TransformedVertices; MF.mesh.RecalculateBounds(); MF.mesh.RecalculateNormals(); //sets angles so it doesn't accumulate and increase speed over time; lastpitchAng = pitchAng; lastyawAng = yawAng; lastrollAng = rollAng; }
private static void Apply(Chain chain, DetectionInfo detection) { int index = detection.index; // Undo relative rotation chain.GetBone(index).Rotate(detection.relative); // Rotate back but only by detection.angle degrees Vector3 axis = Quat.GetAxis(detection.relative); chain.GetBone(index).Rotate(Quaternion.AngleAxis(detection.angle, axis)); // Keep the chain linked for (int i = index + 1; i < chain.BoneCount; i++) { chain.GetBone(i).MoveTo(chain.GetBone(i - 1).Tip.Position); } }
// Update is called once per frame void Update() { t += Time.deltaTime * 0.5f; //defining two rotations Quat q = new Quat(Mathf.PI * 1.0f, new Vector3(0, 1, 0)); Quat r = new Quat(Mathf.PI * 0.5f, new Vector3(1, 0, 0)); //slerped value Quat slerped = Quat.SLERP(q, r, t); //define vector that will be rotated Vector3 p = new Vector3(0, 8, 0); //store that vector in a quaternion Quat K = new Quat(p); //newk will have new position inside Quat newK = slerped * K * slerped.Inverse(); //get this position as avector Vector3 newP = newK.GetAxis(); transform.position = newP; }
//function for applying transformation to the demo object, rotating around to always face the player to make it easier to work out the correct orientation. functions the same as transformModel(); public void transformDemo() { Vector3[] TransformedVertices = new Vector3[ModelSpaceVertices.Length]; Quat q1 = new Quat(pitchAng - lastpitchAng, new Vector3(1, 0, 0)); Quat q2 = new Quat(yawAng - lastyawAng, new Vector3(0, 1, 0)); Quat q3 = new Quat(rollAng - lastrollAng, new Vector3(0, 0, 1)); q = ((q * q2) * q1) * q3; for (int i = 0; i < TransformedVertices.Length; i++) { Quat K = new Quat(ModelSpaceVertices[i]); Quat newK = q * K * q.Inverse(); Vector3 newP = newK.GetAxis(); Vector3 quatVert = newP; Matrix4by4 sMatrix = scale(scaleMul); Vector3 scaVert = sMatrix * quatVert; Matrix4by4 yawMatrix = new Matrix4by4(new Vector3(Mathf.Cos(g), 0, -Mathf.Sin(g)), new Vector3(0, 1, 0), new Vector3(Mathf.Sin(g), 0, Mathf.Cos(g)), Vector3.zero); Vector3 scarotVert = yawMatrix * scaVert; Matrix4by4 tMatrix = translate(new Vector3(pos.x, 0, pos.z)); Vector4 tVert = new Vector4(scarotVert.x, scarotVert.y, scarotVert.z, 1); Vector4 finalVert = tMatrix * tVert; TransformedVertices[i] = finalVert; } MeshFilter MF = GetComponent <MeshFilter>(); MF.mesh.vertices = TransformedVertices; MF.mesh.RecalculateBounds(); MF.mesh.RecalculateNormals(); lastpitchAng = pitchAng; lastyawAng = yawAng; lastrollAng = rollAng; }