Python Forum
Need help on making raycasting faster
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help on making raycasting faster
#1
I need help making my raycaster faster as it takes time to render the image after
input. I have tried doing the turtle.tracer(0, 0) thing but it is still slow.
P.S. I don't know how to do python keyboard detection so you have to press enter every time you press a letter.


import turtle
import math

# Instructions:
# You can only type in one letter at a time and return
# w = forward
# A = left
# d = right
# s = backwards
# Do not run into the walls partly because I haven't coded the collision
# detection and also partly because it displays an error right after


screen = turtle.Screen()
screen.bgcolor("Black")
turt = turtle.Turtle()
turt.speed(0)
turt.penup()
plr = turtle.Turtle()
plr.penup()
plr.sety(-30)
plr.setx(-30)
plr.setheading(45)

draw = turtle.Turtle()
draw.penup()
draw.pensize(6)
draw.speed(0)

user = ""

angle = -48
distance = 0

lis = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
lis2 = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
def fwd():
  plr.forward(20)
def bck():
  plr.forward(-20)
def lft():
  plr.right(20)
def rgt():
  plr.left(20)

while True:
  while True:
    user = str(input("use the W A S D keys to move around:"))
    if (user == "w"):
      plr.forward(5)
      break
    if (user == "s"):
      plr.forward(-5)
      break
    if (user == "d"):
      plr.left(15)
      break
    if (user == "a"):
      plr.right(15)
      break
  angle = -48
  for i in range(96):
    turt.tracer(0, 0)
    turt.setx(plr.xcor())
    turt.sety(plr.ycor())
    turt.setheading(plr.heading() + angle)
    distance = 0
    while distance != 200:
      turt.tracer(0, 0)
      lis2[i] = 0
      # bounderies
      if (turt.xcor() > 50):
        turt.setx(50)
        lis2[i] = 1
        break
      if (turt.ycor() < -50):
        turt.sety(-50)
        lis2[i] = 2
        break
      if (turt.ycor() > 50):
        turt.sety(50)
        lis2[i] = 2
        break
      if (turt.xcor() < -50):
        turt.setx(-50)
        lis2[i] = 1
        break
      # block
      if (turt.xcor() > 30 and turt.xcor() < 31.5 and turt.ycor() > -20 and turt.ycor() < 20):
        lis2[i] = 3
        break
      if (turt.xcor() > 37 and turt.xcor() < 38.5 and turt.ycor() > -20 and turt.ycor() < 20):
        lis2[i] = 3
        break
      if (turt.ycor() > 20 and turt.ycor() < 21 and turt.xcor() > 30 and turt.xcor() < 38.5):
        lis2[i] = 4
        break
      if (turt.ycor() < -20 and turt.ycor() > -21 and turt.xcor() > 30 and turt.xcor() < 38.5):
        lis2[i] = 4
        break
      # block 2
      if (turt.xcor() > 10 and turt.xcor() < 11.5 and turt.ycor() > -20 and turt.ycor() < 20):
        lis2[i] = 3
        break
      if (turt.xcor() > 17 and turt.xcor() < 18.5 and turt.ycor() > -20 and turt.ycor() < 20):
        lis2[i] = 3
        break
      if (turt.ycor() > 20 and turt.ycor() < 21 and turt.xcor() > 10 and turt.xcor() < 18.5):
        lis2[i] = 4
        break
      if (turt.ycor() < -20 and turt.ycor() > -21 and turt.xcor() > 10 and turt.xcor() < 18.5):
        lis2[i] = 4
        break
      
      turt.forward(1)
      distance = distance + 1
    lis[i] = distance * math.sin(math.radians(90 - angle))
    angle += 1
  draw.setx(-200)
  draw.clear()
  for i in range(96):
    if (lis2[i] == 2):
      draw.color("Mediumblue")
    elif (lis2[i] == 1):
      draw.color("Blue")
    elif (lis2[i] == 3):
      draw.color("Red")
    elif (lis2[i] == 4):
      draw.color("Crimson")
    draw.sety(200)
    draw.sety(-1500 / lis[i])
    draw.pendown()
    draw.sety(1500 / lis[i])
    draw.setx(draw.xcor() + 5)
    draw.penup()
    draw.color("Black")
  draw.update()
  
Reply


Messages In This Thread
Need help on making raycasting faster - by robie972003 - Mar-09-2019, 09:34 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Raycasting GPS coordinates drybulkfreight 10 4,749 Aug-18-2019, 02:54 PM
Last Post: drybulkfreight

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020