Help with CollisionDetection !!!

Started by JavaMan, March 11, 2008, 04:06:12 AM

Previous topic - Next topic

JavaMan

Hi, I am learning about collision detection. I have looked around for help with it, and I thought this code would make box1 move until it runs into box2 then stop, but the box just keeps moving with no collision. Why?
Quote
import java.awt.*;
import javax.swing.*;
import com.threed.jpct.*;
import org.lwjgl.opengl.*;
class CanTest {
   public static void main(String args[])throws Exception{
   Config.farPlane=500000;
   final JPanel top = new JPanel();

   final World w = new World();
   Object3D o = new Object3D(200);
   Object3D box1 = Primitives.getCube(60);
   Object3D box2 = Primitives.getCube(60);
   box1.build();
   box2.build();
   box1.translate(900,0,0);
   box1.translateMesh();
   box1.build();
   box1.addCollisionListener(new Lis());
   box2.addCollisionListener(new Lis());
   box1.setTransparency(0);
   box2.setTransparency(0);
   box1.setCollisionMode(Object3D.COLLISION_CHECK_OTHERS|Object3D.COLLISION_CHECK_SELF);
   box2.setCollisionMode(Object3D.COLLISION_CHECK_OTHERS|Object3D.COLLISION_CHECK_SELF);

   w.addObject(box1);
   w.addObject(box2);

   box1.setAdditionalColor(Color.red);
   box2.setAdditionalColor(Color.blue);

   w.setAmbientLight(200,200,200);
   w.newCamera();
   Camera cam = w.getCamera();
   cam.moveCamera(Camera.CAMERA_MOVEOUT,5000);
   cam.moveCamera(Camera.CAMERA_MOVERIGHT,800);

   final FrameBuffer b = new FrameBuffer(800,600,FrameBuffer.SAMPLINGMODE_NORMAL);
   b.enableRenderer(IRenderer.RENDERER_OPENGL,FrameBuffer.SAMPLINGMODE_NORMAL );
   b.disableRenderer(IRenderer.RENDERER_SOFTWARE);

   box1.enableCollisionListeners();
   box2.enableCollisionListeners();

   int i = 0;
   while(i<5000){
         b.clear();
         w.renderScene(b);
         w.draw(b);
         b.update();
         b.displayGLOnly();
   Thread.sleep(100);
   SimpleVector v = box1.checkForCollisionSpherical(new SimpleVector(-10f,0,0),10f);
   box1.translate(v.x,v.y,v.z);
   System.out.println("" + v.x + " " + v.y + " " + v.z);
   }


   }
}

class Lis implements CollisionListener {
   public void collision(CollisionEvent e){
      System.out.println("OUCH!!!!!!!!!!!!");
   }








   public boolean requiresPolygonIDs(){
      return false;
   }
}



Thanks for any help! I really am stumped. ???

EgonOlsen

Your choosen dimensions for the boxes are a bit strange...60 is really huge, which is why you have to zoom out so far to see them. This makes things little less intuitive than they have to be IMHO. Anyway, because of the big boxes, you have to adjust Config.collideOffset to something like 200 to actually detect the collision.

JavaMan

Hey, thanks, that got it to work. I made the boxes smaller. That config class sure is important.!!

EgonOlsen

Quote from: JavaMan on March 11, 2008, 01:55:36 PM
That config class sure is important.!!
It sometimes is as important as it is difficult to know what to look for in it...i shouldn't have put it all into one big Config namespace, but i guess it's too late now... :'(
So if you are unsure of something, take a look at Config to see if it's covered there somewhere.

JavaMan

#4
Do you want me to make some sort of thread sticky on the support section that tells people to look in the Config class for the solution before asking a question? I was having another problem, and I had it solved by changing the setting of farPlane, but I didn't think of looking there for this problem

EgonOlsen

I think that once you are used to the fact that Config contains a lot of stuff from all over the engine, it's fine. I don't see the need for a sticky thread. Take yourself some time and browse the docs for Config so that you've read everything once. That should be sufficient to remember that "there is something in Config" that may help in case of a problem.

JavaMan