def check_collision_2d(self, posx, posy): s = self._base_scale * self._scale offset = Vector3(self.position.x * s, self.position.y * s, self.position.z * s) local_x = (posx - self.position.x) local_y = (posy - self.position.y) print(local_x, local_y) ray = Line(Vector3(local_x / s, local_y / s, 10), Vector3(0, 0, -1)) closest = None clicked = None for name, mesh in self._model.named_meshes.items(): for triangle in mesh.triangles: result = ray.collide(triangle) if result is not False: point, d = result if closest is None or d < closest: closest = d clicked = name return clicked
def create_ray_from_mouseclick(self, mousex, mousey, yisup=False): self.camera_direction.normalize() height = self.canvas_height width = self.canvas_width view = self.camera_direction.copy() h = view.cross(Vector3(0, 0, 1)) v = h.cross(view) h.normalize() v.normalize() rad = 75 * pi / 180.0 vLength = tan(rad / 2) * 1.0 hLength = vLength * (width / height) v *= vLength h *= hLength x = mousex - width / 2 y = height - mousey - height / 2 x /= (width / 2) y /= (height / 2) camerapos = Vector3(self.offset_x, self.offset_z, self.camera_height) pos = camerapos + view * 1.0 + h * x + v * y dir = pos - camerapos if yisup: tmp = pos.y pos.y = -pos.z pos.z = tmp tmp = dir.y dir.y = -dir.z dir.z = tmp return Line(pos, dir)
def mousePressEvent(self, event): if event.buttons() & Qt.RightButton: self.last_move = (event.x(), event.y()) elif event.buttons() & Qt.MiddleButton: print("hi", self.current_render_index) self.current_render_index += 1 #self.re_render(self.model) elif event.buttons() & Qt.LeftButton: self.current_render_index = 0 if self.camera_direction is not None: self.camera_direction.normalize() view = self.camera_direction.copy() h = view.cross(Vector3(0, 0, 1)) v = h.cross(view) h.normalize() v.normalize() rad = 75 * pi / 180.0 vLength = tan(rad / 2) * 1.0 hLength = vLength * (self.width / self.height) v *= vLength h *= hLength mirror_y = self.height - event.y() x = event.x() - self.width / 2 y = mirror_y - self.height / 2 x /= (self.width / 2) y /= (self.height / 2) camerapos = Vector3(self.offset_x, self.offset_y, self.camera_height) pos = camerapos + view * 1.0 + h * x + v * y dir = pos - camerapos #self.lines.append((pos.x+0.5, pos.y, pos.z)) #self.lines.append((pos.x + dir.x*400, pos.y + dir.y*400, pos.z + dir.z*400)) # Plane Intersection line = Line(camerapos, dir) nearest_coll = None nearest_dist = None for tri in self.collision: collision = line.collide(tri) if collision is not False: point, distance = collision if nearest_coll is None or distance < nearest_dist: nearest_coll = point nearest_dist = distance if nearest_coll is not None: collision = nearest_coll self.lines.append((collision.x + dir.x * -100, collision.y + dir.y * -100, collision.z + dir.z * -100)) self.lines.append((collision.x + dir.x * +100, collision.y + dir.y * +100, collision.z + dir.z * +100)) """if not self.plane.is_parallel(dir): d = ((self.plane.origin - pos).dot(self.plane.normal)) / self.plane.normal.dot(dir) if d >= 0: point = pos + (dir*d) self.lines.append((point.x, point.y, point.z-2000)) self.lines.append((point.x, point.y, point.z+2000))""" self.do_redraw()