How to implement the movement of the object to the point specified by touch

Started by sergey, September 20, 2010, 10:51:16 PM

Previous topic - Next topic

sergey

hi

I have a plane

for (int iz = 1; iz < 11; iz++) {
                int z = iz * 12;

                for (int i = 0; i < 10; i++) {
                    int x = i * 12;

                    addHPolygon(box2, x, x + 12, 0, z, z - 12, 1,
                            tm.getTextureID("floor2"));

                }
            }
            box2.setCollisionMode(Object3D.COLLISION_CHECK_OTHERS);


addHPolygon (create 2 triangles)

and  a some box on that plane

box = Loader.load3DS(res.openRawResource(R.raw.crate), 0.095f)[0];
box.setTexture("box1");
box.translate(4, -10, 0);
box.setCollisionMode(Object3D.COLLISION_CHECK_OTHERS);


Camera

Camera cam = world.getCamera();
cam.setPosition(0, -40, 0);
cam.lookAt(new SimpleVector(0, 0, 0));


I want that  "box" is moved in the plane of "box2".  Move to some point, specified by touch.
I fount thread http://www.jpct.net/forum2/index.php/topic,791.0.html where, as i think, what I need.

i try use that,

on touch event:

SimpleVector ray=Interact2D.reproject2D3DWS(world.getCamera(), fb, (int)xpos, (int)ypos);
             
             distance = world.calcMinDistance(world.getCamera().getPosition(), ray, 10000F);
             if (distance!=Object3D.COLLISION_NONE) {
                 SimpleVector offset=new SimpleVector(ray);
              ray.scalarMul(distance);
             
              ray=ray.calcSub(offset);
             
              world.getObjectByName("box").translate(world.getCamera().getPosition());
             
              world.getObjectByName("box").translate(ray);
             }


But it does not work for me. Maybe you could help me by giving advice or an example of such a problem.

thank you.



EgonOlsen

That's not exactly the code from the thread you linked to. I suggest to take that code (the little snippet that i enhanced with comments should be enough)  instead and see if it works. If your plane is aligned to the xz-plane, you can calculate the intersection point much cheaper...i've done this in a prototype game for Android, but i can't access the sources right now. I can post it next week if needed. However, the approach from the mentioned thread should work fine too.

sergey

Thank you for your reply. Your example will be useful. Waiting forward to your sources. :)