Author Topic: Creating an object where i click  (Read 1269 times)

Offline bionicoz

  • byte
  • *
  • Posts: 10
    • View Profile
Creating an object where i click
« on: April 01, 2011, 04:34:54 pm »
Hi everyone, i'm using jpct on android since 3 days, and now i'm stuck. I wanna add some object in my world, positioning them where i click. It's probably because i really didn't understand how coords works.
I grab the 3d position with this:
Code: [Select]
SimpleVector dir=Interact2D.reproject2D3D(cam, fb, (int)xpos, (int)ypos, 10).normalize();
float distance = world.calcMinDistance(cam.getPosition(), dir, 10000);

And I clone and position the new object with this code
Code: [Select]
dir.scalarMul(distance);
objs[n] = obj.cloneObject();
objs[n].translate(dir);
world.addObject(objs[n]);

Sometimes objects appears, but at random position (but at the right height, since i click on a plane).
What am I doing wrong?

Thanks for help,
Bio

P.S. Nice work btw with the engine!
« Last Edit: April 01, 2011, 05:30:34 pm by bionicoz »

Offline Hrolf

  • int
  • **
  • Posts: 84
    • View Profile
Re: Creating an object where i click
« Reply #1 on: April 01, 2011, 06:18:21 pm »
'dir' is the vector from the camera to the place you clicked, so to get world co-ords you need to add the camera position.
Code: [Select]
dir.scalarMul(distance);
dir.add(cam.getPosition()); // <- add this line!
objs[n] = obj.cloneObject();
objs[n].translate(dir);
world.addObject(objs[n]);
Also if you're going to use Object3D.translate() instead of Object3D.setOrigin() then make sure that the object you're cloning is at (0,0,0).

Offline bionicoz

  • byte
  • *
  • Posts: 10
    • View Profile
Re: Creating an object where i click
« Reply #2 on: April 04, 2011, 09:21:06 am »
Ty much for your answer, Hrolf.
.getCenter() of my cloned object it's at (0,0,0), so i thinks its fine.
Btw, I switched to setOrigin() instead of .translate().
However, now all cloned objects lay on the plane, but still in wrong position.
If I rotate the camera in a certain direction (let's say east) i get consistent results, but all objects are more distant than the point I clicked.  
I really don't have a clue. :(  

EDIT:
resolved with this topic: http://www.jpct.net/forum2/index.php/topic,1932.0.html
ty all!
« Last Edit: April 04, 2011, 10:02:50 am by bionicoz »