Author Topic: Help with CollisionDetection !!!  (Read 3591 times)

Offline JavaMan

  • long
  • ***
  • Posts: 231
    • View Profile
Help with CollisionDetection !!!
« on: March 11, 2008, 04:06:12 am »
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. ???
« Last Edit: March 11, 2008, 04:07:57 am by JavaMan »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11777
    • View Profile
    • http://www.jpct.net
Re: Help with CollisionDetection !!!
« Reply #1 on: March 11, 2008, 08:54:02 am »
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.

Offline JavaMan

  • long
  • ***
  • Posts: 231
    • View Profile
Re: Help with CollisionDetection !!!
« Reply #2 on: March 11, 2008, 01:55:36 pm »
Hey, thanks, that got it to work. I made the boxes smaller. That config class sure is important.!!

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11777
    • View Profile
    • http://www.jpct.net
Re: Help with CollisionDetection !!!
« Reply #3 on: March 11, 2008, 03:23:25 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.

Offline JavaMan

  • long
  • ***
  • Posts: 231
    • View Profile
Re: Help with CollisionDetection !!!
« Reply #4 on: March 11, 2008, 11:15:48 pm »
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
« Last Edit: March 11, 2008, 11:20:42 pm by JavaMan »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11777
    • View Profile
    • http://www.jpct.net
Re: Help with CollisionDetection !!!
« Reply #5 on: March 11, 2008, 11:30:54 pm »
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.

Offline JavaMan

  • long
  • ***
  • Posts: 231
    • View Profile
Re: Help with CollisionDetection !!!
« Reply #6 on: March 12, 2008, 12:20:11 am »
Ok, that's fine with me  ;D