From 5dfb1cf78950b7e4a10c5ad15009870ae2a0cdad Mon Sep 17 00:00:00 2001 From: Steve Date: Thu, 3 Feb 2011 12:23:46 +0000 Subject: Updated Ellipsoid to extend ForceableObject. --- src/tesseract/objects/Ellipsoid.java | 109 ++++++++++++++++++----------------- src/tesseract/tests/EggTest.java | 11 +++- 2 files changed, 63 insertions(+), 57 deletions(-) (limited to 'src/tesseract') diff --git a/src/tesseract/objects/Ellipsoid.java b/src/tesseract/objects/Ellipsoid.java index e44d5ea..7e3ad7b 100644 --- a/src/tesseract/objects/Ellipsoid.java +++ b/src/tesseract/objects/Ellipsoid.java @@ -1,14 +1,15 @@ /* + * Class Ellipsoid * TCSS 491 Computational Worlds - * Author Steve Bradshaw + * Steve Bradshaw */ + package tesseract.objects; import javax.media.j3d.Appearance; -import javax.media.j3d.Group; import javax.media.j3d.Transform3D; -import javax.media.j3d.TransformGroup; import javax.vecmath.Matrix3f; +import javax.vecmath.Vector3f; import com.sun.j3d.utils.geometry.Sphere; @@ -22,82 +23,82 @@ import com.sun.j3d.utils.geometry.Sphere; * @author Steve Bradshaw * @version 1 Feb 2011 */ -public class Ellipsoid extends Sphere { - - /** - * The b in the formula (x/a)^2 + (y/b)^2 + (z/c)^2 = 1. - */ - private float my_b; +public class Ellipsoid extends ForceableObject { /** - * The c in the formula (x/a)^2 + (y/b)^2 + (z/c)^2 = 1. + * Default mass. */ - private float my_c; + private static final float DEFAULT_MASS = 1; /** - * The Group containing the new ellipsoid. + * Number of divisions in the sphere. */ - private TransformGroup my_ellipsoidTG; + private static final int DEFAULT_DIVISIONS = 50; /** - * Constructor similar to sphere but with the additions of b and c to change - * the shape. - * This constructor does not have the Appearance as an argument. + * Create a new Ellipsoid. * - * @param radius the radius of the ellipsoid if in sphere form - * @param primflags an int - * @param divisions an int - * @param b to change the shape of the ellipsoid in the y direction - * @param c to change the shape of the ellipsoid in the z direction + * @param position Initial position. + * @param mass Initial mass. + * @param radius the radius of the base sphere. + * @param primflags an int for the base spere primflags. + * @param divisions an in for the shape divisions. + * @param appearance an Appearance object. + * @param b a float for the b portion of the ellipsoid formula. + * @param c a float for the c portion of the ellipsoid formula. */ - public Ellipsoid(final float radius, final int primflags, - final int divisions, final float b, final float c) { - super(radius, primflags, divisions); + public Ellipsoid(final Vector3f position, final float mass, + final float radius, final int primflags, final int divisions, + final Appearance appearance, final float b, final float c) { + super(position, mass); - my_b = b; - my_c = c; - my_ellipsoidTG = new TransformGroup(); - createGeometry(); + createShape(radius, primflags, appearance, divisions, b, c); } /** - * Constructor similar to sphere but with the additions of b and c to change - * the shape. This constructor adds Appearance as an argument. + * Create a new Ellipsoid. * - * @param radius the radius of the ellipsoid if in sphere form - * @param primflags an int - * @param divisions an int - * @param appearance brings an Appearance object for material, color etc. - * @param b to change the shape of the ellipsoid in the y direction - * @param c to change the shape of the ellipsoid in the z direction + * @param position Initial position. + * @param radius a float for the size of the base sphere. */ - public Ellipsoid(final float radius, final int primflags, - final int divisions, final Appearance appearance, - final float b, final float c) { - super(radius, primflags, divisions, appearance); + public Ellipsoid(final Vector3f position, final float radius) { + super(position, DEFAULT_MASS); - my_b = b; - my_c = c; - my_ellipsoidTG = new TransformGroup(); - createGeometry(); + createDefaultEllipsoid(radius); } - /* - * This private method transforms a sphere using a 3D Matrix + /** + * This creates a default Ellipsoid for the 2 argument constructor. + * @param radius the siz of the ellipsoid */ - private void createGeometry() { + private void createDefaultEllipsoid(final float radius) { + + Sphere sphere = new Sphere(radius, new Sphere().getPrimitiveFlags(), + DEFAULT_DIVISIONS); Transform3D tmp = new Transform3D(); - tmp.set(new Matrix3f(1.0f, 0.0f, 0.0f, 0.0f, my_b, 0.0f, 0.0f, 0.0f, my_c)); - my_ellipsoidTG.setTransform(tmp); - my_ellipsoidTG.addChild(this); + tmp.set(new Matrix3f(1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.5f)); + getTransformGroup().setTransform(tmp); + getTransformGroup().addChild(sphere); } /** - * This method is the getter to get custom ellipsoid + * This method creates multiple ellipsoidial shapes. * - * @return Group containing the transformation of the sphere + * @param radius a float for the size of the base sphere + * @param primflags an int for the base sphere + * @param appearance an Appearance object + * @param divisions an int for the number of divisons + * @param b a float for the y axis transform + * @param c a float for the z axis transfrom */ - public Group getEllipsoid() { - return my_ellipsoidTG; + private void createShape(final float radius, final int primflags, + final Appearance appearance, final int divisions, final float b, + final float c) { + + Sphere sphere = new Sphere(radius, primflags, divisions, appearance); + Transform3D tmp = new Transform3D(); + tmp.set(new Matrix3f(1.0f, 0.0f, 0.0f, 0.0f, b, 0.0f, 0.0f, 0.0f, c)); + getTransformGroup().setTransform(tmp); + getTransformGroup().addChild(sphere); } } diff --git a/src/tesseract/tests/EggTest.java b/src/tesseract/tests/EggTest.java index d8ad831..2e254b2 100644 --- a/src/tesseract/tests/EggTest.java +++ b/src/tesseract/tests/EggTest.java @@ -83,13 +83,18 @@ public class EggTest { TransformGroup ellipsoidTG = new TransformGroup(); Appearance eApp = new Appearance(); Material eggMat = new Material(); - eggMat.setDiffuseColor(1f, 0f, 1f); + eggMat.setDiffuseColor(0f, .8f, 1f); eApp.setMaterial(eggMat); eApp.setColoringAttributes(new ColoringAttributes(0f, 1f, 1f, ColoringAttributes.ALLOW_COLOR_WRITE)); - Ellipsoid egg = new Ellipsoid(0.2f, new Sphere().getPrimitiveFlags(), 100, eApp, 0.8f, 1.5f); + Vector3f position = new Vector3f(0.2f,0,0); + + //Test for first constructor. + Ellipsoid egg = new Ellipsoid(position, 1, 0.1f, new Sphere().getPrimitiveFlags(), 100, eApp, 0.2f, 4.0f); + //Test for second constructor. + //Ellipsoid egg = new Ellipsoid(position, 0.3f); //unlike the basic sphere or cube etc., you must use a getter or will throw exception - ellipsoidTG.addChild(egg.getEllipsoid()); + ellipsoidTG.addChild(egg.getGroup()); BranchGroup bg = new BranchGroup(); bg.addChild(ellipsoidTG); -- cgit v1.2.3