def cylinderSDF(p, h, r): inOutRadius = glm.length(p.xy) - r inOutHeight = glm.abs(p.z) - h / 2.0 insideDistance = glm.min(glm.max(inOutRadius, inOutHeight), 0.0) outsideDistance = glm.length( glm.max(glm.vec2(inOutRadius, inOutHeight), 0.0)) return insideDistance + outsideDistance
def align(self, worldOriginRotation, worldDestinationRotation): # rotations angle = worldDestinationRotation - worldOriginRotation angle = (angle + 180.0) % 360.0 - 180.0 absAngle = glm.abs(angle) if absAngle <= self.minAlignDistance: return 0.0 direction = angle / absAngle angularAcceleration = direction * self.maxAngularVelocity return angularAcceleration
def move(self, dir): self.position = self.sposition + dir #if self.chunk.block(int(newpos.x), int(newpos.y), int(newpos.z)).space_type == 0: # self.position = newpos #newpos = self.position + glm.vec3(0,0,-.1) #if self.chunk.block(int(newpos.x), int(newpos.y), int(newpos.z)).space_type == 0: # self.position = newpos #self.position.z -= self.velocity.z adir = glm.abs(dir) if adir.z > adir.x and adir.z > adir.y: return if adir.x > adir.y: self.direction = self.renderer.RIGHT if dir.x > 0 else self.renderer.LEFT else: self.direction = self.renderer.UP if dir.y > 0 else self.renderer.DOWN
def boxSDF(p, size): d = glm.abs(p) - (size / 2.0) insideDistance = glm.min(glm.max(d.x, glm.max(d.y, d.z)), 0.0) outsideDistance = glm.length(glm.max(d, 0.0)) return insideDistance + outsideDistance
def norminf(x): ''' norm L infinite ie. `max(abs(x), abs(y), abs(z))` ''' return max(glm.abs(x))
def move_closer(self): distance = self.cam_distance - self.wheel_speed if glm.abs(distance) >= self._min_distance: self.cam_distance = distance
def intersect(self, ray_origin, ray_direction): size = glm.ivec3(*self.size) abs_d = glm.abs(ray_direction) max_d = max(abs_d.x, max(abs_d.y, abs_d.z)) dir = ray_direction / max_d / glm.vec3(size) origin = ray_origin * glm.vec3(size) map = glm.ivec3(origin + 1) - 1 side = 0 stepAmount = glm.ivec3(0) tDelta = abs(1.0 / dir) tMax = glm.vec3(0) voxel = 0 if dir.x < 0: stepAmount.x = -1 tMax.x = (origin.x - map.x) * tDelta.x elif dir.x > 0: stepAmount.x = 1 tMax.x = (map.x + 1 - origin.x) * tDelta.x else: stepAmount.x = 0 tMax.x = 0 if dir.y < 0: stepAmount.y = -1 tMax.y = (origin.y - map.y) * tDelta.y elif dir.y > 0: stepAmount.y = 1 tMax.y = (map.y + 1 - origin.y) * tDelta.y else: stepAmount.y = 0 tMax.y = 0 if dir.z < 0: stepAmount.z = -1 tMax.z = (origin.z - map.z) * tDelta.z elif dir.z > 0: stepAmount.z = 1 tMax.z = (map.z + 1.0 - origin.z) * tDelta.z else: stepAmount.z = 0 tMax.z = 0 while True: if tMax.x < tMax.y: if tMax.x < tMax.z: map.x += stepAmount.x if (stepAmount.x > 0 and map.x >= size.x) or (stepAmount.x < 0 and map.x < 0): return (False, map, side) tMax.x += tDelta.x side = 0 else: map.z += stepAmount.z if (stepAmount.z > 0 and map.z >= size.z) or (stepAmount.z < 0 and map.z < 0): return (False, map, side) tMax.z += tDelta.z side = 2 else: if tMax.y < tMax.z: map.y += stepAmount.y if (stepAmount.y > 0 and map.y >= size.y) or (stepAmount.y < 0 and map.y < 0): return (False, map, side) tMax.y += tDelta.y side = 1 else: map.z += stepAmount.z if (stepAmount.z > 0 and map.z >= size.z) or (stepAmount.z < 0 and map.z < 0): return (False, map, side) tMax.z += tDelta.z side = 2 voxel = self.lookup(map) if voxel != 0: break return (True, map, side)
def max_abs(v): if abs(v.x) > abs(v.y): return v.x else: return v.y