Creating an object where i click

Started by bionicoz, April 01, 2011, 04:34:54 PM

Previous topic - Next topic

bionicoz

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:
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

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!

Hrolf

'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.

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).

bionicoz

#2
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!