Author Topic: Need to drag an 3D object based on touch  (Read 4235 times)

Offline subhalakshmi27

  • byte
  • *
  • Posts: 21
    • View Profile
Need to drag an 3D object based on touch
« on: November 23, 2011, 07:30:41 am »
Hi,

I have a requirement where I have to drag the object based on user touch movement (basically touch & drag). Based on the topic Picking, I am able to identify if an object is touched.

My understanding to achieve drag is the object must be translated based on touch coordinates.

I understood how to get 3D to 2D coordinates and viceversa. I am unable to understand,

i) how to co relate this to translation?

ii) How the screen width and height is translated to translate factors,

when I tried

object.translate(-100,100,0); the object was not seen. I tried by trial and error and found translate x should be in range of -30 to +30 and y in range of  -45 to +45.

I did the following Math calculation on Touch listener

if (me.getAction() == MotionEvent.ACTION_MOVE) {         
           
         xpos = me.getX();
         ypos = me.getY();
         if(sphereclicked) // if the object is clicked
         {
         SimpleVector touchvector =
            Interact2D.reproject2D3DWS(world.getCamera(), fb, (int)xpos, (int)ypos).normalize();

         sphere_vector = Interact2D.projectCenter3D2D(fb,sphere); // sphere is the 3D object
         
         if(diff == null)
         {
            diff = new SimpleVector();
         }
         diff.x = ((sphere_vector.x) - xpos)/-10.5f; // this is also trial and error , I didnt understand the Math behind it :(
         diff.y = ((sphere_vector.x) - ypos)/-10.5f;
         diff.z = 0;
         
             Log.i("HelloWorldJPCT", "diff : %d %d %d" + diff.x+ " "+
                   diff.y+" "+ diff.z);
           
         }

And on Draw Frame,

if(sphereclicked == true)
         {
            if(diff!=null)
            {
                sphere.clearTranslation();
                sphere.translate(diff);
            }
         
         }

But after this code, object moves in y-axis and x-axis. But in negative y-axis, the object doesn't move till the end of the screen. The same with x-axis, it never moves till the end of X-Axis.

I am attaching my code for your reference. Please help me to understand the concept behind this. How to relate the touch cordinates to translate for an object.

Thanks in Advance,

[attachment deleted by admin]

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Need to drag an 3D object based on touch
« Reply #1 on: November 23, 2011, 08:04:21 pm »
If you move your objects in a fixed plane in world space (for example z=10 or something), you can use reproject2D3D for this. Have a look at this thread: http://www.jpct.net/forum2/index.php/topic,2413.0.html. It's for desktop jPCT, but the actual solution is based on the same idea.

Offline subhalakshmi27

  • byte
  • *
  • Posts: 21
    • View Profile
Re: Need to drag an 3D object based on touch
« Reply #2 on: November 24, 2011, 02:05:11 pm »
Hi Egon,

Thanks a lot. I am able to move the objects.  :D

Regards,
Subha

Offline gourabf9systems

  • byte
  • *
  • Posts: 6
    • View Profile
Re: Need to drag an 3D object based on touch
« Reply #3 on: March 05, 2013, 11:32:35 am »
Hi ,

Can some one post a good working example of moving android object for android. I try two attached code in this topic. But none of them give accurate result. So please provide me a good example.

Offline gourabf9systems

  • byte
  • *
  • Posts: 6
    • View Profile
Re: Need to drag an 3D object based on touch
« Reply #4 on: March 05, 2013, 12:01:48 pm »
Please post working example......

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Need to drag an 3D object based on touch
« Reply #5 on: March 05, 2013, 08:03:23 pm »
I'm sorry, but you have posted this two times, you reported the thread to the admin (me), you have send me a mail and a private message...all within one day and all with the same request for some working code. This isn't how it's going to work here...the other thread in which you've posted contains a working Activity.

Offline gourabf9systems

  • byte
  • *
  • Posts: 6
    • View Profile
Re: Need to drag an 3D object based on touch
« Reply #6 on: March 06, 2013, 11:09:51 am »
Hi EgonOlsen ,

I am sorry for this. But I want to learn something from this forum. I will take care about my mistake. Can you please give me your mailing address so I can chat with you to get some knowledge.

Thanks
Gourab Singha