Cone and Cube intersection volume
So the project has a large cube made of up smaller, equal sized cubes. There will be 360 cones emanating from some center point in one degree increments, with separate conical sections (similar to the middle cone in the picture). I have to calculate the percent of volume that each cone section takes up in the cube. I've made it as far as adding colliders to each and registering the OnTriggerEvent. Now I am struggling to figure out the math to get this volume, as it needs to be precise as possible, preferably to the nearest tenth of a percent. I found a similar question but it might be overly complex since it's only cubes and cones in my situation: http://answers.unity3d.com/questions/1178637/finding-volume-of-two-3d-meshes-intersection.html
There are still many things unclear about your situation:
a "Cone" and "Cube" are 3d volumes. What exactly do you mean by "one degree increments"? Do you arrange those "cones" in one "plane"? So the touching line between two neighboring cones lies in one plane?.
What's the size relation between the cones and the cube?
If it's a 3d problem, can the cube be rotated arbitrary in relation to the cone-ring?
Are you really interested in the actual physical intersection volume as a numeric value or just some sort of percentage. However if you want a percentage you have to define which is 100%. Obviously a ring of cones can never fill a cube shaped volume entirely.
A bit more background information would help to figure out the best way to approach your problem. Arbitrary intersecting a single cone with a cube can result in very nasty shapes when they partially intersect.
I'll answer your questions in order. 1. by one degree increments I mean that for each degree azimuth(the x-z plane in unity), there will be one cone. the cones are all angled with a specific elevation. The next elevation has the same amount of cones, just angled up a bit more. 2. The cones are a preset size, but the cubes can have their size changed (all of the cubes have to be the same size though) 3. the cubes will always be placed separate from the cones, they will always be facing the same way but the larger cube that the smaller ones parent from can be moved. 4. I really just need the percentage of the volume of the cube, taken up by intersecting section of a cone. At farther distances from the cone, the cone section could completely encompass the cube.
Does it have to be cones? There a many ways a cone can intersect even a single plane. Can't you just use a frustum / pyramid. It would still be quite tricky to calculate the intersection.
It highly depends on how exact you need the value. One approach is to simply systematically test points on a 3d grid inside the cube against the intersecting cones. This can be as precise as you want, but the complexity grows with O(x³). So a grid of 10x10x10 are 1000 test points while 20x20x20 are 8000 points and 40x40x40 are 64000 points.
Answer by Bunny83 · Aug 22, 2017 at 09:52 PM
Here's a helper struct that allows you to define a mathematical cone and test if a given point is inside the cone:
public struct Cone
{
public Vector3 pos; // pos is the tip of the cone
public Vector3 dir; // need to be normalized
public float height;
public float radius;
public bool InInside(Vector3 aPoint)
{
Vector3 v = aPoint - pos;
float d = dir.x*v.x + dir.y * v.y + dir.z * v.z;
if (d < 0 || d > height)
return false;
Vector3 r = v - dir * d;
d /= height;
// if "pos" should be at the base of the cone you have to add this line:
// d = 1f - d;
return r.sqrMagnitude < radius * radius * d * d;
}
}
As said in the comments, "pos" defines the start point of the cone. It's usually at the tip of the cone. If i understood your situation correctly that's also how you defined your cones. "dir" is the normalized direction. It defines the orientation of the cone. "height" is the height of the cone along "dir" and "radius" is the radius of the cone at the base.
Now you can simply iterate through the volume of the cube and test as many points you like:
float Intersect(Transform aCube, List<Cone> aCones, int aResolution)
{
var size = aCube.localScale;
var f = aCube.forward * size.z;
var r = aCube.right * size.x;
var u = aCube.up * size.y;
var s = aCube.position - (f + u + r) * 0.5f;
float step = 1f / aResolution;
int count = 0;
for(int x = 0; x < aResolution; x++)
{
for (int y = 0; y < aResolution; y++)
{
for (int z = 0; z < aResolution; z++)
{
for(int i = 0; i < aCones.Count; i++)
{
var p = s + (r * x + u * y + f * z) * step;
if (aCones[i].InInside(p))
{
count++;
break;
}
}
}
}
}
return count * step * step * step;
}
This assumes a default Unity cube. So it's centered and has a size of 1 (half size of 0.5). The "aCones" list should only contain cones that actually intersect with the cube. Be careful what you pass in as aResolution value. A resolution of 100 will result in 1 million test points
Your answer
![](https://koobas.hobune.stream/wayback/20220612133243im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Detect Volume on Collision 1 Answer
2D IsOnGround check providing false positives 0 Answers
Vector2 intersection 0 Answers
Color Change on Collision Without Object Disappearing 0 Answers
Player intersecting with ground during jump landing 1 Answer