Hi all
i am kinda new to JMonkey so excuse if I misunderstand something.
What I am trying to do:
I am trying to create a custom Mesh by dragging the mouse. This kinda works fine already but I have one strange behaviour.
as you can see, it is basicly the TestCustomMesh example from the JMonkey tests. My problem is the free space between the two triangles.
How it works in my code:
I have two listeners, one actionListener and one analogListener. By clicking the first listener sets my start point and as long as i hold the mouse button the analog listener resets the second point and keeps repainting my mesh.
[java]
public void initKeys() {
inputManager.addMapping(“DragMark”, new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
inputManager.addListener(actionListener, “DragMark”);
inputManager.addListener(analogListener, “DragMark”);
}
private AnalogListener analogListener = new AnalogListener(){
public void onAnalog(String name, float value, float tpf){
if(name.equals("DragMark")){
System.out.println("click realeased! ! ");
if(rootNode.getChild("SelectBox") != null){
rootNode.detachChildNamed("SelectBox");
}
CollisionResults results = new CollisionResults();
Vector2f clickedStart = inputManager.getCursorPosition();
Vector3f clickedStart3d = cam.getWorldCoordinates(clickedStart, 0f).clone();
Vector3f cmDirection = cam.getWorldCoordinates(clickedStart, 1f).subtractLocal(clickedStart3d).normalizeLocal();
Ray ray = new Ray(clickedStart3d, cmDirection);
floor.collideWith(ray, results);
if(results.size() > 0){
Vector3f pt = results.getClosestCollision().getContactPoint();
System.out.println("click realeased at " + pt.toString());
//mark2.setLocalTranslation(pt);
//rootNode.attachChild(mark2);
end = pt;
if(start != null && end != null){
drawSelect(start, end);
}
}
}
}
};
private ActionListener actionListener = new ActionListener() {
public void onAction(String name, boolean keyPressed, float tpf) {
if (name.equals("DragMark") && keyPressed) {
System.out.println("KLICK AUSGELĂ–ST! ! ");
//rootNode.detachChild(mark2);
CollisionResults results = new CollisionResults();
Vector2f clickedStart = inputManager.getCursorPosition();
Vector3f clickedStart3d = cam.getWorldCoordinates(clickedStart, 0f).clone();
Vector3f cmDirection = cam.getWorldCoordinates(clickedStart, 1f).subtractLocal(clickedStart3d).normalizeLocal();
Ray ray = new Ray(clickedStart3d, cmDirection);
floor.collideWith(ray, results);
if(results.size() > 0){
Vector3f pt = results.getClosestCollision().getContactPoint();
System.out.println("KLICK AUF " + pt.toString());
//mark.setLocalTranslation(pt);
//rootNode.attachChild(mark);
start = pt;
}
}
}
};
[/java]
my paint funciton:
[java]
public void drawSelect(Vector3f start, Vector3f end){
System.out.println("DRAW AUFGERUFEN: " +start.toString() + " " + end.toString());
Mesh m = new Mesh();
Vector3f start2 = new Vector3f(start.x, 1.1f, end.z);
Vector3f end2 = new Vector3f(end.x, 1.1f, start.z);
Vector3f [] vertices = new Vector3f[4];
vertices[0] = start2;
vertices[1] = end;
vertices[2] = start;
vertices[3] = end2;
Vector2f[] texCoord = new Vector2f[4];
texCoord[0] = new Vector2f(0,0);
texCoord[1] = new Vector2f(1,0);
texCoord[2] = new Vector2f(0,1);
texCoord[3] = new Vector2f(1,1);
int [] indexes = { 2,0,1, 1,3,2 };
m.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(vertices));
m.setBuffer(Type.TexCoord, 2, BufferUtils.createFloatBuffer(texCoord));
m.setBuffer(Type.Index, 1, BufferUtils.createIntBuffer(indexes));
m.updateBound();
Geometry selectBox = new Geometry("SelectBox", m);
Material mat = new Material(assetManager,
"Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", ColorRGBA.Blue);
selectBox.setMaterial(mat);
rootNode.attachChild(selectBox);
}
[/java]
for your understanding. This is how my variables match with the points.
start--------end2
| |
| |
| |
start2------end
Any help appreciated.