该程序我需要让其中一个球体在另一个球体碰撞时被移除,但我无法弄清楚如何这样做。我试图使用Bounds
类来获取不同领域的界限并执行if(bounds.intersects(bounds2))
语句,但这不起作用。我也尝试使用WakeupOnCollisionEntry类,但我无法使其正常工作。任何帮助将不胜感激!
如果您需要查看我的代码以便能够提供帮助,请访问:
package Game;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import java.awt.*;
import java.awt.event.*;
import javax.media.j3d.*;
import javax.swing.JFrame;
import javax.swing.Timer;
import javax.vecmath.*;
public class JumpGame extends JFrame implements ActionListener,KeyListener{
private TransformGroup objTrans,objTrans2;
private Transform3D trans = new Transform3D();
Transform3D transform = new Transform3D();
private Sphere sphere, sphere2;
private float x, dx, height = 0.0f, sign = 1.0f, xloc = 0.0f;
private Timer timer;
public BranchGroup createSceneGraph(){
BranchGroup objRoot = new BranchGroup();
for(float x = -1.0f; x <= -1.0f; x = x + 0.1f){
objTrans = new TransformGroup();
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objRoot.addChild(objTrans);
sphere = new Sphere(0.25f);
objTrans = new TransformGroup();
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
Transform3D pos1 = new Transform3D();
pos1.setTranslation(new Vector3f(0.0f,0.0f,0.0f));
objTrans.setTransform(pos1);
objTrans.addChild(sphere);
objRoot.addChild(objTrans);
BoundingSphere bounds = new BoundingSphere
(new Point3d(0.0,0.0,0.0),100.0);
Color3f light1Color = new Color3f(1.0f,0.0f,0.2f);
Vector3f light1Direction = new Vector3f(+4.0f,-7.0f,-12.0f);
DirectionalLight light1 = new DirectionalLight
(light1Color,light1Direction);
light1.setInfluencingBounds(bounds);
objRoot.addChild(light1);
Color3f ambientColor = new Color3f(1.0f,1.0f,1.0f);
AmbientLight ambientLightNode = new AmbientLight(ambientColor);
ambientLightNode.setInfluencingBounds(bounds);
objRoot.addChild(ambientLightNode);
}
for(float x = -1.0f; x <= -1.0f; x = x + 0.1f){
objTrans2 = new TransformGroup();
objTrans2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objRoot.addChild(objTrans2);
sphere2 = new Sphere(0.25f);
objTrans2 = new TransformGroup();
objTrans2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
Transform3D pos1 = new Transform3D();
pos1.setTranslation(new Vector3f(0.0f,0.0f,0.0f));
objTrans2.setTransform(pos1);
objTrans2.addChild(sphere2);
objRoot.addChild(objTrans2);
BoundingSphere bounds = new BoundingSphere
(new Point3d(0.0,0.0,0.0),100.0);
Color3f light1Color = new Color3f(1.0f,0.0f,0.2f);
Vector3f light1Direction = new Vector3f(+4.0f,-7.0f,-12.0f);
DirectionalLight light1 = new DirectionalLight
(light1Color,light1Direction);
light1.setInfluencingBounds(bounds);
objRoot.addChild(light1);
Color3f ambientColor = new Color3f(1.0f,1.0f,1.0f);
AmbientLight ambientLightNode = new AmbientLight(ambientColor);
ambientLightNode.setInfluencingBounds(bounds);
objRoot.addChild(ambientLightNode);
}
return objRoot;
}
public JumpGame(){
setLayout(new BorderLayout());
setTitle("PAPI");
setVisible(true);
setSize(505,525);
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
Canvas3D c = new Canvas3D(config);
add("Center",c);
c.addKeyListener(this);
c.setSize(500,500);
timer = new Timer(100,this);
timer.start();
BranchGroup scene = createSceneGraph();
SimpleUniverse u = new SimpleUniverse(c);
u.getViewingPlatform().setNominalViewingTransform();
u.addBranchGraph(scene);
for(float i = 0; i < .10f; i++){
x = 1.5f;
dx = -.05f;
}
}
public void keyPressed(KeyEvent e){
if(e.getKeyChar() == 'd'){
xloc = xloc + .1f;
}
if(e.getKeyChar() == 'a'){
xloc = xloc - .1f;
}
}
public void keyReleased(KeyEvent e){
}
public void keyTyped(KeyEvent e){
}
public void actionPerformed(ActionEvent e){
height += .1f * sign;
if(Math.abs(height * 2) >= 1)
sign = -1.0f * sign;
if(height < -.4f){
trans.setScale(new Vector3d(1.0,.8,1.0));
}else{
trans.setScale(new Vector3d(1.0,1.0,1.0));
}
trans.setTranslation(new Vector3f(xloc,height - .15f,0.0f));
objTrans.setTransform(trans);
transform.setTranslation(new Vector3f(x += dx,-.7f,0.0f));
objTrans2.setTransform(transform);
}
public static void main(String[] args){
System.out.println("Program Started");
JumpGame jg = new JumpGame();
jg.addKeyListener(jg);
}
}
答案 0 :(得分:0)
更简单的方法就是将它们自己作为var用于碰撞,并使用xyz监视器来确定它们之间的距离
即
spere1.get.position(x)
int g
g.setvalue(sphere1.getposition(x))
ex...
if(g +[shperes1 diam].equals(e + sphere2 diam)){}