- Home /

# Vector math problem

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): And here is what it does when the planet is not at the origin:

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[i] = (transform.TransformPoint(vertices[i])) - planetPos;
verticesN[i] = (((truevpos[i].normalized) * (radius + (((float) noi.GetValue((truevpos[i].normalized * radius) + planetPos)) * noisemod)))) - (relativePos);
//Debug.Log(planetMaker.name + (truevpos[i].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[i].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?

The best way to debug this is to break your calculations down into discreet steps. Then for each step, output some values or draw something visual on the screen to help you see what each part is doing.

I would venture a guess that you are forgetting to take into account the world position of the planet you're trying to place things on. A simple solution would be to parent all objects of a plant to the planet, so all (local) positions/rotations are relative to that of the parent. I'm not sure if this is applicable to your project, however.

**Answer** by cclaypool1
·
Jul 28, 2013 at 05:59 PM

I have solved this. Jaroma was correct in that I forgot to add planet position to the first spoint that I calculate noise with. It works perfectly now.

**Answer** by jerichaosymphony
·
Jul 28, 2013 at 05:52 PM

Hiya, lattitude and longitude is a set of 2 360 degree angles, and then you would have to raycast along a line from the origin to the latt/long, and find which polygon in intersects and place on top of it.

if you didnt do that, then how did you do the positioning?

I calculate this in the SurfacePoint vector method that I have provided.

```
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));
```

hello i am glad to join your site hello to every body i need help i want to model the trajectories of ten balls in cylindrical chamber that chamber rotates with 300rpm could you please help me?? thanks a lot

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.