Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def boxcast(self, origin, direction=(0,0,1), distance=math.inf, thickness=(1,1), traverse_target=scene, ignore=list(), debug=False):
if isinstance(thickness, (int, float, complex)):
thickness = (thickness, thickness)
resolution = 3
rays = list()
debugs = list()
for y in range(3):
for x in range(3):
pos = origin + Vec3(lerp(-(thickness[0]/2), thickness[0]/2, x/(3-1)), lerp(-(thickness[1]/2), thickness[1]/2, y/(3-1)), 0)
ray = self.raycast(pos, direction, distance, traverse_target, ignore, False)
rays.append(ray)
if debug and ray.hit:
d = Entity(model='cube', origin_z=-.5, position=pos, scale=(.02, .02, distance), ignore=True)
d.look_at(pos + Vec3(direction))
debugs.append(d)
# print(pos, hit.point)
if ray.hit and ray.distance > 0:
d.scale_z = ray.distance
d.color = color.green
from ursina import destroy
# [destroy(e, 1/60) for e in debugs]
rays.sort(key=lambda x : x.distance)
def raycast(self, origin, direction=(0,0,1), distance=math.inf, traverse_target=scene, ignore=list(), debug=False):
self.position = origin
self.look_at(self.position + direction)
self._pickerNode.clearSolids()
# if thickness == (0,0):
if distance == math.inf:
ray = CollisionRay()
ray.setOrigin(Vec3(0,0,0))
# ray.setDirection(Vec3(0,1,0))
ray.setDirection(Vec3(0,0,1))
else:
# ray = CollisionSegment(Vec3(0,0,0), Vec3(0,distance,0))
ray = CollisionSegment(Vec3(0,0,0), Vec3(0,0,distance))
self._pickerNode.addSolid(ray)
if debug:
self._pickerNP.show()
else:
self._pickerNP.hide()
self._picker.traverse(traverse_target)
if self._pq.get_num_entries() == 0:
self.hit = HitInfo(hit=False)
return self.hit
ignore += tuple([e for e in scene.entities if not e.collision])
if isinstance(value, (int, float, complex)):
return Vec3(*(e*value for e in self))
return Vec3(self[0]*value[0], self[1]*value[1], self[2]*value[2])
def __truediv__(self, value):
if isinstance(value, (int, float, complex)):
return Vec3(*(e/value for e in self))
return Vec3(self[0]/value[0], self[1]/value[1], self[2]/value[2])
if __name__ == '__main__':
a = Vec3(1,0,0) * 2
a = Vec3(1,0,1) * Vec3(2,1,2)
b = Vec3(1.252352324,0,1)
b += Vec3(0,1)
print(a)
b.x += 2
print(b.x)
print(round(b))
def _list_to_vec(self, value):
if isinstance(value, (int, float, complex)):
return Vec3(value, value, value)
if len(value) % 2 == 0:
new_value = Vec2()
for i in range(0, len(value), 2):
new_value.add_x(value[i])
new_value.add_y(value[i+1])
if len(value) % 3 == 0:
new_value = Vec3()
for i in range(0, len(value), 3):
new_value.add_x(value[i])
new_value.add_y(value[i+1])
new_value.add_z(value[i+2])
return new_value
def raycast(self, origin, direction=(0,0,1), distance=math.inf, traverse_target=scene, ignore=list(), debug=False):
self.position = origin
self.look_at(self.position + direction)
self._pickerNode.clearSolids()
# if thickness == (0,0):
if distance == math.inf:
ray = CollisionRay()
ray.setOrigin(Vec3(0,0,0))
# ray.setDirection(Vec3(0,1,0))
ray.setDirection(Vec3(0,0,1))
else:
# ray = CollisionSegment(Vec3(0,0,0), Vec3(0,distance,0))
ray = CollisionSegment(Vec3(0,0,0), Vec3(0,0,distance))
self._pickerNode.addSolid(ray)
if debug:
self._pickerNP.show()
else:
self._pickerNP.hide()
self._picker.traverse(traverse_target)
def set_position(self, value, relative_to=scene):
self.setPos(relative_to, Vec3(value[0], value[1], value[2]))
self._pq.sort_entries()
self.entries = [ # filter out ignored entities
e for e in self._pq.getEntries()
if e.get_into_node_path().parent not in ignore
]
if len(self.entries) == 0:
self.hit = HitInfo(hit=False)
return self.hit
self.collision = self.entries[0]
nP = self.collision.get_into_node_path().parent
point = self.collision.get_surface_point(nP)
# point = Vec3(point[0], point[2], point[1])
point = Vec3(point[0], point[1], point[2])
world_point = self.collision.get_surface_point(render)
# world_point = Vec3(world_point[0], world_point[2], world_point[1])
world_point = Vec3(world_point[0], world_point[1], world_point[2])
hit_dist = self.distance(self.world_position, world_point)
if nP.name.endswith('.egg'):
nP = nP.parent
self.hit = HitInfo(hit=True)
for e in scene.entities:
if e == nP:
# print('cast nP to Entity')
self.hit.entity = e
self.hit.point = point
self.hit.world_point = world_point