Hello!
I have struggled with this problem for a month now, so I really need some
help.
I try to develop a game with collision detection with Java3D.
To narrow it down I have made two test-java-files for you.
File 1) Applet where I add all my objects (2 ColorCubes)
File 2) A KeyListener to stear one of the cubes. The key listener also
checks for collisions.
All code is below, just compile and try.
The idea is that the cube that I can stear should stop immediately outside
the other cube from any
direction. I should not intersect at all. But I can't detect that other cube
at all. Now it just run through.
So if any one could try the code and give me some hints or example I would
be most
thankfull.

Best regards
Fredrik

//The Main class
import java.applet.*;
import java.awt.*;
import java.awt.Frame;import java.awt.event.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.behaviors.keyboard.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import javax.swing.*;
import java.util.*;
public class Test extends Applet
{
 BranchGroup branchGroup;
 ColorCube colorCube1 = new ColorCube(0.4);
 //The cube that you can navigate
 ColorCube colorCube2 = new ColorCube(0.4);
 TransformGroup transformGroup1;
 public void init()
 {
  setLayout(new BorderLayout());
  GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
Canvas3D canvas3D = new Canvas3D(config);
  add("Center", canvas3D);
  SimpleUniverse simpleUniverse = new SimpleUniverse(canvas3D);
  branchGroup = new BranchGroup();
  //Cube1
  transformGroup1 = new TransformGroup();
  Transform3D transform3D1 = new Transform3D();
  transform3D1.set(new Vector3f(0.0f, 0.0f, -20.0f));
  transformGroup1.setTransform(transform3D1);
  transformGroup1.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  //for setShapeBounds
  transformGroup1.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
  colorCube1.setCapability(Node.ALLOW_BOUNDS_READ);
  transformGroup1.setPickable(false);
  transformGroup1.addChild(colorCube1);
  branchGroup.addChild(transformGroup1);
  canvas3D.addKeyListener( new TestListener(transformGroup1, this) );
  //Cube2
  TransformGroup transformGroup2 = new TransformGroup();
  Transform3D transform3D2 = new Transform3D();
  transform3D2.set(new Vector3f(0.0f, 2.0f, -20.0f));
  transformGroup2.setTransform(transform3D2);
  colorCube2.setPickable(true);
  transformGroup2.addChild(colorCube2);
  branchGroup.addChild(transformGroup2);
  branchGroup.compile();
  simpleUniverse.addBranchGraph(branchGroup);
 }
 public static void main(String[] args)
 {
 Frame frame = new MainFrame(new Test(), 600, 400);
 }
}

//The KeyListener and Collision detection class

import java.awt.event.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import java.awt.Frame;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.picking.*;
import com.sun.j3d.utils.geometry.*;
public class TestListener implements KeyListener
{
 final static float DISTANCE  = 0.1f;
 final static double TURNANGLE  = 0.1;
 float x = 0.0f;
 float y = 0.0f;
 float z = -20.0f;
 private double angle = 0.0;
 TransformGroup transformGroup;
 Transform3D positionTransform3D = new Transform3D();
 Transform3D angleTransform3D = new Transform3D();
 Test test;
 PickTool pickTool;
 Point3d point3d;
 Vector3d vector3d;
 Transform3D transform3D;
 public TestListener(TransformGroup tfg, Test t)
 {
  test = t;
  transformGroup = tfg;
  pickTool = new PickTool(test.branchGroup);
  pickTool.setCapabilities(test.colorCube2, PickTool.INTERSECT_FULL);
  pickTool.setMode( PickTool.BOUNDS );
  positionTransform3D.set(new Vector3f(x, y, z));
 }

 public void keyTyped(KeyEvent e)
 {
 }

 public void keyPressed(KeyEvent e)
 {
  if( e.getKeyCode() == KeyEvent.VK_UP )
  {
   if(isMovePossible(DISTANCE))
   {
    Transform3D temp = new Transform3D();
    temp.set(new Vector3f(0, DISTANCE, 0));
    positionTransform3D.mul(temp);
    transformGroup.setTransform( positionTransform3D );

   }
  }
  else if( e.getKeyCode() == KeyEvent.VK_DOWN )
  {
   if(isMovePossible(-DISTANCE))
   {
    Transform3D temp = new Transform3D();
    temp.set(new Vector3f(0, -DISTANCE, 0));
    positionTransform3D.mul(temp);
    transformGroup.setTransform( positionTransform3D );
   }
  }
  else if( e.getKeyCode() == KeyEvent.VK_LEFT )
  {
   angle = angle + TURNANGLE;
   angleTransform3D.rotZ(TURNANGLE);
   positionTransform3D.mul(angleTransform3D);
   transformGroup.setTransform( positionTransform3D );
  }
  else if( e.getKeyCode() == KeyEvent.VK_RIGHT )
  {
   angle = angle - TURNANGLE;
   angleTransform3D.rotZ(-TURNANGLE);
   positionTransform3D.mul(angleTransform3D);
   transformGroup.setTransform( positionTransform3D );
  }
 }

 public void keyReleased(KeyEvent e)
 {
 }

 public boolean isMovePossible(float distance)
 {
  boolean retValue = true;
  PickBounds pickBounds = new PickBounds( test.colorCube1.getBounds() );
  pickTool.setShape( pickBounds, getCordinate(test.transformGroup1) );
  PickResult pickResult = pickTool.pickAny( );
  if ( pickResult != null )
  {
   System.out.println("Boink");
   retValue = false;
  }
  else
  {
   retValue = true;
  }
 return retValue;
 }

 public static void main(String[] args)
 {
  Frame frame = new MainFrame(new Test(), 600, 350);
 }

 public Point3d getCordinate(TransformGroup transformGroup)
 {
  Transform3D pointTransform3D = new Transform3D();
  transformGroup.getTransform( pointTransform3D );
  float[] cordinates = new float[16];
  pointTransform3D.get(cordinates);
  Point3d point = new Point3d(cordinates[3], cordinates[7],
  cordinates[11]);  return point;
 }

 public void printOutCordinates(TransformGroup transformGroup)
 {
  Transform3D printOutTransform3D = new Transform3D();
  transformGroup.getTransform( printOutTransform3D );
  float[] cordinates = new float[16];
  printOutTransform3D.get(cordinates);
  for(int i = 0; i < cordinates.length; i++)
  {
   System.out.println(i + ":" + cordinates[i]);
  }
  System.out.println();
 }
}

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA3D-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to