Détection des collisions
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Novembre 2001 */
import java.applet.Applet;
import java.awt.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
public class SimpleCollision extends Applet {
private Group createBox() {
TransformGroup objTrans = new TransformGroup();
Appearance a = new Appearance();
Box shape = new Box(0.15f,0.1f,0.08f,Primitive.GENERATE_NORMALS,a);
objTrans.addChild(shape);
Material m = new Material();
m.setCapability(Material.ALLOW_COMPONENT_WRITE);
m.setDiffuseColor(0.7f,0.6f,0.1f);
a.setCapability(a.ALLOW_MATERIAL_WRITE);
a.setMaterial(m);
CollisionDetector cd = new CollisionDetector(shape);
BoundingSphere bounds;
bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0),100.0);
cd.setSchedulingBounds(bounds);
objTrans.addChild(cd);
return objTrans;
}
public BranchGroup createSceneGraph(SimpleUniverse u) {
BranchGroup objRoot = new BranchGroup();
BoundingSphere bounds;
bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0),100.0);
Vector3f ldir = new Vector3f(1.0F,1.0F,-1.0F);
Color3f lcouldl = new Color3f(1.0F,1.0F,1.0F);
DirectionalLight dl = new DirectionalLight(lcouldl,ldir);
dl.setInfluencingBounds(bounds);
objRoot.addChild(dl);
{ TransformGroup ot = new TransformGroup();
Transform3D t = new Transform3D();
t.setTranslation(new Vector3f(0.3F,0.0F,0.0F));
Transform3D tt = new Transform3D();
tt.setRotation(new AxisAngle4f(1.0F,0.0F,0.0F,(float) Math.PI/2.0f));
t.mul(tt);
ot.setTransform(t);
TransformGroup ot1 = new TransformGroup();
ot1.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
Transform3D tra1 = new Transform3D();
Alpha ra1 = new Alpha(-1,Alpha.INCREASING_ENABLE,
0,0,
5000,0,0,
0,0,0);
RotationInterpolator r1;
r1 = new RotationInterpolator(ra1,
ot1,
tra1,
0.0f,
(float) Math.PI*2.0f);
r1.setSchedulingBounds(bounds);
TransformGroup ot2 = new TransformGroup();
Transform3D t2 = new Transform3D();
t2.setTranslation(new Vector3f(0.3F,0.0F,0.0F));
ot2.setTransform(t2);
TransformGroup ot3 = new TransformGroup();
ot3.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
Transform3D tra3 = new Transform3D();
Alpha ra3 = new Alpha(-1,Alpha.INCREASING_ENABLE,
0,0,
2000,0,0,
0,0,0);
RotationInterpolator r3;
r3 = new RotationInterpolator(ra3,
ot3,
tra3,
0.0f,
(float) Math.PI*2.0f);
r3.setSchedulingBounds(bounds);
Transform3D ttt = new Transform3D();
ttt.setRotation(new AxisAngle4f(1.0F,1.0F,1.0F,(float) Math.PI/2.0f));
TransformGroup ot4 = new TransformGroup();
ot4.setTransform(ttt);
objRoot.addChild(ot);
ot.addChild(ot1);
ot1.addChild(ot2);
ot1.addChild(r1);
ot2.addChild(ot4);
ot4.addChild(r3);
ot4.addChild(ot3);
Group box = createBox();
ot3.addChild(box); }
{ TransformGroup ot = new TransformGroup();
Transform3D t = new Transform3D();
t.setTranslation(new Vector3f(-0.3F,0.0F,0.0F));
Transform3D tt = new Transform3D();
tt.setRotation(new AxisAngle4f(1.0F,0.0F,0.0F,(float) Math.PI/2.0f));
t.mul(tt);
ot.setTransform(t);
TransformGroup ot1 = new TransformGroup();
ot1.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
Transform3D tra1 = new Transform3D();
Alpha ra1 = new Alpha(-1,Alpha.DECREASING_ENABLE,
0,0,
0,0,0,
3900,0,0);
RotationInterpolator r1;
r1 = new RotationInterpolator(ra1,
ot1,
tra1,
0.0f,
(float) Math.PI*2.0f);
r1.setSchedulingBounds(bounds);
TransformGroup ot2 = new TransformGroup();
Transform3D t2 = new Transform3D();
t2.setTranslation(new Vector3f(0.3F,0.0F,0.0F));
ot2.setTransform(t2);
TransformGroup ot3 = new TransformGroup();
ot3.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
Transform3D tra3 = new Transform3D();
Alpha ra3 = new Alpha(-1,Alpha.INCREASING_ENABLE,
0,0,
1100,0,0,
0,0,0);
RotationInterpolator r3;
r3 = new RotationInterpolator(ra3,
ot3,
tra3,
0.0f,
(float) Math.PI*2.0f);
r3.setSchedulingBounds(bounds);
Transform3D ttt = new Transform3D();
ttt.setRotation(new AxisAngle4f(1.0F,1.0F,1.0F,(float) Math.PI/2.0f));
TransformGroup ot4 = new TransformGroup();
ot4.setTransform(ttt);
objRoot.addChild(ot);
ot.addChild(ot1);
ot1.addChild(ot2);
ot1.addChild(r1);
ot2.addChild(ot4);
ot4.addChild(r3);
ot4.addChild(ot3);
Group box = createBox();
ot3.addChild(box); }
return objRoot;
}
public SimpleCollision() {
setLayout(new BorderLayout());
GraphicsConfiguration config;
config = SimpleUniverse.getPreferredConfiguration();
Canvas3D c = new Canvas3D(config);
add("Center",c);
SimpleUniverse u = new SimpleUniverse(c);
BranchGroup scene = createSceneGraph(u);
u.getViewingPlatform().setNominalViewingTransform();
u.addBranchGraph(scene);
}
public static void main(String[] args) {
new MainFrame(new SimpleCollision(),450,250);
}
}
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Novembre 2001 */
import com.sun.j3d.utils.geometry.*;
import java.util.Enumeration;
import javax.media.j3d.*;
import javax.vecmath.*;
public class CollisionDetector extends Behavior {
private static Material highlight;
private boolean inCollision = false;
private Box shape;
private Material shapeMaterial;
private Appearance shapeAppearance;
private WakeupOnCollisionEntry wEnter;
private WakeupOnCollisionExit wExit;
public CollisionDetector(Box s) {
highlight = new Material();
highlight.setDiffuseColor(new Color3f(1.0f,0.0f,0.0f));
shape = s;
shapeAppearance = shape.getAppearance();
shapeMaterial = shapeAppearance.getMaterial();
inCollision = false;
}
public void initialize() {
wEnter = new WakeupOnCollisionEntry(shape);
wExit = new WakeupOnCollisionExit(shape);
wakeupOn(wEnter);
}
public void processStimulus(Enumeration criteria) {
inCollision = !inCollision;
if (inCollision) {
shapeAppearance.setMaterial(highlight);
wakeupOn(wExit);}
else {
shapeAppearance.setMaterial(shapeMaterial);
wakeupOn(wEnter);}
}
}
Remarques,erreurs
nicolas.janey@univ-fcomte.fr