当另一个球体与它发生碰撞时,我正在制作的程序需要将其中的一个球体移除,但我不知道该怎么做。我试图使用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);
}
}