## Get point on sphere's surface from a random point

I am creating a game with Unity and I have a math problem. I have a sphere with a radius of 10 and center of (0, 0, 0). I want the camera to move around that sphere, but I can't find anywhere a way to do what I want to. I move the camera in the X axis and the Y axis (and therefore get a point outside of the sphere) and I want to set it's Z axis so the camera would be back on the sphere, I am using this equation: r^2 = x^2 + y^2 + z^2 => z^2 = r^2 - x^2 - y^2 But it doesn't work… Please help me

**EDIT**

This is my code (in c#):

private void OnMouseDrag() { var newX = mainCameraTransform.position.x + Input.GetAxis("Mouse X"); var newY = mainCameraTransform.position.y + Input.GetAxis("Mouse Y"); var maxDistance = 10.0f; newX = Mathf.Clamp(newX, -maxDistance * 0.85f, maxDistance * 0.85f); newY = Mathf.Clamp(newY, 1.0f * 0.85f, maxDistance * 0.85f); var newZ = Mathf.Sqrt(Mathf.Abs(maxDistance * maxDistance - newX * newX - newY * newY)); mainCameraTransform.position = new Vector3(newX, newY, newZ); mainCameraTransform.LookAt(Vector3.zero); }

As you can see I used Clamp to keep the X and Y less then the radius but it didn't help…

This isn't tested, but it should be pretty close

private void OnMouseDrag(){ Vector3 newPos = mainCameraTransform.position; newPos += mainCameraTransform.up * Input.GetAxis("Mouse Y"); newPos += mainCameraTransform.right * Input.GetAxis("Mouse X"); newPos = newPos.normalized * 10f; mainCameraTransform.position = newPos; mainCameraTransform.LookAt(Vector3.zero, mainCameraTransform.up); }

**Plotting a point on the edge of a sphere,** So we can use instead the point (√17,0,0) and then read off the answer. So go from the center of the sphere 4 units in that direction, to get (0 If we want any area on the sphere to contain approximately the same density of points, there are a number of solutions . One solution is to pick λ ∈ [-180°, 180°) as before and then set φ = cos -1 (2x - 1), where x is uniformly distributed and x ∈ [0, 1).

You have to limit 2D coordinates by circle border

len = Mathf.Sqrt(newX * newX + newY * newY); //perhaps you have Len or Hypot function in your Math library if len > maxDistance then newX = maxDistance * newX / len newY = maxDistance * newY / len;

**How do I find a point on the surface of a sphere,** How do you check if a point is inside a sphere? Spherical coordinates are useful for organizing points on a sphere. However, when it come to geometric operations on a generic point on sphere, it is usually not that easy to use. For this particular problem, it will be easier to describe the new point using vectors.

Drag and drop this script on the camera to orbit it around a target using the right mouse button

using System.Collections; using System.Collections.Generic; using UnityEngine; public class OrbitAroundObject : MonoBehaviour { public Transform target; public float distance = 10.0f; public float xSpeed = 120.0f; public float ySpeed = 120.0f; public float yMinLimit = -20f; public float yMaxLimit = 80f; public float distanceMin = .5f; public float distanceMax = 15f; public float smoothTime = 2f; public float zoomSpeed = 1; float rotationYAxis = 0.0f; float rotationXAxis = 0.0f; float velocityX = 0.0f; float velocityY = 0.0f; // Use this for initialization void Start() { Vector3 angles = transform.eulerAngles; rotationYAxis = angles.y; rotationXAxis = angles.x; // Make the rigid body not change rotation if (GetComponent<Rigidbody>()) { GetComponent<Rigidbody>().freezeRotation = true; } } void LateUpdate() { if (target) { if (Input.GetMouseButton(1)) { velocityX += xSpeed * Input.GetAxis("Mouse X") * 0.02f; velocityY += ySpeed * Input.GetAxis("Mouse Y") * 0.02f; } //distance -= (Input.mouseScrollDelta.y*Time.deltaTime); distance = Mathf.Lerp(distance, distance-(Input.mouseScrollDelta.y*zoomSpeed) , Time.deltaTime * smoothTime); distance = Mathf.Clamp(distance, distanceMin, distanceMax); rotationYAxis += velocityX; rotationXAxis -= velocityY; rotationXAxis = ClampAngle(rotationXAxis, yMinLimit, yMaxLimit); //Quaternion fromRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0); Quaternion toRotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0); Quaternion rotation = toRotation; Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance); Vector3 position = rotation * negDistance + target.position; transform.rotation = rotation; transform.position = position; velocityX = Mathf.Lerp(velocityX, 0, Time.deltaTime * smoothTime); velocityY = Mathf.Lerp(velocityY, 0, Time.deltaTime * smoothTime); } } public static float ClampAngle(float angle, float min, float max) { if (angle < -360F) angle += 360F; if (angle > 360F) angle -= 360F; return Mathf.Clamp(angle, min, max); } }

**Point in Sphere – Miguel Casillas,** To obtain points such that any small area on the sphere is expected to contain the same number of points (right figure above), choose U and V to be random If the point is outside the bounds of the sphere, it is discarded and picked again. This is done again and again till enough points are available. function getPoint () { var d , x , y , z ; do { x = Math . random () * 2.0 - 1.0 ; y = Math . random () * 2.0 - 1.0 ; z = Math . random () * 2.0 - 1.0 ; d = x * x + y * y + z * z ; } while ( d > 1.0 ); return { x : x , y : y , z : z }; }

**Sphere,** The points on the sphere with radius r can be parameterized via HOW CAN I GET IT FROM ANSYS AFTER MESHING A CERTAIN GEOMETRY so i can use it I am asked to find the minimum and maximum distance to point $(1,1,1)$ So nearest point would be the touch point with the surface, and farthest point would be the touchpoint + distance of diameter. Can you help me solve this?

**Sphere Point Picking -- from Wolfram MathWorld,** The normalized vector is then scaled by a uniform random number to get the position of the point. function getPoint() { var x = Math.random() To place N points on the surface of a sphere, define an axis. Divide the surface into N equal area strips perpendicular to the axis. For k in 0 to N-1, on the kth strip, place a point at an angle of k*ga, in the centre of the its width.

**Can anyone tell me how to find coordinates of points lying on the ,** Given co-ordinates of the center of a sphere (cx, cy, cz) and its radius r. Our task is to check whether a point (x, y, z) lies inside, outside or on this sphere. The points on the sphere are all the same distance from a fixed point. Also, the ratio of the distance of its points from two fixed points is constant. The first part is the usual definition of the sphere and determines it uniquely.