2013-04-09 67 views
0

當一個球體與另一個球體發生碰撞時,需要將其中的一個球體去除,但我無法弄清楚如何去除。我曾嘗試使用Bounds類來獲得不同領域的界限並做出if(bounds.intersects(bounds2))聲明,但這並不奏效。我也嘗試使用WakeupOnCollisionEntry類,但是我無法使其正常工作。任何幫助將不勝感激!java-3d中的碰撞檢測?

如果你需要看我的代碼能夠幫助,那就是:

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

TODO這更簡單的方法是使用球他們的自我爲VAR碰撞和有一個XYZ顯示器它們之間的距離

spere1.get.position(x) 
    int g 
    g.setvalue(sphere1.getposition(x)) 
    ex... 

    if(g +[shperes1 diam].equals(e + sphere2 diam)){} 
+0

的[]是不是真正的代碼,它讓你知道的值是 – fftk4323 2013-04-10 02:27:43

+0

你能解釋litlle嗎? – user2247859 2013-04-10 09:29:35

+1

使用球體的尺寸和角度,以X軸,Y軸和Z軸上的中心點位置爲基礎,將球體的直徑與該尺寸相加,然後告訴它是否有任何這些點等於其他球體點比這樣做 – fftk4323 2013-04-10 11:39:59