I am currently running into a slight error involving vector math. The problem I am facing is that I have a planet that is randomly generated using noise values calculated at the vertex position on a sphere, I have come up with a simple method of placing objects on the planet surface at a latitude and longitude.
Here is a picture of what the object does on a planet at the origin (correct positioning): ![alt text][1]
And here is what it does when the planet is not at the origin: ![alt text][2]
Here is the code for this:
public Vector3 SurfacePoint(float latitude, float longitude, float raddif, ModuleBase noi, GameObject planetObject)
{
GenerateNoise();
Mathf.Clamp(latitude, -90, 90);
Mathf.Clamp(longitude, -180, 180);
Vector3 spoint;
float lat = latitude * Mathf.PI/180;
float lon = longitude * Mathf.PI/180;
float rad = radius;
spoint.x = (-rad * Mathf.Cos(lat) * Mathf.Cos(lon));
spoint.y = (rad * Mathf.Sin(lat));
spoint.z = (rad * Mathf.Cos(lat) * Mathf.Sin(lon));
//Vector3 trueplanetPos = spoint - planetPosition;
raddif = (float) noi.GetValue(spoint);
Debug.Log(raddif);
rad = radius + (raddif * noisemod);
spoint.x = (-rad * Mathf.Cos(lat) * Mathf.Cos(lon));
spoint.y = (rad * Mathf.Sin(lat));
spoint.z = (rad * Mathf.Cos(lat) * Mathf.Sin(lon));
return (spoint + planetObject.transform.position);
}
The problem I am running into is with the noise value only being correct for a position on the surface of the planet if the planet is located at the origin. The code that I use for getting the noise values is here:
void Spherify(float radius, ModuleBase noi)
{
Vector3[] vertices = qMesh.vertices;
Vector3[] verticesN = qMesh.vertices;
Vector3[] normals = qMesh.normals;
Vector3[] truevpos = qMesh.vertices;
for (int i = 0; i < vertices.Length; i++)
{
truevpos _= (transform.TransformPoint(vertices*)) - planetPos;*_
verticesN = (((truevpos.normalized) * (radius + (((float) noi.GetValue((truevpos_.normalized * radius) + planetPos)) * noisemod)))) - (relativePos);
//Debug.Log(planetMaker.name + (truevpos.normalized * radius));_
* }*
* transform.rotation = Quaternion.Euler(0,0,0);*
* qMesh.vertices = verticesN;*
* qMesh.RecalculateNormals();*
* qMesh.RecalculateBounds();*
* }*
The most important part of that code is here this bit
noi.GetValue((truevpos_.normalized * radius) + planetPos))
As you can see, I am using the true world position of that vertex along the sphere to get a noise value but for some odd reason when I plug in spoint from the SurfacePoint to the noise function I only get the correct value if the planet is at the origin.
Is there something wrong with my vector math or is this just an oddity caused by something else?
[1]: http://i.imgur.com/hSYbRc4.png*_
_[2]: http://i.imgur.com/6Aunt37.png*_