Author Topic: How to implement the movement of the object to the point specified by touch  (Read 1416 times)

Offline sergey

  • byte
  • *
  • Posts: 3
    • View Profile
hi

I have a plane
Code: [Select]
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
Code: [Select]

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
Code: [Select]
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:

Code: [Select]
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.



Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11776
    • View Profile
    • http://www.jpct.net
Re: How to implement the movement of the object to the point specified by touch
« Reply #1 on: September 21, 2010, 10:58:24 am »
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.

Offline sergey

  • byte
  • *
  • Posts: 3
    • View Profile
Re: How to implement the movement of the object to the point specified by touch
« Reply #2 on: September 21, 2010, 12:01:36 pm »
Thank you for your reply. Your example will be useful. Waiting forward to your sources. :)