我在Java3D中创建了一个房间,并添加了用户使用键盘移动所选对象的功能。我只是想知道是否有可能检测到我的两个物体之间的碰撞。我想简单地允许用户在某个方向上移动一个对象,直到它与另一个任意对象发生碰撞。
如果有帮助,以下是我的两个对象:
地毯:
public class Carpet3D extends Group{
public Carpet3D() {
this.createSceneGraph();
}
public void createSceneGraph() {
Appearance appearance = getCarpetAppearance();
//Carpet
int primitiveflags = Primitive.GENERATE_NORMALS + Primitive.GENERATE_TEXTURE_COORDS;
Box floorShape = new Box(1.7f, .015f, 2.3f,primitiveflags,appearance);
this.addChild(floorShape);
}
}
晚餐表:
public class DinnerTable3D extends Group{
public DinnerTable3D() {
this.createSceneGraph();
}
public void createSceneGraph() {
Appearance appearance = getWoodenAppearance();
//Table TOP
int primitiveflags = Primitive.GENERATE_NORMALS + Primitive.GENERATE_TEXTURE_COORDS;
Box tableTop = new Box(1.2f, .035f, .8f,primitiveflags,appearance);
TransformGroup tableTopGroup = new TransformGroup();
tableTopGroup.addChild(tableTop);
this.addChild(tableTopGroup);
//Dinner Table Legs
Box tableLeg = new Box(0.06f,.72f,0.06f, primitiveflags,appearance);
SharedGroup dinnerTableLegs = new SharedGroup();
dinnerTableLegs.addChild(tableLeg);
//Leg 1
this.addChild(Main.setPositionOfObject(new Vector3f(-1.14f,-0.755f,-.74f), dinnerTableLegs));
//Leg 2
this.addChild(Main.setPositionOfObject(new Vector3f(1.14f,-0.755f,.74f), dinnerTableLegs));
//Leg 3
this.addChild(Main.setPositionOfObject(new Vector3f(1.14f,-0.755f,-.74f), dinnerTableLegs));
//Leg 4
this.addChild(Main.setPositionOfObject(new Vector3f(-1.14f,-0.755f,.74f), dinnerTableLegs));
}
答案 0 :(得分:1)
有可能。 Java3D没有为您检测碰撞检测,因此您必须提出自己的检查碰撞的方法。
这样做的方法是为每个对象计算边界框(或提供一个),然后迭代所有对象以查看边界框之间是否存在任何交叉点。这是一个相当简单的算法,任何游戏编程或3d数学编程书都将涵盖。
如果对象是球形或您不需要精度,也可以使用边界球体。球形交叉检查比边界框碰撞检查便宜一点。两者都相对便宜。
如果您要使用数字对象,最好通过使用二进制空间分区或八叉树等数据结构来进一步优化搜索空间。