DrawLine between many points?

So I have a rayCast function returning a hitPoint vector for every tic and I want to basically use the bge.render.drawLine(fromVec, toVec, color) to draw a line between all the points.

Any ideas? I suspect it’s a simple loop but I cant get it myself.

Thanks in advance. :slight_smile:

What do you mean by hitPoint vector in this case? It’s position in world space or the normal vector from the hitPoint?
Either way, use multiple drawLine calls, one for each line you want to draw.
If you mean normal vector, try something like this:

# Define the color red
red = [255, 0, 0]

# Draw a line from the start of the ray to the end of the ray
drawLine(rayOrigin, rayHitPoint, red)

# Draw a line from the end of the ray to the end of the normal
drawLine(rayHitPoint, normalEnd, red)

# Draw a line from the end of the normal to the start of the ray
drawLine(normalEnd, rayOrigin, red)

You mean an increasing curve? It will become very inefficient pretty fast.

So here is a solution with limited “history”:

curve.py


import bge

PmaxPoints = "maxPoints"; DmaxPoints = 100
'''Maximum Number of Curve Points; Optional with default'''
Pcurve = "curve"

RED = [1,0,0]
def addHitPointAndDraw(cont):
    hitPosition = findHitPosition(cont.sensors)
    if hitPosition is None:
        return
    
    obj = cont.owner
    curve = getCurve(obj)
    maxPoints = obj.get(PmaxPoints, DmaxPoints)

    if len(curve) >= maxPoints:
        curve.pop(0)
    curve.append(hitPosition)
    
def draw(cont):
    drawCurve( getCurve(cont.owner), RED )
    
def drawCurve(curve, color):
    if not curve:
        return
    startPoint = curve[0]
    for point in curve:
        if point is startPoint:
            continue
        bge.render.drawLine(startPoint, point, color)
        startPoint = point
        
def getCurve(obj):
    try:
        return obj[Pcurve]
    except KeyError:
        curve = []
        obj[Pcurve] = curve
        return curve

def findHitPosition(sensors):
    for sensor in sensors:
        if not sensor.positive:
            continue
        try:
            return sensor.hitPosition
        except AttributeError:
            pass

Attachments

DrawHistoryCurve.blend (123 KB)

Cool, thanks Monster and gruntbatch. I will have a play.

just put all vector in one list , then when make a loop cut the last item …(len(L)-1)



import bge
from mathutils import Matrix


def m(cont):
    own = cont.owner


    rot = Matrix.Rotation(0.1,4,"Z")
    move = Matrix.Translation((0,1,0.01))
    
    
    L = []
    m = Matrix()
    for i in range(1000):
        m *= rot * move
        L.append(m.translation)
    
    draw = bge.render.drawLine
    for i in range(len(L)-1):
        draw(L[i],L[i+1],[1,0,0])