From 350a482b32512191596daad55104beec98af9981 Mon Sep 17 00:00:00 2001 From: Jesse Morgan Date: Wed, 16 Feb 2011 08:12:56 +0000 Subject: Added alden's code, moved his objects into the object class and made them extend physical object. Fixed a node selection problem in TesseractUI. --- src/tesseract/TesseractUI.java | 20 ++++++++++---- src/tesseract/objects/Box.java | 36 ++++++++++++++++++++++++ src/tesseract/objects/Circle.java | 55 +++++++++++++++++++++++++++++++++++++ src/tesseract/objects/Particle.java | 2 +- src/tesseract/objects/Polygon.java | 43 +++++++++++++++++++++++++++++ src/tesseract/objects/Sphere.java | 35 +++++++++++++++++++++++ 6 files changed, 185 insertions(+), 6 deletions(-) create mode 100644 src/tesseract/objects/Box.java create mode 100644 src/tesseract/objects/Circle.java create mode 100644 src/tesseract/objects/Polygon.java create mode 100644 src/tesseract/objects/Sphere.java (limited to 'src/tesseract') diff --git a/src/tesseract/TesseractUI.java b/src/tesseract/TesseractUI.java index 1e8ff9c..ca882e2 100644 --- a/src/tesseract/TesseractUI.java +++ b/src/tesseract/TesseractUI.java @@ -12,6 +12,7 @@ import java.awt.event.MouseWheelListener; import javax.media.j3d.BoundingBox; import javax.media.j3d.Canvas3D; +import javax.media.j3d.Node; import javax.media.j3d.Transform3D; import javax.media.j3d.TransformGroup; import javax.swing.JCheckBoxMenuItem; @@ -39,6 +40,7 @@ import tesseract.menuitems.IcosahedronMenuItem; import tesseract.menuitems.ParticleEmitterMenuItem; import tesseract.menuitems.ParticleMenuItem; import tesseract.menuitems.PlanarPolygonMenuItem; +import tesseract.objects.Box; import tesseract.objects.ChainLink2; import tesseract.objects.PhysicalObject; @@ -149,6 +151,7 @@ public class TesseractUI extends JFrame { ChainLink2 o = new ChainLink2(new Vector3f(), 1); o.setRotation(); myWorld.addObject(o); + myWorld.addObject(new Box(0.20f, 0.09f, 0.10f, new Vector3f(0, 0.25f, 0))); } /** @@ -342,11 +345,18 @@ public class TesseractUI extends JFrame { pc.setShapeLocation(e); PickResult r = pc.pickClosest(); - if (r != null && r.getObject().getUserData() instanceof PhysicalObject) { - myCurrentObject = - (PhysicalObject) r.getObject().getUserData(); - - myCurrentObject.selected(true); + if (r != null) { + for (int i = r.getSceneGraphPath().nodeCount() - 1; + i > 0; i--) { + Node n = r.getSceneGraphPath().getNode(i); + if (n.getUserData() instanceof PhysicalObject) { + myCurrentObject = + (PhysicalObject) n.getUserData(); + myCurrentObject.selected(true); + + break; + } + } } } 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); + } +} -- cgit v1.2.3