1
$\begingroup$

How can I make a Python code or mix python with blender things to make the user draw a track(will be a ball rolling over it) by leaving spacebar or click pressed to decide how will the track looks. maybe creating a curve and making any object, like box attach to it

Note: I want it to be made in 2D.

later I could make something, I achieve to let the User press spacebar and while he moves the mouse it puts little cylinders that make kind a path thing, but they are so far one from each other, so is not exactly what I want... enter image description here

$\endgroup$
  • $\begingroup$ Not sure if this is possible without some serious scripting, if at all. Anyway this is a pretty broad question as it stands. Have you tried anything so far, what have you got? $\endgroup$ – Duarte Farrajota Ramos May 25 '17 at 12:51
  • $\begingroup$ My answer might help you. $\endgroup$ – David May 25 '17 at 21:57
  • $\begingroup$ @DuarteFarrajotaRamos thanks for your answer, I hope someone solve this, I have search a lot and nothing exactly of what I want, like the thing showed I liked it, but can I put physics to that? can I make the ball crush with that? If I can, then is what I want. Anyway, I got something, I mixed the thing is shown here and some little knowledge to put cylinders when I let Spacebar pressed, but they are so distant one from each other... I want my path to be continuous. $\endgroup$ – TOMAS LONDOÑO MURILLO May 26 '17 at 2:44
  • $\begingroup$ @David thanks, but I guess it's not what I want... as I said before, if I can make the ball cruch with that like any mesh object, it is what I want, but otherwise is not. But thanks, I like so much that Answer, maybe usefull later. $\endgroup$ – TOMAS LONDOÑO MURILLO May 26 '17 at 2:46
  • $\begingroup$ Another method is to deform a mesh along the trail the mouse makes, but this is quite processor intensive if you want physics to be updated while drawing. If you're fine with the graphics being correct, but the physics being "no collision" until the end, then a deformation method could work quite well. I'll give it a shot this evening. $\endgroup$ – sdfgeoff May 26 '17 at 9:50
1
$\begingroup$

Trail Object

This object represents a single step on the trail. The x-dimension should be exactly 1.0. Make it "no collision" in physics tab to avoid interaction with other objects.

To appear as a line use plan that gets scaled to 0 on the y-axis. Give it a material with wire frame to make it visible with exactly one pixel width. It should reside on an hidden layer (otherwise add object will not work).

enter image description here

Trailed object

Is the object that should leave a trail. Typically you add one trail per step. Then align the trail to the last position of the trailed object and change the size that it completely fills the step.

enter image description here

The script can be like that:

import bge

INTERNAL_PROPERTY_LAST_POSITION = "_last.position"

AXIS_X = 0

controller = bge.logic.getCurrentController()

owner = controller.owner

lastPosition = owner.get(INTERNAL_PROPERTY_LAST_POSITION)
currentPosition = owner.worldPosition.copy()
owner[INTERNAL_PROPERTY_LAST_POSITION] = currentPosition

if lastPosition is not None:
    direction = lastPosition - currentPosition 
    for actuator in controller.actuators:
        actuator.instantAddObject()
        trail = actuator.objectLastCreated

        trail.alignAxisToVect(direction, AXIS_X)
        trail.worldScale = 3*[direction.length]

Result

enter image description here

Remarks

  • This creates quite a lot of trail objects. At a certain number it will cause lags.
  • You can increase the step size by increasing "skip" at the always sensor of the trailed object. It creates less objects but results in less precise trailing and a small delay.
  • trail objects with a bigger width can result in overlapping and gaps. This depends on the shape and material.
$\endgroup$

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.