i try getting mouse cursor start and end position and draw rectangle and collides with objects
but still can’t draw rectangle with mouse
package mousetest;
import com.jme3.app.SimpleApplication;
import com.jme3.collision.CollisionResults;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Ray;
import com.jme3.math.Rectangle;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.scene.Mesh;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.VertexBuffer.Type;
import com.jme3.scene.shape.Box;
import com.jme3.scene.shape.Quad;
import com.jme3.scene.shape.RectangleMesh;
import com.jme3.util.BufferUtils;
/**
* This is the Main Class of your Game. It should boot up your game and do initial initialisation
* Move your Logic into AppStates or Controls or other java classes
*/
public class MouseTest extends SimpleApplication {
public static void main(String[] args) {
MouseTest app = new MouseTest();
app.setShowSettings(false); //Settings dialog not supported on mac
app.start();
}
Node shootables;
@Override
public void simpleInitApp() {
setUpLight();
shootables = new Node("Shootables");
Spatial model = assetManager.loadModel("Models/level.glb");
shootables.attachChild(model);
rootNode.attachChild(shootables);
inputManager.addMapping("Target",new MouseButtonTrigger(MouseInput.BUTTON_RIGHT)); // trigger 2: left-button click
inputManager.addListener(actionListener,"Target");
}
Vector3f startPoint;
Vector3f endPoint;
private boolean press = false;
final private ActionListener actionListener = new ActionListener() {
@Override
public void onAction(String name, boolean keyPressed, float tpf) {
if (name.equals("Target") ) {
if(!keyPressed){
Vector3f origin = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.0f);
Vector3f direction = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.3f);
direction.subtractLocal(origin).normalizeLocal();
CollisionResults results = new CollisionResults();
//create ray
Ray ray = new Ray( cam.getLocation() , direction );
//collide mouse click wit npc node
shootables.collideWith( ray, results );
if(results.size() > 0){
press = true;
startPoint = results.getCollision(0).getContactPoint();
}
}
if(!keyPressed && press){
Vector3f origin = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.0f);
Vector3f direction = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.3f);
direction.subtractLocal(origin).normalizeLocal();
CollisionResults results = new CollisionResults();
//create ray
Ray ray = new Ray( cam.getLocation() , direction );
//collide mouse click wit npc node
shootables.collideWith( ray, results );
if(results.size() > 0){
endPoint = results.getCollision(0).getContactPoint();
System.out.println(startPoint);
System.out.println(endPoint);
Rectangle rectangle = new Rectangle();
rectangle.setA(startPoint);
rectangle.setB(new Vector3f(endPoint.x,startPoint.y +2,startPoint.z));
rectangle.setC(new Vector3f(startPoint.x,endPoint.y + 2,endPoint.z));
RectangleMesh mesh = new RectangleMesh(rectangle);
Geometry geo = new Geometry("OurMesh", mesh); // using our custom mesh object
Material mat = new Material(assetManager,
"Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", ColorRGBA.Blue);
geo.setMaterial(mat);
rootNode.attachChild(geo);
}
}
}
}
};
@Override
public void simpleUpdate(float tpf) {
//this method will be called every game tick and can be used to make updates
}
@Override
public void simpleRender(RenderManager rm) {
//add render code here (if any)
}
private void setUpLight() {
// We add light so we see the scene
AmbientLight al = new AmbientLight();
al.setColor(ColorRGBA.White.mult(5.3f));
rootNode.addLight(al);
DirectionalLight dl = new DirectionalLight();
dl.setColor(ColorRGBA.White);
dl.setDirection(new Vector3f(2.8f, -2.8f, -2.8f).normalizeLocal());
rootNode.addLight(dl);
DirectionalLight dl2 = new DirectionalLight();
dl2.setColor(ColorRGBA.White);
dl2.setDirection(new Vector3f(100,3,26).normalizeLocal());
rootNode.addLight(dl2);
DirectionalLight dl3 = new DirectionalLight();
dl3.setColor(ColorRGBA.White);
dl3.setDirection(new Vector3f(-100,3,26).normalizeLocal());
rootNode.addLight(dl3);
}
}