001: /*
002: * $RCSfile: TickTockCollision.java,v $
003: *
004: * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * - Redistribution of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * - Redistribution in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * Neither the name of Sun Microsystems, Inc. or the names of
019: * contributors may be used to endorse or promote products derived
020: * from this software without specific prior written permission.
021: *
022: * This software is provided "AS IS," without a warranty of any
023: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
024: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
025: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
026: * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
027: * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
028: * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
029: * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
030: * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
031: * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
032: * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
033: * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
034: * POSSIBILITY OF SUCH DAMAGES.
035: *
036: * You acknowledge that this software is not designed, licensed or
037: * intended for use in the design, construction, operation or
038: * maintenance of any nuclear facility.
039: *
040: * $Revision: 1.4 $
041: * $Date: 2007/02/09 17:21:34 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.collision;
046:
047: import java.awt.*;
048: import java.awt.event.*;
049: import com.sun.j3d.utils.universe.*;
050: import com.sun.j3d.utils.geometry.ColorCube;
051: import javax.media.j3d.*;
052: import javax.vecmath.*;
053: import java.awt.GraphicsConfiguration;
054:
055: /**
056: * Simple Java 3D example program to display how collision work.
057: */
058: public class TickTockCollision extends javax.swing.JFrame {
059:
060: private SimpleUniverse univ = null;
061: private BranchGroup scene = null;
062:
063: public BranchGroup createSceneGraph() {
064: // Create the root of the branch graph
065: BranchGroup objRoot = new BranchGroup();
066:
067: // Create a Transformgroup to scale all objects so they
068: // appear in the scene.
069: TransformGroup objScale = new TransformGroup();
070: Transform3D t3d = new Transform3D();
071: t3d.setScale(0.4);
072: objScale.setTransform(t3d);
073: objRoot.addChild(objScale);
074:
075: // Create a bounds for the background and behaviors
076: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
077: 0.0, 0.0), 100.0);
078:
079: // Set up the background
080: Color3f bgColor = new Color3f(0.05f, 0.05f, 0.2f);
081: Background bg = new Background(bgColor);
082: bg.setApplicationBounds(bounds);
083: objScale.addChild(bg);
084:
085: // Create a pair of transform group nodes and initialize them to
086: // identity. Enable the TRANSFORM_WRITE capability so that
087: // our behaviors can modify them at runtime. Add them to the
088: // root of the subgraph.
089: TransformGroup objTrans1 = new TransformGroup();
090: objTrans1.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
091: objScale.addChild(objTrans1);
092:
093: TransformGroup objTrans2 = new TransformGroup();
094: objTrans2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
095: objTrans1.addChild(objTrans2);
096:
097: // Create the positioning and scaling transform group node.
098: Transform3D t = new Transform3D();
099: t.set(0.3, new Vector3d(0.0, -1.5, 0.0));
100: TransformGroup objTrans3 = new TransformGroup(t);
101: objTrans2.addChild(objTrans3);
102:
103: // Create a simple shape leaf node, add it to the scene graph.
104: objTrans3.addChild(new ColorCube());
105:
106: // Create a new Behavior object that will perform the desired
107: // rotation on the specified transform object and add it into
108: // the scene graph.
109: Transform3D yAxis1 = new Transform3D();
110: yAxis1.rotX(Math.PI / 2.0);
111: Alpha tickTockAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE
112: | Alpha.DECREASING_ENABLE, 0, 0, 5000, 2500, 200, 5000,
113: 2500, 200);
114:
115: RotationInterpolator tickTock = new RotationInterpolator(
116: tickTockAlpha, objTrans1, yAxis1,
117: -(float) Math.PI / 2.0f, (float) Math.PI / 2.0f);
118: tickTock.setSchedulingBounds(bounds);
119: objTrans2.addChild(tickTock);
120:
121: // Create a new Behavior object that will perform the desired
122: // rotation on the specified transform object and add it into
123: // the scene graph.
124: Transform3D yAxis2 = new Transform3D();
125: Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0,
126: 0, 4000, 0, 0, 0, 0, 0);
127:
128: RotationInterpolator rotator = new RotationInterpolator(
129: rotationAlpha, objTrans2, yAxis2, 0.0f,
130: (float) Math.PI * 2.0f);
131: rotator.setSchedulingBounds(bounds);
132: objTrans2.addChild(rotator);
133:
134: // Now create a pair of rectangular boxes, each with a collision
135: // detection behavior attached. The behavior will highlight the
136: // object when it is in a state of collision.
137:
138: Group box1 = createBox(0.3, new Vector3d(-1.3, 0.0, 0.0));
139: Group box2 = createBox(0.3, new Vector3d(1.3, 0.0, 0.0));
140:
141: objScale.addChild(box1);
142: objScale.addChild(box2);
143:
144: // Have Java 3D perform optimizations on this scene graph.
145: objRoot.compile();
146:
147: return objRoot;
148: }
149:
150: private Group createBox(double scale, Vector3d pos) {
151: // Create a transform group node to scale and position the object.
152: Transform3D t = new Transform3D();
153: t.set(scale, pos);
154: TransformGroup objTrans = new TransformGroup(t);
155:
156: // Create a simple shape leaf node and add it to the scene graph
157: Shape3D shape = new Box(0.5, 5.0, 1.0);
158: objTrans.addChild(shape);
159:
160: // Create a new ColoringAttributes object for the shape's
161: // appearance and make it writable at runtime.
162: Appearance app = shape.getAppearance();
163: ColoringAttributes ca = new ColoringAttributes();
164: ca.setColor(0.6f, 0.3f, 0.0f);
165: app.setCapability(app.ALLOW_COLORING_ATTRIBUTES_WRITE);
166: app.setColoringAttributes(ca);
167:
168: // Create a new Behavior object that will perform the collision
169: // detection on the specified object, and add it into
170: // the scene graph.
171: CollisionDetector cd = new CollisionDetector(shape);
172: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
173: 0.0, 0.0), 100.0);
174: cd.setSchedulingBounds(bounds);
175:
176: // Add the behavior to the scene graph
177: objTrans.addChild(cd);
178:
179: return objTrans;
180: }
181:
182: private Canvas3D createUniverse() {
183: // Get the preferred graphics configuration for the default screen
184: GraphicsConfiguration config = SimpleUniverse
185: .getPreferredConfiguration();
186:
187: // Create a Canvas3D using the preferred configuration
188: Canvas3D c = new Canvas3D(config);
189:
190: // Create simple universe with view branch
191: univ = new SimpleUniverse(c);
192:
193: // This will move the ViewPlatform back a bit so the
194: // objects in the scene can be viewed.
195: univ.getViewingPlatform().setNominalViewingTransform();
196:
197: // Ensure at least 5 msec per frame (i.e., < 200Hz)
198: univ.getViewer().getView().setMinimumFrameCycleTime(5);
199:
200: return c;
201: }
202:
203: /**
204: * Creates new form TickTockCollision
205: */
206: public TickTockCollision() {
207: // Initialize the GUI components
208: initComponents();
209:
210: // Create Canvas3D and SimpleUniverse; add canvas to drawing panel
211: Canvas3D c = createUniverse();
212: drawingPanel.add(c, java.awt.BorderLayout.CENTER);
213:
214: // Create the content branch and add it to the universe
215: scene = createSceneGraph();
216: univ.addBranchGraph(scene);
217: }
218:
219: // ----------------------------------------------------------------
220:
221: /** This method is called from within the constructor to
222: * initialize the form.
223: * WARNING: Do NOT modify this code. The content of this method is
224: * always regenerated by the Form Editor.
225: */
226: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
227: private void initComponents() {
228: drawingPanel = new javax.swing.JPanel();
229:
230: setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
231: setTitle("TickTockCollision");
232: drawingPanel.setLayout(new java.awt.BorderLayout());
233:
234: drawingPanel.setPreferredSize(new java.awt.Dimension(700, 700));
235: getContentPane()
236: .add(drawingPanel, java.awt.BorderLayout.CENTER);
237:
238: pack();
239: }// </editor-fold>//GEN-END:initComponents
240:
241: /**
242: * @param args the command line arguments
243: */
244: public static void main(String args[]) {
245: java.awt.EventQueue.invokeLater(new Runnable() {
246: public void run() {
247: new TickTockCollision().setVisible(true);
248: }
249: });
250: }
251:
252: // Variables declaration - do not modify//GEN-BEGIN:variables
253: private javax.swing.JPanel drawingPanel;
254: // End of variables declaration//GEN-END:variables
255:
256: }
|