Need to drag an 3D object based on touch

Started by subhalakshmi27, November 23, 2011, 07:30:41 AM

Previous topic - Next topic

subhalakshmi27

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]

EgonOlsen

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.

subhalakshmi27

Hi Egon,

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

Regards,
Subha

gourabf9systems

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.

gourabf9systems


EgonOlsen

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.

gourabf9systems

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