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

import java.util.List;

import javax.media.j3d.GeometryArray;
import javax.media.j3d.Node;
import javax.media.j3d.Shape3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Quat4f;
import javax.vecmath.Vector3f;

import alden.CollidableObject;

import com.sun.j3d.utils.geometry.Primitive;

/**
 * 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 class PhysicalObject extends CollidableObject {
	protected boolean collidable;
	
	protected boolean mySelected;
	
	public PhysicalObject(final Vector3f thePosition, final float mass) {
		super(mass);
		this.position.set(thePosition);
		collidable = true;
		mySelected = false;
	}

	public List<PhysicalObject> spawnChildren(final float duration) {
		return null;
	}
	
	public void addForce(final Vector3f force) {
		this.forceAccumulator.add(force);
	}
	
	public void updateState(final float duration) {
		if (!mySelected) { 
			super.updateState(duration);
			this.updateTransformGroup();
		}
	}
	
	public void setShape(final Node node) {
		TG.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
		TG.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
		
		node.setUserData(this);
		
		node.setCapability(javax.media.j3d.Node.ALLOW_BOUNDS_READ);
		node.setCapability(javax.media.j3d.Node.ALLOW_LOCAL_TO_VWORLD_READ);

		if (node instanceof Primitive) {
			Primitive prim = (Primitive) node;
			int index = 0;
			Shape3D shape;
			while ((shape = prim.getShape(index++)) != null) {
				shape.setCapability(Shape3D.ALLOW_GEOMETRY_READ);
				shape.setCapability(Node.ALLOW_LOCAL_TO_VWORLD_READ);
				shape.setCapability(Node.ALLOW_LOCAL_TO_VWORLD_READ);

				for (int i = 0; i < shape.numGeometries(); i++) {
					shape.getGeometry(i).setCapability(
							GeometryArray.ALLOW_COUNT_READ);
					shape.getGeometry(i).setCapability(
							GeometryArray.ALLOW_COORDINATE_READ);
				}

			}
		}

		super.setShape(node);
	}

	public void resolveCollisions(final PhysicalObject other) {
		if (collidable && other.collidable) {
			super.resolveCollisions(other);
		}
	}
	
	/**
	 * TODO: Test code to mess with the orientation of an object.
	 */
	public void setRotation() {
		this.orientation.x = (float) (Math.PI / 4);
		this.orientation.w = 1;
		//this.angularVelocity.x = 0.5f;
	}
	
	/**
	 * @return The position of the object.
	 */
	public Vector3f getPosition() {
		return position;
	}
	
	/**
	 * @return The orientation of the object.
	 */
	public Quat4f getOrientation() {
		return this.orientation;
	}

	/**
	 * When set to true, the object will ignore all updateState calls.
	 * 
	 * @param b true to ignore updateState. False to heed.
	 */
	public void selected(final boolean b) {
		mySelected = b;
	}
	
	/**
	 * Update the transform group after changing the position.
	 */
	public void updateTranformGroup() {
		super.updateTransformGroup();
	}
}