I am trying to create a 3D-model of the solar system using Java 3D, I am very new to Java 3D, Java and programming in general so I may be missing something really obvious.
I need to to write behaviours which are active constantly in order to maintain the relations between objects when "off-screen", I have tried to do this using the BoundingLeaf node attached to the ViewingPlatform and to a Background as suggested in the 3D Tutorial chapter 3, then I set the bounds of my behaviour to this bounding leaf. This compiles o.k and renders but the behaviour never wakes up.
I would be very grateful for any help as to where I'm going wrong.
Cheers
Jenni.
-- x
import java.lang.Math; import java.applet.Applet; import java.awt.BorderLayout; import java.lang.Math; import java.awt.Frame; import java.awt.event.*; import com.sun.j3d.utils.applet.MainFrame; import com.sun.j3d.utils.universe.*; import com.sun.j3d.utils.geometry.*; import javax.media.j3d.*; import javax.vecmath.*;
public class EarthOrb extends Applet{ public EarthOrb(){ setLayout(new BorderLayout()); Canvas3D canvas3D = new Canvas3D(null); add("Center", canvas3D); //Create Simple Universe SimpleUniverse simpleU = new SimpleUniverse(canvas3D); //Set viewing platform geometry including Bounding Leaf BoundingLeaf boundingLeaf = new BoundingLeaf(); PlatformGeometry platformGeom = new PlatformGeometry(); platformGeom.addChild(boundingLeaf); platformGeom.compile(); simpleU.getViewingPlatform().setPlatformGeometry(platformGeom); //Get and Set viewing platform transform TransformGroup vpTrans = null; vpTrans = simpleU.getViewingPlatform().getViewPlatformTransform(); Vector3f translate = new Vector3f(); translate.set(0.0f,0.0f,16.0f); Transform3D T3D = new Transform3D(); T3D.setTranslation(translate); vpTrans.setTransform(T3D); //Call function to create content branchs and compile. BranchGroup scene = new BranchGroup(); scene.addChild(createSunGraph()); BranchGroup earth = new BranchGroup(); TransformGroup ETrans = createEarthGraph(); //add Background to content branch group Background backg = new Background(1.0f, 1.0f, 1.0f); backg.setApplicationBoundingLeaf(boundingLeaf); scene.addChild(backg); //Add behaviour to earth Branch Group OrbitBehavior earthOrbit = new OrbitBehavior(ETrans); earthOrbit.setSchedulingBoundingLeaf(boundingLeaf); ETrans.addChild(earthOrbit); earth.addChild(ETrans); //Add to content branch group scene.addChild(earth); scene.compile(); //add content Branch simpleU.addBranchGraph(scene); } public BranchGroup createSunGraph(){ // Create root of Sun Branch BranchGroup objRoot = new BranchGroup(); Sphere sphere = new Sphere(0.695f); Color3f sunColor = new Color3f(1.0f, 0.7f, 0.3f); //give default appearance and material Appearance appear = new Appearance(); Material material = new Material(); material.setSpecularColor(sunColor); material.setAmbientColor(sunColor); material.setDiffuseColor(sunColor); material.setEmissiveColor(sunColor); appear.setMaterial(material); sphere.setAppearance(appear); objRoot.addChild(sphere); //add PointLight PointLight lightP = new PointLight(); objRoot.addChild(lightP); return objRoot; } public TransformGroup createEarthGraph(){ //Create and set capability of TG Transform3D startpos = new Transform3D(); Vector3d position = new Vector3d(10.536d, 0.0d, -10.536d); startpos.setTranslation(position); TransformGroup orbit = new TransformGroup(startpos); orbit.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); //Create the earth and color it Sphere sphere = new Sphere(0.064f); Color3f earthColor = new Color3f(0.0f, 1.0f, 1.0f); Appearance appear = new Appearance(); Material material = new Material(); material.setSpecularColor(earthColor); material.setAmbientColor(earthColor); material.setDiffuseColor(earthColor); material.setEmissiveColor(earthColor); appear.setMaterial(material); sphere.setAppearance(appear); orbit.addChild(sphere); return orbit; } }
import java.lang.Math; import java.applet.Applet; import java.awt.BorderLayout; import java.awt.Frame; import java.awt.event.*; import com.sun.j3d.utils.applet.MainFrame; import com.sun.j3d.utils.universe.*; import com.sun.j3d.utils.geometry.*; import javax.media.j3d.*; import javax.vecmath.*; import java.util.Enumeration; public class OrbitBehavior extends Behavior{ private TransformGroup targetTG; private Transform3D set_pos = new Transform3D(); private double angle = 45; public OrbitBehavior(TransformGroup targetTG){ this.targetTG = targetTG; } public void initialize(){ this.wakeupOn(new WakeupOnElapsedTime(100)); } public void processStimulus(Enumeration Criteria){ System.out.println("processStimulus reached"); Vector3d co_ords = new Vector3d(proceedOrbitCo_ords()); set_pos.setTranslation(co_ords); targetTG.setTransform(set_pos); this.wakeupOn(new WakeupOnElapsedTime(100)); System.out.println("reset of behavior"); return; } private Vector3d proceedOrbitCo_ords(){ double x, y, z; if((angle + 360/365.25) < 360) angle += 360/365.26; else angle += (360/365.25)-360; x = Math.sin(angle)*14900; z = Math.cos(angle)*14900; y = 0; Vector3d co_ord = new Vector3d(x,y,z); return co_ord; } }