Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by cclaypool1 · Jul 28, 2013 at 05:31 PM · c#terrainproceduralvectorsplanets

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

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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nsxdavid · Jul 28, 2013 at 05:41 PM 0
Share

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.

avatar image Jamora · Jul 28, 2013 at 05:47 PM 1
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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?

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cclaypool1 · Jul 28, 2013 at 05:58 PM 0
Share

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

  Vector3 spoint;
         float lat = latitude * $$anonymous$$athf.PI/180;
          float lon = longitude * $$anonymous$$athf.PI/180;
         float rad = radius;
          spoint.x = (-rad * $$anonymous$$athf.Cos(lat) * $$anonymous$$athf.Cos(lon));
          spoint.y =  (rad * $$anonymous$$athf.Sin(lat));
          spoint.z =  (rad * $$anonymous$$athf.Cos(lat) * $$anonymous$$athf.Sin(lon));
avatar image farhaad.saba · Jul 28, 2013 at 06:15 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Procedural terrain bigger than I set it, heightmap slightly off 0 Answers

How do I deform a Marching Cube/Voxel Terrain? 1 Answer

spawning/instantiating based on a specific texture on terrain 1 Answer

Generating Structures on Infinite Terrain 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges