summaryrefslogtreecommitdiff
path: root/src/tesseract/objects
diff options
context:
space:
mode:
Diffstat (limited to 'src/tesseract/objects')
-rw-r--r--src/tesseract/objects/Box.java36
-rw-r--r--src/tesseract/objects/Circle.java55
-rw-r--r--src/tesseract/objects/Particle.java2
-rw-r--r--src/tesseract/objects/Polygon.java43
-rw-r--r--src/tesseract/objects/Sphere.java35
5 files changed, 170 insertions, 1 deletions
diff --git a/src/tesseract/objects/Box.java b/src/tesseract/objects/Box.java
new file mode 100644
index 0000000..fb3dc42
--- /dev/null
+++ b/src/tesseract/objects/Box.java
@@ -0,0 +1,36 @@
+package tesseract.objects;
+import javax.media.j3d.Appearance;
+import javax.media.j3d.Geometry;
+import javax.media.j3d.Material;
+import javax.media.j3d.Node;
+import javax.vecmath.Vector3f;
+
+import com.sun.j3d.utils.geometry.Primitive;
+
+public class Box extends PhysicalObject {
+ public Box(float width, float height, float depth, Vector3f position) {
+ this(1, width, height, depth, position);
+ }
+
+ public Box(float mass, float width, float height, float depth, Vector3f position) {
+ super(position, mass);
+ setShape(createShape(width, height, depth));
+
+ previousPosition.set(position);
+ if (inverseMass != 0) {
+ inverseInertiaTensor.m00 = 1f / 12 / inverseMass * (height * height + depth * depth);
+ inverseInertiaTensor.m11 = 1f / 12 / inverseMass * (width * width + depth * depth);
+ inverseInertiaTensor.m22 = 1f / 12 / inverseMass * (width * width + height * height);
+ inverseInertiaTensor.invert();
+ }
+ updateTransformGroup();
+ }
+
+ protected Node createShape(float width, float height, float depth) {
+ Appearance appearance = new Appearance();
+ Material material = new Material();
+ material.setDiffuseColor(0.7f, 1, 0.7f);
+ appearance.setMaterial(material);
+ return new com.sun.j3d.utils.geometry.Box(width / 2, height / 2, depth / 2, appearance);
+ }
+}
diff --git a/src/tesseract/objects/Circle.java b/src/tesseract/objects/Circle.java
new file mode 100644
index 0000000..3019a2b
--- /dev/null
+++ b/src/tesseract/objects/Circle.java
@@ -0,0 +1,55 @@
+package tesseract.objects;
+import com.sun.j3d.utils.image.*;
+import javax.media.j3d.*;
+import javax.vecmath.*;
+
+public class Circle extends Polygon {
+ public Circle(float radius, Vector3f position, Vector3f normal) {
+ this(1, radius, position, normal);
+ }
+
+ public Circle(float mass, float radius, Vector3f position, Vector3f normal) {
+ super(mass, position, normal);
+ setShape(createShape(radius, 22));
+ if (inverseMass != 0) {
+ inverseInertiaTensor.m00 = 1f / 4 / inverseMass * radius * radius;
+ inverseInertiaTensor.m11 = 2 * inverseInertiaTensor.m00;
+ inverseInertiaTensor.m22 = inverseInertiaTensor.m00;
+ inverseInertiaTensor.invert();
+ }
+ updateTransformGroup();
+ }
+
+ protected Node createShape(float radius, int divisions) {
+ TriangleFanArray geometry = new TriangleFanArray(divisions, TriangleFanArray.COORDINATES | TriangleFanArray.TEXTURE_COORDINATE_2, new int[] {divisions});
+ for (int i = 0; i < divisions; i++) {
+ float baseX = (float)Math.cos(2 * Math.PI * i / divisions);
+ float baseZ = -(float)Math.sin(2 * Math.PI * i / divisions);
+ geometry.setCoordinate(i, new Point3f(radius * baseX, 0, radius * baseZ));
+ geometry.setTextureCoordinate(0, i, new TexCoord2f((baseX + 1) / 2, (-baseZ + 1) / 2));
+ }
+
+ TextureLoader tl = new TextureLoader("wood.jpg", null);
+ ImageComponent2D image = tl.getImage();
+ int width = image.getWidth();
+ int height = image.getHeight();
+ Texture2D texture = new Texture2D(Texture.MULTI_LEVEL_MIPMAP, Texture.RGB, width, height);
+
+ int imageLevel = 0;
+ texture.setImage(imageLevel, image);
+ while (width > 1 || height > 1) {
+ imageLevel++;
+ if (width > 1) width /= 2;
+ if (height > 1) height /= 2;
+ texture.setImage(imageLevel, tl.getScaledImage(width, height));
+ }
+ texture.setMagFilter(Texture2D.NICEST);
+ texture.setMinFilter(Texture2D.NICEST);
+
+ Appearance appearance = new Appearance();
+ appearance.setTexture(texture);
+ PolygonAttributes polyAttr = new PolygonAttributes(PolygonAttributes.POLYGON_FILL, PolygonAttributes.CULL_NONE, 0);
+ appearance.setPolygonAttributes(polyAttr);
+ return new Shape3D(geometry, appearance);
+ }
+}
diff --git a/src/tesseract/objects/Particle.java b/src/tesseract/objects/Particle.java
index 6dcc864..b3b6274 100644
--- a/src/tesseract/objects/Particle.java
+++ b/src/tesseract/objects/Particle.java
@@ -75,7 +75,7 @@ public class Particle extends PhysicalObject {
cAttr = new ColoringAttributes(color, ColoringAttributes.FASTEST);
Appearance appearance = new Appearance();
appearance.setColoringAttributes(cAttr);
- Sphere sphere = new Sphere(RADIUS, Sphere.ENABLE_GEOMETRY_PICKING | Sphere.GEOMETRY_NOT_SHARED,
+ Sphere sphere = new Sphere(RADIUS, Sphere.ENABLE_GEOMETRY_PICKING,
DIVISIONS, appearance);
TransformGroup tg = new TransformGroup();
tg.addChild(sphere);
diff --git a/src/tesseract/objects/Polygon.java b/src/tesseract/objects/Polygon.java
new file mode 100644
index 0000000..c8fcf53
--- /dev/null
+++ b/src/tesseract/objects/Polygon.java
@@ -0,0 +1,43 @@
+package tesseract.objects;
+import javax.media.j3d.Transform3D;
+import javax.vecmath.*;
+
+import alden.CollidableObject;
+
+public abstract class Polygon extends PhysicalObject {
+ public Vector3f normal;
+ // Right-hand side of the plane equation: Ax + By + Cz = D
+ public float intercept;
+
+ public Polygon(Vector3f position, Vector3f normal) {
+ this(1, position, normal);
+ }
+
+ public Polygon(float mass, Vector3f position, Vector3f normal) {
+ super(position, mass);
+
+ this.normal = new Vector3f(normal);
+ this.normal.normalize();
+ intercept = this.normal.dot(position);
+ Vector3f newX = new Vector3f(1, 0, 0);
+ if (Math.abs(newX.dot(this.normal)) == 1)
+ newX = new Vector3f(0, -1, 0);
+ newX.scaleAdd(-newX.dot(this.normal), this.normal, newX);
+ newX.normalize();
+ Vector3f newZ = new Vector3f();
+ newZ.cross(newX, this.normal);
+ new Matrix4f(new Matrix3f(newX.x, this.normal.x, newZ.x, newX.y, this.normal.y, newZ.y, newX.z, this.normal.z, newZ.z), position, 1).get(orientation);
+ }
+
+ protected void updateTransformGroup() {
+ super.updateTransformGroup();
+ Transform3D tmp = new Transform3D();
+ TG.getTransform(tmp);
+ Matrix3f rot = new Matrix3f();
+ tmp.get(rot);
+ normal.x = rot.m01;
+ normal.y = rot.m11;
+ normal.z = rot.m21;
+ intercept = normal.dot(position);
+ }
+}
diff --git a/src/tesseract/objects/Sphere.java b/src/tesseract/objects/Sphere.java
new file mode 100644
index 0000000..4ec4aaa
--- /dev/null
+++ b/src/tesseract/objects/Sphere.java
@@ -0,0 +1,35 @@
+package tesseract.objects;
+import javax.media.j3d.*;
+import javax.vecmath.*;
+
+import alden.CollidableObject;
+
+public class Sphere extends PhysicalObject {
+ public float radius;
+
+ public Sphere(float radius, Vector3f position) {
+ this(1, radius, position);
+ }
+
+ public Sphere(float mass, float radius, Vector3f position) {
+ super(position, mass);
+ setShape(createShape(radius, 22));
+ this.radius = radius;
+
+ if (inverseMass != 0) {
+ inverseInertiaTensor.m00 = 2f / 5 / inverseMass * radius * radius;
+ inverseInertiaTensor.m11 = inverseInertiaTensor.m00;
+ inverseInertiaTensor.m22 = inverseInertiaTensor.m00;
+ inverseInertiaTensor.invert();
+ }
+ updateTransformGroup();
+ }
+
+ protected Node createShape(float radius, int divisions) {
+ Appearance appearance = new Appearance();
+ Material material = new Material();
+ material.setDiffuseColor(0.7f, 0.7f, 1);
+ appearance.setMaterial(material);
+ return new com.sun.j3d.utils.geometry.Sphere(radius, com.sun.j3d.utils.geometry.Sphere.GENERATE_NORMALS, divisions, appearance);
+ }
+}