Lines and trails

ok, i tried this and here are my conclusions :

first of all, about the cloning thing (yeah, i need to clone it too), it’s because the trailcontrol assert that the line control is on the same spatial. And this is not true. In the given example, the linecontrol is attached to a geometry (an immobile geometry) and the trailcontrol is attached to an other geometry which move. It seems that you can attach the trail geometry and the line geometry on the same spatial but you get strange results (the queue of the trail moves too)

If somebody wants to clone it, i found a “simple” way to do that : a delayed creation.

i.e.

[java]
/*

  • To change this template, choose Tools | Templates
  • and open the template in the editor.
    */
    package real.client.controllers.trail;

import com.jme3.asset.AssetManager;
import com.jme3.material.Material;
import com.jme3.material.RenderState.FaceCullMode;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.AbstractControl;
import com.jme3.scene.shape.Box;
import kapplication.Initializable;
import real.client.RealClient;

/**
*

  • @author Bubuche
    */
    public class DelayedTrailCreation extends AbstractControl implements Initializable<RealClient>, Cloneable
    {
    private RealClient application;

@Override
protected void controlUpdate(float tpf)
{
createTrail();

spatial.removeControl(this);

}

@Override
protected void controlRender(RenderManager rm, ViewPort vp)
{
}

private void createTrail()
{
AssetManager assetManager = application.getAssetManager();

Box b = new Box(Vector3f.ZERO, 1, 1, 1);
Spatial geom = spatial;

Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", ColorRGBA.Blue);

//geom.setMaterial(mat);
mat.setTexture(“ColorMap”, assetManager.loadTexture(“Textures/F3Fb0.png”));

Geometry trailGeometry = new Geometry();
LineControl line = new LineControl(new LineControl.Algo1CamDirBB(), true);
trailGeometry.addControl(line);
TrailControl trailControl = new TrailControl(line);
geom.addControl(trailControl);

//rootNode.attachChild(trail); // either attach the trail geometry node to the root…

trailGeometry.setIgnoreTransform(true); // or set ignore transform to true. this should be most useful when attaching nodes in the editor
Node test = new Node();
test.attachChild(trailGeometry);
test.setLocalTranslation(new Vector3f(0, 2, 0));  // without ignore transform this would offset the trail
application.getRootNode().attachChild(test);

mat.getAdditionalRenderState().setFaceCullMode(FaceCullMode.Off);
trailGeometry.setMaterial(mat);

}

@Override
public void init(RealClient application)
{
this.application = application;
}
}
[/java]

This is directly copy past from my program, so you’ll need to clean it. I give it like this cause i want to make sure that people will understand that the delayed controls need to have a reference to the assetmanager, or a way to access it. Why ? Because the only job done by this control is to “copy past” the “example” code. so, you don’t really clone the trail, you create a new instance (but you can clone the instance creator, i.e. this control).
it’s ugly but it works.
I have a way through my “init” method, but you need to find your own. Keep in mind that if you give a reference once (for example you can have 2 constructors for the control and one that take the assetManager as parameter) the reference will be clone when you’ll call the “clone” method (its the default comportement). So, you just need to ensure that your base item (the item you’ll clone over and over again) has a valid reference.
Or you can find an other way, of course :wink:

In the code i gave, you still need to add a way to clean things when the trail is over. I’ll investigate on a good way to do this (cause i added a node an a control on it, they’ll stay in the scene if you don’t remove them)

However … the trail itself has a problem (besides the fact that it is opaque) : when you don’t look at it from a side, when you look at it from behind or front, you will not see it, and you’ll even see how “plan” it is. I am not sure why i have this, as for me the geometry that need the control should be used to give a 3d view of the trail in such case, but i have this comportement. So, i am still looking for a way to create 3D trail.

(p.s. i want to achieve a visual effect close to this one http://image.jeuxvideo.com/images/pc/t/i/tiocpc001_m.jpg )