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
#2
The while loop for distance is a big efficiency sink. Every one of those method calls takes time to call and they're the same methods repeatedly. There are 34 method calls in a loop that iterates up to 200 times so, it's potentially 6,800 method calls. It would be faster to call those methods once, store the value in a variable, and then do the logic test; this would equate to 400 methods calls maximum.

Plus, the structure of that if statement should be if...elif...else instead of using break continually.

while distance != 200:
    turt.tracer(0, 0)
    lis2[i] = 0
    # bounderies
    x = turt.xcor()
    y = turt.ycor()

    if x > 50:
        turt.setx(50)
        lis2[i] = 1
    elif y < -50:
        turt.sety(-50)
        lis2[i] = 2
    elif y > 50:
        turt.sety(50)
        lis2[i] = 2
    elif x < -50:
        turt.setx(-50)
        lis2[i] = 1
    # block
    elif 30 < x < 31.5 and -20 < y < 20:
        lis2[i] = 3
    elif 37 < x < 38.5 and -20 < y < 20:
        lis2[i] = 3
    elif 20 < x < 21 and 30 < y < 38.5:
        lis2[i] = 4
    elif -21 < x < -20 and 30 < y < 38.5:
        lis2[i] = 4
    # block 2
    elif 10 < x < 11.5 and -20 < y < 20):
        lis2[i] = 3
    elif 17 < x < 18.5 and -20 < y < 20:
        lis2[i] = 3
    elif 20 < x < 21 and 10 < y  < 18.5:
        lis2[i] = 4
    elif -21 < x < -21 and 10 < y < 18.5:
        lis2[i] = 4

    if lis2[i] != 0:
       break

    turt.forward(1)
    distance += 1
The coloring loop at the end can be simplified with a small dictionary too.

colors = {1: "Blue", 2: "Mediumblue", 3: "Red", 4: "Crimson"}
        
for i in range(96):
    draw.color(colors.get(lis2[i]))
Reply


Messages In This Thread
RE: Need help on making raycasting faster - by stullis - Mar-10-2019, 03:14 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Raycasting GPS coordinates drybulkfreight 10 4,715 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