Modifying something ynjh posted a while ago, I am experimenting with adding a simple motion trail to a moving object. I’d like to have a 10 segment line that starts with RGB(1,1,1,1) and gradually changes to RGB(1,1,1,0.1) at the end of the trail. Creating the segments works, but adding the
self.line.setVertexColor(index,1.0,1.0,1.0,alpha)
stuff crashes the app with this output:
DirectStart: Starting the game.
Known pipe types:
osxGraphicsPipe
(all display modules loaded.)
1 0.1
1 0.1
2 0.2
1 0.1
2 0.2
3 0.3
1 0.1
2 0.2
3 0.3
4 0.4
Assertion failed: ((int)size <= _total_heap_array_size), function heap_free_array, file dtool/src/dtoolbase/memoryHook.cxx, line 347.
Abort trap
Notice that the script crashes randomly sooner or later, sometimes getting to index = 2, 4 or 6.
This is the modified code:
from pandac.PandaModules import *
import direct.directbase.DirectStart
from direct.showbase.DirectObject import DirectObject
from direct.task.Task import Task
class World(DirectObject):
def __init__(self):
self.drawingLine()
def drawingLine(self):
self.line = LineSegs()
self.line.setThickness(4)
self.line.setColor(1.0,1.0,1.0,1.0)
self.lineNP=NodePath('')
self.lineNP.setTransparency(True)
self.points=[]
self.lastMpos=None
#taskMgr.add(self.mousePos, "mousePos")
taskMgr.doMethodLater(0.1, self.mousePos, "mousePos")
def mousePos(self, task):
index = 0
alpha = 0
if base.mouseWatcherNode.hasMouse():
mpos = base.mouseWatcherNode.getMouse()
if mpos!=self.lastMpos:
if self.lastMpos:
self.points.append(Point3(self.lastMpos[0],0,self.lastMpos[1]))
else:
self.points.append(Point3(mpos[0],0,mpos[1]))
# self.line.reset()
if len(self.points) >= 10:
del self.points[0]
# print self.points
self.line.moveTo(self.points[0])
for p in self.points[1:]:
#self.line.drawTo(p)
index += 1
alpha += 0.1
print index, alpha
self.line.setVertexColor(index,1.0,1.0,1.0,alpha)
self.line.drawTo(p)
self.lineNP.removeNode()
self.lineNP=render2d.attachNewNode(self.line.create())
self.lastMpos=Point2(mpos)
return Task.again #Task continues infinitely
w = World()
run()
In a related note, what does
LineSegs.reset()
actually do? Makes no visible change it seems to me.