【发布时间】:2013-11-28 12:45:48
【问题描述】:
我开始使用 jMonkey 开发游戏。 我刚刚从我自己制作的“实体”类中创建了一个对象,其中包含 3D 模型、物理等。 代码如下:
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();”
有人知道我该怎么做吗?我将非常感谢任何想法:) 干杯 - 丹尼尔
【问题讨论】:
-
你应该考虑修改你完全不清楚的标题。
-
一旦你拍摄了这个物体,你应该会发现它的名字纹在它的耳朵上。 :-)
-
@Kiwy - 他在说什么非常清楚。 :-) :-)
-
@StephenC 不是因为“拍摄和任何东西”的所有部分都与解决方案无关。它只是在运行时找到对象的对象类,我认为这已经在 stackoverflow 上多次回答了
-
@Kiwy - Ermm ...您注意到我的 cmets 末尾的奇怪标点了吗?
标签: java collision-detection jmonkeyengine