Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何检测组之间的冲突-Java3D_Java_Collision Detection_Collision_Java 3d - Fatal编程技术网

如何检测组之间的冲突-Java3D

如何检测组之间的冲突-Java3D,java,collision-detection,collision,java-3d,Java,Collision Detection,Collision,Java 3d,我用Java3D制作了一个房间,并为用户添加了使用键盘移动选定对象的功能。我只是想知道是否有可能检测到我的两个物体之间的碰撞。我只想允许用户向某个方向移动一个对象,直到它与另一个任意对象碰撞 以下是我的两个对象,如果有帮助的话: 地毯: public class Carpet3D extends Group{ public Carpet3D() { this.createSceneGraph(); } public void createScene

我用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));
    }

这是可能的。Java3D不会为您检测冲突检测,因此您必须提出自己的检查冲突的方法

这样做的方法是为每个对象计算一个边界框(或提供一个边界框),然后迭代所有对象,查看边界框之间是否存在任何交点。这是一个相当简单的算法,任何游戏编程或3d数学编程书都会涉及到

如果对象是球形或不需要精度,也可以使用边界球体。球体相交检查比边界框碰撞检查便宜一点。不过,两者都相对便宜


如果你打算使用数字对象,那么最好使用诸如二进制空间分区或八叉树之类的数据结构来进一步优化搜索空间。

当然有可能,只需要进行大量细致的计算。我想我理解。我认为可能有一种比迭代场景中的每个对象更直接的方法,但这已经足够公平了。谢谢你的回复!