我开始用jMonkey开发游戏。我刚刚从我自己制作的类"实体"中创建了一个对象,其中包含三维模型、物理等。这是代码:
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;
}
}
在jMonkey网站教程的帮助下,我成功地实现了"拍摄"。沿着我的凸轮方向的简单光线。以下是如果它与某种东西相撞会发生什么的代码:
} 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());
}
}
所以这很好用!此行:
System.out.println(results.getCollision(0).getGeometry().getName());
显示我刚刚拍摄的"几何体"的名称。但现在的问题是,我的对象不是几何体!我不知道我该如何实现,无论如何我都能得到这个对象的名称。对我来说最好的方法是如果results.getCollision(0)会返回我的对象,这样我就可以说"object.getName();"
有人知道我该怎么做吗?如果有任何想法,我将不胜感激:)干杯-Daniel
您可以在Java 7:中使用类似的东西
switch (results.getCollision(0).getClass().getName()){
case "ObjectName1":
Code
break;
case "ObjectName2":
Code
break;
}
或者Java 6 中的
String objectName=results.getCollision(0).getClass().getName()){
if ("ObjectName1"==objectName){
Code
}elseif ("ObjectName2"==objectName){
Code
}
Kiwy建议的选项1是为每个几何体指定一个唯一的名称,并使用该名称查找对象。选项2是扩展Geometry,使其是您的对象,或者包含对象。然而,这需要使用instanceof
和铸造。
在这个例子中,我将把Geometry扩展到OwnedGeometry,以携带所有者
public class OwnedGeometry extends Geometry {
MyClass owner;
public OwnedGeometry(String name, Mesh mesh,MyClass owner){
super(name,mesh);
this.owner=owner;
}
public MyClass getOwner(){
return owner;
}
}
这个类可以像你以前使用Geometry一样使用,它只是携带了额外的字段
然后,当你收到你的几何图形时,你可以检索所有者
CollisionResults results = new CollisionResults();
// Aim the ray from cam loc to cam direction.
Ray ray = new Ray(cam.getLocation(), cam.getDirection());
//Collect intersections between Ray and Shootables in results list.
shootables.collideWith(ray, results);
System.out.println(results.size());
if (results.size() >= 1) {
Geometry closest=results.getCollision(0).getGeometry());
if (closest instanceof OwnedGeometry){
OwnedGeometry ownedGeometry=(OwnedGeometry)closest;
MyClass myClass=ownedGeometry.getOwner;
//success!
}
}
这个方法直接返回类,但确实需要一点instanceof和casting,这不是理想的