碰撞检测

package Collectiontext;

import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.applet.MainFrame;//application
import com.sun.j3d.utils.behaviors.mouse.*;
import java.applet.Applet;
import javax.media.j3d.*;
import javax.vecmath.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.Enumeration;
import java.util.Vector;

public class Collection extends Applet {
	BackgroundSound myc;

	public BranchGroup createBranchGroup() {
		BranchGroup BranchGroupRoot = new BranchGroup();
		BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
		Color3f bgColor = new Color3f(1.0f, 1.0f, 1.0f); // 白色背景
		Background bg = new Background(bgColor);
		bg.setApplicationBounds(bounds);
		BranchGroupRoot.addChild(bg);
		Color3f directionalColor = new Color3f(1.0f, 1.0f, 1.0f);
		Vector3f vec = new Vector3f(-1.0f, -1.0f, -1.0f);
		DirectionalLight directionalLight = new DirectionalLight(directionalColor, vec); // 白色平行光
		directionalLight.setInfluencingBounds(bounds);
		BranchGroupRoot.addChild(directionalLight);
		Appearance app1 = new Appearance();
		Material material1 = new Material();
		material1.setDiffuseColor(new Color3f(1.0f, .0f, .0f));
		app1.setMaterial(material1);
		TransparencyAttributes transparence = new TransparencyAttributes(1, 0.f);
		app1.setTransparencyAttributes(transparence);
		Appearance app2 = new Appearance();
		Material material2 = new Material();
		material2.setDiffuseColor(new Color3f(.0f, 1.0f, .0f));
		app2.setMaterial(material2);

		TransformGroup transformgroup = new TransformGroup();
		transformgroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
		transformgroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		BranchGroupRoot.addChild(transformgroup);
		MouseRotate mouserotate = new MouseRotate();
		mouserotate.setTransformGroup(transformgroup);
		BranchGroupRoot.addChild(mouserotate);
		mouserotate.setSchedulingBounds(bounds);
		MouseZoom mousezoom = new MouseZoom();// 鼠标左键旋转,右键平移,转轮放缩
		mousezoom.setTransformGroup(transformgroup);
		BranchGroupRoot.addChild(mousezoom);
		mousezoom.setSchedulingBounds(bounds);
		MouseTranslate mousetranslate = new MouseTranslate();
		mousetranslate.setTransformGroup(transformgroup);
		BranchGroupRoot.addChild(mousetranslate);
		mousetranslate.setSchedulingBounds(bounds);

		Transform3D t = new Transform3D();
		t.setTranslation(new Vector3f(0.f, -0.62f, 0.6f));
		// t.setTranslation(new Vector3f(0.f, .25f, 0.6f));
		TransformGroup tg1 = new TransformGroup(t);
		tg1.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
		tg1.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		tg1.addChild(new Box(0.8f, 0.003f, 0.8f, app1));
		t = new Transform3D();
		t.setTranslation(new Vector3f(-0.f, -0.4f, 0.f));
		TransformGroup tg2 = new TransformGroup(t);
		tg2.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
		tg2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		tg2.addChild(new Cylinder(0.1f, 0.02f, Cylinder.GENERATE_NORMALS, 200, 200, app1));
		t = new Transform3D();
		t.setScale(0.1f);
		t.setTranslation(new Vector3f(-0.15f, 0.7f, 0.3f));
		TransformGroup tg3 = new TransformGroup(t);
		Appearance ap = new Appearance();
		ColoringAttributes colorA = new ColoringAttributes(1.f, .0f, .0f, 1);
		ap.setColoringAttributes(colorA);
		ap.setMaterial(new Material());
		Font3D font = new Font3D(new Font("sansSerif", Font.PLAIN, 1), new FontExtrusion());
		Text3D te = new Text3D(font, "your score");
		Shape3D shape = new Shape3D(te, ap);
		tg3.addChild(shape);
		t = new Transform3D();
		t.setScale(0.1f);
		t.setTranslation(new Vector3f(0.35f, 0.7f, 0.3f));
		TransformGroup tg4 = new TransformGroup(t); // 三维字体:分数
		Text3D tex = new Text3D(font, "000000");
		shape = new Shape3D(tex, ap);
		shape.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
		tex.setCapability(Text3D.ALLOW_STRING_READ);
		tex.setCapability(Text3D.ALLOW_STRING_WRITE);
		tg4.addChild(shape);
		t = new Transform3D();
		t.setScale(0.1f);
		t.setTranslation(new Vector3f(-0.7f, 0.7f, 0.3f));
		TransformGroup tg5 = new TransformGroup(t);
		tg5.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
		tg5.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		Text3D text = new Text3D(font, "game start");
		shape = new Shape3D(text, ap);
		shape.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
		text.setCapability(Text3D.ALLOW_STRING_READ);
		text.setCapability(Text3D.ALLOW_STRING_WRITE);
		tg5.addChild(shape);

		MyBehavior behavior = new MyBehavior(tg2);// tg2:小圆盘
		behavior.setSchedulingBounds(bounds);
		transformgroup.addChild(behavior);
		t = new Transform3D();
		t.setScale(0.1f);
		TransformGroup tg6 = new TransformGroup(t);
		tg6.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		Sphere sp = new Sphere(0.1f, Sphere.GENERATE_NORMALS, 1000);
		tg6.addChild(sp);
		transformgroup.addChild(tg1);
		transformgroup.addChild(tg2);
		transformgroup.addChild(tg3);
		transformgroup.addChild(tg4);
		transformgroup.addChild(tg5);
		transformgroup.addChild(tg6);

		MediaContainer media = new MediaContainer();
		// media.setURLString(getClass().getClassLoader().getResource("./water.wav").toString());
		media.setURLString("file:./bin/onage.wav");
		myc = new BackgroundSound();
		myc.setSchedulingBounds(bounds);
		myc.setInitialGain(.7f);
		myc.setSoundData(media);
		myc.setEnable(true);
		transformgroup.addChild(myc);
		MediaContainer media1 = new MediaContainer();
		media1.setURLString("file:./bin/liu.wav");
		BackgroundSound gaover = new BackgroundSound();
		gaover.setSchedulingBounds(bounds);
		gaover.setInitialGain(.7f);
		gaover.setSoundData(media1);
		gaover.setCapability(BackgroundSound.ALLOW_ENABLE_WRITE);
		transformgroup.addChild(gaover);
		FallingObject f = new FallingObject(tg6, tg4, 800, sp, tg5, tex, text, gaover);// tg6小球坐标,tg4分数坐标,下落间隔,小球,tg5提示坐标,分数字,提示字
		f.setSchedulingBounds(bounds);
		transformgroup.addChild(f);
		Sounds sounds = new Sounds(sp, transformgroup);
		sounds.setSchedulingBounds(bounds);
		transformgroup.addChild(sounds);
		// 通过方法名返回BranchGroupRoot
		return BranchGroupRoot;
	}

	public Collection() {
		setLayout(new BorderLayout());// 设置一个BorderLayout
		GraphicsConfiguration gc = SimpleUniverse.getPreferredConfiguration();
		Canvas3D c = new Canvas3D(gc);
		add("Center", c);// 将图形环境放入BorderLayout的中心位置
		BranchGroup scene = createBranchGroup();
		SimpleUniverse u = new SimpleUniverse(c);// 生成虚拟空间
		u.getViewingPlatform().setNominalViewingTransform();// 设置观察者的位置为缺省位置
		u.addBranchGraph(scene);
		System.setProperty("j3d.audiodevice", "com.sun.j3d.audioengines.javasound.JavaSoundMixer");
		AudioDevice audioDev = u.getViewer().createAudioDevice();
		// 把场景放入虚拟空间中
	}

	public static void main(String[] args) {
		new MainFrame(new Collection(), 500, 500);

	}
}

class FallingObject extends Behavior {
	private long millseconds;
	private Transform3D t = new Transform3D();
	private TransformGroup trans4;
	private TransformGroup trans3;
	private TransformGroup textTrans;
	private Sphere sp;
	private float x, y, z, positionX, positionZ;
	private Text3D text, text1;
	private String s;
	private WakeupCondition wakeupCondition;
	private WakeupCriterion[] cond;
	private Vector3f position;
	private int score = 0;
	BackgroundSound gaover;
	Appearance app = new Appearance();
	Material mater = new Material();

	public FallingObject(TransformGroup trans4, TransformGroup trans3, long millseconds, Sphere sp,
			TransformGroup textTrans, Text3D text, Text3D text1,BackgroundSound gaover) {
		this.millseconds = millseconds;
		this.trans3 = trans3;
		this.trans4 = trans4;
		this.textTrans = textTrans;
		this.sp = sp;
		this.text = text;
		this.text1 = text1;
		this.gaover=gaover;
		app.setCapability(Appearance.ALLOW_TEXGEN_READ);
		mater.setCapability(Material.ALLOW_COMPONENT_READ);
		mater.setCapability(Material.ALLOW_COMPONENT_WRITE);
		app.setMaterial(mater);
		sp.setAppearance(app);
		t = new Transform3D();
		positionX = (float) (Math.random() * 2 - 1);
		t.setTranslation(new Vector3f(positionX, 0.8f, 0.f));
		trans4.setTransform(t);
		cond = new WakeupCriterion[2];
		cond[0] = new WakeupOnElapsedTime(millseconds);
		cond[1] = new WakeupOnCollisionEntry(sp);
		wakeupCondition = new WakeupOr(cond);
	}

	public void initialize() {
		wakeupOn(wakeupCondition);
	}

	public void processStimulus(Enumeration criteria) {
		WakeupCriterion wakeup;
		wakeup = (WakeupCriterion) criteria.nextElement();
		if (wakeup instanceof WakeupOnElapsedTime) {
			position = new Vector3f();
			t.get(position);
			x = position.x;
			y = position.y - 0.1f;
			z = position.z;
			t.setTranslation(new Vector3f(x, y, z));
			trans4.setTransform(t);
			if (y <= -0.4f) {
				text1.setString("game over");
				gaover.setEnable(true);
			} else {
				wakeupCondition = new WakeupOr(cond);

				wakeupOn(wakeupCondition);
			}
		}
		if (wakeup instanceof WakeupOnCollisionEntry) {
			score += 1;
			s = (String) Integer.toString(score);
			while (s.length() < 8) {
				s = "0" + s;
			}
			text.setString(s);
			positionX = (float) (Math.random() * 2 - 1);
			positionZ = (float) (Math.random() * 2 - 1);
			t.setTranslation(new Vector3f(positionX, 0.8f, 0.f));
			trans4.setTransform(t);
			if (millseconds > 700)
				millseconds -= 700;
			if (millseconds <= 200 && millseconds > 100)
				millseconds -= 90;
			if (millseconds <= 50 && millseconds > 10)
				millseconds -= 5;
			if (millseconds <= 10 && millseconds > 1)
				millseconds -= 1;
			if (millseconds <= 1)
				millseconds -= 1;
			mater.setDiffuseColor(new Color3f((float) Math.random(), (float) Math.random(), (float) Math.random()));
			mater.setSpecularColor(new Color3f((float) Math.random(), (float) Math.random(), (float) Math.random()));
			mater.setShininess(150);
			wakeupCondition = new WakeupOr(cond);
			wakeupOn(wakeupCondition);

		}
	}

}

class MyBehavior extends Behavior {
	private TransformGroup target;
	private Transform3D t = new Transform3D();
	private float x, y, z;

	public MyBehavior(TransformGroup target) {
		this.target = target;
		t.setTranslation(new Vector3f(0.f, -0.4f, .0f));
	}

	public void initialize() {
		wakeupOn(new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED));
	}

	public void processStimulus(Enumeration criteria) {
		WakeupOnAWTEvent ev;
		WakeupCriterion gener;
		AWTEvent[] events;
		KeyEvent event;
		Vector3f position = new Vector3f();
		gener = (WakeupCriterion) criteria.nextElement();
		if (gener instanceof WakeupOnAWTEvent) {
			ev = (WakeupOnAWTEvent) gener;
			events = ev.getAWTEvent();
			if (events[0] instanceof KeyEvent) {
				event = (KeyEvent) events[0];
				t.get(position);
				x = position.x;
				y = position.y;
				z = position.z;
				if (event.getKeyCode() == KeyEvent.VK_LEFT) {
					x -= 0.1;
					t.setTranslation(new Vector3f(x, y, z));
					target.setTransform(t);
				} else if (event.getKeyCode() == KeyEvent.VK_RIGHT) {
					x += 0.1;
					t.setTranslation(new Vector3f(x, y, z));
					target.setTransform(t);
				} else if (event.getKeyCode() == KeyEvent.VK_UP) {
					y += 0.1;
					t.setTranslation(new Vector3f(x, y, z));
					target.setTransform(t);

				} else if (event.getKeyCode() == KeyEvent.VK_DOWN) {
					y -= 0.1;
					t.setTranslation(new Vector3f(x, y, z));
					target.setTransform(t);
				} else if (event.getKeyCode() == KeyEvent.VK_PAGE_UP) {
					z -= 0.1;
					t.setTranslation(new Vector3f(x, y, z));
					target.setTransform(t);
				} else if (event.getKeyCode() == KeyEvent.VK_PAGE_DOWN) {

					z += 0.1;
					t.setTranslation(new Vector3f(x, y, z));
					target.setTransform(t);
				}
			}
		}
		wakeupOn(new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED));
	}
}

class Sounds extends Behavior {
	private WakeupCondition wakeupCondition;
	BackgroundSound myc;
	Sphere sp;
	TransformGroup transformgroup;
	private Vector3f position;

	// private WakeupCriterion[] cond;
	public Sounds(Sphere sp, TransformGroup transformgroup) {
		this.sp = sp;
		this.transformgroup = transformgroup;
		// cond = new WakeupCriterion[2];
		// cond[0] = new WakeupOnElapsedTime(millseconds);
		// cond[1] = new WakeupOnCollisionEntry(sp);
		Bounds bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
		MediaContainer media = new MediaContainer();
		// media.setURLString(getClass().getClassLoader().getResource("./water.wav").toString());
		media.setURLString("file:./bin/hike.wav");
		myc = new BackgroundSound();
		myc.setCapability(BackgroundSound.ALLOW_ENABLE_WRITE);
		myc.setSchedulingBounds(bounds);
		myc.setInitialGain(.3f);
		myc.setSoundData(media);
		myc.setContinuousEnable(true);
		myc.setPause(false);
		transformgroup.addChild(myc);
		wakeupCondition = new WakeupOnCollisionEntry(sp);
	}

	public void initialize() {
		wakeupOn(wakeupCondition);
	}

	public void processStimulus(Enumeration criteria) {
		WakeupCriterion wakeup;
		wakeup = (WakeupCriterion) criteria.nextElement();
		if (wakeup instanceof WakeupOnCollisionEntry) {
			myc.setEnable(true);
			System.out.print("sound:  ");
			System.out.print("isplaying? ");
			System.out.print(myc.isPlaying());
			System.out.print("     isplayingSilenty? ");
			System.out.println(myc.isPlayingSilently());
			// System.out.println(myc.isLive());// 没有被激活
			// System.out.println(myc.isReady());
			// myc.setContinuousEnable(true);
		}
		wakeupCondition = new WakeupOnCollisionEntry(sp);
		wakeupOn(wakeupCondition);

	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值