From 5d414f403fa98094c6c571ad7c91342bfcc69883 Mon Sep 17 00:00:00 2001 From: Jesse Morgan Date: Wed, 9 Mar 2011 02:48:37 +0000 Subject: Adding more stuff to common. --- src/common/Box.java | 33 ++++++++++++++++++++++++++++ src/common/Circle.java | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ src/common/Earth.java | 42 +++++++++++++++++++++++++++++++++++ src/common/Particle.java | 31 ++++++++++++++++++++++++++ src/common/Polygon.java | 43 ++++++++++++++++++++++++++++++++++++ src/common/Sphere.java | 35 +++++++++++++++++++++++++++++ 6 files changed, 241 insertions(+) create mode 100644 src/common/Box.java create mode 100644 src/common/Circle.java create mode 100644 src/common/Earth.java create mode 100644 src/common/Particle.java create mode 100644 src/common/Polygon.java create mode 100644 src/common/Sphere.java (limited to 'src/common') diff --git a/src/common/Box.java b/src/common/Box.java new file mode 100644 index 0000000..5864606 --- /dev/null +++ b/src/common/Box.java @@ -0,0 +1,33 @@ +package common; + +import javax.media.j3d.*; +import javax.vecmath.*; + +@SuppressWarnings("restriction") +public class Box extends CollidableObject { + 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(mass); + setShape(createShape(width, height, depth)); + this.position.set(position); + 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/common/Circle.java b/src/common/Circle.java new file mode 100644 index 0000000..b5a3136 --- /dev/null +++ b/src/common/Circle.java @@ -0,0 +1,57 @@ +package common; + +import com.sun.j3d.utils.image.*; +import javax.media.j3d.*; +import javax.vecmath.*; + +@SuppressWarnings("restriction") +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/common/Earth.java b/src/common/Earth.java new file mode 100644 index 0000000..635e74c --- /dev/null +++ b/src/common/Earth.java @@ -0,0 +1,42 @@ +package common; + +import com.sun.j3d.utils.image.*; +import javax.media.j3d.*; +import javax.vecmath.*; + +@SuppressWarnings("restriction") +public class Earth extends Sphere { + protected float radius; + + public Earth(float radius, Vector3f position) { + this(1, radius, position); + } + + public Earth(float mass, float radius, Vector3f position) { + super(mass, radius, position); + } + + protected Node createShape(float radius, int divisions) { + TextureLoader tl = new TextureLoader("earth.png", 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); + appearance.setMaterial(new Material()); + return new com.sun.j3d.utils.geometry.Sphere(radius, com.sun.j3d.utils.geometry.Sphere.GENERATE_NORMALS | com.sun.j3d.utils.geometry.Sphere.GENERATE_TEXTURE_COORDS, divisions, appearance); + } +} diff --git a/src/common/Particle.java b/src/common/Particle.java new file mode 100644 index 0000000..f469f09 --- /dev/null +++ b/src/common/Particle.java @@ -0,0 +1,31 @@ +package common; + +import com.sun.j3d.utils.geometry.Sphere; +import java.awt.*; +import javax.media.j3d.*; +import javax.vecmath.*; + +@SuppressWarnings("restriction") +public class Particle extends CollidableObject { + protected static final float RADIUS = 0.04f; + + public Particle(Color3f color, Vector3f position, Vector3f velocity) { + this(1, color, position, velocity); + } + + public Particle(float mass, Color3f color, Vector3f position, Vector3f velocity) { + super(mass); + setShape(createShape(color)); + this.position.set(position); + this.velocity = new Vector3f(velocity); + updateTransformGroup(); + } + + private Node createShape(Color3f color) { + if (color == null) + color = new Color3f(Color.getHSBColor((float)Math.random(), 1, 1)); + Appearance appearance = new Appearance(); + appearance.setColoringAttributes(new ColoringAttributes(color, ColoringAttributes.FASTEST)); + return new Sphere(RADIUS, 0, 8, appearance); + } +} diff --git a/src/common/Polygon.java b/src/common/Polygon.java new file mode 100644 index 0000000..9240e9c --- /dev/null +++ b/src/common/Polygon.java @@ -0,0 +1,43 @@ +package common; + +import javax.media.j3d.Transform3D; +import javax.vecmath.*; + +@SuppressWarnings("restriction") +public abstract class Polygon extends CollidableObject { + protected Vector3f normal; + // Right-hand side of the plane equation: Ax + By + Cz = D + protected float intercept; + + public Polygon(Vector3f position, Vector3f normal) { + this(1, position, normal); + } + + public Polygon(float mass, Vector3f position, Vector3f normal) { + super(mass); + this.position.set(position); + 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/common/Sphere.java b/src/common/Sphere.java new file mode 100644 index 0000000..af755d9 --- /dev/null +++ b/src/common/Sphere.java @@ -0,0 +1,35 @@ +package common; + +import javax.media.j3d.*; +import javax.vecmath.*; + +@SuppressWarnings("restriction") +public class Sphere extends CollidableObject { + protected float radius; + + public Sphere(float radius, Vector3f position) { + this(1, radius, position); + } + + public Sphere(float mass, float radius, Vector3f position) { + super(mass); + setShape(createShape(radius, 22)); + this.radius = radius; + this.position.set(position); + 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); + } +} -- cgit v1.2.3