Advertisement
shadowplaycoding

P006_PositionMath

Mar 7th, 2017
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.48 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class PositionMath {
  5.  
  6.     // This method converts a distance and angle (polar coordinates)
  7.     public static Vector3 PolarToCart(float distance, float angle)
  8.     {
  9.         Vector3 cartPosition = new Vector3(distance * Mathf.Cos(angle), 0, distance * Mathf.Sin(angle));
  10.  
  11.         return cartPosition;
  12.     }
  13.  
  14.     // This method returns a random angle between 0 and 2*PI
  15.     public static float RandomAngle()
  16.     {
  17.         float angle = Random.Range(0, 2 * Mathf.PI);
  18.  
  19.         return angle;
  20.     }
  21.  
  22.     // This method creates a random polar coordinate then converts and returns it as a Cartesian coordinate
  23.     public static Vector3 RandomPosition(float minRad, float maxRad)
  24.     {
  25.         float distance = Random.Range(minRad, maxRad);
  26.         float angle = RandomAngle();
  27.  
  28.         Vector3 cartPosition = PolarToCart(distance, angle);
  29.  
  30.         return cartPosition;
  31.     }
  32.  
  33.     // This method creates a positon for a planet based on its number in the planetList (see Star Class)
  34.     public static Vector3 PlanetPosition(int planetListNumber)
  35.     {
  36.         float distance = (planetListNumber + 1) * 5;
  37.         float angle = RandomAngle();
  38.  
  39.         Vector3 cartPosition = PolarToCart(distance, angle);
  40.  
  41.         return cartPosition;
  42.     }
  43.  
  44.     /*
  45.     Copyright Shadowplay Coding 2017 - see www.shadowplaycoding.com for licensing details
  46.     Removing this comment forfits any rights given to the user under licensing.
  47.     */
  48.  
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement