Also ich muss erst die Figur drehen und dann wieder auf die Kugel runter setzen?Marco13 hat gesagt.:Code:Wurzel -> TransformGroup für die Rotation -> TransformGroup für das Verschieben der Figur an die Spitze der Kugel -> Die Figur
package scuttle_ball;
import com.sun.j3d.utils.behaviors.vp.OrbitBehavior;
import com.sun.j3d.utils.picking.PickResult;
import com.sun.j3d.utils.picking.PickTool;
import com.sun.j3d.utils.universe.SimpleUniverse;
import java.awt.event.KeyEvent;
import java.util.Enumeration;
import javax.media.j3d.Behavior;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.PickRay;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.WakeupCondition;
import javax.media.j3d.WakeupCriterion;
import javax.media.j3d.WakeupOnAWTEvent;
import javax.media.j3d.WakeupOnCollisionEntry;
import javax.media.j3d.WakeupOnCollisionExit;
import javax.media.j3d.WakeupOnElapsedTime;
import javax.media.j3d.WakeupOr;
import javax.vecmath.*;
import javax.vecmath.Vector3d;
public class FigurBehavior extends Behavior{
private TransformGroup ziel;
private WakeupCondition wc;
private PickRay picker;
private PickTool myPicker;
private boolean kollision = false;
private BranchGroup bg;
private double distanz = 100;
private double letzteDistanz = 0;
private int irgendwas = 0;
private SimpleUniverse su;
private OrbitBehavior ob;
private double aenderung = 0.01f;
// XXX
private Matrix4f currentRotation = new Matrix4f();
private void rotateY(float delta)
{
Matrix4f transform = new Matrix4f();
transform.setIdentity();
transform.rotY(delta);
currentRotation.mul(transform);
}
private void rotateX(float delta)
{
Matrix4f transform = new Matrix4f();
transform.setIdentity();
transform.rotX(delta);
currentRotation.mul(transform);
}
public FigurBehavior(TransformGroup zielInc, BranchGroup bgInc, SimpleUniverse universum, OrbitBehavior obInc) {
ziel = zielInc;
picker = new PickRay();
bg = bgInc;
myPicker = new PickTool(bg);
su = universum;
ob = obInc;
currentRotation.setIdentity();
}
public void initialize(){
WakeupCriterion kriterien[] = new WakeupCriterion[3];
kriterien[0] = new WakeupOnCollisionEntry(ziel);
kriterien[1] = new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED);
kriterien[2] = new WakeupOnCollisionExit(ziel);
wc = new WakeupOr(kriterien);
this.wakeupOn(wc);
}
public double getAktuelleDistance(){
return distanz;
}
public void processStimulus(Enumeration criteria){
Object element;
Vector3d position = new Vector3d();
Vector3d winkelding = new Vector3d();
Transform3D kamera = new Transform3D();
Transform3D posi = new Transform3D();
ziel.getTransform(posi);
posi.get(position);
Point3d v3d = new Point3d(position.getX(),position.getY(),position.getZ());
myPicker.setMode(PickTool.BOUNDS);
myPicker.setShapeRay(v3d,new Vector3d(0,-1,0));
PickResult pickResultat = myPicker.pickClosest();
while(criteria.hasMoreElements()){
element = criteria.nextElement();
WakeupCriterion wup = (WakeupCriterion) element;
if(wup instanceof WakeupOnElapsedTime){
if(pickResultat.numIntersections() > 0){
letzteDistanz = distanz;
distanz = pickResultat.getIntersection(0).getDistance();
}
if(getAktuelleDistance() > 0.7){
position.setY(position.getY()-0.02f);
}else{
if(getAktuelleDistance() < 0.67){
position.setY(position.getY()+0.02f);
}
}
posi.set(position);
}
if(wup instanceof WakeupOnAWTEvent){
WakeupOnAWTEvent event = (WakeupOnAWTEvent)element;
//aus dem AWTEvent ein KeyEvent casten
KeyEvent ke = (KeyEvent)event.getAWTEvent()[0];
//Bei welchem key was geschehen soll
ob.setRotationCenter(v3d);
// XXX
switch(ke.getKeyCode()){
case KeyEvent.VK_RIGHT:{
rotateY(-0.1f);
break;
}
case KeyEvent.VK_LEFT:{
rotateY(0.1f);
break;
}
case KeyEvent.VK_UP:{
rotateX(-0.01f);
break;
}
case KeyEvent.VK_DOWN:{
rotateX(0.01f);
break;
}
}
ziel.setTransform(new Transform3D(currentRotation));
if(pickResultat.numIntersections() > 0){
letzteDistanz = distanz;
distanz = pickResultat.getIntersection(0).getDistance();
}
if(getAktuelleDistance() > 0.71){
position.setY(position.getY()-aenderung);
}else{
if(getAktuelleDistance() < 0.67){
position.setY(position.getY()+aenderung);
}
}
posi.set(position);
}
if(wup instanceof WakeupOnCollisionEntry){
if(!kollision){
System.out.println("Kollision");
kollision = true;
}
}
if(wup instanceof WakeupOnCollisionExit){
if(kollision){
System.out.println("Kollision exit");
kollision = false;
}
}
}
//ziel.setTransform(posi);
wakeupOn(wc);
}
}
package scuttle_ball;
import com.sun.j3d.loaders.IncorrectFormatException;
import com.sun.j3d.loaders.ParsingErrorException;
import com.sun.j3d.utils.behaviors.vp.OrbitBehavior;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.universe.SimpleUniverse;
import com.sun.j3d.utils.universe.ViewingPlatform;
import java.awt.BorderLayout;
import java.io.FileNotFoundException;
import javax.media.j3d.AmbientLight;
import javax.media.j3d.Appearance;
import javax.media.j3d.Background;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.ColoringAttributes;
import javax.media.j3d.ImageComponent2D;
import javax.media.j3d.Material;
import javax.media.j3d.PolygonAttributes;
import javax.media.j3d.SpotLight;
import javax.media.j3d.Texture2D;
import javax.media.j3d.TextureAttributes;
import javax.media.j3d.Transform3D;
import javax.vecmath.*;
import javax.vecmath.Color4f;
import javax.vecmath.Point3d;
import com.jlindamood.MS3D.*;
import com.sun.j3d.loaders.Scene;
import com.sun.j3d.loaders.objectfile.ObjectFile;
import com.sun.j3d.utils.geometry.Sphere;
import javax.media.j3d.Node;
import javax.media.j3d.Shape3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Vector3d;
public class Panel3D extends javax.swing.JPanel {
private SimpleUniverse universe;
private OrbitBehavior orbit;
public Panel3D() {
setLayout(new BorderLayout());
Canvas3D canvas = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
add("Center", canvas);
universe = new SimpleUniverse(canvas);
//OrbitBehavior
orbit = new OrbitBehavior(canvas, OrbitBehavior.REVERSE_ALL );
BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
orbit.setSchedulingBounds(bounds);
orbit.setZoomFactor(5.0);
ViewingPlatform viewingplatform = universe.getViewingPlatform();
viewingplatform.setViewPlatformBehavior(orbit);
universe.getViewingPlatform().setNominalViewingTransform();
universe.addBranchGraph(setLicht());
BranchGroup scene = createScene();
scene.compile();
universe.addBranchGraph(scene);
}
public BranchGroup createScene(){
TransformGroup figurRotation;
TransformGroup figurTranslation;
FigurBehavior figurVerhalten;
Transform3D objTransform = new Transform3D();
BranchGroup figurBranchGroup;
BranchGroup objRoot = new BranchGroup();
MilkLoader ML = new MilkLoader();
ML.setFlags(MilkLoader.LOAD_ALL);
try {
Scene figur = ML.load("TeufelMitBones.ms3d");
figurBranchGroup = figur.getSceneGroup();
MilkAnimation ma = (MilkAnimation) figur.getBehaviorNodes()[0];
ma.setDuration(30);
ma.setSchedulingBounds(new BoundingSphere(new Point3d(0, 0, 0), Double.MAX_VALUE));
figurBranchGroup.addChild(figur.getBehaviorNodes()[0]);
//objTransform.setTranslation(new Vector3d(0,5,0));
// XXX
figurTranslation = new TransformGroup();
Transform3D translation = new Transform3D();
translation.set(new Vector3f(0,4.65f,0));
figurTranslation.setTransform(translation);
figurRotation = new TransformGroup(objTransform);
figurRotation.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
figurRotation.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
figurRotation.setCapability(TransformGroup.ALLOW_COLLIDABLE_READ);
Node figurAusBranchgroupScene = figurBranchGroup.getChild(0);
figurAusBranchgroupScene.setCollidable(true);
figurAusBranchgroupScene.setPickable(false);
figurBranchGroup.removeChild(0);
figurTranslation.addChild(figurAusBranchgroupScene);
figurRotation.addChild(figurTranslation);
objRoot.addChild(figurBranchGroup);
objRoot.addChild(figurRotation);
figurVerhalten = new FigurBehavior(figurRotation,objRoot,universe,orbit);
figurVerhalten.setSchedulingBounds(new BoundingSphere(new Point3d(), 10000000));
objRoot.addChild(figurVerhalten);
} catch (ParsingErrorException ex) {
ex.printStackTrace();
} catch (IncorrectFormatException ex) {
ex.printStackTrace();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
Sphere kugel = new Sphere(4f,3,50);
kugel.setAppearance(make_Appearance());
objRoot.addChild(kugel);
return objRoot;
}
// Szene laden
// neues Aussehen geben
private Appearance make_Appearance(){
Appearance a = new Appearance();
PolygonAttributes pa = new PolygonAttributes();
pa.setBackFaceNormalFlip(false);
pa.setCullFace(PolygonAttributes.CULL_NONE);
a.setPolygonAttributes(pa);
ColoringAttributes color = new ColoringAttributes(1,1,0,1);
a.setColoringAttributes(color);
TextureLoader loader = new TextureLoader("texdtur.png", this);
ImageComponent2D image = loader.getImage();
Texture2D texture = new Texture2D();
int form = texture.getFormat();
int base = texture.getMipMapMode();
System.out.println("form: " + form + " base " + base);
texture = new Texture2D(base,form,256,256);
texture.setImage(0, image);
Appearance appear = new Appearance();
a.setTexture(texture);
Transform3D t3d = new Transform3D();
t3d.setScale(20d);
TextureAttributes texat = new TextureAttributes(TextureAttributes.MODULATE,t3d,new Color4f(),TextureAttributes.NICEST);
a.setTextureAttributes(texat);
a.setMaterial(new Material());
return a;
}
public BranchGroup setLicht() {
BranchGroup model = new BranchGroup();
// weißes SpotLicht
SpotLight spot = new SpotLight();
spot.setInfluencingBounds(new BoundingSphere());
spot.setColor(new Color3f(1.0f,1.0f,1.0f));
spot.setPosition(-8.0f, 10.0f, 5.0f);
model.addChild(spot);
// weißes SpotLicht
SpotLight spot1 = new SpotLight();
spot1.setInfluencingBounds(new BoundingSphere(new Point3d(0,0,0),50));
spot1.setColor(new Color3f(1.0f,1.0f,1.0f));
spot1.setPosition(8.0f, -10.0f, -5.0f);
model.addChild(spot1);
// Richtungsloses Umgebungslicht
AmbientLight ambientLight = new AmbientLight();
ambientLight.setInfluencingBounds(new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0));
model.addChild(ambientLight);
model.addChild(new AmbientLight(true, new Color3f(1.0f,1.0f,1.0f)));
// Background erzeugen
Background bg = new Background();
bg.setColor(new Color3f(0.0f, 0.0f, 0.0f));
bg.setApplicationBounds(new BoundingSphere());
model.addChild(bg);
model.compile();
return model;
}
public Scene loadobj(String filename) {
ObjectFile file = new ObjectFile(ObjectFile.RESIZE);
Scene scene = null;
try {
scene = file.load(filename);
System.out.println("erfolgreich geladen");
} catch (FileNotFoundException ex) {
System.out.println("nich da");
System.exit(1);
} catch (IncorrectFormatException ex) {
ex.printStackTrace();
System.exit(1);
} catch (ParsingErrorException ex) {
ex.printStackTrace();
System.exit(1);
}
return scene;
}
// <editor-fold defaultstate="collapsed" desc=" Erzeugter Quelltext ">//GEN-BEGIN:initComponents
private void initComponents() {
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
}// </editor-fold>//GEN-END:initComponents
// Variablendeklaration - nicht modifizieren//GEN-BEGIN:variables
// Ende der Variablendeklaration//GEN-END:variables
}