Chien Yang,
Here is the code. It mostly comes from the code of bug 4910683 submitted by Peter
Antoniac(Thanks goes to him:-) Until I carefully read through this piece of code have
I realized we are having the same problem, forgive me, anyway, I am a newcomer to
j3d.....), What I have changed is in line 80:
The orginal code is:
pCHandle=new CollisionHandler(pBox);
I change this to:
pCHandle=new CollisionHandler(pTGroup_1);
When press \'R\' or \'r\', NullPointerException will come out
---------------BEGIN OF CODE----------------------
/*
* testCollision.java
*
* Modified on line 80, May 21, 2004, 0:01 PM, by Samuel Liu
*
* Created on October 17, 2002, 11:39 PM By Peter Antoniac
*
*/
import javax.media.j3d.*;
import java .awt.*;
import javax.vecmath.*;
/**
*
* @author Peter Antoniac
*/
public class testCollision extends javax.swing.JFrame {
public testCollision() {
super("Testing Collision removal");
setSize(300,400);
getContentPane().setLayout(new java.awt.BorderLayout());
setBackground(new java.awt.Color(255, 255, 255));
setName("Testing Window");
// Create the 3D Objects
pGTmpl3D = new GraphicsConfigTemplate3D();
pGConf = getGraphicsConfiguration();
pGDev = pGConf.getDevice();
pCanvas3D = new Canvas3D(pGTmpl3D.getBestConfiguration(pGDev.getConfigurations()));
getContentPane().add(pCanvas3D);
setVisible(true);
pUniverse = new VirtualUniverse();
pLocale = new Locale(pUniverse);
pView = new View();
pVPlatform = new ViewPlatform();
pBGroup_0 = new BranchGroup();
pTGroup_0 = new TransformGroup();
/**
* Construct the ViewBranch
*/
pView.addCanvas3D(pCanvas3D);
pView.setPhysicalBody(new PhysicalBody());
pView.setPhysicalEnvironment(new PhysicalEnvironment());
pTGroup_0.addChild(pVPlatform);
pBGroup_0.addChild(pTGroup_0);
pLocale.addBranchGraph(pBGroup_0);
pView.attachViewPlatform(pVPlatform);
/**
* Creating the Content Branch including the Collision...
*/
pBGroup_1 = new BranchGroup();
pTGroup_1 = new TransformGroup();
// Capabilities settings
pBGroup_1.setCapability(BranchGroup.ALLOW_DETACH);
// this is for debugging only!
pBGroup_1.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
/** this is for moving the node
* pTGroup_1.setCapability(Node.ALLOW_LOCAL_TO_VWORLD_READ);
* pTGroup_1.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
* pTGroup_1.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
* pTGroup_1.setCapability(Group.ALLOW_CHILDREN_EXTEND);
*/
pTGroup_1.setCapability(TransformGroup.ALLOW_CHILDREN_EXTEND);
pTGroup_1.setCapability(TransformGroup.ALLOW_CHILDREN_READ);
pTGroup_1.setCapability(TransformGroup.ALLOW_CHILDREN_WRITE);
pAppearance = new Appearance();
pAppearance.setMaterial(new Material(
new Color3f(1,0,0), //ambient color
new Color3f(1,0,0), //emisive color
new Color3f(1,0,0), //difuse color
new Color3f(1,1,1), //specular color
100f));
pBox=new com.sun.j3d.utils.geometry.Box(0.2f,0.2f,0.2f,pAppearance);
pT3D_0 = new Transform3D();
pT3D_1 = new Transform3D();
pT3D_0.setTranslation(new Vector3d(0,0,-4));
pT3D_1.rotX(Math.PI/4);
pT3D_0.mul(pT3D_1);
pTGroup_1.setTransform(pT3D_0);
pTGroup_1.addChild(pBox);
pBGroup_1.addChild(pTGroup_1);
// Setting the collision object
pCHandle=new CollisionHandler(pTGroup_1); //Here I change it. Samuel Liu
pCHandle.setSchedulingBounds(new BoundingSphere(new Point3d(0,0,0),0.4d));
pTGroup_1.addChild(pCHandle);
// Live
pLocale.addBranchGraph(pBGroup_1);
// setResizable(false);
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
pCanvas3D.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent e) {
switch (e.getKeyChar()) {
case \'P\':
case \'p\':
if (!pBGroup_1.isLive()){
System.out.println("Placing the box");
pLocale.addBranchGraph(pBGroup_1);
}
break;
case \'R\':
case \'r\':
if (pBGroup_1.isLive()&&pCHandle.isLive()){
System.out.println("Removing the box");
// Using Locale.removeBranchGraph or detach has the same effect! :((
// pLocale.removeBranchGraph(pBGroup_1);
pBGroup_1.detach();
}
break;
default:
System.out.println("Wrong key. Press P or R keys!");
}
}
});
pCanvas3D.requestFocus();
pack();
}
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new testCollision().show();
}
// Variables declaration
private VirtualUniverse pUniverse;
private Canvas3D pCanvas3D;
private Locale pLocale;
private View pView;
private ViewPlatform pVPlatform;
private TransformGroup pTGroup_0,pTGroup_1;
private BranchGroup pBGroup_0,pBGroup_1;
private Transform3D pT3D_0,pT3D_1;
private Appearance pAppearance;
private com.sun.j3d.utils.geometry.Box pBox;
private GraphicsConfigTemplate3D pGTmpl3D;
private GraphicsDevice pGDev;
private GraphicsConfiguration pGConf;
private CollisionHandler pCHandle;
class CollisionHandler extends Behavior {
/** Creates a new instance of CollissionHandler */
public CollisionHandler(Node pNode) {
armingNode(pNode);
}
/** Initialize should be call after armingNode! Best practice is to first set the
node
* and later to add the object to the scenegraph */
public void initialize() {
wakeupOn(pWOr);
}
public void processStimulus(java.util.Enumeration e) {
System.out.println("Debug collision: BINGO!");
while(e.hasMoreElements()){
WakeupCriterion pWCriterion = (WakeupCriterion) e.nextElement();
if (pWCriterion instanceof WakeupOnCollisionEntry){
//something
}
else if (pWCriterion instanceof WakeupOnCollisionExit){
//something else
}
}
// set next WakeupCriterion
wakeupOn(pWOr);
}
void armingNode(Node pNode) {
aWCriteria[0]= pWUEnter = new
WakeupOnCollisionEntry(pNode,WakeupOnCollisionEntry.USE_GEOMETRY);
aWCriteria[1]= pWUExit = new
WakeupOnCollisionExit(pNode,WakeupOnCollisionExit.USE_GEOMETRY);
pWOr=new WakeupOr(aWCriteria);
}
private WakeupCriterion[] aWCriteria = new WakeupCriterion[2];
private WakeupOr pWOr;
private WakeupOnCollisionEntry pWUEnter;
private WakeupOnCollisionExit pWUExit;
}
}
------------------END OF CODE---------------------
----- Original Message -----
From: Chien Yang <[EMAIL PROTECTED]>
To: [EMAIL PROTECTED]
Cc: [EMAIL PROTECTED]
Subject: Re: [JAVA3D]
Samuel,
Very strange. Can you send us a reproducible test program ?
- Chien Yang
Java 3D, Sun Microsystems.
Liu Samuel wrote:
>Chien Yang:
> I tried it out, but unfortunately failed, the exception comes out again.
> Any more suggestions to have this job done?
> The weird thing is even I disable these behaviors before I detach the BranchGroup,
> this problem still will be there. Really troublesome.
> Thanks for your reply!
>
>Samuel Liu
>
>----- Original Message -----
>From: Chien Yang <[EMAIL PROTECTED]>
>To: [EMAIL PROTECTED]
>Cc: [EMAIL PROTECTED]
>Subject: Re: [JAVA3D]
>
>Samuel,
> Have you tried doing the detach in a j3d Behavior ?
>There is a known MT bug with detach() executed in the user thread.
>
>- Chien Yang
> Java 3D, Sun Microsystems Inc.
>
>
>
>
>Liu Samuel wrote:
>
>
>>hi all,
>>There is a NullPointerException occured when I attempted to detach a BranchGroup
>>from the locale, there is a collision detection behavior serves as a child of the
>>BranchGroup. The call trace is:
>>
>>java.lang.NullPointerException
>> at
>> javax.media.j3d.TransformGroupRetained.getCurrentChildLocalToVworld(TransformGroupRetained.java:1029)
>> at
>> javax.media.j3d.TransformGroupRetained.getCurrentChildLocalToVworld(TransformGroupRetained.java:1025)
>> at
>> javax.media.j3d.TransformGroupRetained.transformBounds(TransformGroupRetained.java:1000)
>> at
>> javax.media.j3d.WakeupOnCollisionEntry.updateCollisionBounds(WakeupOnCollisionEntry.java:410)
>> at
>> javax.media.j3d.GeometryStructure.processCollisionDetection(GeometryStructure.java:866)
>> at
>> javax.media.j3d.GeometryStructure.processMessages(GeometryStructure.java:214)
>> at javax.media.j3d.StructureUpdateThread.doWork(StructureUpdateThread.java:83)
>> at javax.media.j3d.J3dThread.run(J3dThread.java:250)
>>
>>Seems to be an internel exception. Why? Bug of j3d collision detection? Any
>>walkarounds?
>>
>>Many many many thanks for your help:-)
>>
>>Liu
>>
>>
>
>
>
>
>
>