package CollisionTest1BG;

import java.applet.Applet;
import java.awt.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.behaviors.vp.OrbitBehavior;

public class CollisionTest extends Applet {
    
    public BranchGroup createSceneGraph() {
        // Create the root of the branch graph
        BranchGroup objRoot = new BranchGroup();
        
        // Create a TransformGroup for the OrbitBehavior
        TransformGroup objTrans = new TransformGroup();
        objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        objRoot.addChild(objTrans);
        
        // Create a TransformGroup tg1 and a ColorCube
        TransformGroup tg1 = new TransformGroup();
        tg1.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
        tg1.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        Transform3D t3d = new Transform3D();
        t3d.set(new Vector3f(-5f,0f,0f));
        tg1.setTransform(t3d);
        tg1.addChild(new ColorCube(0.4));
        objTrans.addChild(tg1);
        
        // add a PositionInterpolator to tg1 to make the ColorCube move
        bounds = new BoundingSphere();
        int creasing = Alpha.INCREASING_ENABLE | Alpha.DECREASING_ENABLE;
        Alpha alpha = new Alpha(-1, creasing, 0, 0, 10000, 5000, 0, 10000, 5000, 0);
        alpha.setStartTime(System.currentTimeMillis());
        t3d.set(new Vector3f(1f,0f,0f));
        PositionInterpolator interpolator1 = new PositionInterpolator(alpha,tg1, t3d, -5f, 5f);
        interpolator1.setSchedulingBounds(bounds);
        objTrans.addChild(interpolator1);
        
        // a secound Colorcube who's moving around
        // Create a TransformGroup tg2 and a ColorCube
        TransformGroup tg2 = new TransformGroup();
        tg2.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
        tg2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        t3d.set(new Vector3f(0f,-5f,0f));
        tg2.setTransform(t3d);
        tg2.addChild(new ColorCube(0.4));
        objTrans.addChild(tg2);
        
        // add a PositionInterpolator to tg1 to make the ColorCube move
        t3d.rotZ(-Math.PI/2);
        alpha = new Alpha(-1, creasing, 0, 0, 5000, 2500, 0, 5000, 2500, 0);
        alpha.setStartTime(System.currentTimeMillis());
        PositionInterpolator interpolator2 = new PositionInterpolator(alpha,tg2, t3d, -3f, 3f);
        interpolator2.setSchedulingBounds(bounds);
        objTrans.addChild(interpolator2);
        
        // Create an Object in the center of the Scene as a CollisionDetector
        // Create Appearance for the Behavior
        Appearance appear = new Appearance();
        appear.setCapability(Appearance.ALLOW_MATERIAL_READ);
        appear.setCapability(Appearance.ALLOW_MATERIAL_WRITE);
        Material mat = new Material();
        mat.setDiffuseColor(0f,0f,1f);
        mat.setAmbientColor(0f,1f,0f);
        mat.setSpecularColor(1f,0f,0f);
        appear.setMaterial(mat);
        PolygonAttributes pa = new PolygonAttributes();
        pa.setCullFace(PolygonAttributes.CULL_NONE);
        appear.setPolygonAttributes(pa);
        Cone primitive = new Cone(1.5f,1.5f,Cone.CAP,appear);
        
        Shape3D collObj = new Shape3D(primitive.getShape(0).getGeometry(), appear);
        collObj.setCapability(Shape3D.ALLOW_APPEARANCE_READ);
        collObj.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
        collObj.setAppearance(appear);
        objTrans.addChild(collObj);
        
        // with this Behavior the collObj will change its color
        // every time another Object will enter or exit the collObj
        CollisionBehavior coll = new CollisionBehavior(collObj);
        coll.setSchedulingBounds(bounds);
        objRoot.addChild(coll);
        
        return objRoot;
    }
    
    public CollisionTest() {
    }
    
    public void init() {
        setLayout(new BorderLayout());
        GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
        
        Canvas3D c = new Canvas3D(config);
        add("Center", c);
        
        // Create a simple scene and attach it to the virtual universe
        BranchGroup scene = createSceneGraph();
        u = new SimpleUniverse(c);
        
        //create some lights
        bounds = new BoundingSphere(new Point3d(0,0,0),100.0);
        ViewingPlatform vp = u.getViewingPlatform();
        PlatformGeometry pg = new PlatformGeometry();
        
        Color3f ambientColor = new Color3f(0.5f, 0.5f, 0.5f);
        AmbientLight ambientLightNode = new AmbientLight(ambientColor);
        ambientLightNode.setInfluencingBounds(bounds);
        pg.addChild(ambientLightNode);
        
        Color3f light1Color = new Color3f(0.8f, 0.8f, 0.8f);
        Vector3f light1Direction = new Vector3f(-1f, -1f, -1.0f);
        DirectionalLight light1 = new DirectionalLight(light1Color, light1Direction);
        light1.setInfluencingBounds(bounds);
        pg.addChild(light1);
        
        vp.setPlatformGeometry(pg);
        
        // This will move the ViewPlatform back about 10f so the
        // objects in the scene can be viewed.
        Transform3D t3d = new Transform3D();
        t3d.set(new Vector3f(0f,0f,10f));
        u.getViewingPlatform().getViewPlatformTransform().setTransform(t3d);
        
        // add an OrbitBehavior for rotating the whole World
        OrbitBehavior orbitBeh = new OrbitBehavior(c);
        orbitBeh.setSchedulingBounds(bounds);
        u.getViewingPlatform().setViewPlatformBehavior(orbitBeh);
        
        u.addBranchGraph(scene);
        
    }
    
    public void destroy() {
        u.removeAllLocales();
    }
    
    //
    // The following allows CollisionTest to be run as an application
    // as well as an applet
    //
    public static void main(String[] args) {
        new MainFrame(new CollisionTest(), 256, 256);
    }
    
    private SimpleUniverse u = null;
    private BoundingSphere bounds;
    
}
