[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Java3Djp:00576] KeyEventからのAnimation2



★★ 新聞記事自動出力サービスの1週間無料体験募集中(原則法人向)★★
 当日朝刊28紙から貴方の指定したテーマの記事が朝一番でFAXされます
 メールで担当の辻宛お申し込み下さい(d07686@xxxxxxxxxx)
 必須記入項目1.会社名2.会社住所3.部署名4.電話番号5.FAX番号
           6.希望する記事の内容(できるだけ具体的に)
 (株)電通 エレクトロニック・ライブラリー事業局(03-5551-7047) 辻
===================== http://www.elnet.co.jp ======================

先日KeyEventからのAnimationを質問したのですが、その後
物体を複数生成して、一つ一つにKeyを割り当てて動かしたんですが、
一つの物体は最後まで見えるんですが、他の物体は一度動かすと見えなくなって
しまいます。

以下はソースですが、キーボードのAをPressしたら、Cubeを下に回転させ
て、Release
したら上に上げて、同様にキーボードのSでもう一つのCubeを同じ動きをさ
せようと
しています。

何がぬけてるんでしょう?
Jun Sakurai

public class HelloJava3dk extends Applet implements KeyListener
{
	Alpha			rotAlpha;
	BoundingSphere		bounds;
	BranchGroup 		objRoot;
	BranchGroup		behaviorBranch_do, behaviorBranch_re;
	RotationInterpolator	ipr_do_down, ipr_do_up;
	RotationInterpolator	ipr_re_down, ipr_re_up;
	
	TransformGroup 		tg_do, tg_re;
	Transform3D		t3_do, t3_re;
	ColorCube		c_do, c_re;
	Transform3D		rotAxis_do, rotAxis_re;
	
	double 			scale=0.4;
	boolean			animationOn_do=false;
	boolean			downO_don=true;
	boolean			animationOn_re=false;
	boolean			downOn_re=true;

	
	public HelloJava3dk()
	{
		setLayout(new BorderLayout());
		Canvas3D canvas3D = new Canvas3D(null);
		canvas3D.addKeyListener(this);
		add("Center", canvas3D);
		
		BranchGroup scene = createSceneGraph();
		
		setupAnimationData();
		createInterpolators();
		startInterpolator_do();
		startInterpolator_re();
		scene.compile();
		
				// SimpleUniverse is a Convenient Utility class
		SimpleUniverse simpleU = new SimpleUniverse(canvas3D);
		
		TransformGroup viewpoint;
		Transform3D view_translation = new Transform3D();
		// set the view point at ( 0.0, 1.0, 4.0)
		Transform3D view_rotate = new Transform3D();
		view_rotate.rotX(  -Math.atan( 3.0 / 10.0 ));
		view_translation.mul(view_rotate, view_translation);
		view_translation.setTranslation(new Vector3d(0.0, 3.0, 10.0));
		// This moves the ViewPlatform bask a bit so the
		// objects in the scene can be viewed.
		
		ViewingPlatform vplt = simpleU.getViewingPlatform();
		viewpoint = vplt.getViewPlatformTransform();
		viewpoint.setTransform(view_translation);
		
		simpleU.addBranchGraph(scene);
	}
	
	public BranchGroup createSceneGraph()
	{
		
		
		objRoot = new BranchGroup();
		
		//
		c_do = new ColorCube(scale);
		t3_do = new Transform3D();
		tg_do = new TransformGroup(t3_do);
		tg_do.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		tg_do.addChild(c_do);
		
		c_re = new ColorCube(scale);
		t3_re = new Transform3D();
		t3_re.setTranslation( new Vector3d(1.0*scale, 0.0, 0.0));
		tg_re = new TransformGroup(t3_re);
		tg_re.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		tg_re.addChild(c_re);
			
		objRoot.addChild(tg_do);
		objRoot.addChild(tg_re);
	
		bounds = new BoundingSphere();
		
		return objRoot;
	}
	
	private void setupAnimationData()
	{
		Transform3D rotAxisRotate_do = new Transform3D();
		rotAxisRotate_do.rotZ( Math.PI / 2.0d );		
		Transform3D rotAxisRotate_re = new Transform3D();
		rotAxisRotate_re.rotZ( Math.PI / 2.0d );

		rotAxis_do = new Transform3D();
		rotAxis_do.mul(rotAxisRotate_do);
		rotAxis_do.setTranslation(new Vector3d(0.0*scale, 0.0, -9.0*scale));
		rotAxis_re = new Transform3D();
		rotAxis_re.mul(rotAxisRotate_re);
		rotAxis_re.setTranslation(new Vector3d(1.0*scale, 0.0, -9.0*scale));		
		
		rotAlpha = new Alpha();
		rotAlpha.setLoopCount(1);
		rotAlpha.setIncreasingAlphaDuration(2000);
	}
	
	private void createInterpolators()
	{
		Double max_angle_d = new Double( Math.PI/12.0d );
		float max_angle = max_angle_d.floatValue();
		
		behaviorBranch_do = new BranchGroup();
		ipr_do_down = new RotationInterpolator(rotAlpha, tg_do, rotAxis_do, 0.
0f, - max_angle);
		ipr_do_up   = new RotationInterpolator(rotAlpha, tg_do, rotAxis_do, - 
max_angle, 0.0f);
		ipr_do_down.setSchedulingBounds(bounds);
		ipr_do_up.setSchedulingBounds(bounds);
		
		behaviorBranch_re = new BranchGroup();
		ipr_re_down = new RotationInterpolator(rotAlpha, tg_re, rotAxis_re, 0.
0f, - max_angle);
		ipr_re_up   = new RotationInterpolator(rotAlpha, tg_re, rotAxis_re, - 
max_angle, 0.0f);
		ipr_re_down.setSchedulingBounds(bounds);
		ipr_re_up.setSchedulingBounds(bounds);
		
		behaviorBranch_do.addChild(ipr_do_down);
		behaviorBranch_do.addChild(ipr_do_up);
		tg_do.addChild(behaviorBranch_do);
		
		behaviorBranch_re.addChild(ipr_re_down);
		behaviorBranch_re.addChild(ipr_re_up);
		tg_re.addChild(behaviorBranch_re);
		
	}
	
	public void startInterpolator_do()
	{
		if (animationOn_do)
		{
			if(downOn_do)
			{
				ipr_do_down.setEnable(true);
				ipr_do_up.setEnable(false);
						}
			else
			{
				ipr_do_down.setEnable(false);
				ipr_do_up.setEnable(true);
			}
		}
		else
		{
			ipr_do_down.setEnable(false);
			ipr_do_up.setEnable(false);
		}
	}
	
	public void startInterpolator_re()
	{
		if (animationOn_re)
		{
			if(downOn_re)
			{
				ipr_re_down.setEnable(true);
				ipr_re_up.setEnable(false);
			}
			else
			{
				ipr_re_down.setEnable(false);
				ipr_re_up.setEnable(true);
			}
		}
		else
		{
			ipr_re_down.setEnable(false);
			ipr_re_up.setEnable(false);
		}
	}

	
	public void keyTyped( KeyEvent e)
	{
	}
	
	
	public void keyPressed( KeyEvent e)
	{
		int jkeyCode = e.getKeyCode();
		String jcode=KeyEvent.getKeyText(jkeyCode);
		if (jcode.equals("A")==true)
		{
			try
			{		animationOn_do = true;
					downOn_do = true;
					startInterpolator_do();
			}
			catch (Exception excpn)
			{
				System.err.println("Kick Failure" + excpn);
			}
		}
		
		if (jcode.equals("S")==true)
		{
			try
			{		animationOn_re = true;
					downOn_re = true;
					startInterpolator_re();
			}
			catch (Exception excpn)
			{
				System.err.println("Kick Failure" + excpn);
			}
		}	
	}
	
	public void keyReleased ( KeyEvent e)
	{
		int jkeyCode = e.getKeyCode();
		String jcode=KeyEvent.getKeyText(jkeyCode);
		if (jcode.equals("A")==true)
		{
			try
			{
					animationOn = true;
					downOn = false;
					startInterpolator_do();
			}
			catch (Exception excpn)
			{
				System.err.println(excpn);
			}
	
		}	
		
		if (jcode.equals("S")==true)
		{
			try
			{		animationOn = true;
					downOn = false;
					startInterpolator_re();
			}
			catch (Exception excpn)
			{
				System.err.println(excpn);
			}
		}
		
	}
	public static void main(String[] args)
	{
		Frame frame = new MainFrame( new HelloJava3dk(), 800, 600);
	}
}