summaryrefslogtreecommitdiff
path: root/src/tesseract/World.java
blob: 67527ca897d2a39ef86dc343ed444c67a63ffe39 (plain)
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
package tesseract;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import javax.media.j3d.BoundingBox;
import javax.media.j3d.BoundingLeaf;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.Group;
import javax.media.j3d.IndexedLineArray;
import javax.media.j3d.Light;
import javax.media.j3d.Node;
import javax.media.j3d.Shape3D;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3f;

import tesseract.forces.Force;
import tesseract.objects.HalfSpace;
import tesseract.objects.Particle;
import tesseract.objects.PhysicalObject;

import common.CollidableObject;
import common.CollisionDetector;
import common.CollisionInfo;
import common.Peer;
import common.PeerInformation;

/**
 * Model of the 3D world.
 * 
 * @author Jesse Morgan
 */
public class World implements Observer {
	/**
	 * Root element of the world.
	 */
	private BranchGroup myScene;

	/**
	 * Bounding box of the world.
	 */
	private BoundingBox myVirtualWorldBounds;
	
	/**
	 * A list of the objects in the world. 
	 */
	private List<PhysicalObject> myObjects;
	
	/**
	 * A list of the forces in the world.
	 */
	private List<Force> myForces;
	
	/**
	 * The peer object for this world.
	 */
	private Peer myPeer;
	
	/**
	 * Update rate for the world.
	 */
	private static final int UPDATE_RATE = 30;
	
	/**
	 * Top HalfSpace.
	 */
	private HalfSpace my_top;
	
	/**
	 * Bottom HalfSpace
	 */
	private HalfSpace my_bottom;
	
	/**
	 * Side of HalfSpace for transmission decisions.
	 */
	private HalfSpace my_side1;
	
	/**
	 * Side of HalfSpace for transmission decisions.
	 */
	private HalfSpace my_side2;
	
	/**
	 * Side of HalfSpace for transmission decisions.
	 */
	private HalfSpace my_side3;
	
	/**
	 * Side of HalfSpace for transmission decisions.
	 */
	private HalfSpace my_side4;
	
	/**
	 * Create a new world.
	 * 
	 * @param bounds The bounding box of the world.
	 */
	public World(final BoundingBox bounds, final Peer peer) {
		myVirtualWorldBounds = bounds;
		myPeer = peer;
		myPeer.addObserver(this);
		
		myForces = new LinkedList<Force>();
		myObjects = new LinkedList<PhysicalObject>();
		
		// TODO: Should this go here?
		myScene = new BranchGroup();
		myScene.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
		myScene.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
		
		BoundingLeaf originLeaf = new BoundingLeaf(new BoundingSphere());
		myScene.addChild(originLeaf);
		
		myScene.addChild(createVirtualWorldBoundsShape());
		
		addLights();
		addHalfspaces();
		
		myScene.compile();
	}

	/**
	 * Create the visual bounding box around the world.
	 * 
	 * @return A shape of the bounding box.
	 */
	private Node createVirtualWorldBoundsShape() {
		Point3d lower = new Point3d();
		Point3d upper = new Point3d();
		myVirtualWorldBounds.getLower(lower);
		myVirtualWorldBounds.getUpper(upper);
		
		double[] coordinates = { lower.x, lower.y, lower.z, upper.x, lower.y,
				lower.z, upper.x, lower.y, upper.z, lower.x, lower.y, upper.z,
				lower.x, upper.y, lower.z, upper.x, upper.y, lower.z, upper.x,
				upper.y, upper.z, lower.x, upper.y, upper.z };
		
		int[] coordinateIndices = { 0, 1, 1, 2, 2, 3, 3, 0, 4, 5, 5, 6, 6, 7,
				7, 4, 0, 4, 1, 5, 2, 6, 3, 7 };
		
		IndexedLineArray geometry = new IndexedLineArray(
				coordinates.length / 3, IndexedLineArray.COORDINATES,
				coordinateIndices.length);
		
		geometry.setCoordinates(0, coordinates);
		geometry.setCoordinateIndices(0, coordinateIndices);
		
		return new Shape3D(geometry);
	}
	
	private void addHalfspaces() {
		Point3d lower = new Point3d();
		Point3d upper = new Point3d();
		myVirtualWorldBounds.getLower(lower) ;
		myVirtualWorldBounds.getUpper(upper);
		
		// Bottom
		my_bottom = new HalfSpace(new Vector3f(lower), new Vector3f(0, 1, 0));
		myObjects.add(my_bottom);
		
		// Top
		my_top = new HalfSpace(new Vector3f(upper), new Vector3f(0, -1, 0));
		myObjects.add(my_top);
		
		// Sides         
		my_side1 = new HalfSpace(new Vector3f(upper), new Vector3f(0, 0, -1));
		myObjects.add(my_side1);
		my_side2 = new HalfSpace(new Vector3f(upper), new Vector3f(-1, 0, 0));
		myObjects.add(my_side2);
		
		my_side3 = new HalfSpace(new Vector3f(lower), new Vector3f(0, 0, 1));
		myObjects.add(my_side3);
		my_side4 = new HalfSpace(new Vector3f(lower), new Vector3f(1, 0, 0));
		myObjects.add(my_side4);
		
	}

	/**
	 * Add some standard lights to the world.
	 */
	private void addLights() {
		Light light = new DirectionalLight(
				new Color3f(1f, 1f, 1f), new Vector3f(-1f, -1f, -1f));
		
		light.setInfluencingBounds(
				new BoundingSphere(new Point3d(0, 0, 0), 10));
		
		myScene.addChild(light);
		
		light = new DirectionalLight(
				new Color3f(0.3f, 0.1f, 0.1f), new Vector3f(1f, 0f, 0f));
		
		light.setInfluencingBounds(
				new BoundingSphere(new Point3d(0, 0, 0), 10));
		
		myScene.addChild(light);
	}
	
	/**
	 * Update the state of the world.
	 */
	public void tick() {
		// Iterate over objects in the world.
		Iterator<PhysicalObject> itr = myObjects.iterator();
	
		List<PhysicalObject> children = new LinkedList<PhysicalObject>();
		
		while (itr.hasNext()) {
			CollidableObject obj = itr.next();

			// Apply forces
			for (Force force : myForces) {
				force.applyForceTo((PhysicalObject) obj);
			}
			
			// Update the object's state.
			obj.updateState(1f / UPDATE_RATE);
			
			// Spawn new objects?
			List<PhysicalObject> newChildren =
				((PhysicalObject) obj).spawnChildren(1f / UPDATE_RATE);
			
			if (newChildren != null) {
				children.addAll(newChildren);
			}
		}
		
		/*
		  In the "tick" method of your application, rather than call the old form of 
		  resolveCollisions to completely handle a collision, you can now:
		 
 		 	1.Directly call CollisionDetector.calculateCollisions to receive an
			  ArrayList<CollisionInfo> object.
			2.If the list is empty, then there is no collision between the pair
			  of objects and nothing further need be done.
			3.If the list is not empty, then a collision has occurred and you can
			  check whether the objects involved necessitate a transmission or a standard
			  collision resolution (a.k.a. bounce).
			4.If a standard collision resolution is called for, use the new form of
			   resolveCollisions to pass in the ArrayList<CollisionInfo> object.
			   
		  The goal of this change is to prevent the computationally expensive 
		  collision detection algorithm from being executed twice when objects collide.
		 */

		for (int i = 0; i < myObjects.size() - 1; i++) {
			for (int j = i + 1; j < myObjects.size(); j++) {
				ArrayList<CollisionInfo> collisions = 
					CollisionDetector.calculateCollisions(myObjects.get(i), myObjects.get(j));
				
				if (collisions.size() > 0) {
					HalfSpace hs = null;
					PhysicalObject o = null;
					
					if (myObjects.get(i) instanceof HalfSpace) {
						// If i is a halfspace, j must be an object
						hs = (HalfSpace) myObjects.get(i);
						o = myObjects.get(j);
						
						
					} else if (myObjects.get(j) instanceof HalfSpace) {
						// If j is a halfspace, i must be an object
						hs = (HalfSpace) myObjects.get(j);
						o = myObjects.get(i);
					}
					
					// Was there a halfspace involved? If so, was it a side?
					if (hs != null && hs.normal.y != 1 && hs.normal.y != -1) {
						// Side collision, is there a peer?
						PeerInformation peer = myPeer.getPeerInDirection(o.getVelocity().x, -o.getVelocity().z);
						
						if (peer != null) {
							o.switchX();
							o.switchZ();
							myPeer.sendPayloadToPeer(peer, o);
							o.detach();
							myObjects.remove(o);
							
							// Moving on
							continue;
						}
					}
					
					// Collision as usual...
					myObjects.get(i).resolveCollisions(myObjects.get(j), collisions);
				}
			}
		}

		// Add new children to the world.
		for (PhysicalObject obj : children) {
			myScene.addChild(obj.getGroup());
		}
		
		myObjects.addAll(children);
	}
	
	/**
	 * @return the root BG of the scene.
	 */
	public BranchGroup getScene() {
		return myScene;
	}
	
	/**
	 * Add a new object to the world.
	 * 
	 * @param obj The object to add
	 */
	public void addObject(final PhysicalObject obj) {
		myScene.addChild(obj.getGroup());
		myObjects.add(obj);
	}
	
	/**
	 * Add a new force to the world.
	 * 
	 * @param force the force to add.
	 */
	public void addForce(final Force force) {
		myForces.add(force);
	}
	
	/**
	 * Remove a force from the world.
	 * 
	 * @param force The force to remove.
	 */
	public void removeForce(final Force force) {
		myForces.remove(force);
	}
	
	/**
	 * Remove all forces and objects from the world.
	 */
	public void resetWorld() {
		myForces.clear();
		
		for (CollidableObject obj : myObjects) {
			obj.detach();
		}
		myObjects.clear();
		
		addHalfspaces();
	}

	/**
	 * Observer Callback.
	 * Called when a PAYLOAD or EXTRA peer message is recieved.
	 * 
	 * @param peer The network peer.
	 * @param obj The object from the network.
	 */
	public void update(final Observable peer, final Object obj) {
		if (obj != null) {
			if (obj instanceof PhysicalObject) {
				addObject((PhysicalObject) obj);
				
			} else if (obj instanceof CollidableObject) {
				addObject(new PhysicalObject((CollidableObject) obj));
			}
		}
	}
}