# Torus maths - what am I doing wrong?

I’m creating a series of nested torus meshes for use as intersection triggers, and I can’t seem to quite figure out why the inputs I’m passing in don’t have the expected results. Note how the green inner-most torus doesn’t even have a diameter larger than the 100-unit `star` mesh, even though its’ diameter is specified to be 300 units:

You see in the screenshot that `-124.769: entered zone - Inner System` followed by `127.213: exited zone - Inner System`? That gives a hypothetical “thickness” value of `251.982`, which although near to the expected value of `300`, is something like 16% off of expected.

Here is the PG I’m trying all of this out in - I’m defining the torus diameter and thickness directly as they are passed into the call to `MeshBuilder.CreateTorus`, but when I set up intersection enter/exit triggers that log out the position of the box at the time of the event, I’m finding that the actual numbers are nowhere near the expected figures.

Wait for about 10s or so after the PG loads and it will output the current location of the box and whether it is entering or exiting a particular zone. To help keep the math simple, I’m only moving along the x-axis.

For example, if I specify a torus diameter (`outerDiameter` in the PG) of `800`, I would expect that the intersection exit trigger would fire at `position.x >= outerDiameter`, but instead it fires at `position.x = 503` or 584, etc… I expect some amount of inaccuracy from the maths but this is too large to ignore. Furthermore, when you take the difference between the entrance and exit positions, it should be equivalent (or close enough) to the `thickness` parameter, but it’s not even close either.

I’m not sure why it would be involved, but somehow I feel the invisible hand of the constant `Math.PI / 2 ~= 1.57` at work. Maybe. I’m terribad at math, and it’s probably something simple and dumb that’s been staring at me in the face but needs someone elses’ eyes to see. It’s maybe even something unrelated to any sort of math issue. I dunno, that’s why I’m posting this question!

P.S. – the scales on the PG have been modified to try and highlight the discrepancies with the numbers
Thanks!

I’ve updated the PG to rev 16 which among other unrelated stuff makes it easier to see things as I’ve described

I think there’s a slightly confusion on how the torus is defined. When a torus is defined, the “diameter” value establishes the size of a “base” circle around which passes a tube with the given thickness. I think pictures work way better than words here, so I’ll let this picture help illustrate my point:

(picture credits: Calculate volume and surface area of Torus - GeeksforGeeks)

So, based on your most recent playground, let’s look at the “Inner System” torus. It has a diameter of 800 and a thickness of 300, which means its leftmost point should be at position -(800/2) (the radius of the torus) - (150/2) (half of the thickness of the torus) = -550.
And so, the actual size of the torus is diameter + thickness, and the intersection exit trigger should fire at something close to outerDiameter/2+thickness/2.

I also think the way the position is being updated might pose some problems. I’m racking my brain here trying to find a good way to explain my reasoning, but I think going with numeric values might work. Let’s see what happens when we subtract some close values together on this function:

6000sin(0.001) - 6000sin(0) = 5.9999 - 0 = 5.999
6000sin(1000.001) - 6000sin(1000) = 4964.6490 - 4961.2772 = 3.772
6000sin(1000000.001) - 6000sin(1000000) = -2094.3395 - (- 2099.9661) = 5.6266

You can see that, even all those values are different by 0.001, the difference between the functions varies quite a bit, so the value of x isn’t being changed uniformly, which means the points where the intersection will be evaluated also aren’t uniformly spaced.

I changed the update function to uniformly update the value of x by one, and these are the results I got:

Looking at the Outer System total diameter, we get 5551, which is only 1 of it’s diameter of 5550!

Here’s the modified example: https://playground.babylonjs.com/#735KL4#14

Hope this helps!

5 Likes

Thank you for the simply fantastic response and explanation, @carolhmj !

I indeed was taking the diameter parameter of the createTorus method to mean the diameter of a circle running around the equator of the outer part of the shape, not the proper definition you presented. Funny, the sources I read had the same diagram, but were all missing that black line going through both circles (or, also likely is that I simply missed it and it took your explanation for me to note it’s significance).

I also appreciate your correction of the position function. It’s easier to visually see correctness when movement is smooth!

1 Like