summaryrefslogtreecommitdiff
path: root/src/tesseract/objects/PhysicalObject.java
blob: 23da7340ebc97d1585693abbc4b54492d09792a3 (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
package tesseract.objects;

import java.util.Enumeration;
import java.util.List;

import javax.media.j3d.Behavior;
import javax.media.j3d.BoundingBox;
import javax.media.j3d.BoundingLeaf;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.WakeupOnTransformChange;
import javax.vecmath.Point3d;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3f;

/**
 * This class is the parent of all objects in the world.
 * 
 * Note: The constructor of a child class must add its shape
 *  to the transform group for it to be visible.
 * 
 * @author Jesse Morgan
 */
public abstract class PhysicalObject extends TransformGroup implements Physical {
	/**
	 * The object's current position.
	 */
	protected Vector3f myPosition;
	
	/**
	 * BranchGroup of the object.
	 */
	private BranchGroup myBranchGroup;
	
	/**
	 * TransformGroup for the object.
	 */
	private TransformGroup myTransformGroup;
	
	/**
	 * Does the object still exist in the world.
	 */
	protected boolean myExistance;

	/**
	 * 
	 */
	private int skipTGUpdates;
	
	/**
	 * Constructor for a PhysicalObject.
	 * 
	 * @param position Initial position.
	 */
	
	public PhysicalObject(final Vector3f position) {
		skipTGUpdates = 0;
		myPosition = new Vector3f(position);
		
		myExistance = true;
		
		myTransformGroup = this; //new TransformGroup();
		myTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		myTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
		myTransformGroup.setCapability(TransformGroup.ENABLE_PICK_REPORTING);

		
		myBranchGroup = new BranchGroup();
		myBranchGroup.setCapability(BranchGroup.ALLOW_DETACH);
		myBranchGroup.addChild(myTransformGroup);
		//myBranchGroup.addChild(new TGUpdateBehavior(null));

		updateTransformGroup();
	}

	public void setTransform(Transform3D t1) {
		super.setTransform(t1);
		
		Point3f pos = new Point3f(myPosition);
		t1.transform(pos);
		myPosition = new Vector3f(pos);
	}
	
	/**
	 * @return The object's position.
	 */
	public Vector3f getPosition() {
		return myPosition;
	}
	
	/**
	 * Update the object's position.
	 * 
	 * @param position The new position.
	 */
	public void setPosition(final Vector3f position) {
		myPosition = new Vector3f(position);
		updateTransformGroup();
	}
	
	/**
	 * @return The transform group of the object.
	 */
	protected TransformGroup getTransformGroup() {
		return myTransformGroup;
	}
	
	
	/**
	 * @return Get the BranchGroup.
	 */
	public BranchGroup getGroup() {
		return myBranchGroup;
	}
	
	/**
	 * Remove the object from the world.
	 */
	public void detach() {
		myBranchGroup.detach();
		myExistance = false;
	}
	
	/**
	 * Does this object still exist.
	 * @return true if it exists.
	 */
	public boolean isExisting() {
		return myExistance;
	}

	/**
	 * Update the TransformGroup to the new position.
	 */
	protected void updateTransformGroup() {
		Transform3D tmp = new Transform3D();
		tmp.setTranslation(myPosition);
		
		skipTGUpdates++;
		super.setTransform(tmp);
	}

	/**
	 * Update the state of the object.
	 * @param duration How much time has passed.
	 * @return New objects to add to the world.
	 */
	public List<PhysicalObject> updateState(final float duration) {
		return null;
	}
	
	private class TGUpdateBehavior extends Behavior {
		public TGUpdateBehavior(final BoundingLeaf boundingLeaf) {
			//setSchedulingBoundingLeaf(boundingLeaf);
			setSchedulingBounds(new BoundingBox(new Point3d(-0.5, -0.5, -0.5), 
					new Point3d(0.5, 0.5, 0.5)));
		}

		public void initialize() {
			wakeupOn(new WakeupOnTransformChange(getTransformGroup()));
		}

		public void processStimulus(final Enumeration e) {
			if (skipTGUpdates == 0) {
				System.out.println(myPosition);
				
				Transform3D t3d = new Transform3D();
				getTransformGroup().getTransform(t3d);
				
				Point3f pos = new Point3f(myPosition);
				t3d.transform(pos);
				System.out.println(pos);
				myPosition = new Vector3f(pos);
			} else {
				skipTGUpdates--;
				System.out.println("Skip");
			}
			
			wakeupOn(new WakeupOnTransformChange(getTransformGroup()));
		}
	}
}