package mousetest;
import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.collision.Collidable;
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.material.RenderState.BlendMode;
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.renderer.queue.RenderQueue.Bucket;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;
import com.jme3.scene.shape.RectangleMesh;
import java.time.Instant;
/**
* 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;
BulletAppState bulletAppState;
@Override
public void simpleInitApp() {
setUpLight();
inputManager.setCursorVisible(true);
flyCam.setDragToRotate(true);
shootables = new Node("Shootables");
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
Spatial model = assetManager.loadModel("Models/level.glb");
shootables.attachChild(model);
shootables.attachChild(makeCube("a Dragon", -2f, 0f, 1f));
shootables.attachChild(makeCube("a tin can", 1f, 0f, 0f));
shootables.attachChild(makeCube("the Sheriff", 0f, 0f, -2f));
shootables.attachChild(makeCube("the Deputy", 1f, 0f, -4f));
rootNode.attachChild(shootables);
inputManager.addMapping("Target",new MouseButtonTrigger(MouseInput.BUTTON_RIGHT)); // trigger 2: left-button click
inputManager.addListener(actionListener,"Target");
}
private Geometry makeCube(String name, float x, float y, float z) {
Box box = new Box(1, 1, 1);
Geometry cube = new Geometry(name, box);
cube.setLocalTranslation(x, y, z);
Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat1.setColor("Color", ColorRGBA.randomColor());
cube.setMaterial(mat1);
return cube;
}
Vector3f startPoint;
Vector3f endPoint;
private boolean press = false;
Geometry area;
long areaDeleteTime = 0;
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();
Rectangle rectangle = new Rectangle();
if(startPoint.z > endPoint.z){
rectangle.setA(new Vector3f(startPoint.x,startPoint.y+0.3f,startPoint.z));
rectangle.setB(new Vector3f(endPoint.x,startPoint.y+0.3f ,startPoint.z));
rectangle.setC(new Vector3f(startPoint.x,endPoint.y+0.3f ,endPoint.z));
}else{
rectangle.setA(new Vector3f(startPoint.x,startPoint.y+0.3f,startPoint.z));
rectangle.setC(new Vector3f(endPoint.x,startPoint.y +0.3f,startPoint.z));
rectangle.setB(new Vector3f(startPoint.x,endPoint.y +0.3f,endPoint.z));
}
RectangleMesh mesh = new RectangleMesh(rectangle);
area = new Geometry("OurMesh", mesh); // using our custom mesh object
Material mat = new Material(assetManager,
"Common/MatDefs/Misc/Unshaded.j3md");
mat.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
mat.setColor("Color", ColorRGBA.Blue);
area.setQueueBucket(Bucket.Transparent);
area.setMaterial(mat);
RigidBodyControl collidable = new RigidBodyControl(1f);
area.addControl(collidable);
rootNode.attachChild(area);
areaDeleteTime = Instant.now().getEpochSecond();
}
results = new CollisionResults();
// 2. Aim the ray from cam loc to cam direction.
// 3. Collect intersections between Ray and Shootables in results list.
shootables.collideWith(area, results);
// 4. Print the results
System.out.println("----- Collisions? " + results.size() + "-----");
for (int i = 0; i < results.size(); i++) {
// For each hit, we know distance, impact point, name of geometry.
float dist = results.getCollision(i).getDistance();
Vector3f pt = results.getCollision(i).getContactPoint();
String hit = results.getCollision(i).getGeometry().getName();
System.out.println("* Collision #" + i);
System.out.println(" You shot " + hit + " at " + pt + ", " + dist + " wu away.");
}
}
}
}
};
@Override
public void simpleUpdate(float tpf) {
if(area != null && areaDeleteTime < Instant.now().getEpochSecond()){
rootNode.detachChild(area);
}
}
@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);
}
}
area is geometry
shootable is node of objects
error
com.jme3.collision.UnsupportedCollisionException: Collidable:OurMesh (Geometry)
at com.jme3.collision.bih.BIHTree.collideWith(BIHTree.java:470)
at com.jme3.scene.Mesh.collideWith(Mesh.java:1036)
at com.jme3.scene.Geometry.collideWith(Geometry.java:471)
at com.jme3.scene.Node.collideWith(Node.java:619)
at com.jme3.scene.Node.collideWith(Node.java:619)
at mousetest.MouseTest$1.onAction(MouseTest.java:169)
at com.jme3.input.InputManager.invokeActions(InputManager.java:174)
at com.jme3.input.InputManager.onMouseButtonEventQueued(InputManager.java:451)
at com.jme3.input.InputManager.processQueue(InputManager.java:873)
at com.jme3.input.InputManager.update(InputManager.java:923)
at com.jme3.app.LegacyApplication.update(LegacyApplication.java:777)
at com.jme3.app.SimpleApplication.update(SimpleApplication.java:248)
at com.jme3.system.lwjgl.LwjglWindow.runLoop(LwjglWindow.java:580)
at com.jme3.system.lwjgl.LwjglWindow.run(LwjglWindow.java:669)
at java.base/java.lang.Thread.run(Thread.java:829)