I started working on a game with jMonkey. I just created an object from the class "Entity" I made myself which holds an 3D Model, Physics and so on. Here is the code:
package mygame.entities;
import com.jme3.asset.AssetManager;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.renderer.queue.RenderQueue.ShadowMode;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
public class Entity {
private AssetManager assetManager;
private Node rootNode;
public Spatial model;
private Geometry object;
private String itsName;
private int life;
private boolean destroyAble;
private boolean destroyed;
public Entity(BulletAppState bas, AssetManager manager, Node rootNode, String name, int lifes, boolean destroyable, float x, float y, float z) {
itsName = name;
life = lifes;
destroyAble = destroyable;
model = manager.loadModel("Models/woodlog.j3o");
model.setLocalTranslation(x, y, z);
model.setShadowMode(ShadowMode.Cast);
model.setName(name);
model.setUserData("lifes", 3);
RigidBodyControl body = new RigidBodyControl(2);
model.addControl(body);
bas.getPhysicsSpace().add(body);
rootNode.attachChild(model);
}
public String getName() {
return itsName;
}
public int getLife() {
return life;
}
public void setLife(int lifes) {
life = lifes;
}
public boolean isDestroyable() {
return destroyAble;
}
public boolean isDestroyed() {
if (destroyAble && life <= 0) {
destroyed = true;
} else {
destroyed = false;
}
return destroyed;
}
}
With help from the tutorial on the jMonkey website I managed to implement the "shooting". A simple ray that follows my cam direction. Here is the code for what happens if it collides with something:
} else if (binding.equals("Fire") && !isPressed) {
// 1. Reset results list.
CollisionResults results = new CollisionResults();
// 2. Aim the ray from cam loc to cam direction.
Ray ray = new Ray(cam.getLocation(), cam.getDirection());
// 3. Collect intersections between Ray and Shootables in results list.
shootables.collideWith(ray, results);
// 4. Print results.
System.out.println(results.size());
if (results.size() >= 1) {
System.out.println(results.getCollision(0).getGeometry().getName());
//Material material = results.getCollision(0).getGeometry().getMaterial();
//material.setColor("Color", ColorRGBA.randomColor());
}
}
So that works just fine! This line:
System.out.println(results.getCollision(0).getGeometry().getName());
displays the name of that "Geometry" i just shot. But the problem now is, that my object is not a Geometry! And I dont know how I can implement that I get the name of this object anyway. The best way for me would be if the results.getCollision(0) would return my object so I could just say "object.getName();"
Does anyone know how I can do that? I would be very grateful for any ideas :) cheers - Daniel