D
Developer_X
Gast
Hi, ich hab einen komplexeren Code, indem ich die Booleans b1,b2,b3 und b4 abfrage, aufdass sie falsch "false" oder richtig "true" sind.
Doch irgendwie scheint das ganze nicht zu klappen, wieso, wenn ihr mir helfen könnt, bitte helft mir, für euch ist das ein Klax, für mich harte Arbeit, die sich schon über viele, glaubt mir viele Wochen verläuft, ohne einen Hoffnungsschimmer, dann kam ich auf die Idee mein Problem mit Boolean zu lösen, welches eigentlich funktionieren sollte:
Ich habe 2 Klassen, "Keybehavior.java" und "WrapMaze3D.java".
In "Keybehavior" gibt es 4 Booleans, wenn ich nun auf eine Pfeil taste klicke, sollte ein boolean true gesetzt werden und alle anderen false, usw....
in "Keybehavior" gibt es auch folgendes:
public Keybehavior(Boolean b1, Boolean b2...)
bitte schaut euch dies genau an!
Dann in WrapMaze3D wird ein Keybehavior geaddet. Er sagt, dass wenn b1 true ist, eine gewisse TransformGroup (ein Ausdruck in der Java3D Programmier Sprache) etwas adden soll. Kann mir einer bitte, zum letzten mal, bitte sagen, was ich falsch mache?
KEYBEHAVIOR
WrapMaze3D
Doch irgendwie scheint das ganze nicht zu klappen, wieso, wenn ihr mir helfen könnt, bitte helft mir, für euch ist das ein Klax, für mich harte Arbeit, die sich schon über viele, glaubt mir viele Wochen verläuft, ohne einen Hoffnungsschimmer, dann kam ich auf die Idee mein Problem mit Boolean zu lösen, welches eigentlich funktionieren sollte:
Ich habe 2 Klassen, "Keybehavior.java" und "WrapMaze3D.java".
In "Keybehavior" gibt es 4 Booleans, wenn ich nun auf eine Pfeil taste klicke, sollte ein boolean true gesetzt werden und alle anderen false, usw....
in "Keybehavior" gibt es auch folgendes:
public Keybehavior(Boolean b1, Boolean b2...)
bitte schaut euch dies genau an!
Dann in WrapMaze3D wird ein Keybehavior geaddet. Er sagt, dass wenn b1 true ist, eine gewisse TransformGroup (ein Ausdruck in der Java3D Programmier Sprache) etwas adden soll. Kann mir einer bitte, zum letzten mal, bitte sagen, was ich falsch mache?
KEYBEHAVIOR
Code:
import java.awt.AWTEvent;
import java.awt.event.*;
import java.util.Enumeration;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.behaviors.vp.*;
public class KeyBehavior extends ViewPlatformBehavior
{
private static final int FORWARD = 0;
private static final int LEFT = 1;
private static final int BACK = 2;
private static final int RIGHT = 3;
private Boolean bb1=false;
private Boolean bb2=false;
private Boolean bb3=false;
private Boolean bb4=false;
private final static double MOVE_AMT = 0.2;
private final static double ROT_AMT = Math.PI / 40.0;
private static final Vector3d VFWD = new Vector3d(0,0,-MOVE_AMT);
private static final Vector3d VBACK = new Vector3d(0,0,MOVE_AMT);
private static final Vector3d VLEFT = new Vector3d(-MOVE_AMT,0,0);
private static final Vector3d VRIGHT = new Vector3d(MOVE_AMT,0,0);
private static final Vector3d VDOWN = new Vector3d(0, -MOVE_AMT ,0);
private static final Vector3d VUP = new Vector3d(0, MOVE_AMT ,0);
private int forwardKey = KeyEvent.VK_UP;
private int backKey = KeyEvent.VK_DOWN;
private int leftKey = KeyEvent.VK_LEFT;
private int rightKey = KeyEvent.VK_RIGHT;
private WakeupCondition keyPress;
private MazeManager mm;
private BirdsEye be;
private int zOffset;
private TransformGroup camera2TG;
// for repeated calcs
private Transform3D t3d = new Transform3D();
private Transform3D toMove = new Transform3D();
private Transform3D toRot = new Transform3D();
private Vector3d trans = new Vector3d();
public KeyBehavior(MazeManager mazeMan, BirdsEye bird, TransformGroup c2,Boolean b1, Boolean b2, Boolean b3, Boolean b4)
{
keyPress = new WakeupOnAWTEvent( KeyEvent.KEY_PRESSED );
bb1 = b1;
bb2 = b2;
bb3 = b3;
bb4 = b4;
mm = mazeMan;
be = bird;
camera2TG = c2;
zOffset = 0;
}
public void initialize()
{ wakeupOn( keyPress ); }
public void processStimulus( Enumeration criteria )
{
WakeupCriterion wakeup;
AWTEvent[] event;
while( criteria.hasMoreElements() ) {
wakeup = (WakeupCriterion) criteria.nextElement();
if( wakeup instanceof WakeupOnAWTEvent ) {
event = ((WakeupOnAWTEvent)wakeup).getAWTEvent();
for( int i = 0; i < event.length; i++ ) {
if( event[i].getID() == KeyEvent.KEY_PRESSED )
processKeyEvent((KeyEvent)event[i]);
}
}
}
wakeupOn( keyPress );
}
private void processKeyEvent(KeyEvent eventKey)
{
int keyCode = eventKey.getKeyCode();
if( eventKey.isAltDown() )
altMove(keyCode);
else
standardMove(keyCode);
}
private void standardMove(int keycode)
{
if(keycode == forwardKey)
{
moveBy(VFWD, FORWARD, VBACK);
bb1=true;
bb2=false;
bb3=false;
bb4=false;
}
else if(keycode == backKey)
{
moveBy(VBACK, BACK, VFWD);
bb1=false;
bb2=false;
bb3=false;
bb4=true;
}
else if(keycode == leftKey)
{
doRotateY(ROT_AMT, LEFT);
bb1=false;
bb2=false;
bb3=true;
bb4=false;
}
else if(keycode == rightKey)
{
doRotateY(-ROT_AMT, RIGHT);
bb1=false;
bb2=true;
bb3=false;
bb4=false;
}
}
private void altMove(int keycode)
{
if(keycode == backKey) {
if (zOffset > 0) {
doMove(VDOWN);
doMoveC2(VDOWN);
bb1=false;
bb2=false;
bb3=false;
bb4=true;
zOffset--;
}
}
else if(keycode == forwardKey) {
doMove(VUP);
doMoveC2(VUP);
bb1=true;
bb2=false;
bb3=false;
bb4=false;
zOffset++;
}
else if(keycode == leftKey)
{
moveBy(VLEFT, LEFT, VRIGHT);
bb1=false;
bb2=false;
bb3=true;
bb4=false;
}
else if(keycode == rightKey)
{
moveBy(VRIGHT, RIGHT, VLEFT);
bb1=false;
bb2=true;
bb3=false;
bb4=false;
}
}
private void moveBy(Vector3d theMove, int dir, Vector3d theMoveC2)
{
Point3d nextLoc = possibleMove(theMove);
if (mm.canMoveTo(nextLoc.x, nextLoc.z)) {
targetTG.setTransform(t3d);
doMoveC2(theMoveC2);
be.setMove(dir);
}
else
{
be.bangAlert();
}
}
private Point3d possibleMove(Vector3d theMove)
{
targetTG.getTransform(t3d);
toMove.setTranslation(theMove);
t3d.mul(toMove);
t3d.get(trans);
return new Point3d( trans.x, trans.y, trans.z);
}
private void doMove(Vector3d theMove)
{
targetTG.getTransform(t3d);
toMove.setTranslation(theMove);
t3d.mul(toMove);
targetTG.setTransform(t3d);
}
private void doMoveC2(Vector3d theMoveC2)
{
camera2TG.getTransform(t3d);
toMove.setTranslation(theMoveC2);
t3d.mul(toMove);
camera2TG.setTransform(t3d);
}
private void doRotateY(double radians, int dir)
{
targetTG.getTransform(t3d);
toRot.rotY(radians);
t3d.mul(toRot);
targetTG.setTransform(t3d);
camera2TG.getTransform(t3d);
t3d.mul(toRot);
camera2TG.setTransform(t3d);
be.setRotation(dir);
}
}
Code:
import javax.swing.*;
import java.awt.*;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
public class WrapMaze3D extends JPanel
{
private static final int PWIDTH = 1450; // size of panel
private static final int PHEIGHT = 600;
private Boolean b1=false;
private Boolean b2=false;
private Boolean b3=false;
private Boolean b4=false;
private static final int BOUNDSIZE = 10000; // larger than world
private SimpleUniverse su;
private BranchGroup sceneBG;
private BoundingSphere bounds; // for environment nodes
private MazeManager mazeMan; // maze manager
private TransformGroup camera2TG; // back-facing camera
void createStarBackground(BranchGroup bg)
{
java.util.Random rand = new java.util.Random();
float mag;
BranchGroup BGBranch=new BranchGroup();
Background BG=new Background();
Sphere BGSphere;
PointArray starfield = new PointArray(15000, PointArray.COORDINATES|PointArray.COLOR_3);
float[] point = new float[3];
float[] brightness = new float[3];
for (int i = 0; i < 15000; i++)
{
point[0] = (rand.nextInt(2) == 0) ? rand.nextFloat() * -1.0f :rand.nextFloat();
point[1] = (rand.nextInt(2) == 0) ? rand.nextFloat() * -1.0f :rand.nextFloat();
point[2] = (rand.nextInt(2) == 0) ? rand.nextFloat() * -1.0f :rand.nextFloat();
starfield.setCoordinate(i, point);
mag=(rand.nextFloat()+0.5f)/1.5f;
brightness[0]=mag;
brightness[1]=mag;
brightness[2]=mag;
starfield.setColor(i, brightness);
}
Shape3D StarShape=new Shape3D(starfield);
StarShape.setAppearance(new Appearance());
StarShape.getAppearance().setPointAttributes(new PointAttributes(1f,true));
BGBranch.addChild(StarShape);
BG.setGeometry(BGBranch);
BG.setApplicationBounds(new BoundingSphere(new Point3d(),100.0));
Appearance SphereApp = new Appearance();
SphereApp.setTexture((new TextureLoader("F:/Rayman/BonusGame_1/Sky.jpg",null)).getTexture());
SphereApp.setTextureAttributes(new TextureAttributes(TextureAttributes.MODULATE,new Transform3D(),new Color4f(),TextureAttributes.FASTEST));
BGSphere=new Sphere(1.0f,Sphere.GENERATE_NORMALS_INWARD|Sphere.GENERATE_TEXTURE_COORDS,50,SphereApp);
BGBranch.addChild(BGSphere);
bg.addChild(BG);
}
public WrapMaze3D(MazeManager mm, BirdsEye be, TransformGroup c2TG)
// construct the scene and the main camera
{
mazeMan = mm; // ref to maze manager
camera2TG = c2TG; // ref to second back-facong camera
setLayout( new BorderLayout() );
setOpaque( false );
setPreferredSize( new Dimension(PWIDTH, PHEIGHT));
GraphicsConfiguration config =
SimpleUniverse.getPreferredConfiguration();
Canvas3D canvas3D = new Canvas3D(config);
add("Center", canvas3D);
canvas3D.setFocusable(true);
canvas3D.requestFocus();
su = new SimpleUniverse(canvas3D);
createSceneGraph();
prepareViewPoint(be);
su.addBranchGraph( sceneBG );
} // end of WrapMaze3D()
void createSceneGraph()
// initilise the scene
{
sceneBG = new BranchGroup();
bounds = new BoundingSphere(new Point3d(0,0,0), BOUNDSIZE);
lightScene(); // add the lights
TexturedFloor floor = new TexturedFloor();
sceneBG.addChild( floor.getBG() );
sceneBG.addChild( mazeMan.getMaze() ); // add maze, using MazeManager
sceneBG.addChild( camera2TG ); // add second camera
createStarBackground(sceneBG);
sceneBG.compile(); // fix the scene
} // end of createSceneGraph()
private void lightScene()
// *No* ambient light, 2 directional lights
{
Color3f white = new Color3f(0.3f, 0.3f, 0.3f);
// Set up the ambient light
AmbientLight ambientLightNode = new AmbientLight(white);
ambientLightNode.setInfluencingBounds(bounds);
// sceneBG.addChild(ambientLightNode); // ambient commented out
// Set up the directional lights
Vector3f light1Direction = new Vector3f(-1.0f, -1.0f, -1.0f);
// left, down, backwards
Vector3f light2Direction = new Vector3f(1.0f, -1.0f, 1.0f);
// right, down, forwards
DirectionalLight light1 =
new DirectionalLight(white, light1Direction);
light1.setInfluencingBounds(bounds);
sceneBG.addChild(light1);
DirectionalLight light2 =
new DirectionalLight(white, light2Direction);
light2.setInfluencingBounds(bounds);
sceneBG.addChild(light2);
} // end of lightScene()
private void prepareViewPoint(BirdsEye be)
{
// adjust viewpoint parameters
View userView = su.getViewer().getView();
userView.setFieldOfView( Math.toRadians(90.0)); // wider FOV
// 10 and 0.1; keep ratio between 100-1000
userView.setBackClipDistance(60); // can see a long way
userView.setFrontClipDistance(0.05); // can see close things
ViewingPlatform vp = su.getViewingPlatform();
// add a spotlight and avatar to viewpoint
KeyBehavior keybeh = new KeyBehavior(mazeMan, be, camera2TG,b1,b2,b3,b4);
keybeh.setSchedulingBounds(bounds);
vp.setViewPlatformBehavior(keybeh);
PlatformGeometry pg = new PlatformGeometry();
pg.addChild( makeSpot() );
if(b1==true)
{
pg.addChild(makeAvatar()); // avatar not used here
}
vp.setPlatformGeometry( pg );
// fix starting position and orientation of viewpoint
TransformGroup steerTG = vp.getViewPlatformTransform();
initViewPosition(steerTG);
// set up keyboard controls
} // end of prepareViewPoint()
private void initViewPosition(TransformGroup steerTG)
// rotate and move the viewpoint
{
Transform3D t3d = new Transform3D();
steerTG.getTransform(t3d);
Transform3D toRot = new Transform3D();
toRot.rotY(-Math.PI);
// rotate 180 degrees around Y-axis, so facing along positive z-axis
t3d.mul(toRot);
t3d.setTranslation( mazeMan.getMazeStartPosn() ); // place at maze start
steerTG.setTransform(t3d);
} // end of initViewPosition()
private SpotLight makeSpot()
// a spotlight to help the user see in the (relative) darkness
{
SpotLight spot = new SpotLight();
spot.setPosition(0.0f, 0.5f, 0.0f); // a bit above the user
spot.setAttenuation(0.0f, 1.2f, 0.0f); // linear attentuation
spot.setSpreadAngle( (float)Math.toRadians(30.0)); // smaller angle
spot.setConcentration(5.0f); // reduce strength quicker
spot.setInfluencingBounds(bounds);
return spot;
} // end of makeSpot()
private TransformGroup makeAvatar()
{
TransformGroup U = new TransformGroup();
Appearance SphereApp = new Appearance();
SphereApp.setPolygonAttributes(new PolygonAttributes(PolygonAttributes.POLYGON_LINE,PolygonAttributes.CULL_NONE,0));
U.addChild(new Sphere(1,Sphere.GENERATE_NORMALS_INWARD,80,SphereApp));
return U;
}
}