Calculating the Hit Point on a Sphere (with Rotation, Based on Static Nodes)
Hi,
I am currently working on procedurally generating spheres at runtime using a subdivided icosahedron.
So far, I have managed to create an icosahedron and subdivide this a specified number of times. I am then mapping the vertices of the subdivided shape as the center points of the hexagons (and 12 pentagons) that are being created, and generating the hexagons from the center points of all adjacent triangles.
These "nodes" (the hexagon center points) are generated at game launch and are static; they never move. When clicking on the surface of the sphere, I loop through all these nodes and find the nearest one to the mouse click and create a simple cube at the center of the hexagon.
This works great when the sphere has a rotation of [0, 0, 0]. Even when the camera is rotated around the sphere at weird angles, the logic holds up for clicking on the surface of the sphere. However, when the sphere itself starts to rotate, the logic breaks down.
I know exactly why this is the case; I am mapping to the position of the nodes (which are static). With the sphere rotating, the nodes no longer map to their visual counterparts (the hexagons) and, as such, the placement logic does not work.
I understand the basic premise for calculating the correct visual placement:
Take the click point on the sphere and rotate the point opposite to the sphere's rotation. This will map my click point back to the original node positions.
Calculate the closest node using the existing logic now that the point is in "node" space.
With the closest node found, map the node's position back to the sphere's rotation so that the cube is placed at the center of the correct hexagon visually.
I have been trying to work this out for a few days but I cannot find how to do this. I believe the main issue relies in rotating my click position in "world" space to "node" space and, then, vice versa when the nearest node has been found.
Is anybody able to help in calculating the mathematical function that is required to convert my point from "world" to "node".
I have looked into Quaternion.Inverse to 'reverse' the sphere's rotation and used the following function to rotate a point around a point (the origin):
private Vector3 RotateAroundPoint( Vector3 point, Vector3 pivot, Quaternion rotation )
{
return rotation * ( point - pivot ) + pivot;
}
but neither of these methods seem to be working for what I want. The cubes sometimes even place themselves way away from the surface of the sphere, which is definitely wrong!!
Any help would be greatly appreciated as I cannot solve this by myself :)
Your answer
Follow this Question
Related Questions
Finding the Correct Node on a Rotated Sphere 1 Answer
How to make camera rotate spherically around an object 0 Answers
Broken rotation (c#) 0 Answers
How to make an attack that moves the players position relative the target. 1 Answer
Get LineRenderer positions or perform a transformation to an array of Vector3s 1 Answer