Dean,
Set
the collidable property on the Nodes you want Java 3D to check for collisions.
Set the scheduling bounds on the Collision Behavior large enough to include all
the Nodes. Add the collision behavior to the scenegraph. Set the Node.ENABLE_COLLISION_REPORTING
capability.
I
would also make your behavior wake up on EITHER wEnter OR wExit as you may miss
notifications. I.e. you might get two exits and no corresponding enter. The
collision detection is running on a separate thread and is pretty liberal about
when it tells you about collisions.
Hope
that gets you running!
Sincerely,
Daniel
Selman
[EMAIL PROTECTED] http://www.tornadolabs.com
Hi All,
I am trying to write a collision detection
routine. I have followed the examples in the "Ready-to-Run Java
3D" book and also from mail archive from Tony Burrows. I have
been unable to get it to work. Whenever I try to use the collision
detection routine it hangs the process and I have to go into Task Manager on
WinNT and shut down the task. I have also looked at the
"TickTockCollision" Example and have not been able to get any
farther. I am reading in a VRML file with named nodes and am setting
collision detection on those nodes. Can anyone help?
Please.
Also, do I set the collision detection on the
node that I am moving or on all the other nodes? That part was not
quite clear to me.
Below is my collision detector
class:
import java.util.*; import
javax.media.j3d.*; import javax.vecmath.*; import
com.sun.j3d.utils.geometry.Primitive;
public class CollisionDetector extends
Behavior { private boolean
inCollision = false; private WakeupOnCollisionEntry
wEnter; private WakeupOnCollisionExit wExit; private
TransformGroup targetObject;
public
CollisionDetector(TransformGroup obj) { inCollision =
false; targetObject = obj;
System.out.println("Collider
Created..."); }
public void
initialize() { wEnter = new
WakeupOnCollisionEntry(targetObject); wExit = new
WakeupOnCollisionExit(targetObject); wakeupOn(wEnter);
System.out.println("Collider
Initialized..."); }
public void
processStimulus(Enumeration criteria) { inCollision =
!inCollision; if (inCollision) {
System.out.println("Collision Detected ...");
wakeupOn(wExit); } else {
wakeupOn(wEnter); } } }
Below is where I instantiate the collision
detector class:
file://Load in
the Fixture VRML file Scene scene = null;
VrmlLoader loader = new VrmlLoader(); try
{ URL loadURL = new
URL(this.getCodeBase(),fixtureFile); try
{ System.out.println("loadURL = " +
loadURL.toString()); scene =
loader.load(loadURL); } catch (Exception
e) {
System.out.println("Exception loading URL:" + e);
} } catch (MalformedURLException badURL)
{ file://location may be a
path name try { scene
= loader.load(fixtureFile); } catch
(Exception e) {
System.out.println("Exception loading file from path: " +
e.getMessage()); } } if (scene !=
null) { fixture_BG =
scene.getSceneGroup();
fixture_BG.setCapability(BranchGroup.ALLOW_DETACH);
fixture_BG.setCapability(BranchGroup.ALLOW_BOUNDS_READ);
fixture_BG.setCapability(BranchGroup.ALLOW_BOUNDS_WRITE);
file://Add the machine to the
transform group
fixture_TG.addChild(fixture_BG); file://Set capabilities on all named
nodes TransformGroup nodeGroup = new
TransformGroup(); String nodeID;
Hashtable allNodes = scene.getNamedObjects(); if
(!allNodes.isEmpty()) { Enumeration e
= allNodes.keys(); if
(e.hasMoreElements()) {
do { nodeID =
(String)e.nextElement(); if
(allNodes.get(nodeID).getClass() ==
nodeGroup.getClass())
{ System.out.println("NodeID =
" + nodeID);
file://Get the node group (transformaion
group) nodeGroup =
(TransformGroup)allNodes.get(nodeID);
file://Set node group
capabilities
nodeGroup.setCapability(TransformGroup.ALLOW_BOUNDS_READ);
nodeGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
nodeGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
nodeGroup.setCapability(TransformGroup.ALLOW_COLLISION_BOUNDS_READ);
nodeGroup.setCapability(TransformGroup.ALLOW_COLLISION_BOUNDS_WRITE);
nodeGroup.setCapability(TransformGroup.ALLOW_COLLIDABLE_READ);
nodeGroup.setCapability(TransformGroup.ALLOW_COLLIDABLE_WRITE);
if
(nodeID.equals("Part"))
{ file://Create the collision detector for this
node. CollisionDetector
cd = new
CollisionDetector(nodeGroup); //
cd.setSchedulingBounds(nodeBounds);
nodeGroup.addChild(cd);
detectors.put((String)nodeID,(CollisionDetector)cd);
}
System.out.println("number of children = " +
nodeGroup.numChildren());
file://Set collidable status for the node group to
true;
nodeGroup.setCollidable(true);
validNodes.put((String)nodeID,(TransformGroup)nodeGroup);
} } while
(e.hasMoreElements()); }
allNodes.clear(); } }
file://Add the scene to the scene graph to the
locale locale.addBranchGraph(world_BG); file://now that the scene group is "live"
calculate the new bounds BoundingSphere sceneBounds =
(BoundingSphere)world_BG.getBounds(); file://set up a view point to include the
bounds SetViewPoint(sceneBounds);
file://Now that a scene bounds has been
established, Set the scheduling bounds for each of the collision detectors
created. CollisionDetector temp_CD; TransformGroup
temp_Node; if (!validNodes.isEmpty())
{ String nodeID; Enumeration e =
validNodes.keys(); if (e.hasMoreElements())
{ do
{ nodeID =
(String)e.nextElement();
temp_Node = (TransformGroup)
validNodes.get(nodeID);
temp_Node.setCollisionBounds(temp_Node.getBounds());
validNodes.put((String)nodeID,
(TransformGroup)temp_Node);
if
(detectors.contains(nodeID))
{ temp_CD = (CollisionDetector)
detectors.get(nodeID);
temp_CD.setSchedulingBounds(temp_Node.getBounds());
System.out.println("temp_Node = " +
temp_Node.getBounds()); }
} while (e.hasMoreElements());
} detectors.clear(); }
Thanks for any suggestion that you can give
me.
Dean
|