1
$\begingroup$

I've tried do to draw a curve with the bge module, assuming I have a list of locations :

for i in range(1, len(positions) - 1):
    bge.render.drawLine(positions[i - 1], positions[i], (1, 0, 0))

But the following doesn't work at all. What is wrong with this ? This is the piece of code I've written, and I'd like to be able to draw the trajectory of the object.

import bge

own = bge.logic.getCurrentController().owner
scene = bge.logic.getCurrentScene()
own["positions"] = []
own.applyForce((1, 1, 1), 0)
own["positions"].append(own.worldPosition)
for i in range(1, len(own["positions"]) - 1):
    bge.render.drawLine(own["positions"][i - 1], own["positions"][i], (1, 0, 0))

EDIT:

Changed the script according to Chebhou's answer:

import bge
own = bge.logic.getCurrentController().owner
scene = bge.logic.getCurrentScene()
if "positions" not in own :
    own["positions"] = []
own.applyForce((1, 1, 1), 0)
own["positions"].append(own.worldPosition.copy())
for i in range(1, len(own["positions"]) - 1):
    bge.render.drawLine(own["positions"][i - 1], own["positions"][i], (1, 0, 0))
$\endgroup$
  • $\begingroup$ what errors you are getting ?, how are saving the position ? $\endgroup$ – Chebhou May 25 '15 at 15:57
  • $\begingroup$ The trails are not shown at all. $\endgroup$ – user14219 May 25 '15 at 16:07
  • $\begingroup$ position is cleared every logic tic, you are doing that with own["positions"] = [] $\endgroup$ – Chebhou May 25 '15 at 16:20
  • $\begingroup$ I know. How to fix that ? $\endgroup$ – user14219 May 25 '15 at 16:26
0
$\begingroup$

positions variable is being cleared at every execution of the script by the line own["positions"] = [] to prevent this replace that line with :

if 'positions' not in own :
    own["positions"] = []

Edit :
The position list was not extended properly it was taking pointers to the object position to fix this replace own["positions"].append(own.position) with own["positions"].append(own.position.copy())

$\endgroup$
  • $\begingroup$ Thank you. But I have an other problem : I can't plot the trail, although the locations are putted in the 'positions' list. $\endgroup$ – user14219 May 25 '15 at 16:48
  • $\begingroup$ @user14219 see if this will work $\endgroup$ – Chebhou May 25 '15 at 17:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.