Author Topic: Move object  (Read 3220 times)

Offline 4ebureG

  • byte
  • *
  • Posts: 2
    • View Profile
Move object
« on: October 18, 2013, 07:05:43 pm »
Hello all!
I am a novice in jpct! Can you help me?
I want my object to move but it looks horrible:

 @Override
   ......
       public boolean onTouchEvent(MotionEvent me) {
       if (me.getAction() == MotionEvent.ACTION_DOWN) {     
          return true;
      }
        if (me.getAction() == MotionEvent.ACTION_UP) {
              x=me.getX();
              y=me.getY();         
       return true;
       }
         if (me.getAction() == MotionEvent.ACTION_MOVE) {           
       return true;
   }
                          try {
               Thread.sleep(15);
           } catch (Exception e) {
           }
           return super.onTouchEvent(me);
       }
            ....
              public void onDrawFrame(GL10 gl) {
      cub = new SimpleVector(Interact2D.projectCenter3D2D(fb, cube));
      if ((x!=0) && (Math.abs(cub.x-x)>5) && (Math.abs(cub.y-y)>5))
      {
      trans = new SimpleVector(Interact2D.reproject2D3DWS(world.getCamera(), fb, (int)x,(int)y));
      trans.z= cube.getCenter().z;
      cub.translate(trans);
      }
              .....
      }
Modify message

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Move object
« Reply #1 on: October 18, 2013, 08:40:12 pm »
So this is supposed to detect if the object is close to the touch position and if it isn't, it should move it there? If so, it think that it has two flaws:

  • It doesn't seem to take into account that translations are cumulative
  • It uses the wrong depth (i.e. none aka 1) for the back-projection into 3d

Try something like

Code: [Select]
trans = new SimpleVector(Interact2D.reproject2D3DWS(world.getCamera(), fb, (int)x,(int)y, cube.getCenter().z));
cube.clearTranslation();
cube.translate(trans);

Offline 4ebureG

  • byte
  • *
  • Posts: 2
    • View Profile
Re: Move object
« Reply #2 on: October 21, 2013, 06:44:35 am »
Thank you for answer!
If I do how you wrote above, my object is moving on a pixel because of cube.clearTranslation();
Now I do so:

            ..........
              if (me.getAction() == MotionEvent.ACTION_UP) {
              x=me.getX();
              y=me.getY();
          
              trans = new SimpleVector(Interact2D.reproject2D3DWS(world.getCamera(), fb, (int)x,(int) y));
               pos = world.getCamera().getPosition();
               float Z_PLANE=0;
            float a = (Z_PLANE - pos.z) / trans.z;
               float xn = pos.x + a * trans.x;
               float yn = pos.y + a * trans.y;
               moveVector = new SimpleVector(xn,yn,Z_PLANE);

               return true;

           }


                   ..........             
                   if (moveVector!=null){
              сube.translate(moveVector.normalize());
                }

But my object is moving properly. Help me please!

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Move object
« Reply #3 on: October 21, 2013, 08:31:48 pm »
You code doesn't do what i suggested. You are still ignoring the depth in the back-projection and that won't work.