Author Topic: Collision strange behaviour  (Read 1170 times)

Offline Gvrv89

  • byte
  • *
  • Posts: 4
    • View Profile
Collision strange behaviour
« on: October 03, 2011, 02:15:40 pm »
Hi all !

It's been two days I try to make collisions works. Unsuccessfully...

I load a 3ds terrain and want to stick 3ds trees on it.

Here's the project (simplified) : https://docs.google.com/viewer?a=v&pid=explorer&chrome=true&srcid=0B34S8VzVRdKMMGQxY2UwNDItNWM2OC00YzdkLTlmZmEtMjNjNDg3NmIwZWMz&hl=fr

The important code parts are :

Code: [Select]
ground = Loader.load3DS("res" + c + "ground.3ds", 70f)[0];
ground.rotateX((float) (-Math.PI / 2));
ground.translate(-100f, 10f, 2f);
ground.setSpecularLighting(true);
ground.setShadingMode(Object3D.SHADING_GOURAUD);
ground.setTexture("ground");
ground.setCollisionMode(Object3D.COLLISION_CHECK_OTHERS);
ground.setCollisionOptimization(Object3D.COLLISION_DETECTION_OPTIMIZED);
ground.build();
world.addObject(ground);

And :

Code: [Select]
public TreeEngine(World world, Object3D ground)
{
char c = File.separatorChar;

Object3D tree = Loader.load3DS("res" + c + "tree.3ds", 3f)[0];
tree.setSpecularLighting(true);
tree.setShadingMode(Object3D.SHADING_GOURAUD);

trees = new Vector<Object3D>();
for (int i = 0; i < 25; i++)
{
trees.add(tree.cloneObject());
trees.get(i).rotateX((float) (-Math.PI / 2));
trees.get(i).rotateY((float) (-Math.PI / Math.random() * 2));
//trees.get(i).scale((float) Math.random() * 2f);
trees.get(i).setCenter(ground.getTransformedCenter());
trees.get(i).translate(new SimpleVector(Math.random() * 800f - 400f, 0, Math.random() * 800f - 400f));
trees.get(i).setCollisionMode(Object3D.COLLISION_CHECK_SELF);
trees.get(i).setCollisionOptimization(Object3D.COLLISION_DETECTION_OPTIMIZED);
trees.get(i).build();
world.addObject(trees.get(i));

dir = trees.get(i).checkForCollisionEllipsoid(dir, new SimpleVector(1, 1, 1), 1);
//System.out.print(dir);
trees.get(i).translate(dir);
}
}

Did your expert eyes seen something wrong on this ? :)

Btw I set "Config.collideOffset = 40;".

Thanks in advance.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11777
    • View Profile
    • http://www.jpct.net
Re: Collision strange behaviour
« Reply #1 on: October 03, 2011, 08:09:03 pm »
You haven't written what the actual problem is...???

However, what is "dir" in your code? I don't see a definition of it in your code and even it there were one, your loop would constantly modify it. Also make sure that a tree's center is actually above the ground when you start the detection or otherwise, it will never be detected.

Offline Gvrv89

  • byte
  • *
  • Posts: 4
    • View Profile
Re: Collision strange behaviour
« Reply #2 on: October 03, 2011, 08:59:42 pm »
The problem is that the trees are floating above or under the terrain, depending the initialisation location.

dir is a SimpleVector(0, 1, 0).

I tried various values of this, nothing changes...

PS : Look at the zip attached to my previous post for more details.

Thanks.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11777
    • View Profile
    • http://www.jpct.net
Re: Collision strange behaviour
« Reply #3 on: October 03, 2011, 09:55:01 pm »
Have you tried to use a higher value for dir? It's not a direction vector, it's the translation. Maybe 0,1,0 just causes no collision. In addition, your loop changes the value of dir once a collision happens. I can't see any part that resets it to it initial value. And if tree are below the ground, then their initial position can't work. As said, you have to start above the ground to make this work. You can't trigger a collision if the tree is alread located below it and moves even lower. I suggest to increase the Config.collideOffset to something like 1000 and move all the trees 300 units up as initial position. Then let dir be 0,600,0 or something and try again.

Offline Gvrv89

  • byte
  • *
  • Posts: 4
    • View Profile
Re: Collision strange behaviour
« Reply #4 on: October 03, 2011, 10:34:52 pm »
Ok, thanks, I'll try it now.

And, yes, I put my tree above the ground to make sure the collision is computed.

I'll tell you if it works.

Nope, stille the same result...

I give you the new code :

http://pastebin.com/96nxsZVZ [HelloWorldOGL.java]
http://pastebin.com/96nxsZVZ [TreeEngine.java]

I'm giving up, if I don't succed, I put the trees by hand. :)
Nah, I'll play with variable settings tomorrow.
« Last Edit: October 03, 2011, 10:44:40 pm by Gvrv89 »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11777
    • View Profile
    • http://www.jpct.net
Re: Collision strange behaviour
« Reply #5 on: October 03, 2011, 10:50:37 pm »
Both links link to the same source code...

Offline Gvrv89

  • byte
  • *
  • Posts: 4
    • View Profile
Re: Collision strange behaviour
« Reply #6 on: October 04, 2011, 12:42:14 pm »
Sorry for my mistake :P

Here are the links :

http://pastebin.com/96nxsZVZ [HelloWorldOGL.java]
http://pastebin.com/KCxLLmdi [TreeEngine.java]

EDIT :

I am thinking of a thing : Can I simply get axises mesh's point, thus I can set the Object3D.SetCenter() stick the the mesh's point (sorry for bad english).
« Last Edit: October 04, 2011, 12:55:58 pm by Gvrv89 »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11777
    • View Profile
    • http://www.jpct.net
Re: Collision strange behaviour
« Reply #7 on: October 04, 2011, 08:41:45 pm »
I see...the issue is most likely that you expected the collision setting of tree to be cloned. That's not the case. Try to add

Code: [Select]
trees.get(i).setCollisionMode(Object3D.COLLISION_CHECK_SELF);
trees.get(i).setCollisionOptimization(Object3D.COLLISION_DETECTION_OPTIMIZED);

and see if that helps. I'll extend the docs to cover this.