2013-04-10 3 views
2

나는 Java 3-d와 Im을 사용하여 적을 공격하여 글꼴을 얻는 게임을 만들기 시작했습니다. 충돌 감지를 제외한 모든 작업을 수행하는 방법을 알고 있습니다. 적의 공이 플레이어 공과 접촉 할 때 플레이어 공을 제거해야합니다. 이 if(sphere.getBounds().intersects(sphere2.getBounds())처럼 보이는 id 문을 실행하려고 시도했지만 어떤 이유로 게임이 시작될 때 동시에 실행합니다. 나는 아마도 WakeupOnCollisionEntry 클래스를 사용할 필요가 있음을 알고 있지만 플레이어 공을 제거하는 데 제대로 사용하는 방법을 모르겠습니다. 나는 objTrans.removeChild(sphere)과 함께 WakeupOnCollisionEntry 클래스를 사용하면 효과가있을 것이라고 생각 하겠지만, 다른 클래스와 함께 사용하는 방법을 모릅니다. 당신이 내 코드가 도움이 할 수있는 볼 필요가있는 경우Java-3d collision detection

, 여기있다 :

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 PAPITest extends JFrame implements ActionListener,KeyListener{ 
    private TransformGroup objTrans,objTrans2; 
    private Transform3D trans = new Transform3D(); 
    private BranchGroup objRoot; 
    private Sphere sphere, sphere2; 
    private float x, dx, height = 0.0f, sign = 1.0f, xloc = 0.0f; 
    private Timer timer; 

    public BranchGroup createSceneGraph(){ 
     objRoot = new BranchGroup(); 
     objTrans = new TransformGroup(); 
     objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); 
     objRoot.addChild(objTrans); 
     sphere = new Sphere(0.25f); 
     sphere.setCollidable(true); 
     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(0.2f,1.0f,1.0f); 
     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 BranchGroup createSceneGraph2(){ 
     objRoot = new BranchGroup(); 
     objTrans2 = new TransformGroup(); 
     objTrans2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); 
     objRoot.addChild(objTrans2); 
     sphere2 = new Sphere(0.25f); 
     sphere2.setCollidable(true); 
     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); 
     return objRoot; 
    } 

    public PAPITest(){ 
     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(); 
     BranchGroup scene2 = createSceneGraph2(); 
     SimpleUniverse u = new SimpleUniverse(c); 
     u.getViewingPlatform().setNominalViewingTransform(); 
     u.addBranchGraph(scene); 
     u.addBranchGraph(scene2); 
     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); 
     if(height < -.4f){ 
      trans.setScale(new Vector3d(1.0,1.0,1.0)); 
     } 
     trans.setTranslation(new Vector3f(x += dx,-.7f,0.0f)); 
     objTrans2.setTransform(trans); 
    } 

    public static void main(String[] args){ 
     System.out.println("Program Started"); 
     PAPITest pt = new PAPITest(); 
     pt.addKeyListener(pt); 
    } 
} 

편집 : 내가 더 정확하게 만들 수있는 경우 일 것이라고 생각 지금 추가 한 뭔가. 나는이 두 가지 방법을 추가 :

public void getSpherePosition(){ 
     positionX = (int) xloc; 
     positionY = (int) height; 
     position = positionX + positionY; 
    } 

    public void getSphere2Position(){ 
     positionX2 = (int) x; 
     positionY2 = (int) -.7f; 
     position2 = positionX2 + positionY2; 
    } 

을 나는 actionPerformed 메소드이 추가 다음 그것은 플레이어의 볼이 적을 볼 근처에있을 때 텍스트 만 표시 근무 때문에 좀 작동

 getSpherePosition(); 
     getSphere2Position(); 
     if(position == position2){ 
      System.out.println("It Worked"); 
     } 

, 프로그램을 처음 시작할 때 표시됩니다.

더 좋은 방법이 있습니까? 그렇다면 어떻게 그렇게 할 수 있습니까? 그리고 어떻게하면 내 길을 더 정확하게 만들 수 있으며 프로그램을 시작할 때 텍스트를 직접 보여주지 않을 수 있습니까?

나는 3 개의 변수를 함께 사용했기 때문에 정확하지 않다는 것을 알고 있습니다. 따라서 플레이와 적이 서로 접촉하지 않아도 위치는 position2와 같을 수 있습니다.하지만 어떻게해야합니까? 다른 방법.

+0

http://stackoverflow.com/questions/3232318/sphere-sphere-collision-detection-reaction이 답을 줄 수 있습니다. 단순한 형상입니다. 중심점 사이의 거리를 계산하고 합계 된 반경보다 큰지 확인합니다. – Dariusz

답변

0

다시 연락을 취할 때까지 죄송합니다. Java3d는 모든 지식 중 가장 어두운 곳에 묻혀있는 것처럼 보이는 메소드를 내장하고 있습니다. 나는 충돌 탐지 방법이 조잡하지만 작동한다는 것을 발견했다.

다음은 충돌을 불러 오기 위해 호출 할 수있는 메서드에 대한 간단한 스 니킵입니다.

import java.util.Enumeration; 
import javax.media.j3d.Behavior; 
import javax.media.j3d.Bounds; 
import javax.media.j3d.Node; 
import javax.media.j3d.Shape3D; 
import javax.media.j3d.WakeupCriterion; 
import javax.media.j3d.WakeupOnCollisionEntry; 
import javax.media.j3d.WakeupOnCollisionExit; 
import javax.media.j3d.WakeupOnCollisionMovement; 
import javax.media.j3d.WakeupOr; 



/** 
* 
* @author sawyera.2016 
*/ 
public class Coll extends Behavior { 
/** The separate criteria used to wake up this beahvior. */ 
protected WakeupCriterion[] theCriteria; 

/** The OR of the separate criteria. */ 
protected WakeupOr oredCriteria; 

/** The shape that is watched for collision. */ 
    protected Shape3D collidingShape; 

    /** 
    * @param theShape 
    *   Shape3D that is to be watched for collisions. 
    * @param theBounds 
    *   Bounds that define the active region for this behaviour 
    */ 
    public Coll(Shape3D theShape, Bounds theBounds) { 
    collidingShape = theShape; 
    setSchedulingBounds(theBounds); 
    } 


    /** 
    * This creates an entry, exit and movement collision criteria. These are 
    * then OR'ed together, and the wake up condition set to the result. 
    */ 
    public void initialize() { 
    theCriteria = new WakeupCriterion[3]; 
    theCriteria[0] = new WakeupOnCollisionEntry(collidingShape); 
    theCriteria[1] = new WakeupOnCollisionExit(collidingShape); 
    theCriteria[2] = new WakeupOnCollisionMovement(collidingShape); 
    oredCriteria = new WakeupOr(theCriteria); 
    wakeupOn(oredCriteria); 
    } 

    /** 
    * Where the work is done in this class. A message is printed out using the 
    * userData of the object collided with. The wake up condition is then set 
    * to the OR'ed criterion again. 
    */ 
    public void processStimulus(Enumeration criteria) { 
    WakeupCriterion theCriterion = (WakeupCriterion) criteria.nextElement(); 
    if (theCriterion instanceof WakeupOnCollisionEntry) { 
     Node theLeaf = ((WakeupOnCollisionEntry) theCriterion) 
      .getTriggeringPath().getObject(); 
     System.out.println("Collided with " + theLeaf.getUserData()); 
    } else if (theCriterion instanceof WakeupOnCollisionExit) { 
     Node theLeaf = ((WakeupOnCollisionExit) theCriterion) 
      .getTriggeringPath().getObject(); 
     System.out.println("Stopped colliding with " 
     + theLeaf.getUserData()); 
    } else { 
     Node theLeaf = ((WakeupOnCollisionMovement) theCriterion) 
     .getTriggeringPath().getObject(); 
    System.out.println("Moved whilst colliding with " 
     + theLeaf.getUserData()); 
    } 
    wakeupOn(oredCriteria); 
    } 
} 

나는 내가 웹에서 그것에 대해 몇 가지 정보를 얻었다,이에 대한 전체 신용을받을 수 없어하지만 난 java3d의 1.2 버전에서이 버전을했다.

+0

1) 동일한 사용자입니다. 2) 중복 된 내용을 신고하지 않습니다. – Dariusz