package com.codebynumber.java3d.breakout; import javax.media.j3d.Appearance; import javax.media.j3d.BoundingSphere; import javax.media.j3d.ColoringAttributes; import javax.media.j3d.Group; import javax.media.j3d.Material; import javax.media.j3d.Transform3D; import javax.media.j3d.TransformGroup; import javax.vecmath.AxisAngle4d; import javax.vecmath.Color3f; import javax.vecmath.Point3d; import javax.vecmath.Vector3d; import com.sun.j3d.utils.geometry.Cylinder; /** * @author codebynumber.com * */ public class GameBoard { private BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0); public static final int LEFT = 0; public static final int RIGHT = 1; public static final int FRONT = 2; public GameBoard() { } /** * Creates the bumpers on the side of the game board. * @param side either LEFT or RIGHT for which bumper to create * @return Group that contains bumper and collision detector */ public Group createBumper(int side, Ball ball) { float radius = 0.1f; float length = (float) Breakout.length + 1.0f; Appearance app = new Appearance(); Color3f color = new Color3f(0.2f, 0.6f, 1.0f); Color3f black = new Color3f(0.0f, 0.0f, 0.0f); Color3f white = new Color3f(1.0f, 1.0f, 1.0f); Material bumperMat = new Material(color, black, color, white, 100.0f); bumperMat.setLightingEnable(true); app.setMaterial(bumperMat); // Cylinder bumper = new Cylinder(radius, length, Cylinder.GEOMETRY_NOT_SHARED | Cylinder.GENERATE_NORMALS, app); Cylinder bumper = new Cylinder(radius, length, app); Transform3D t3dleft = new Transform3D(); int coef = (side == LEFT ? -1 : 1); t3dleft.setTranslation(new Vector3d(coef * Breakout.width / 2, 0.0, -length / 2)); t3dleft.setRotation(new AxisAngle4d(1.0, 0.0, 0.0, -Math.PI / 2)); TransformGroup tg = new TransformGroup(t3dleft); tg.addChild(bumper); //Create collision detector to bump ball off side Vector3d sidevect = new Vector3d(-1.0, 0.0, 1.0); Collision bumperCol = new Collision(ball, tg, sidevect); bumperCol.setSchedulingBounds(bounds); tg.addChild(bumperCol); return tg; } /** * * @param b * @return */ public Group createFrontBorder(Ball b) { Transform3D t3d = new Transform3D(); t3d.setTranslation(new Vector3d(0.0, 0.0, -0.1)); TransformGroup tg = new TransformGroup(t3d); Appearance app = new Appearance(); ColoringAttributes ca = new ColoringAttributes(new Color3f(0.6f, 0.6f, 0.6f), 0); app.setColoringAttributes(ca); Box box = new Box(Breakout.width-0.2, 0.4, 0.1); box.setAppearance(app); box.setDissapear(false); Vector3d vect = new Vector3d(1.0, 0.0, -1.0); Collision bump = new Collision(b, box, vect); bump.setSchedulingBounds(bounds); tg.addChild(bump); tg.addChild(box); return tg; } public Group createRearBorder(Ball b) { Transform3D t3d = new Transform3D(); t3d.setTranslation(new Vector3d(0.0, 0.0, -11.0)); TransformGroup tg = new TransformGroup(t3d); Appearance app = new Appearance(); ColoringAttributes ca = new ColoringAttributes(new Color3f(0.6f, 0.6f, 0.6f), 0); app.setColoringAttributes(ca); Box box = new Box(Breakout.width-0.2, 0.4, 0.1); box.setAppearance(app); box.setDissapear(false); Vector3d vect = new Vector3d(1.0, 0.0, -1.0); Collision bump = new Collision(b, box, vect); bump.setSchedulingBounds(bounds); tg.addChild(bump); tg.addChild(box); return tg; } }