Rotate a point around a mesh vertex
I've hit a bit of a bump in something I've been looking to try out.
I'm looking to have a point be rotated around a vertex. Whilst I can successfully get a mesh point to be rotated, it keeps rotating around the world space origin: I guess because of the use of Vector3.forward in my code below:
using UnityEngine;
using System.Collections;
public class PlaneController : MonoBehaviour
{
private Mesh mesh;
public float gizmoSphereSize = 1f;
Vector3 rotatedPoint;
Vector3 rotatedVert;
Vector3 newPos;
void Update()
{
mesh = GetComponent<MeshFilter>().mesh;
rotatedVert = transform.TransformPoint(mesh.vertices[0]);
Vector3 dir = rotatedVert - (transform.position);
newPos = Quaternion.Euler(Vector3.forward * Time.time * 100) * dir;
rotatedPoint = newPos + transform.position;
// alternative
/*rotatedPoint = transform.TransformPoint(mesh.vertices[0]);
rotatedPoint = Quaternion.Euler(Vector3.forward * Time.time * 100) * rotatedPoint;*/
Debug.DrawRay(transform.position, rotatedPoint, Color.black);
}
private void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawSphere(rotatedPoint, gizmoSphereSize);
}
}
The result is it looks like this:
https://drive.google.com/file/d/0B1wVSPywFDMaSGlTb2hwNlhYYXc/view?usp=sharing
When what I'm wanting is this:
Any ideas on how to set this up properly?
Answer by RonDQuigley · Jun 23, 2016 at 12:48 PM
Solved it! In the end it was a completely simple fix I overlooked. Most transformation tutorials I've read that involve rotating around an arbitary point miss out an additional step you need to do if you want the above result. The extra step is that along with resetting the vert (which is your axis of rotation) back to its original position, you then need to offset the rotating vert by the computed vertex direction. I've cleaned up the code and added comments in case this helps others:
using UnityEngine;
using System.Collections;
public class PlaneController : MonoBehaviour
{
private Mesh mesh;
private Vector3 dirVert;
public Vector3 origin;
public float gizmoSphereSize = 1f;
public float offset;
Vector3 rotatedVertReset;
Vector3 vertToRotate;
Vector3 rotatedVert;
void Update()
{
// get the mesh we are working with
mesh = GetComponent<MeshFilter>().mesh;
// get the vert in world space
vertToRotate = transform.TransformPoint(mesh.vertices[1]);
// get the vert direction in relation to the transform's origin
dirVert = vertToRotate - (transform.position);
// rotate the vert around whatever axis around the origin
rotatedVert = Quaternion.Euler(Vector3.forward * Time.time * 100) * dirVert;
// reset the vert back to its original position
rotatedVertReset = rotatedVert + transform.position;
// EXTRA STEP - offset the rotated vert by the previous vert direction
Vector3 rotatedVertWithOffset = rotatedVertReset + dirVert;
Debug.DrawRay(vertToRotate, rotatedVertReset, Color.black);
}
private void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawSphere(rotatedVertReset + dirVert
, gizmoSphereSize);
}
}
Your answer
Follow this Question
Related Questions
Mesh Vertices not rotating with object 0 Answers
Set Rotation 1 Answer
Point on a triangle. 1 Answer
Vertex colors not working in shader graph. 0 Answers
Complex if and/or statement for finding neighboring triangles in a mesh 1 Answer