Random insideUnitSphere but outside other UnitSphere?
Asked Answered
A

3

0

I have a question, sorry for my bad English.
I know and use Random.insideUnitSphere. Now I would like to create a random point inside the sphere, but outside another smaller sphere (with the same center).

Like:

Apteryx answered 22/4, 2024 at 16:7 Comment(1)

Well the easy but not good solution would be to use a loop in that you create a random point in the outer sphere, check if it is inside the small excluded space and then create a new one if it is. That should do for most problems but can take a few iterations until you find a suitable point and it is not really a nice solution. The better solution would probably be to create a random point within the radius of the outer sphere - radius of the inner sphere. Then you translate the point by a vector of length of the inner sphere radius into the negative direction towards the sphere center.

Alvira
J
0
randomValue = Random.insideUnitSphere  * ((1.0-smallSphereRadius/bigSphereRadius) + (smallSphereRadius/bigSphereRadius)) * bigSphereRadius;

To simplify/Optimize if needed

TESTED - but still imperfect answer

So, if I recall, I was trying to find a solution without normalizing because it still costs some CPU.

Solution with normalizing (corresponding to OP’s solution) :

float delta = BigRadius - SmallRadius;
float length = SmallRadius + delta * Random.value;
Vector3 position = Random.insideUnitSphere.normalized * length;

Solution without normalizing (Notice it’s using onUnitSphere)

float delta = BigRadius - SmallRadius;
float length = delta * Random.value;
Vector3 pos = Random.onUnitSphere * (BigRadius - length);

I’ve tested both and they seem to work the same.

I’ve timed both methods with a release build on an i7: method without normalizing is around 2.4 time faster (repeating a million time the last 2 lines, results might differ with IL2CPP). I could not see the internal code Unity uses because it’s in C++

That being said, visually, it seems that both methods generate more points in the center than in the outer sphere (obvious when Comparing with a simple Vector3 pos = Random.insideUnitSphere * BigRadius It shows that the distribution is not good with both methods (that rely on the same principle because the normalization is equivalent as taking a point on the sphere). There must be more math involved to have a uniform distribution (though it seems correct to discard values from Random.insideUnitSphere * BigRadius that are inside SmallRadius, not CPU efficient but uniform, as far as I understand)

EDIT 2 - better solution

Ok, so I’ve recalled what I was trying to do first: I was trying to “compress” the random points inside the sphere = imagine a sphere with random points in it, then you slowly inflate the center until it reaches SmallRadius size, compressing all the points but not ejecting them out of the sphere. I think this conserves the uniform distribution, but I’m not mathematically sure it does. Visually, it works well and looks uniform, even with extreme values of radius (SmallRadius = 0 and SmallRadius = BigRadius - epsilon)

Vector3 posInSphere = Random.insideUnitSphere;
float length = posInSphere.magnitude;
float ratioRadius = SmallRadius/BigRadius;
Vector3 pos =  (((1.0f-ratioRadius)*length + ratioRadius) / length) * BigRadius * posInSphere;

It involves computing a vector’s magnitude so it’s close to the cost of a normalization but it’s better distributed anyway :slight_smile:

Judaea answered 22/4, 2024 at 16:11 Comment(0)
A
0

Solved

// paste this code here so people can copy it

The result in editor at runtime:

Work fine!

Apteryx answered 22/4, 2024 at 16:12 Comment(2)

I thought the Unity 2018 package manager only handles Unity-own packages, not custom .unitypackage packages. Did I get something wrong? Where is your info from?

Obturate

We're working on a final game so we're not switching to an unstable 2018 version.

Lyns
H
0

I know this is an old post but a simple way to do this is to not use random inside. Use random on radius and change the radius to be random:

Random.onUnitSphere * Random.Range(minDistance, maxDistance);
Henriques answered 22/4, 2024 at 16:8 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.