Код для Raycasting висит, когда выходит за рамки 90 градусов [дублировать]Python

Программы на Python
Ответить Пред. темаСлед. тема
Anonymous
 Код для Raycasting висит, когда выходит за рамки 90 градусов [дублировать]

Сообщение Anonymous »

Все. Я пытался создать заклинатель для проекта, но я застрял с этой проблемой; Мой код висит, когда значение угла выходит за пределы 90 градусов. Почему это происходит? Есть решение? Вот мой код: < /p>

Код: Выделить всё

class Ray:
def __init__(self, depth, character, world_map):
self.depth = depth
self.character = character
self.map = world_map

def check_horizontal_offset(self, coordinate, sin, tan):
# if sin > 0, then character is pointing down. else, up.
if sin > 0:
offset_y = self.map.cell_size - (coordinate % self.map.cell_size) - 0.0001
else:
offset_y = -(coordinate % self.map.cell_size)

offset_x = offset_y / tan
hypotenuse = math.sqrt(offset_x ** 2 + offset_y ** 2)

return offset_x, offset_y, hypotenuse

def check_vertical_offset(self, coordinate, cos, tan):
# if cos > 0, character is pointing right, else left
if cos > 0:
offset_x = self.map.cell_size - (coordinate % self.map.cell_size) - 0.0001
else:
offset_x = -(coordinate % self.map.cell_size)

offset_y = offset_x * tan
hypotenuse = math.sqrt(offset_x ** 2 + offset_y ** 2)

return offset_x, offset_y, hypotenuse

def cast_ray(self, origin, sin, cos, tan):
ray_distance = 0
x, y = origin

while ray_distance < self.depth:
x_1, y_1, hypotenuse_1 = self.check_horizontal_offset(x, sin, tan)
x_2, y_2, hypotenuse_2 = self.check_vertical_offset(y, cos, tan)

if hypotenuse_1 < hypotenuse_2:
ray_distance += hypotenuse_1
print("hyp 1  ", hypotenuse_1)
x += x_1
y += y_1
else:
ray_distance += hypotenuse_2
print("hyp 2  ", hypotenuse_2)
x += x_2
y += y_2

pygame.draw.line(comms.screen, (255, 255, 0), self.character.get_position(), (x, y), 2)

def update(self, angle):
# calculate all trigonometric values for calculation of intersections
sin = math.sin(angle)
cos = math.cos(angle)
tan = math.tan(angle)

# safeguard for divide by zero error
if tan == 0:
tan = 0.000001

self.cast_ray(self.character.get_position(), sin, cos, tan)
Любая помощь с этим будет оценена.

Подробнее здесь: https://stackoverflow.com/questions/794 ... 90-degrees
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Python»