Java/3D/Collision
Содержание
Intersect Test
/*
* @(#)IntersectTest.java 1.10 02/10/21 13:48:59
*
* Copyright (c) 1996-2002 Sun Microsystems, Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met: -
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer. - Redistribution in binary
* form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* Neither the name of Sun Microsystems, Inc. or the names of contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
* IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
* NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
* LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
* OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
* LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
* INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
* CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
* OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGES.
*
* You acknowledge that Software is not designed,licensed or intended for use in
* the design, construction, operation or maintenance of any nuclear facility.
*/
import java.applet.Applet;
import java.awt.AWTEvent;
import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;
import java.awt.event.MouseEvent;
import java.util.Enumeration;
import javax.media.j3d.AmbientLight;
import javax.media.j3d.Appearance;
import javax.media.j3d.Behavior;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.GeometryArray;
import javax.media.j3d.Group;
import javax.media.j3d.LineArray;
import javax.media.j3d.Material;
import javax.media.j3d.Node;
import javax.media.j3d.PointArray;
import javax.media.j3d.PolygonAttributes;
import javax.media.j3d.QuadArray;
import javax.media.j3d.Shape3D;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.TriangleArray;
import javax.media.j3d.View;
import javax.media.j3d.WakeupCriterion;
import javax.media.j3d.WakeupOnAWTEvent;
import javax.vecmath.Color3f;
import javax.vecmath.Color4f;
import javax.vecmath.Point3d;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3d;
import javax.vecmath.Vector3f;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.behaviors.keyboard.KeyNavigatorBehavior;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.picking.PickCanvas;
import com.sun.j3d.utils.picking.PickIntersection;
import com.sun.j3d.utils.picking.PickResult;
import com.sun.j3d.utils.picking.PickTool;
import com.sun.j3d.utils.universe.SimpleUniverse;
public class IntersectTest extends Applet {
BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0),
1000.0);
private SimpleUniverse u = null;
public BranchGroup createSceneGraph() {
// Create the root of the branch graph
BranchGroup objRoot = new BranchGroup();
// Set up the ambient light
Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f);
AmbientLight ambientLightNode = new AmbientLight(ambientColor);
ambientLightNode.setInfluencingBounds(bounds);
objRoot.addChild(ambientLightNode);
// Set up the directional lights
Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f);
Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);
Color3f light2Color = new Color3f(0.3f, 0.3f, 0.4f);
Vector3f light2Direction = new Vector3f(-6.0f, -2.0f, -1.0f);
DirectionalLight light1 = new DirectionalLight(light1Color,
light1Direction);
light1.setInfluencingBounds(bounds);
objRoot.addChild(light1);
DirectionalLight light2 = new DirectionalLight(light2Color,
light2Direction);
light2.setInfluencingBounds(bounds);
objRoot.addChild(light2);
Transform3D t3 = new Transform3D();
// Shapes
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 3; y++) {
for (int z = 0; z < 3; z++) {
t3.setTranslation(new Vector3d(-4 + x * 4.0, -4 + y * 4.0,
-20 - z * 4.0));
TransformGroup objTrans = new TransformGroup(t3);
objRoot.addChild(objTrans);
// Create a simple shape leaf node, add it to the scene
// graph.
GeometryArray geom = null;
if (((x + y + z) % 2) == 0) {
geom = new RandomColorCube();
} else {
geom = new RandomColorTetrahedron();
}
Shape3D shape = new Shape3D(geom);
// use the utility method to set the capabilities
PickTool.setCapabilities(shape, PickTool.INTERSECT_FULL);
objTrans.addChild(shape);
}
}
}
// Lines
Point3f[] verts = { new Point3f(-2.0f, 0.0f, 0.0f),
new Point3f(2.0f, 0.0f, 0.0f) };
Color3f grey = new Color3f(0.7f, 0.7f, 0.7f);
Color3f[] colors = { grey, grey };
for (int y = 0; y < 5; y++) {
for (int z = 0; z < 5; z++) {
t3.setTranslation(new Vector3d(7.0, -4 + y * 2.0, -20.0 - z
* 2.0));
TransformGroup objTrans = new TransformGroup(t3);
objRoot.addChild(objTrans);
LineArray la = new LineArray(verts.length,
LineArray.COORDINATES | LineArray.COLOR_3);
la.setCoordinates(0, verts);
la.setColors(0, colors);
Shape3D shape = new Shape3D();
shape.setGeometry(la);
// use the utility method to set the capabilities
PickTool.setCapabilities(shape, PickTool.INTERSECT_FULL);
objTrans.addChild(shape);
}
}
// Points
for (double x = -2.0; x <= 2.0; x += 1.0) {
for (double y = -2.0; y <= 2.0; y += 1.0) {
for (double z = -2.0; z <= 2.0; z += 1.0) {
t3.setTranslation(new Vector3d(-10.0 + 2.0 * x,
0.0 + 2.0 * y, -20.0 + 2.0 * z));
TransformGroup objTrans = new TransformGroup(t3);
objRoot.addChild(objTrans);
PointArray pa = new PointArray(1, PointArray.COORDINATES
| PointArray.COLOR_3);
pa.setCoordinate(0, new Point3d(0.0, 0.0, 0.0));
pa.setColor(0, grey);
Shape3D shape = new Shape3D();
shape.setGeometry(pa);
// use the utility method to set the capabilities
PickTool.setCapabilities(shape, PickTool.INTERSECT_FULL);
objTrans.addChild(shape);
}
}
}
return objRoot;
}
public IntersectTest() {
}
public void init() {
setLayout(new BorderLayout());
GraphicsConfiguration config = SimpleUniverse
.getPreferredConfiguration();
Canvas3D c = new Canvas3D(config);
add("Center", c);
// Create a simple scene and attach it to the virtual universe
BranchGroup scene = createSceneGraph();
u = new SimpleUniverse(c);
// Add picking behavior
IntersectInfoBehavior behavior = new IntersectInfoBehavior(c, scene,
0.05f);
behavior.setSchedulingBounds(bounds);
scene.addChild(behavior);
TransformGroup vpTrans = u.getViewingPlatform()
.getViewPlatformTransform();
KeyNavigatorBehavior keybehavior = new KeyNavigatorBehavior(vpTrans);
keybehavior.setSchedulingBounds(bounds);
scene.addChild(keybehavior);
scene.setCapability(Group.ALLOW_CHILDREN_EXTEND);
scene.rupile();
u.addBranchGraph(scene);
View view = u.getViewer().getView();
view.setBackClipDistance(100000);
}
public void destroy() {
u.cleanup();
}
//
// The following allows IntersectTest to be run as an application
// as well as an applet
//
public static void main(String[] args) {
String s = "\n\nIntersectTest:\n-----------\n";
s += "Pick with the mouse over the primitives\n";
s += "- A sphere will be placed to indicate the picked point.\n";
s += "If color information is available, the sphere will change color to reflect\n";
s += "the interpolated color.\n";
s += "- Other spheres will be placed to show the vertices of the selected polygon\n";
s += "- Information will be displayed about the picking operation\n\n\n";
System.out.println(s);
new MainFrame(new IntersectTest(), 640, 640);
}
}
class RandomColorTetrahedron extends TriangleArray {
RandomColorTetrahedron() {
super(12, GeometryArray.COORDINATES | GeometryArray.COLOR_3);
Point3f verts[] = new Point3f[4];
Color3f colors[] = new Color3f[4];
verts[0] = new Point3f(0.5f, 0.5f, 0.5f);
verts[1] = new Point3f(0.5f, -0.5f, -0.5f);
verts[2] = new Point3f(-0.5f, -0.5f, 0.5f);
verts[3] = new Point3f(-0.5f, 0.5f, -0.5f);
colors[0] = new Color3f(1.0f, 0.0f, 0.0f);
colors[1] = new Color3f(0.0f, 1.0f, 0.0f);
colors[2] = new Color3f(0.0f, 0.0f, 1.0f);
Point3f pnts[] = new Point3f[12];
Color3f clrs[] = new Color3f[12];
pnts[0] = verts[2];
clrs[0] = colors[(int) (Math.random() * 3.0)];
pnts[1] = verts[1];
clrs[1] = colors[(int) (Math.random() * 3.0)];
pnts[2] = verts[0];
clrs[2] = colors[(int) (Math.random() * 3.0)];
pnts[3] = verts[3];
clrs[3] = colors[(int) (Math.random() * 3.0)];
pnts[4] = verts[2];
clrs[4] = colors[(int) (Math.random() * 3.0)];
pnts[5] = verts[0];
clrs[5] = colors[(int) (Math.random() * 3.0)];
pnts[6] = verts[1];
clrs[6] = colors[(int) (Math.random() * 3.0)];
pnts[7] = verts[2];
clrs[7] = colors[(int) (Math.random() * 3.0)];
pnts[8] = verts[3];
clrs[8] = colors[(int) (Math.random() * 3.0)];
pnts[9] = verts[1];
clrs[9] = colors[(int) (Math.random() * 3.0)];
pnts[10] = verts[3];
clrs[10] = colors[(int) (Math.random() * 3.0)];
pnts[11] = verts[0];
clrs[11] = colors[(int) (Math.random() * 3.0)];
setCoordinates(0, pnts);
setColors(0, clrs);
}
}
class RandomColorCube extends QuadArray {
RandomColorCube() {
super(24, GeometryArray.COORDINATES | GeometryArray.COLOR_3);
Point3f verts[] = new Point3f[8];
Color3f colors[] = new Color3f[3];
verts[0] = new Point3f(0.5f, 0.5f, 0.5f);
verts[1] = new Point3f(-0.5f, 0.5f, 0.5f);
verts[2] = new Point3f(-0.5f, -0.5f, 0.5f);
verts[3] = new Point3f(0.5f, -0.5f, 0.5f);
verts[4] = new Point3f(0.5f, 0.5f, -0.5f);
verts[5] = new Point3f(-0.5f, 0.5f, -0.5f);
verts[6] = new Point3f(-0.5f, -0.5f, -0.5f);
verts[7] = new Point3f(0.5f, -0.5f, -0.5f);
colors[0] = new Color3f(1.0f, 0.0f, 0.0f);
colors[1] = new Color3f(0.0f, 1.0f, 0.0f);
colors[2] = new Color3f(0.0f, 0.0f, 1.0f);
Point3f pnts[] = new Point3f[24];
Color3f clrs[] = new Color3f[24];
pnts[0] = verts[0];
clrs[0] = colors[(int) (Math.random() * 3.0)];
pnts[1] = verts[3];
clrs[1] = colors[(int) (Math.random() * 3.0)];
pnts[2] = verts[7];
clrs[2] = colors[(int) (Math.random() * 3.0)];
pnts[3] = verts[4];
clrs[3] = colors[(int) (Math.random() * 3.0)];
pnts[4] = verts[1];
clrs[4] = colors[(int) (Math.random() * 3.0)];
pnts[5] = verts[5];
clrs[5] = colors[(int) (Math.random() * 3.0)];
pnts[6] = verts[6];
clrs[6] = colors[(int) (Math.random() * 3.0)];
pnts[7] = verts[2];
clrs[7] = colors[(int) (Math.random() * 3.0)];
pnts[8] = verts[0];
clrs[8] = colors[(int) (Math.random() * 3.0)];
pnts[9] = verts[4];
clrs[9] = colors[(int) (Math.random() * 3.0)];
pnts[10] = verts[5];
clrs[10] = colors[(int) (Math.random() * 3.0)];
pnts[11] = verts[1];
clrs[11] = colors[(int) (Math.random() * 3.0)];
pnts[12] = verts[3];
clrs[12] = colors[(int) (Math.random() * 3.0)];
pnts[13] = verts[2];
clrs[13] = colors[(int) (Math.random() * 3.0)];
pnts[14] = verts[6];
clrs[14] = colors[(int) (Math.random() * 3.0)];
pnts[15] = verts[7];
clrs[15] = colors[(int) (Math.random() * 3.0)];
pnts[16] = verts[0];
clrs[16] = colors[(int) (Math.random() * 3.0)];
pnts[17] = verts[1];
clrs[17] = colors[(int) (Math.random() * 3.0)];
pnts[18] = verts[2];
clrs[18] = colors[(int) (Math.random() * 3.0)];
pnts[19] = verts[3];
clrs[19] = colors[(int) (Math.random() * 3.0)];
pnts[20] = verts[7];
clrs[20] = colors[(int) (Math.random() * 3.0)];
pnts[21] = verts[6];
clrs[21] = colors[(int) (Math.random() * 3.0)];
pnts[22] = verts[5];
clrs[22] = colors[(int) (Math.random() * 3.0)];
pnts[23] = verts[4];
clrs[23] = colors[(int) (Math.random() * 3.0)];
setCoordinates(0, pnts);
setColors(0, clrs);
}
}
/**
* Class: IntersectInfoBehavior
*
* Description: Used to respond to mouse pick and drag events in the 3D window.
* Displays information about the pick.
*
* Version: 1.0
*
*/
class IntersectInfoBehavior extends Behavior {
float size;
PickCanvas pickCanvas;
PickResult[] pickResult;
Appearance oldlook, redlookwf, redlook, greenlook, bluelook;
Node oldNode = null;
GeometryArray oldGeom = null;
Color3f redColor = new Color3f(1.0f, 0.0f, 0.0f);
TransformGroup[] sphTrans = new TransformGroup[6];
Sphere[] sph = new Sphere[6];
Transform3D spht3 = new Transform3D();
public IntersectInfoBehavior(Canvas3D canvas3D, BranchGroup branchGroup,
float size) {
pickCanvas = new PickCanvas(canvas3D, branchGroup);
pickCanvas.setTolerance(5.0f);
pickCanvas.setMode(PickCanvas.GEOMETRY_INTERSECT_INFO);
this.size = size;
// Create an Appearance.
redlook = new Appearance();
Color3f objColor = new Color3f(0.5f, 0.0f, 0.0f);
Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
redlook.setMaterial(new Material(objColor, black, objColor, white,
50.0f));
redlook.setCapability(Appearance.ALLOW_MATERIAL_WRITE);
redlookwf = new Appearance();
redlookwf.setMaterial(new Material(objColor, black, objColor, white,
50.0f));
PolygonAttributes pa = new PolygonAttributes();
pa.setPolygonMode(pa.POLYGON_LINE);
pa.setCullFace(pa.CULL_NONE);
redlookwf.setPolygonAttributes(pa);
oldlook = new Appearance();
objColor = new Color3f(1.0f, 1.0f, 1.0f);
oldlook.setMaterial(new Material(objColor, black, objColor, white,
50.0f));
greenlook = new Appearance();
objColor = new Color3f(0.0f, 0.8f, 0.0f);
greenlook.setMaterial(new Material(objColor, black, objColor, white,
50.0f));
bluelook = new Appearance();
objColor = new Color3f(0.0f, 0.0f, 0.8f);
bluelook.setMaterial(new Material(objColor, black, objColor, white,
50.0f));
for (int i = 0; i < 6; i++) {
switch (i) {
case 0:
sph[i] = new Sphere(size * 1.15f, redlook);
break;
case 1:
sph[i] = new Sphere(size * 1.1f, greenlook);
break;
default:
sph[i] = new Sphere(size, bluelook);
break;
}
sph[i].setPickable(false);
sphTrans[i] = new TransformGroup();
sphTrans[i].setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
sphTrans[i].setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
// Add sphere, transform
branchGroup.addChild(sphTrans[i]);
sphTrans[i].addChild(sph[i]);
}
}
public void initialize() {
wakeupOn(new WakeupOnAWTEvent(MouseEvent.MOUSE_PRESSED));
}
public void processStimulus(Enumeration criteria) {
WakeupCriterion wakeup;
AWTEvent[] event;
int eventId;
while (criteria.hasMoreElements()) {
wakeup = (WakeupCriterion) criteria.nextElement();
if (wakeup instanceof WakeupOnAWTEvent) {
event = ((WakeupOnAWTEvent) wakeup).getAWTEvent();
for (int i = 0; i < event.length; i++) {
eventId = event[i].getID();
if (eventId == MouseEvent.MOUSE_PRESSED) {
int x = ((MouseEvent) event[i]).getX();
int y = ((MouseEvent) event[i]).getY();
pickCanvas.setShapeLocation(x, y);
Point3d eyePos = pickCanvas.getStartPosition();
pickResult = pickCanvas.pickAllSorted();
// Use this to do picking benchmarks
/*
* long start = System.currentTimeMillis(); for (int
* l=0;l <3;l++) { if (l == 0) System.out.print
* ("BOUNDS: "); if (l == 1) System.out.print
* ("GEOMETRY: "); if (l == 2) System.out.print
* ("GEOMETRY_INTERSECT_INFO: ");
*
* for (int k=0;k <1000;k++) { if (l == 0) {
* pickCanvas.setMode(PickTool.BOUNDS); pickResult =
* pickCanvas.pickAllSorted(); } if (l == 1) {
* pickCanvas.setMode(PickTool.GEOMETRY); pickResult =
* pickCanvas.pickAllSorted(); } if (l == 2) {
* pickCanvas.setMode(PickTool.GEOMETRY_INTERSECT_INFO);
* pickResult = pickCanvas.pickAllSorted(); } } long
* delta = System.currentTimeMillis() - start;
* System.out.println ("\t"+delta+" ms / 1000 picks"); }
*/
if (pickResult != null) {
// Get closest intersection results
PickIntersection pi = pickResult[0]
.getClosestIntersection(eyePos);
GeometryArray curGeomArray = pi.getGeometryArray();
// Position sphere at intersection point
Vector3d v = new Vector3d();
Point3d intPt = pi.getPointCoordinatesVW();
v.set(intPt);
spht3.setTranslation(v);
sphTrans[0].setTransform(spht3);
// Position sphere at closest vertex
Point3d closestVert = pi
.getClosestVertexCoordinatesVW();
v.set(closestVert);
spht3.setTranslation(v);
sphTrans[1].setTransform(spht3);
Point3d[] ptw = pi.getPrimitiveCoordinatesVW();
Point3d[] pt = pi.getPrimitiveCoordinates();
int[] coordidx = pi.getPrimitiveCoordinateIndices();
Point3d ptcoord = new Point3d();
for (int k = 0; k < pt.length; k++) {
v.set(ptw[k]);
spht3.setTranslation(v);
sphTrans[k + 2].setTransform(spht3);
}
// Get interpolated color (if available)
Color4f iColor4 = null;
Color3f iColor = null;
Vector3f iNormal = null;
if (curGeomArray != null) {
int vf = curGeomArray.getVertexFormat();
if (((vf & (GeometryArray.COLOR_3 | GeometryArray.COLOR_4)) != 0)
&& (null != (iColor4 = pi
.getPointColor()))) {
iColor = new Color3f(iColor4.x, iColor4.y,
iColor4.z);
// Change the point"s color
redlook.setMaterial(new Material(iColor,
new Color3f(0.0f, 0.0f, 0.0f),
iColor, new Color3f(1.0f, 1.0f,
1.0f), 50.0f));
}
if (((vf & GeometryArray.NORMALS) != 0)
&& (null != (iNormal = pi
.getPointNormal()))) {
System.out.println("Interpolated normal: "
+ iNormal);
}
}
System.out.println("=============");
System.out
.println("Coordinates of intersection pt:"
+ intPt);
System.out.println("Coordinates of vertices: ");
for (int k = 0; k < pt.length; k++) {
System.out.println(k + ":" + ptw[k].x + " "
+ ptw[k].y + " " + ptw[k].z);
}
System.out
.println("Closest vertex: " + closestVert);
if (iColor != null) {
System.out.println("Interpolated color: "
+ iColor);
}
if (iNormal != null) {
System.out.println("Interpolated normal: "
+ iNormal);
}
}
}
}
}
}
wakeupOn(new WakeupOnAWTEvent(MouseEvent.MOUSE_PRESSED));
}
}
The use of the CollisionDetector
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;
import javax.media.j3d.AmbientLight;
import javax.media.j3d.Appearance;
import javax.media.j3d.Behavior;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.Bounds;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.IndexedQuadArray;
import javax.media.j3d.Locale;
import javax.media.j3d.Material;
import javax.media.j3d.Node;
import javax.media.j3d.PhysicalBody;
import javax.media.j3d.PhysicalEnvironment;
import javax.media.j3d.Shape3D;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.View;
import javax.media.j3d.ViewPlatform;
import javax.media.j3d.VirtualUniverse;
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.Point3f;
import javax.vecmath.Vector3d;
import javax.vecmath.Vector3f;
import com.sun.j3d.utils.picking.behaviors.PickTranslateBehavior;
/**
* This class demonstrates the use of the CollisionDetector class to perform
* processing when objects collide. When this program is run the white cube can
* be selected and moved by dragging on it with the right mouse button. You
* should notice that there is a problem if the movable cube comes into contact
* with both of the static cubes at one time. A way round this is given in the
* SimpleCollision2 application.
*
* @see CollisionDetector
* @see SimpleCollision2
* @author I.J.Palmer
* @version 1.0
*/
public class SimpleCollision extends Frame implements ActionListener {
protected Canvas3D myCanvas3D = new Canvas3D(null);
protected Button exitButton = new Button("Exit");
protected BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0,
0.0), 100.0);
/** Transform for the left cube. */
protected TransformGroup leftGroup;
/** Transform for the right cube */
protected TransformGroup rightGroup;
/**
* Transform for the movable cube. This has read, write and pick reporting
* capabilities enabled.
*/
protected TransformGroup moveGroup;
/** A transform to change the size of the movable cube. */
protected TransformGroup scaleGroup;
/** The left static cube. */
protected Shape3D leftCube;
/** The right static cube. */
protected Shape3D rightCube;
/** The movable cube that will collide with the other two cubes */
protected Shape3D moveCube;
/**
* This builds the view branch of the scene graph.
*/
protected BranchGroup buildViewBranch(Canvas3D c) {
BranchGroup viewBranch = new BranchGroup();
Transform3D viewXfm = new Transform3D();
viewXfm.set(new Vector3f(0.0f, 0.0f, 10.0f));
TransformGroup viewXfmGroup = new TransformGroup(viewXfm);
ViewPlatform myViewPlatform = new ViewPlatform();
PhysicalBody myBody = new PhysicalBody();
PhysicalEnvironment myEnvironment = new PhysicalEnvironment();
viewXfmGroup.addChild(myViewPlatform);
viewBranch.addChild(viewXfmGroup);
View myView = new View();
myView.addCanvas3D(c);
myView.attachViewPlatform(myViewPlatform);
myView.setPhysicalBody(myBody);
myView.setPhysicalEnvironment(myEnvironment);
return viewBranch;
}
/**
* This adds some lights to the content branch of the scene graph.
*
* @param b
* The BranchGroup to add the lights to.
*/
protected void addLights(BranchGroup b) {
Color3f ambLightColour = new Color3f(0.5f, 0.5f, 0.5f);
AmbientLight ambLight = new AmbientLight(ambLightColour);
ambLight.setInfluencingBounds(bounds);
Color3f dirLightColour = new Color3f(1.0f, 1.0f, 1.0f);
Vector3f dirLightDir = new Vector3f(-1.0f, -1.0f, -1.0f);
DirectionalLight dirLight = new DirectionalLight(dirLightColour,
dirLightDir);
dirLight.setInfluencingBounds(bounds);
b.addChild(ambLight);
b.addChild(dirLight);
}
/**
* Creates the content branch of the scene graph.
*
* @return BranchGroup with content attached.
*/
protected BranchGroup buildContentBranch() {
//First create a different appearance for each cube
Appearance app1 = new Appearance();
Appearance app2 = new Appearance();
Appearance app3 = new Appearance();
Color3f ambientColour1 = new Color3f(1.0f, 0.0f, 0.0f);
Color3f ambientColour2 = new Color3f(1.0f, 1.0f, 0.0f);
Color3f ambientColour3 = new Color3f(1.0f, 1.0f, 1.0f);
Color3f emissiveColour = new Color3f(0.0f, 0.0f, 0.0f);
Color3f specularColour = new Color3f(1.0f, 1.0f, 1.0f);
Color3f diffuseColour1 = new Color3f(1.0f, 0.0f, 0.0f);
Color3f diffuseColour2 = new Color3f(1.0f, 1.0f, 0.0f);
Color3f diffuseColour3 = new Color3f(1.0f, 1.0f, 1.0f);
float shininess = 20.0f;
app1.setMaterial(new Material(ambientColour1, emissiveColour,
diffuseColour1, specularColour, shininess));
app2.setMaterial(new Material(ambientColour2, emissiveColour,
diffuseColour2, specularColour, shininess));
app3.setMaterial(new Material(ambientColour3, emissiveColour,
diffuseColour3, specularColour, shininess));
//Create the vertex data for the cube. Since each shape is
//a cube we can use the same vertex data for each cube
IndexedQuadArray indexedCube = new IndexedQuadArray(8,
IndexedQuadArray.COORDINATES | IndexedQuadArray.NORMALS, 24);
Point3f[] cubeCoordinates = { new Point3f(1.0f, 1.0f, 1.0f),
new Point3f(-1.0f, 1.0f, 1.0f),
new Point3f(-1.0f, -1.0f, 1.0f),
new Point3f(1.0f, -1.0f, 1.0f), new Point3f(1.0f, 1.0f, -1.0f),
new Point3f(-1.0f, 1.0f, -1.0f),
new Point3f(-1.0f, -1.0f, -1.0f),
new Point3f(1.0f, -1.0f, -1.0f) };
Vector3f[] cubeNormals = { new Vector3f(0.0f, 0.0f, 1.0f),
new Vector3f(0.0f, 0.0f, -1.0f),
new Vector3f(1.0f, 0.0f, 0.0f),
new Vector3f(-1.0f, 0.0f, 0.0f),
new Vector3f(0.0f, 1.0f, 0.0f), new Vector3f(0.0f, -1.0f, 0.0f) };
int cubeCoordIndices[] = { 0, 1, 2, 3, 7, 6, 5, 4, 0, 3, 7, 4, 5, 6, 2,
1, 0, 4, 5, 1, 6, 7, 3, 2 };
int cubeNormalIndices[] = { 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3,
3, 3, 4, 4, 4, 4, 5, 5, 5, 5 };
indexedCube.setCoordinates(0, cubeCoordinates);
indexedCube.setNormals(0, cubeNormals);
indexedCube.setCoordinateIndices(0, cubeCoordIndices);
indexedCube.setNormalIndices(0, cubeNormalIndices);
//Create the three cubes
leftCube = new Shape3D(indexedCube, app1);
rightCube = new Shape3D(indexedCube, app2);
moveCube = new Shape3D(indexedCube, app3);
//Define the user data so that we can print out the
//name of the colliding cube.
leftCube.setUserData(new String("left cube"));
rightCube.setUserData(new String("right cube"));
//Create the content branch and add the lights
BranchGroup contentBranch = new BranchGroup();
addLights(contentBranch);
//Create and set up the movable cube"s TransformGroup.
//This scales and translates the cube and then sets the
// read, write and pick reporting capabilities.
Transform3D moveXfm = new Transform3D();
moveXfm.set(0.7, new Vector3d(0.0, 2.0, 1.0));
moveGroup = new TransformGroup(moveXfm);
moveGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
moveGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
moveGroup.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
//Create the left cube"s TransformGroup
Transform3D leftGroupXfm = new Transform3D();
leftGroupXfm.set(new Vector3d(-1.5, 0.0, 0.0));
leftGroup = new TransformGroup(leftGroupXfm);
//Create the right cube"s TransformGroup
Transform3D rightGroupXfm = new Transform3D();
rightGroupXfm.set(new Vector3d(1.5, 0.0, 0.0));
rightGroup = new TransformGroup(rightGroupXfm);
//Add the behaviour to allow us to move the cube
PickTranslateBehavior pickTranslate = new PickTranslateBehavior(
contentBranch, myCanvas3D, bounds);
contentBranch.addChild(pickTranslate);
//Add our CollisionDetector class to detect collisions with
//the movable cube.
CollisionDetector myColDet = new CollisionDetector(moveCube, bounds);
contentBranch.addChild(myColDet);
//Create the content branch hierarchy.
contentBranch.addChild(moveGroup);
contentBranch.addChild(leftGroup);
contentBranch.addChild(rightGroup);
moveGroup.addChild(moveCube);
leftGroup.addChild(leftCube);
rightGroup.addChild(rightCube);
return contentBranch;
}
/**
* Process the exit button action to exit the application.
*/
public void actionPerformed(ActionEvent e) {
if (e.getSource() == exitButton) {
dispose();
System.exit(0);
}
}
public SimpleCollision() {
VirtualUniverse myUniverse = new VirtualUniverse();
Locale myLocale = new Locale(myUniverse);
myLocale.addBranchGraph(buildViewBranch(myCanvas3D));
myLocale.addBranchGraph(buildContentBranch());
setTitle("SimpleWorld");
setSize(400, 400);
setLayout(new BorderLayout());
Panel bottom = new Panel();
bottom.add(exitButton);
add(BorderLayout.CENTER, myCanvas3D);
add(BorderLayout.SOUTH, bottom);
exitButton.addActionListener(this);
setVisible(true);
}
public static void main(String[] args) {
SimpleCollision sw = new SimpleCollision();
}
}
/**
* A simple collision detector class. This responds to a collision event by
* printing a message with information about the type of collision event and the
* object that has been collided with.
*
* @author I.J.Palmer
* @version 1.0
*/
class CollisionDetector extends Behavior {
/** The separate criteria used to wake up this beahvior. */
protected WakeupCriterion[] theCriteria;
/** The OR of the separate criteria. */
protected WakeupOr oredCriteria;
/** The shape that is watched for collision. */
protected Shape3D collidingShape;
/**
* @param theShape
* Shape3D that is to be watched for collisions.
* @param theBounds
* Bounds that define the active region for this behaviour
*/
public CollisionDetector(Shape3D theShape, Bounds theBounds) {
collidingShape = theShape;
setSchedulingBounds(theBounds);
}
/**
* This creates an entry, exit and movement collision criteria. These are
* then OR"ed together, and the wake up condition set to the result.
*/
public void initialize() {
theCriteria = new WakeupCriterion[3];
theCriteria[0] = new WakeupOnCollisionEntry(collidingShape);
theCriteria[1] = new WakeupOnCollisionExit(collidingShape);
theCriteria[2] = new WakeupOnCollisionMovement(collidingShape);
oredCriteria = new WakeupOr(theCriteria);
wakeupOn(oredCriteria);
}
/**
* Where the work is done in this class. A message is printed out using the
* userData of the object collided with. The wake up condition is then set
* to the OR"ed criterion again.
*/
public void processStimulus(Enumeration criteria) {
WakeupCriterion theCriterion = (WakeupCriterion) criteria.nextElement();
if (theCriterion instanceof WakeupOnCollisionEntry) {
Node theLeaf = ((WakeupOnCollisionEntry) theCriterion)
.getTriggeringPath().getObject();
System.out.println("Collided with " + theLeaf.getUserData());
} else if (theCriterion instanceof WakeupOnCollisionExit) {
Node theLeaf = ((WakeupOnCollisionExit) theCriterion)
.getTriggeringPath().getObject();
System.out.println("Stopped colliding with "
+ theLeaf.getUserData());
} else {
Node theLeaf = ((WakeupOnCollisionMovement) theCriterion)
.getTriggeringPath().getObject();
System.out.println("Moved whilst colliding with "
+ theLeaf.getUserData());
}
wakeupOn(oredCriteria);
}
}
The use of two collision detectors to overcome the
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;
import javax.media.j3d.AmbientLight;
import javax.media.j3d.Appearance;
import javax.media.j3d.Behavior;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.Bounds;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.IndexedQuadArray;
import javax.media.j3d.Locale;
import javax.media.j3d.Material;
import javax.media.j3d.PhysicalBody;
import javax.media.j3d.PhysicalEnvironment;
import javax.media.j3d.Shape3D;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.View;
import javax.media.j3d.ViewPlatform;
import javax.media.j3d.VirtualUniverse;
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.Point3f;
import javax.vecmath.Vector3d;
import javax.vecmath.Vector3f;
import com.sun.j3d.utils.picking.behaviors.PickTranslateBehavior;
/**
* This class demonstrates the use of two collision detectors to overcome the
* problem of an object colliding with more than one object at a time. The white
* cube is movable by dragging it with the right mouse button.
*
* @see CollisionDetector2
* @author I.J.Palmer
* @version 1.0
*/
public class SimpleCollision2 extends Frame implements ActionListener {
protected Canvas3D myCanvas3D = new Canvas3D(null);
protected Button exitButton = new Button("Exit");
protected BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0,
0.0), 100.0);
/** Transform for the left cube. */
protected TransformGroup leftGroup;
/** Transform for the right cube */
protected TransformGroup rightGroup;
/**
* Transform for the movable cube. This has read, write and pick reporting
* capabilities enabled.
*/
protected TransformGroup moveGroup;
/** The left static cube. */
protected Shape3D leftCube;
/** The right static cube. */
protected Shape3D rightCube;
/** The movable cube that will collide with the other two cubes */
protected Shape3D moveCube;
/**
* This builds the view branch of the scene graph.
*
* @return BranchGroup with viewing objects attached.
*/
protected BranchGroup buildViewBranch(Canvas3D c) {
BranchGroup viewBranch = new BranchGroup();
Transform3D viewXfm = new Transform3D();
viewXfm.set(new Vector3f(0.0f, 0.0f, 10.0f));
TransformGroup viewXfmGroup = new TransformGroup(viewXfm);
ViewPlatform myViewPlatform = new ViewPlatform();
PhysicalBody myBody = new PhysicalBody();
PhysicalEnvironment myEnvironment = new PhysicalEnvironment();
viewXfmGroup.addChild(myViewPlatform);
viewBranch.addChild(viewXfmGroup);
View myView = new View();
myView.addCanvas3D(c);
myView.attachViewPlatform(myViewPlatform);
myView.setPhysicalBody(myBody);
myView.setPhysicalEnvironment(myEnvironment);
return viewBranch;
}
/**
* This adds some lights to the content branch of the scene graph.
*
* @param b
* The BranchGroup to add the lights to.
*/
protected void addLights(BranchGroup b) {
Color3f ambLightColour = new Color3f(0.5f, 0.5f, 0.5f);
AmbientLight ambLight = new AmbientLight(ambLightColour);
ambLight.setInfluencingBounds(bounds);
Color3f dirLightColour = new Color3f(1.0f, 1.0f, 1.0f);
Vector3f dirLightDir = new Vector3f(-1.0f, -1.0f, -1.0f);
DirectionalLight dirLight = new DirectionalLight(dirLightColour,
dirLightDir);
dirLight.setInfluencingBounds(bounds);
b.addChild(ambLight);
b.addChild(dirLight);
}
/**
* Creates the content branch of the scene graph.
*
* @return BranchGroup with content attached.
*/
protected BranchGroup buildContentBranch() {
//First create a different appearance for each cube
Appearance app1 = new Appearance();
Appearance app2 = new Appearance();
Appearance app3 = new Appearance();
Color3f ambientColour1 = new Color3f(1.0f, 0.0f, 0.0f);
Color3f ambientColour2 = new Color3f(1.0f, 1.0f, 0.0f);
Color3f ambientColour3 = new Color3f(1.0f, 1.0f, 1.0f);
Color3f emissiveColour = new Color3f(0.0f, 0.0f, 0.0f);
Color3f specularColour = new Color3f(1.0f, 1.0f, 1.0f);
Color3f diffuseColour1 = new Color3f(1.0f, 0.0f, 0.0f);
Color3f diffuseColour2 = new Color3f(1.0f, 1.0f, 0.0f);
Color3f diffuseColour3 = new Color3f(1.0f, 1.0f, 1.0f);
float shininess = 20.0f;
app1.setMaterial(new Material(ambientColour1, emissiveColour,
diffuseColour1, specularColour, shininess));
app2.setMaterial(new Material(ambientColour2, emissiveColour,
diffuseColour2, specularColour, shininess));
app3.setMaterial(new Material(ambientColour3, emissiveColour,
diffuseColour3, specularColour, shininess));
//Build the vertex array for the cubes. We can use the same
//data for each cube so we just define one set of data
IndexedQuadArray indexedCube = new IndexedQuadArray(8,
IndexedQuadArray.COORDINATES | IndexedQuadArray.NORMALS, 24);
Point3f[] cubeCoordinates = { new Point3f(1.0f, 1.0f, 1.0f),
new Point3f(-1.0f, 1.0f, 1.0f),
new Point3f(-1.0f, -1.0f, 1.0f),
new Point3f(1.0f, -1.0f, 1.0f), new Point3f(1.0f, 1.0f, -1.0f),
new Point3f(-1.0f, 1.0f, -1.0f),
new Point3f(-1.0f, -1.0f, -1.0f),
new Point3f(1.0f, -1.0f, -1.0f) };
Vector3f[] cubeNormals = { new Vector3f(0.0f, 0.0f, 1.0f),
new Vector3f(0.0f, 0.0f, -1.0f),
new Vector3f(1.0f, 0.0f, 0.0f),
new Vector3f(-1.0f, 0.0f, 0.0f),
new Vector3f(0.0f, 1.0f, 0.0f), new Vector3f(0.0f, -1.0f, 0.0f) };
int cubeCoordIndices[] = { 0, 1, 2, 3, 7, 6, 5, 4, 0, 3, 7, 4, 5, 6, 2,
1, 0, 4, 5, 1, 6, 7, 3, 2 };
int cubeNormalIndices[] = { 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3,
3, 3, 4, 4, 4, 4, 5, 5, 5, 5 };
indexedCube.setCoordinates(0, cubeCoordinates);
indexedCube.setNormals(0, cubeNormals);
indexedCube.setCoordinateIndices(0, cubeCoordIndices);
indexedCube.setNormalIndices(0, cubeNormalIndices);
//Create the three cubes
leftCube = new Shape3D(indexedCube, app1);
rightCube = new Shape3D(indexedCube, app2);
moveCube = new Shape3D(indexedCube, app3);
//Define some user data so that we can print meaningful messages
leftCube.setUserData(new String("left cube"));
rightCube.setUserData(new String("right cube"));
//Create the content branch and add the lights
BranchGroup contentBranch = new BranchGroup();
addLights(contentBranch);
//Set up the transform to position the left cube
Transform3D leftGroupXfm = new Transform3D();
leftGroupXfm.set(new Vector3d(-1.5, 0.0, 0.0));
leftGroup = new TransformGroup(leftGroupXfm);
//Set up the transform to position the right cube
Transform3D rightGroupXfm = new Transform3D();
rightGroupXfm.set(new Vector3d(1.5, 0.0, 0.0));
rightGroup = new TransformGroup(rightGroupXfm);
//Create the movable cube"s transform with a scale and
//a translation. Set up the
//capabilities so it can be moved by the behaviour
Transform3D moveXfm = new Transform3D();
moveXfm.set(0.7, new Vector3d(0.0, 2.0, 1.0));
moveGroup = new TransformGroup(moveXfm);
moveGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
moveGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
moveGroup.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
//Create the behaviour to move the movable cube
PickTranslateBehavior pickTranslate = new PickTranslateBehavior(
contentBranch, myCanvas3D, bounds);
contentBranch.addChild(pickTranslate);
//Create and add the two colision detectors
CollisionDetector2 myColDetLeft = new CollisionDetector2(leftCube,
bounds);
contentBranch.addChild(myColDetLeft);
CollisionDetector2 myColDetRight = new CollisionDetector2(rightCube,
bounds);
contentBranch.addChild(myColDetRight);
//Set up the scene graph
contentBranch.addChild(moveGroup);
contentBranch.addChild(leftGroup);
contentBranch.addChild(rightGroup);
moveGroup.addChild(moveCube);
leftGroup.addChild(leftCube);
rightGroup.addChild(rightCube);
return contentBranch;
}
/** Process exit button"s action to quit */
public void actionPerformed(ActionEvent e) {
if (e.getSource() == exitButton) {
dispose();
System.exit(0);
}
}
public SimpleCollision2() {
VirtualUniverse myUniverse = new VirtualUniverse();
Locale myLocale = new Locale(myUniverse);
myLocale.addBranchGraph(buildViewBranch(myCanvas3D));
myLocale.addBranchGraph(buildContentBranch());
setTitle("SimpleWorld");
setSize(400, 400);
setLayout(new BorderLayout());
Panel bottom = new Panel();
bottom.add(exitButton);
add(BorderLayout.CENTER, myCanvas3D);
add(BorderLayout.SOUTH, bottom);
exitButton.addActionListener(this);
setVisible(true);
}
public static void main(String[] args) {
SimpleCollision2 sw = new SimpleCollision2();
}
}
/**
* A simple collision detector class. This responds to a collision event by
* printing a message with information about the type of collision event and the
* object involved. This is a variation of the CollisionDetector class that
* prints information about the object that is associated with this behaviour
* rather than the object that has been collided with. An example of its use is
* given in the SimpleCollision2 class.
*
* @author I.J.Palmer
* @version 1.0
* @see CollisionDetector
* @see SimpleCollision2
*/
class CollisionDetector2 extends Behavior {
/** The shape that is being watched for collisions. */
protected Shape3D collidingShape;
/** The separate criteria that trigger this behaviour */
protected WakeupCriterion[] theCriteria;
/** The result of the "OR" of the separate criteria */
protected WakeupOr oredCriteria;
/**
* @param theShape
* Shape3D that is to be watched for collisions.
* @param theBounds
* Bounds that define the active region for this behaviour
*/
public CollisionDetector2(Shape3D theShape, Bounds theBounds) {
collidingShape = theShape;
setSchedulingBounds(theBounds);
}
/**
* This sets up the criteria for triggering the behaviour. It creates an
* entry, exit and movement trigger, OR"s these together and then sets the
* OR"ed criterion as the wake up condition.
*/
public void initialize() {
theCriteria = new WakeupCriterion[3];
WakeupOnCollisionEntry startsCollision = new WakeupOnCollisionEntry(
collidingShape);
WakeupOnCollisionExit endsCollision = new WakeupOnCollisionExit(
collidingShape);
WakeupOnCollisionMovement moveCollision = new WakeupOnCollisionMovement(
collidingShape);
theCriteria[0] = startsCollision;
theCriteria[1] = endsCollision;
theCriteria[2] = moveCollision;
oredCriteria = new WakeupOr(theCriteria);
wakeupOn(oredCriteria);
}
/**
* This is where the work is done. This identifies the type of collision
* (entry, exit or movement) and prints a message stating that an object has
* collided with this object. The userData field of the shape associated
* with this collision detector # is used to identify the object. Finally,
* the wake up condition is set to be the OR"ed criterion again.
*/
public void processStimulus(Enumeration criteria) {
while (criteria.hasMoreElements()) {
WakeupCriterion theCriterion = (WakeupCriterion) criteria
.nextElement();
if (theCriterion instanceof WakeupOnCollisionEntry) {
System.out.println("Collided with "
+ collidingShape.getUserData());
} else if (theCriterion instanceof WakeupOnCollisionExit) {
System.out.println("Stopped colliding with "
+ collidingShape.getUserData());
} else {
System.out.println("Moved whilst colliding with "
+ collidingShape.getUserData());
}
}
wakeupOn(oredCriteria);
}
}
Tick Tock Collision
/*
* @(#)TickTockCollision.java 1.16 02/10/21 13:58:00
*
* Copyright (c) 1996-2002 Sun Microsystems, Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistribution in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of Sun Microsystems, Inc. or the names of contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
* IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
* NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
* LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
* OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
* LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
* INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
* CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
* OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGES.
*
* You acknowledge that Software is not designed,licensed or intended for use in
* the design, construction, operation or maintenance of any nuclear facility.
*/
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;
import java.util.Enumeration;
import javax.media.j3d.Alpha;
import javax.media.j3d.Appearance;
import javax.media.j3d.Background;
import javax.media.j3d.Behavior;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.ColoringAttributes;
import javax.media.j3d.Group;
import javax.media.j3d.QuadArray;
import javax.media.j3d.RotationInterpolator;
import javax.media.j3d.Shape3D;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.WakeupOnCollisionEntry;
import javax.media.j3d.WakeupOnCollisionExit;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.universe.SimpleUniverse;
public class TickTockCollision extends Applet {
private SimpleUniverse u = null;
public BranchGroup createSceneGraph() {
// Create the root of the branch graph
BranchGroup objRoot = new BranchGroup();
// Create a Transformgroup to scale all objects so they
// appear in the scene.
TransformGroup objScale = new TransformGroup();
Transform3D t3d = new Transform3D();
t3d.setScale(0.4);
objScale.setTransform(t3d);
objRoot.addChild(objScale);
// Create a bounds for the background and behaviors
BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0),
100.0);
// Set up the background
Color3f bgColor = new Color3f(0.05f, 0.05f, 0.2f);
Background bg = new Background(bgColor);
bg.setApplicationBounds(bounds);
objScale.addChild(bg);
// Create a pair of transform group nodes and initialize them to
// identity. Enable the TRANSFORM_WRITE capability so that
// our behaviors can modify them at runtime. Add them to the
// root of the subgraph.
TransformGroup objTrans1 = new TransformGroup();
objTrans1.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objScale.addChild(objTrans1);
TransformGroup objTrans2 = new TransformGroup();
objTrans2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objTrans1.addChild(objTrans2);
// Create the positioning and scaling transform group node.
Transform3D t = new Transform3D();
t.set(0.3, new Vector3d(0.0, -1.5, 0.0));
TransformGroup objTrans3 = new TransformGroup(t);
objTrans2.addChild(objTrans3);
// Create a simple shape leaf node, add it to the scene graph.
objTrans3.addChild(new ColorCube());
// Create a new Behavior object that will perform the desired
// rotation on the specified transform object and add it into
// the scene graph.
Transform3D yAxis1 = new Transform3D();
yAxis1.rotX(Math.PI / 2.0);
Alpha tickTockAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE
| Alpha.DECREASING_ENABLE, 0, 0, 5000, 2500, 200, 5000, 2500,
200);
RotationInterpolator tickTock = new RotationInterpolator(tickTockAlpha,
objTrans1, yAxis1, -(float) Math.PI / 2.0f,
(float) Math.PI / 2.0f);
tickTock.setSchedulingBounds(bounds);
objTrans2.addChild(tickTock);
// Create a new Behavior object that will perform the desired
// rotation on the specified transform object and add it into
// the scene graph.
Transform3D yAxis2 = new Transform3D();
Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0,
4000, 0, 0, 0, 0, 0);
RotationInterpolator rotator = new RotationInterpolator(rotationAlpha,
objTrans2, yAxis2, 0.0f, (float) Math.PI * 2.0f);
rotator.setSchedulingBounds(bounds);
objTrans2.addChild(rotator);
// Now create a pair of rectangular boxes, each with a collision
// detection behavior attached. The behavior will highlight the
// object when it is in a state of collision.
Group box1 = createBox(0.3, new Vector3d(-1.3, 0.0, 0.0));
Group box2 = createBox(0.3, new Vector3d(1.3, 0.0, 0.0));
objScale.addChild(box1);
objScale.addChild(box2);
// Have Java 3D perform optimizations on this scene graph.
objRoot.rupile();
return objRoot;
}
private Group createBox(double scale, Vector3d pos) {
// Create a transform group node to scale and position the object.
Transform3D t = new Transform3D();
t.set(scale, pos);
TransformGroup objTrans = new TransformGroup(t);
// Create a simple shape leaf node and add it to the scene graph
Shape3D shape = new Box(0.5, 5.0, 1.0);
objTrans.addChild(shape);
// Create a new ColoringAttributes object for the shape"s
// appearance and make it writable at runtime.
Appearance app = shape.getAppearance();
ColoringAttributes ca = new ColoringAttributes();
ca.setColor(0.6f, 0.3f, 0.0f);
app.setCapability(app.ALLOW_COLORING_ATTRIBUTES_WRITE);
app.setColoringAttributes(ca);
// Create a new Behavior object that will perform the collision
// detection on the specified object, and add it into
// the scene graph.
CollisionDetector cd = new CollisionDetector(shape);
BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0),
100.0);
cd.setSchedulingBounds(bounds);
// Add the behavior to the scene graph
objTrans.addChild(cd);
return objTrans;
}
public TickTockCollision() {
}
public void init() {
setLayout(new BorderLayout());
GraphicsConfiguration config = SimpleUniverse
.getPreferredConfiguration();
Canvas3D c = new Canvas3D(config);
add("Center", c);
// Create a simple scene and attach it to the virtual universe
BranchGroup scene = createSceneGraph();
u = new SimpleUniverse(c);
// This will move the ViewPlatform back a bit so the
// objects in the scene can be viewed.
u.getViewingPlatform().setNominalViewingTransform();
u.addBranchGraph(scene);
}
public void destroy() {
u.cleanup();
}
//
// The following allows TickTockCollision to be run as an application
// as well as an applet
//
public static void main(String[] args) {
new MainFrame(new TickTockCollision(), 700, 700);
}
}
class CollisionDetector extends Behavior {
private static final Color3f highlightColor = new Color3f(0.0f, 1.0f, 0.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;
public CollisionDetector(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(Enumeration criteria) {
inCollision = !inCollision;
if (inCollision) {
shapeAppearance.setColoringAttributes(highlight);
wakeupOn(wExit);
} else {
shapeAppearance.setColoringAttributes(shapeColoring);
wakeupOn(wEnter);
}
}
}
class Box extends Shape3D {
public Box(double xsize, double ysize, double zsize) {
super();
double xmin = -xsize / 2.0;
double xmax = xsize / 2.0;
double ymin = -ysize / 2.0;
double ymax = ysize / 2.0;
double zmin = -zsize / 2.0;
double zmax = zsize / 2.0;
QuadArray box = new QuadArray(24, QuadArray.COORDINATES);
Point3d verts[] = new Point3d[24];
// front face
verts[0] = new Point3d(xmax, ymin, zmax);
verts[1] = new Point3d(xmax, ymax, zmax);
verts[2] = new Point3d(xmin, ymax, zmax);
verts[3] = new Point3d(xmin, ymin, zmax);
// back face
verts[4] = new Point3d(xmin, ymin, zmin);
verts[5] = new Point3d(xmin, ymax, zmin);
verts[6] = new Point3d(xmax, ymax, zmin);
verts[7] = new Point3d(xmax, ymin, zmin);
// right face
verts[8] = new Point3d(xmax, ymin, zmin);
verts[9] = new Point3d(xmax, ymax, zmin);
verts[10] = new Point3d(xmax, ymax, zmax);
verts[11] = new Point3d(xmax, ymin, zmax);
// left face
verts[12] = new Point3d(xmin, ymin, zmax);
verts[13] = new Point3d(xmin, ymax, zmax);
verts[14] = new Point3d(xmin, ymax, zmin);
verts[15] = new Point3d(xmin, ymin, zmin);
// top face
verts[16] = new Point3d(xmax, ymax, zmax);
verts[17] = new Point3d(xmax, ymax, zmin);
verts[18] = new Point3d(xmin, ymax, zmin);
verts[19] = new Point3d(xmin, ymax, zmax);
// bottom face
verts[20] = new Point3d(xmin, ymin, zmax);
verts[21] = new Point3d(xmin, ymin, zmin);
verts[22] = new Point3d(xmax, ymin, zmin);
verts[23] = new Point3d(xmax, ymin, zmax);
box.setCoordinates(0, verts);
setGeometry(box);
setAppearance(new Appearance());
}
}