Pygame 的 rect.clip 3D 函数
Pygame's rect.clip function in 3D
(想象图片是 3D 的)
我制作了一个 Cuboid 对象,它存储了长方体中所有角的坐标,就像 pygame 矩形对象一样。我需要两个上述长方体的重叠体积,然后从中制作一个新的长方体对象(图片中的灰色矩形,再次想象它是 3D 的)。
是否有一个通用的代数公式,或者我必须为每个特定情况制定一个方法?
我用的是pyglets坐标系,也就是说往上是正y,往右是正x,往前是正z。
长方体Class:
def __init__(self, pos, width, height, depth):
self.pos = pos
self.width = width
self.height = height
self.depth = depth
self.Update()
def Update(self):
self.size = (self.width, self.height, self.depth)
self.topleft_front = (self.pos[0], self.pos[1] + self.height, -self.pos[2])
self.topright_front = (self.pos[0] + self.width, self.pos[1] + self.height, -self.pos[2])
self.bottomleft_front = (self.pos[0], self.pos[1], -self.pos[2])
self.bottomright_front = (self.pos[0] + self.width, self.pos[1], -self.pos[2])
self.topleft_back = (self.pos[0], self.pos[1] + self.height, -self.pos[2] - self.depth)
self.topright_back = (self.pos[0] + self.width, self.pos[1] + self.height, -self.pos[2] - self.depth)
self.bottomleft_back = (self.pos[0], self.pos[1], -self.pos[2] - self.depth)
self.bottomright_back = (self.pos[0] + self.width, self.pos[1], -self.pos[2] - self.depth)
self.center = (self.pos[0] + self.width / 2, self.pos[1] + self.height / 2, -self.pos[2] - self.depth / 2)
def collidecube(self, cube):
if (self.pos[0] < cube.pos[0] + cube.width) and (
self.pos[0] + self.width > cube.pos[0]) and (
self.pos[1] < cube.pos[1] + cube.height) and (
self.pos[1] + self.height > cube.pos[1]) and (
self.pos[2] < cube.pos[2] + cube.depth) and (
self.pos[2] + self.depth > cube.pos[2]):
return True
else:
return False
def Clip(self, cube):
if self.collidecube(cube):
pass
找到两个范围的重叠范围可以减少问题。
例如范围 [a0, a1] ^ [b0, b1] = [c0, c1]
a0 a1
x----------------x
x-----------------x
b0 b1
x======x
c0 c1
如果a0
小于a1
且b0
小于b1
,则
c0 = max(a0, b0)
c1 = min(a1, b1)
如果 c1
小于 c0
,则范围不相交。该算法可以应用于所有 3 个维度。
如果足以验证范围是否相交,则条件为:
isect = a0 < b1 and b0 < a1
注意,如果b0
大于a1
或b1
小于a0
,则没有重叠范围。
我制作了一个 Cuboid 对象,它存储了长方体中所有角的坐标,就像 pygame 矩形对象一样。我需要两个上述长方体的重叠体积,然后从中制作一个新的长方体对象(图片中的灰色矩形,再次想象它是 3D 的)。
是否有一个通用的代数公式,或者我必须为每个特定情况制定一个方法?
我用的是pyglets坐标系,也就是说往上是正y,往右是正x,往前是正z。
长方体Class:
def __init__(self, pos, width, height, depth):
self.pos = pos
self.width = width
self.height = height
self.depth = depth
self.Update()
def Update(self):
self.size = (self.width, self.height, self.depth)
self.topleft_front = (self.pos[0], self.pos[1] + self.height, -self.pos[2])
self.topright_front = (self.pos[0] + self.width, self.pos[1] + self.height, -self.pos[2])
self.bottomleft_front = (self.pos[0], self.pos[1], -self.pos[2])
self.bottomright_front = (self.pos[0] + self.width, self.pos[1], -self.pos[2])
self.topleft_back = (self.pos[0], self.pos[1] + self.height, -self.pos[2] - self.depth)
self.topright_back = (self.pos[0] + self.width, self.pos[1] + self.height, -self.pos[2] - self.depth)
self.bottomleft_back = (self.pos[0], self.pos[1], -self.pos[2] - self.depth)
self.bottomright_back = (self.pos[0] + self.width, self.pos[1], -self.pos[2] - self.depth)
self.center = (self.pos[0] + self.width / 2, self.pos[1] + self.height / 2, -self.pos[2] - self.depth / 2)
def collidecube(self, cube):
if (self.pos[0] < cube.pos[0] + cube.width) and (
self.pos[0] + self.width > cube.pos[0]) and (
self.pos[1] < cube.pos[1] + cube.height) and (
self.pos[1] + self.height > cube.pos[1]) and (
self.pos[2] < cube.pos[2] + cube.depth) and (
self.pos[2] + self.depth > cube.pos[2]):
return True
else:
return False
def Clip(self, cube):
if self.collidecube(cube):
pass
找到两个范围的重叠范围可以减少问题。
例如范围 [a0, a1] ^ [b0, b1] = [c0, c1]
a0 a1
x----------------x
x-----------------x
b0 b1
x======x
c0 c1
如果a0
小于a1
且b0
小于b1
,则
c0 = max(a0, b0)
c1 = min(a1, b1)
如果 c1
小于 c0
,则范围不相交。该算法可以应用于所有 3 个维度。
如果足以验证范围是否相交,则条件为:
isect = a0 < b1 and b0 < a1
注意,如果b0
大于a1
或b1
小于a0
,则没有重叠范围。