Move object

Started by 4ebureG, October 18, 2013, 07:05:43 PM

Previous topic - Next topic

4ebureG

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

EgonOlsen

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


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

4ebureG

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!

EgonOlsen

You code doesn't do what i suggested. You are still ignoring the depth in the back-projection and that won't work.