Login  Register

Re: Collision Detection Bug in Java 3D 1.5.2

Posted by Christian on Oct 16, 2015; 6:09pm
URL: https://forum.jogamp.org/Collision-Detection-Bug-in-Java-3D-1-5-2-tp4035467p4035492.html

I had a look to the source code of "Arabian Flights". The developer don't use the Behavior class for collision detection. If I see it right he made his own detection in class PhysicsEngine method collisionCheck(Particle part1, Particle part2). I think out intention should be to solve that the Java3D Behavior for collision detection is working properly.

I do not believe that the problem has to do with adding a node and a children of the node to the SceneGraph, because I didn't. The TransformGroup consisting the Sphere I added to the SceneGraph is not a children of the Behaviour class in the SceneGraph. It is just a attribute in the class.

If you concern about this I improved the code a bit to make it more clear:

package com.example.coll;

import java.util.Enumeration;

import javax.media.j3d.Behavior;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.DirectionalLight;

import javax.media.j3d.Node;
import javax.media.j3d.Shape3D;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.WakeupCriterion;
import javax.media.j3d.WakeupOnCollisionEntry;
import javax.media.j3d.WakeupOnCollisionExit;
import javax.media.j3d.WakeupOnCollisionMovement;
import javax.media.j3d.WakeupOr;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;

import javax.vecmath.Vector3d;
import javax.vecmath.Vector3f;


import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.universe.SimpleUniverse;

public class CollisionBugExample {

        public CollisionBugExample() {
                SimpleUniverse universe = new SimpleUniverse();
                BranchGroup group = new BranchGroup();

                Particle shape1 = new Particle(0.3f);
                group.addChild(shape1.getSphereTGrp());
                shape1.setPosition(new Vector3d(+0.2, 0.0, 0.0));
           
                Particle shape2 = new Particle(0.3f);
                group.addChild(shape2.getSphereTGrp());
                shape2.setPosition(new Vector3d(-0.2, 0.0, 0.0));
           
                Particle shape3 = new Particle(0.1f);
                group.addChild(shape3.getSphereTGrp());
               
                CollisionDetection collDet = new CollisionDetection(shape3.getShape());
                group.addChild(collDet);
               
                Color3f light1Color = new Color3f(1.8f, 0.1f, 0.1f);
                BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
                Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);
                DirectionalLight light1 = new DirectionalLight(light1Color, light1Direction);
                light1.setInfluencingBounds(bounds);
                group.addChild(light1);

                universe.getViewingPlatform().setNominalViewingTransform();
                universe.addBranchGraph(group);
           
                // move the moving sphere
           
                for(double i = 1.0; i > -1.0; i -= 0.01) {
                        shape3.setPosition(new Vector3d(i, 0.0, 0.0));
                        try {
                                Thread.sleep(100);
                        } catch (InterruptedException e) {
                                e.printStackTrace();
                        }
                }
        }

        public static void main(String[] args) {
                new CollisionBugExample();
        }
}

class Particle {
       
        private Sphere sphere;
       
        private TransformGroup transGrp;
       
        public Particle(float size) {
                transGrp = new TransformGroup();
                transGrp.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
                sphere = new Sphere(size);
                transGrp.addChild(sphere);
        }

        public TransformGroup getSphereTGrp() {
                return transGrp;
        }
       
        public Shape3D getShape() {
                return sphere.getShape();
        }
       
        public void setPosition(Vector3d position) {
                Transform3D transform = new Transform3D();
                transform.setTranslation(position);
                transGrp.setTransform(transform);
        }
}

class CollisionDetection extends Behavior {
       
        private Shape3D shape;

        private WakeupCriterion[] collisionCriteria;
        private WakeupOr oredCriteria;
       
        public CollisionDetection(Shape3D shape) {
                this.shape = shape;
                setSchedulingBounds(new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0));
        }
       
        @Override
        public void initialize() {
                collisionCriteria = new WakeupCriterion[3];
                collisionCriteria[0] = new WakeupOnCollisionEntry(shape);
                collisionCriteria[1] = new WakeupOnCollisionExit(shape);
                collisionCriteria[2] = new WakeupOnCollisionMovement(shape);
                oredCriteria = new WakeupOr(collisionCriteria);
                wakeupOn(oredCriteria);
        }

        @Override
        public void processStimulus(Enumeration criteria) {
                WakeupCriterion theCriterion = (WakeupCriterion) criteria.nextElement();
                Node collisionShape;

                if(theCriterion instanceof WakeupOnCollisionEntry) {
                        collisionShape = ((WakeupOnCollisionEntry) theCriterion).getTriggeringPath().getObject();
                } else if(theCriterion instanceof WakeupOnCollisionExit) {
                        collisionShape = ((WakeupOnCollisionExit) theCriterion).getTriggeringPath().getObject();
                } else {
                        collisionShape = ((WakeupOnCollisionMovement) theCriterion).getTriggeringPath().getObject();
                }
               
                System.out.println(theCriterion + " : collide with " + collisionShape);
               
                wakeupOn(oredCriteria);
        }
}

With this implementation the problem still exists.

Christian