I've written a simple program on the lines of TickTockCollision to detect
collision between 2 cylinders that can be translated and rotated.
But due to some bug, as soon as the program starts the cylinders fall into
collision(as indicated by highlighted color) and never exit.
can someone please find the bug for me
Thanks
import java.applet.Applet;
import com.sun.j3d.utils.behaviors.picking.*;
import java.awt.BorderLayout;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import com.sun.j3d.utils.geometry.*;
import javax.vecmath.*;
import java.awt.*;
class newproject extends Applet{
BranchGroup createscene(Canvas3D c){
BranchGroup objroot = new BranchGroup();
TransformGroup objscale = new TransformGroup();
Transform3D t3d = new Transform3D();
t3d.setScale(0.4);
objscale.setTransform(t3d);
Appearance genapp=new Appearance();
ColoringAttributes ca = new ColoringAttributes();
ca.setColor(new Color3f(0.0f,1.0f,0.0f));
genapp.setCapability(Appearance.ALLOW_COLORING_ATTRIBUTES_WRITE);
genapp.setColoringAttributes(ca);
TransformGroup cylindertg = new TransformGroup();
cylindertg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
cylindertg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE)
cylindertg.setCapability(TransformGroup.ENABLE_PICK_REPORTING)
Transform3D gent3d = new Transform3D();
gent3d.setTranslation(new Vector3d(-2,-0.2,0));
cylindertg.setTransform(gent3d);
Cylinder mycylinder1 = new Cylinder(0.2f,1.0f,genapp);
mycylinder1.setAppearance(genapp);
cylindertg.addChild(mycylinder1);
TransformGroup cylindertg1 = new TransformGroup();
cylindertg1.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
cylindertg1.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
cylindertg1.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
gent3d.setTranslation(new Vector3d(1.5,-0.2,0));
cylindertg1.setTransform(gent3d);
Cylinder mycylinder2 = new Cylinder(0.5f,1.0f,genapp);
cylindertg1.addChild(mycylinder2);
myCollisionDetector detectcollide = new
myCollisionDetector(mycylinder1.getShape(0));
BoundingSphere bounds = new BoundingSphere(new Point3d(0,0,0),100);
detectcollide.setSchedulingBounds(bounds);
cylindertg.addChild(detectcollide);
objscale.addChild(cylindertg);
objscale.addChild(cylindertg1);
objroot.addChild(objscale);
PickRotateBehavior behavior1 = new PickRotateBehavior(objroot,c,bounds);
PickTranslateBehavior behavior2 = new
PickTranslateBehavior(obroot,c,bounds);
objroot.addChild(behavior1);
objroot.addChild(behavior2);
objroot.compile();
return objroot ;
}
public newproject(){
setLayout(new BorderLayout());
Canvas3D c = new Canvas3D(null);
add("Center",c);
BranchGroup scene = createscene(c);
SimpleUniverse u = new SimpleUniverse(c);
u.getViewingPlatform().setNominalViewingTransform();
u.addBranchGraph(scene);
}
public static void main(String args[]) {
new MainFrame(new newproject(),500,350);
}
}
class myCollisionDetector extends Behavior {
private static final Color3f highlightColor =
new Color3f(0.0f, 0.0f, 1.0f);
private static final ColoringAttributes highlight =
new ColoringAttributes(highlightColor,
ColoringAttributes.SHADE_GOURAUD);
private boolean inCollision = false;
private Shape3D shape;
private ColoringAttributes shapeColoring;
private Appearance shapeAppearance;
private WakeupOnCollisionEntry wEnter;
private WakeupOnCollisionExit wExit;
private TransformGroup linetg = new TransformGroup();
public myCollisionDetector(Shape3D s) {
shape = s;
shapeAppearance = shape.getAppearance();
shapeColoring = shapeAppearance.getColoringAttributes();
inCollision = false;
}
public void initialize() {
wEnter = new WakeupOnCollisionEntry(shape);
wExit = new WakeupOnCollisionExit(shape);
wakeupOn(wEnter);
}
public void processStimulus(java.util.Enumeration criteria) {
inCollision = !inCollision;
if (inCollision) {
shapeAppearance.setColoringAttributes(highlight);
wakeupOn(wExit);
}
else {
shapeAppearance.setColoringAttributes(shapeColoring);
wakeupOn(wEnter);
}
}
}
=====================================================================
To subscribe/unsubscribe, send mail to [EMAIL PROTECTED]
Java 3D Home Page: http://java.sun.com/products/java-media/3D/