Java3D / Kugeln die auf eine Fläche fallen und verschwinden.



  • Hallo,

    ich arbeite an einem Java3D Projekt, wo ich per Mausklick auf eine Fläche, kugeln darauf fallen lasse. Die Kugeln beschleunigen, dazu habe ich schon die passende Behaviorklasse geschrieben.

    Das Problem: Immer wenn ich auf die Fläche drücke. Flackert die kugel kurz und ist weg. Ich hab schon vieles probiert und weiß nicht weiter. Hier die code ausschnitte, vll. erkennt ihr das Problem schneller.

    public class MyPickBehavior extends PickMouseBehavior{
    
    	BranchGroup click;
    	BranchGroup target;
    	Bounds bounds;
    
    	public MyPickBehavior(Canvas3D canvas, BranchGroup click,BranchGroup target, Bounds bounds){
    		super(canvas, click, bounds);
    		setSchedulingBounds(bounds);
    		this.click = click;
    		this.bounds = bounds;
    		this.target = target;
    		pickCanvas.setMode(PickTool.GEOMETRY_INTERSECT_INFO);
    	}
    
    	@Override
    	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() == MouseEvent.MOUSE_CLICKED)
    						processMouseEvent((MouseEvent) event[i]);
    				}
    			}
    		}
    		wakeupOn(new WakeupOnAWTEvent(MouseEvent.MOUSE_CLICKED));
    	}
    
    	void processMouseEvent(MouseEvent eventKey) {
    		  int keyCode = eventKey.getButton();
    		  if(keyCode==1){
    			  updateScene(eventKey.getX(),eventKey.getY());
    		  }
    	} 
    
    	public void updateScene(int x, int y) {
    
    		pickCanvas.setShapeLocation(x, y);
    		// register mouse pointer location on the screen (canvas)
    
    		Point3d eyePos = pickCanvas.getStartPosition();
    		// get the viewer's eye location
    
    		PickResult pickResult = null;
    		pickResult = pickCanvas.pickClosest();
    		// get the intersected shape closest to the viewer
    
    		if (pickResult != null) {
    
    			PickIntersection pi = pickResult.getClosestIntersection(eyePos);
    			// get the closest intersect to the eyePos point
    			Point3d intercept = pi.getPointCoordinatesVW();
    
    			Vector3d vec = new Vector3d(intercept.x,intercept.y+1,intercept.z);
    
    			BranchGroup newObj = new BranchGroup();
    			newObj.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
    
    			Sphere sphere = new Sphere(0.1f);
    			sphere.setAppearance(new Appearance());
    
    			TransformGroup animaElements = new TransformGroup(); 
    			animaElements.setCapability( TransformGroup.ALLOW_TRANSFORM_WRITE); 
    			animaElements.addChild(sphere);
    
    			Transform3D move = new Transform3D();
    			move.setTranslation(vec);
    			animaElements.setTransform(move);
    
    			AccelerateBehavior animaBehavior = new AccelerateBehavior(new Alpha(-1,3000),animaElements);
    //			LissajouAnimation animaBehavior = new LissajouAnimation(new Alpha(1,1000), new Alpha(1,1000), animaElements, 0, 5, 5);
    			animaBehavior.setSchedulingBounds(bounds);
    
    			newObj.addChild(animaElements);
    			newObj.addChild(animaBehavior);
    
    			newObj.compile();
    
    			click.addChild(newObj);
    		}
    	}
    }
    
    public class AccelerateBehavior extends Behavior{
    
    	private Alpha alpha;
    	private TransformGroup targetTG;
    	private float increaseSpeed;
    	private float startSpeed;
    	private float position;
    	private float alphaEnd;
    	private WakeupCondition wakeup;
    	private Transform3D accelerator = new Transform3D();
    
    		AccelerateBehavior(Alpha alpha,TransformGroup targetTG){
    			this.targetTG = targetTG;
    			this.alpha = alpha;
    			startSpeed = 0.5f;
    			position = startSpeed;
    			increaseSpeed = 1.0010f;
    			alphaEnd = 0.97f;
    		}
    
    		public void initialize(){
    			long t = System.currentTimeMillis(); 
    			alpha.setStartTime(t);
    			wakeup = new WakeupOnElapsedFrames(0);
    			wakeupOn(wakeup);
    		}
    
     		public void processStimulus(Enumeration criteria){
     			if(alpha.value()<alphaEnd){
     				position *= increaseSpeed;
     			}else if(alpha.value()>alphaEnd){
     				position = startSpeed;
     			}
    			accelerator.setTranslation(new Vector3d(position,0,0));
    			targetTG.setTransform(accelerator);
    			wakeupOn(wakeup);
    	}
    }
    

    Liebe Grüße



  • Bist du sicher dass du die Framerate mit einberechnest?


Log in to reply