Im trying to create a 3D torus in a block game, so i need to evaluate a range of coordinates to see if they are inside a torus. The way i did it with spheres is:
shapefunc = function (pos,fields)
map = {}
pos.x = math.floor(pos.x+0.5)
pos.y = math.floor(pos.y+0.5)
pos.z = math.floor(pos.z+0.5)
for x=-fields.radius,fields.radius do
for y=-fields.radius,fields.radius do
for z=-fields.radius,fields.radius do
if x*x+y*y+z*z <= fields.radius*fields.radius then
table.insert(map,{x=pos.x+x,y=pos.y+y,z=pos.z+z})
end
end
end
end
return map
end
given height (on y axis), a minor and major radius (on xz axes), and a point of origin, none of the evaluation expressions i tried have given me anything close to a torus.
According to this it's a matter of testing for the sign of the expression:
(x^2+y^2+z^2-(a^2+b^2))^2 - 4*a*b*(b^2-z^2)
where the point is {x,y,z} and the minor radius of the torus is b, and the major radius a.
The formula above didn't work for me. Consider b = 0, the formula should reduce to a circle, when in fact in the accepted answer it reduces to a sphere.
From my calculation you must test the sign of the expression
(x^2+y^2+z^2+a^2-b^2)^2-4a^2(x^2+y^2)
where the point is {x,y,z} and the minor radius of the torus is b, and the major radius a.
edit: equivalently, and closer to the original accepted answer the expression would be
(x^2+y^2+z^2-(a^2+b^2))^2-4a^2(b^2-z^2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With