我在 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);
}
}
餐 table :
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));
}
最佳答案
这是可能的。 Java3D 不会为您检测碰撞检测,因此您必须想出自己的方法来检查碰撞。
这样做的方法是为每个对象计算一个边界框(或提供一个边界框),然后迭代所有对象以查看边界框之间是否存在任何交集。这是一个相当简单的算法,任何游戏编程或 3D 数学编程书籍都会介绍。
如果对象是球形或者不需要精度,您还可以使用边界球体。球体相交检查比边界框碰撞检查便宜一些。不过两者都相对便宜。
如果您要拥有数字对象,那么明智的做法是使用二进制空间分区或八叉树等数据结构来进一步优化搜索空间。
关于java - 如何检测组之间的冲突 - Java3D,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16266929/