[Snippet] Make the rays casted in the game engine visible.

Snippet name : VisualRay
Author : dberube4 (duh)
Snippet : http://www.pasteall.org/38877/python
Example : visualray.blend (467 KB)
Description: Represents rays casted in the game engine at runtime (it do not cast the ray itself) .
Basically its just a drawline function that last for a set number of frames.
Options (also in the snippet):

  • <b>start</b> ( 3-tuple (x, y, z) ): Coord where the ray starts 
  • <b>end</b> ( 3-tuple (x, y, z) ): Coord where the ray ends 
  • <b>life</b> ( int ): Number of frames the line will live (ex: With 200, 50 fps = 2 seconds). Default value : 200 
  • <b>color</b> ( 3-tuple (r, g, b) ): The color of the line. Default value : (1,1,1) (white) 

Use: add_line(start, end, life, color)
Example: add_line(target.worldPosition, owner.worldPosition, 500, (1,0,1) )

Preview (from the example):

This thing is awesome as a debug tool :smiley: Thank you very much.

Hmm, interesting. I like it.

I wonder if you could make this into a more useful debug tool by writing some functions:


def ray_cast(priority, obj, life, color, objto, objfrom, dist=0, prop="", face=0, xray=False, poly=0):
    if DEBUG_LEVEL &gt;= priority:
        add_line(start, end, life, color)
    return obj.rayCast(objto, objfrom, dist, prop, face, xray, poly)

def debug(priority, *args)
    if DEBUG_LEVEL &gt;= priority:

So that you can toggle the displaying of certain rays and certain print messages using a variable at the top of your script.

Yes. That would be possible.
I might also rewrite the entire script to be more memory wise.

Instead of using multiple classes, I could create a single debug class that store the lines to draw and the debug level.
Each line could have a debug property that shows whether they should be drawed or not.
I think I will use flags.