1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
|
package tesseract;
import java.awt.GraphicsConfiguration;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.media.j3d.Background;
import javax.media.j3d.BoundingBox;
import javax.media.j3d.BoundingLeaf;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.Node;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3f;
import tesseract.chatbox.Chatbox;
import tesseract.forces.AirDrag;
import tesseract.forces.CircularXY;
import tesseract.forces.CircularXZ;
import tesseract.forces.Force;
import tesseract.forces.Gravity;
import tesseract.forces.LinearOrigin;
import tesseract.forces.QuadradicOrigin;
import tesseract.generators.GeneratorsMenu;
import tesseract.newmenu.MenuItem;
import tesseract.newmenu.NewChainLinkMenuItem;
import tesseract.newmenu.NewEllipsoidMenuItem;
import tesseract.newmenu.NewIcosahedronMenuItem;
import tesseract.newmenu.NewParticleEmitterMenuItem;
import tesseract.newmenu.NewParticleMenuItem;
import tesseract.newmenu.NewPlanarPolygonMenuItem;
import tesseract.newmenu.NewSurfBoardMenuItem;
import tesseract.newmenu.NewToroidMenuItem;
import tesseract.objects.Ground;
import tesseract.objects.PhysicalObject;
import tesseract.objects.blimp.Blimp;
import tesseract.objects.remote.RemoteObjectMenu;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.picking.PickCanvas;
import com.sun.j3d.utils.picking.PickResult;
import com.sun.j3d.utils.universe.SimpleUniverse;
import common.Peer;
/**
* This class is the main UI for the Tesseract Project.
*
* @author Jesse Morgan
*/
public class TesseractUI extends JFrame {
/**
* Generated serialVersionUID.
*/
private static final long serialVersionUID = 4097744746899308736L;
/**
* Update Rate.
*/
private static final int UPDATE_RATE = 30;
/**
* Measure of 1 unite of space in the world.
*/
private static final float UNIT = 1;
/**
* Number of miliseconds in 1 second.
*/
private static final int MILISECONDS_IN_SECOND = 1000;
/**
* A reference to the world.
*/
private World myWorld;
/**
* The Peer Object
*/
private Peer myPeer;
/**
* The Chatbox.
*/
private Chatbox myChatbox;
/**
* The Canvas.
*/
private Canvas3D myCanvas;
/**
* Camera TransformGroup.
*/
private TransformGroup cameraTG;
/**
* Camera position information.
*/
private double cameraXRotation, cameraYRotation, cameraDistance;
/**
* Object Menu Items.
*/
private JMenuItem[] myObjectMenuItems;
/**
* World Timer.
*/
private Timer myTimer;
/**
* Currently selected object.
*/
private PhysicalObject myCurrentObject;
/**
* Remote Objects.
*/
private RemoteObjectMenu myRemoteObjects;
/**
* UI Constructor.
*/
public TesseractUI() {
super("Tesseract Project");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myPeer = new Peer(true);
myPeer.createNetwork();
setTitle("Tesseract Project " + myPeer.getMyName());
myChatbox = new Chatbox(myPeer);
myChatbox.setLocationRelativeTo(this);
myChatbox.setMyName();
myWorld = new World(
new BoundingBox(new Point3d(-UNIT / 2, -UNIT / 2, -UNIT / 2),
new Point3d(UNIT / 2, UNIT / 2, UNIT / 2)),
myPeer);
createBackground();
createGround();
Blimp blimp = new Blimp(new Vector3f(0,0,0), .7f);
myWorld.addObject(blimp);
myCurrentObject = null;
myObjectMenuItems = new JMenuItem[] {
new NewParticleEmitterMenuItem(myWorld),
new NewParticleMenuItem(myWorld),
new NewPlanarPolygonMenuItem(myWorld),
new NewEllipsoidMenuItem(myWorld),
new NewIcosahedronMenuItem(myWorld),
new NewChainLinkMenuItem(myWorld),
new NewToroidMenuItem(myWorld),
new NewSurfBoardMenuItem(myWorld)
};
myRemoteObjects = new RemoteObjectMenu(myWorld);
createMenu();
setupCanvas();
pack();
// Maximize the windows
if (Toolkit.getDefaultToolkit().
isFrameStateSupported(JFrame.MAXIMIZED_BOTH)) {
setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
}
// Shutdown Nicely
addWindowListener(new WindowAdapter() {
public void windowClosing(final WindowEvent e) {
myPeer.disconnectFromNetwork();
}
});
}
/**
* Create the menu.
*/
private void createMenu() {
JMenuBar menuBar = new JMenuBar();
// Added by Steve: Fixes viewing menu problem with Canvas3D on both my windows machines
JPopupMenu.setDefaultLightWeightPopupEnabled(false);
//Simulator
JMenu simulationMenu = new JMenu("Simulation");
menuBar.add(simulationMenu);
// Simulator Start/Stop
JMenuItem runSim = new JCheckBoxMenuItem("Run Simulator", true);
runSim.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
if (((JCheckBoxMenuItem) e.getSource()).isSelected()) {
myTimer.start();
} else {
myTimer.stop();
}
}
});
simulationMenu.add(runSim);
//Objects
JMenu objectsMenu = new JMenu("Add Object");
for (JMenuItem item : myObjectMenuItems) {
objectsMenu.add(item);
((MenuItem) item).setParent(this);
}
menuBar.add(objectsMenu);
//Network
JMenu networkMenu = new JMenu("Network");
final JMenuItem join = new JMenuItem("Join Network");
join.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String ip = JOptionPane.showInputDialog("Enter the IP to connect to: ");
if (ip != null ) {
myPeer.disconnectFromNetwork();
myPeer.connectToNetwork(ip);
setTitle("Tesseract Project " + myPeer.getMyName());
}
myChatbox.setMyName(); //sets myName Field of Chatbox to Peer ID.
}
});
networkMenu.add(join);
menuBar.add(networkMenu);
//Forces
JMenu forcesMenu = new JMenu("Add Forces");
final JMenuItem gravity = new JCheckBoxMenuItem("Gravity", false);
gravity.addActionListener(new ActionListener() {
private Force me;
// Constructor
{
me = new Gravity();
}
public void actionPerformed(ActionEvent e) {
if (((JCheckBoxMenuItem) e.getSource()).isSelected()) {
myWorld.addForce(me);
} else {
myWorld.removeForce(me);
}
}
});
forcesMenu.add(gravity);
final JMenuItem circularXZ = new JCheckBoxMenuItem("Tangential force in the XZ plane", false);
circularXZ.addActionListener(new ActionListener() {
private Force me;
// Constructor
{
me = new CircularXZ(.5f);
}
public void actionPerformed(ActionEvent e) {
if (((JCheckBoxMenuItem) e.getSource()).isSelected()) {
myWorld.addForce(me);
} else {
myWorld.removeForce(me);
}
}
});
forcesMenu.add(circularXZ);
final JMenuItem circularXY = new JCheckBoxMenuItem("Tangential force in the XY plane", false);
circularXY.addActionListener(new ActionListener() {
private Force me;
// Constructor
{
me = new CircularXY(.5f);
}
public void actionPerformed(ActionEvent e) {
if (((JCheckBoxMenuItem) e.getSource()).isSelected()) {
myWorld.addForce(me);
} else {
myWorld.removeForce(me);
}
}
});
forcesMenu.add(circularXY);
final JMenuItem originLinear = new JCheckBoxMenuItem("Linear proportional force towards the origin", false);
originLinear.addActionListener(new ActionListener() {
private Force me;
// Constructor
{
me = new LinearOrigin(.5f);
}
public void actionPerformed(ActionEvent e) {
if (((JCheckBoxMenuItem) e.getSource()).isSelected()) {
myWorld.addForce(me);
} else {
myWorld.removeForce(me);
}
}
});
forcesMenu.add(originLinear);
final JMenuItem originQuadradic = new JCheckBoxMenuItem("Quadratic proportional force towards the origin", false);
originQuadradic.addActionListener(new ActionListener() {
private Force me;
// Constructor
{
me = new QuadradicOrigin(.5f);
}
public void actionPerformed(ActionEvent e) {
if (((JCheckBoxMenuItem) e.getSource()).isSelected()) {
myWorld.addForce(me);
} else {
myWorld.removeForce(me);
}
}
});
forcesMenu.add(originQuadradic);
final JMenuItem airDrag = new JCheckBoxMenuItem("Air Drag", false);
airDrag.addActionListener(new ActionListener() {
private Force me;
// Constructor
{
me = new AirDrag();
}
public void actionPerformed(ActionEvent e) {
if (((JCheckBoxMenuItem) e.getSource()).isSelected()) {
myWorld.addForce(me);
} else {
myWorld.removeForce(me);
}
}
});
forcesMenu.add(airDrag);
menuBar.add(forcesMenu);
// Generators
menuBar.add(new GeneratorsMenu(myWorld));
// Add reset Simulator menu item
JMenuItem resetSim = new JMenuItem("Reset Simulator");
resetSim.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
myWorld.resetWorld();
gravity.setSelected(false);
circularXZ.setSelected(false);
circularXY.setSelected(false);
originLinear.setSelected(false);
originQuadradic.setSelected(false);
airDrag.setSelected(false);
createGround();
}
});
simulationMenu.add(resetSim);
// Exit Menu Item
JMenuItem exit = new JMenuItem("Exit");
exit.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
// TODO: I feel this is the wrong way of exiting...
// TODO: Properly shutdown network connection here!
myPeer.disconnectFromNetwork();
System.exit(0);
}
});
simulationMenu.add(exit);
//*
//JMenu networkMenu = new JMenu("Communication");
JMenuItem chat = new JMenuItem("Chat");
chat.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
myChatbox.setVisible(true);
}
});
networkMenu.add(chat);
// Remote Objects
menuBar.add(myRemoteObjects);
setJMenuBar(menuBar);
}
/**
* Create and show the UI.
*/
private void setupCanvas() {
GraphicsConfiguration config
= SimpleUniverse.getPreferredConfiguration();
myCanvas = new Canvas3D(config);
SimpleUniverse universe = new SimpleUniverse(myCanvas);
//universe.getViewer().getView().setSceneAntialiasingEnable(true);
// Set the camera
cameraTG = universe.getViewingPlatform().getViewPlatformTransform();
cameraDistance = 3 * UNIT;
updateCamera();
// Add the scene BG.
universe.addBranchGraph(myWorld.getScene());
// Add the canvas to the frame.
add(myCanvas);
// Test Picking
final PickCanvas pc = new PickCanvas(myCanvas, myWorld.getScene());
pc.setMode(PickCanvas.GEOMETRY);
myCanvas.addMouseListener(new MouseAdapter() {
public void mousePressed(final MouseEvent e) {
pc.setShapeLocation(e);
PickResult r = pc.pickClosest();
if (r != null) {
if (r.getObject().getUserData() instanceof PhysicalObject) {
myCurrentObject =
(PhysicalObject) r.getObject().getUserData();
myCurrentObject.selected(true);
} else {
// The PhysicalObject was not found in the selected
// object... Check parents.
for (int i = r.getSceneGraphPath().nodeCount() - 1;
i >= 0; i--) {
Node n = r.getSceneGraphPath().getNode(i);
if (n.getUserData() instanceof PhysicalObject) {
myCurrentObject =
(PhysicalObject) n.getUserData();
myCurrentObject.selected(true);
break;
}
}
}
}
}
public void mouseReleased(final MouseEvent e) {
if (myCurrentObject != null) {
myCurrentObject.selected(false);
}
myCurrentObject = null;
}
});
// Event listener time
myCanvas.addMouseMotionListener(new MouseMotionAdapter() {
private MouseEvent lastDragEvent = null;
public void mouseDragged(final MouseEvent e) {
if (lastDragEvent != null) {
//Disable the ground from being pickable
if (myCurrentObject != null && !(myCurrentObject instanceof Ground)) {
float scale = 0.001f;
int xdiff = e.getX() - lastDragEvent.getX();
int ydiff = -e.getY() + lastDragEvent.getY();
Point3f p = new Point3f(scale * xdiff, scale
* ydiff, 0);
Transform3D t3d = new Transform3D();
t3d.rotX(cameraXRotation);
Transform3D tmp = new Transform3D();
tmp.rotY(cameraYRotation);
t3d.mul(tmp);
t3d.invert();
t3d.transform(p);
if (e.isAltDown()) {
myCurrentObject.getOrientation().y += p.x;
myCurrentObject.getOrientation().x += p.y;
myCurrentObject.getOrientation().z += p.z;
myCurrentObject.getOrientation().w = 1;
myCurrentObject.getOrientation().normalize();
} else {
myCurrentObject.getPosition().x += p.x;
myCurrentObject.getPosition().y += p.y;
myCurrentObject.getPosition().z += p.z;
}
myCurrentObject.updateTranformGroup();
} else {
cameraXRotation +=
Math.toRadians(e.getY() - lastDragEvent.getY()) / 3;
if (cameraXRotation > Math.PI / 2) {
cameraXRotation = Math.PI / 2;
} else if (cameraXRotation < -Math.PI / 2) {
cameraXRotation = -Math.PI / 2;
}
cameraYRotation +=
Math.toRadians(e.getX() - lastDragEvent.getX()) / 3;
updateCamera();
}
}
lastDragEvent = e;
}
public void mouseMoved(final MouseEvent e) {
lastDragEvent = null;
}
});
myCanvas.addMouseWheelListener(new MouseWheelListener() {
public void mouseWheelMoved(final MouseWheelEvent e) {
if (e.getWheelRotation() > 0) {
cameraDistance *= 1.05;
} else if (e.getWheelRotation() < 0) {
cameraDistance *= 0.95;
}
updateCamera();
}
});
// Keyboard Events
myCanvas.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
myRemoteObjects.sendKeyToObjects(e);
}
});
// Setup the timer.
myTimer = new Timer(MILISECONDS_IN_SECOND / UPDATE_RATE,
new ActionListener() {
public void actionPerformed(final ActionEvent e) {
myCanvas.stopRenderer();
myWorld.tick();
myCanvas.startRenderer();
}
});
myTimer.start();
}
/**
* Method to update the camera.
*/
private void updateCamera() {
Transform3D camera3D = new Transform3D();
camera3D.setTranslation(new Vector3f(0f, 0f, (float) -cameraDistance));
Transform3D tmp = new Transform3D();
tmp.rotX(cameraXRotation);
camera3D.mul(tmp);
tmp.rotY(cameraYRotation);
camera3D.mul(tmp);
camera3D.invert();
cameraTG.setTransform(camera3D);
}
/**
* Start up the program.
*
* @param args Unused commandline arguments.
*/
public static void main(final String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TesseractUI().setVisible(true);
}
});
}
/**
* Creates the ground
*/
public void createGround(){
Ground ground = new Ground(new Vector3f(0f, -.51f, 0f), 5f);
myWorld.addObject(ground);
}
/**
* Create background image
*/
private void createBackground() {
//Set Background
BoundingBox bounds = myWorld.getBounds();
TextureLoader t = new TextureLoader("Alien.jpg", myCanvas);
Background background = new Background(t.getImage());
background.setImageScaleMode(Background.SCALE_FIT_MAX); // Tiles the image
background.setApplicationBounds(bounds);
BranchGroup bg = new BranchGroup();
bg.addChild(background);
BranchGroup scene = myWorld.getScene();
scene.addChild(bg);
}
}
|