有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java3d中的几何体碰撞检测?

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

共 (1) 个答案

  1. # 1 楼答案

    一种更简单的方法是使用球体本身作为碰撞的变量,并使用xyz监视器来监视它们之间的距离

    ie

       spere1.get.position(x)
       int g
       g.setvalue(sphere1.getposition(x))
        ex...
    
        if(g +[shperes1 diam].equals(e + sphere2 diam)){}