Pergunta

The program im making need to have one of the spheres in it being removed when another one of the spheres collides with it but I can't figure out how to do so. I have tried to use the Bounds class to get the bound of the different spheres and doing a if(bounds.intersects(bounds2)) statement but that didn't work. I have also tried to use the WakeupOnCollisionEntry class but I couldn't make it work properly. Any help would be greatly appreciated!

If you need to see my code to be able to help, here it is:

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);
    }
}
Foi útil?

Solução

an easier way todo this is to use the spheres them selves as var for collision and the have an xyz monitor for the distance between them

ie

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

    if(g +[shperes1 diam].equals(e + sphere2 diam)){}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top