Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def find_collision(self):
self.collisions = list()
self.collision = None
if not self.raycast or self._pq.get_num_entries() == 0:
self.unhover_everything_not_hit()
return False
self._pq.sortEntries()
for entry in self._pq.getEntries():
for entity in scene.entities:
if entry.getIntoNodePath().parent == entity and entity.collision:
if entity.collision:
hit = HitInfo(
hit = entry.collided(),
entity = entity,
distance = distance(entry.getSurfacePoint(scene), camera.getPos()),
point = entry.getSurfacePoint(entity),
world_point = entry.getSurfacePoint(scene),
normal = entry.getSurfaceNormal(entity),
world_normal = entry.getSurfaceNormal(scene),
)
self.collisions.append(hit)
break
if self.collisions:
self.collision = self.collisions[0]
def unhover_everything_not_hit(self):
for e in scene.entities:
if e == self.hovered_entity:
continue
if e.hovered:
e.hovered = False
if hasattr(e, 'on_mouse_exit'):
e.on_mouse_exit()
for s in e.scripts:
if hasattr(s, 'on_mouse_exit'):
s.on_mouse_exit()
def _destroy(entity):
if not entity:
print('entity is None')
return
if entity in scene.entities:
scene.entities.remove(entity)
if hasattr(entity, 'on_destroy'):
entity.on_destroy()
if hasattr(entity, 'scripts'):
for s in entity.scripts:
del s
if hasattr(entity, 'animations'):
for anim in entity.animations:
anim.finish()
anim.kill()
if hasattr(entity, 'tooltip'):
destroy(entity.tooltip)
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
self.hit.distance = hit_dist
normal = self.collision.get_surface_normal(self.collision.get_into_node_path().parent)
# self.hit.normal = (normal[0], normal[2], normal[1])
self.hit.normal = (normal[0], normal[1], normal[2])
normal = self.collision.get_surface_normal(render)
# self.hit.world_normal = (normal[0], normal[2], normal[1])
self.hit.world_normal = (normal[0], normal[1], normal[2])
return self.hit
def __init__(self, add_to_scene_entities=True, **kwargs):
super().__init__(self.__class__.__name__)
self.name = camel_to_snake(self.type)
self.enabled = True # disabled entities wil not be visible nor run code
self.visible = True
self.ignore = False # if True, will not try to run code
self.eternal = False # eternal entities does not get destroyed on scene.clear()
self.ignore_paused = False
self.ignore_input = False
self.parent = scene
self.add_to_scene_entities = add_to_scene_entities # set to False to be ignored by the engine, but still get rendered.
if add_to_scene_entities:
scene.entities.append(self)
self.model = None # set model with model='model_name' (without file type extention)
self.color = color.white
self.texture = None # set model with texture='texture_name'. requires a model to be set beforehand.
self.reflection_map = scene.reflection_map
self.reflectivity = 0
self.render_queue = 0
self.double_sided = False
# self.always_on_top = False
self.collision = False # toggle collision without changing collider.
self.collider = None # set to 'box'/'sphere'/'mesh' for auto fitted collider.
self.scripts = list() # add with add_script(class_instance). will assign an 'entity' variable to the script.
self.animations = list()
self.hovered = False # will return True if mouse hovers entity.
def generate_cube_map(self, size=512, name=f'cube_map_{len(scene.entities)}'):
from ursina import camera
_name = 'textures/' + name
org_pos = camera.position
camera.position = self.position
base.saveCubeMap(_name+'.jpg', size=size)
camera.position = org_pos
print('saved cube map:', name + '.jpg')
self.model.setTexGen(TextureStage.getDefault(), TexGenAttrib.MWorldCubeMap)
self.reflection_map = _name + '#.jpg'
self.model.setTexture(loader.loadCubeMap(_name + '#.jpg'), 1)
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])
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])
def display_mode(self, value):
self._display_mode = value
print('display mode:', value)
base.wireframeOff()
# disable collision display mode
if hasattr(self, 'original_colors'):
for i, e in enumerate([e for e in scene.entities if hasattr(e, 'color')]):
e.color = self.original_colors[i]
if e.collider:
e.collider.visible = False
for e in [e for e in scene.entities if e.model and e.alpha]:
e.setShaderAuto()
if value == 'wireframe':
base.wireframeOn()
if value == 'colliders':
self.original_colors = [e.color for e in scene.entities if hasattr(e, 'color')]
for e in scene.entities:
e.color = color.clear
if e.collider:
# e.visible = False