5

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.

4

3 に答える 3

2

これによれば、式の符号をテストすることです:

(x^2+y^2+z^2-(a^2+b^2))^2 - 4*a*b*(b^2-z^2)

ここで、点は {x,y,z} で、トーラスの短半径は b、長半径は a です。

于 2012-11-19T21:04:13.227 に答える