Pergunta

I have made a room in Java3D and added the ability for the User to move selected objects using the keyboard. I was just wondering if it would be possible to detect a collision between two of my objects. I wanted to simply allow the User to move an object in some direction until it collides with another arbitrary object.

Here are two of my objects if that helps:

Carpet:

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

Dinner 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));
    }
Foi útil?

Solução

It is possible. Java3D does not detect collision detection for you so you will have to come up with your own method of checking for a collision.

The way of doing this is calculating a bounding box(or supplying one) for each object and then iterating over all of the objects to see if there are any intersections between the bounding boxes. It's a fairly simple algorithm that any game programming or 3d math programming book will cover.

You can also use bounding spheres if the object is spherical or you don't need precision. A sphere intersection check is a bit cheaper than a bounding box collision check. Both are relatively cheap though.

If you are going to have number objects, it would be wise to look into further optimizing the search space by using data structures such as binary space partitions or octrees.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top