Author Topic: why i cannot move my object?  (Read 3009 times)

Offline mike_pu

  • byte
  • *
  • Posts: 6
    • View Profile
why i cannot move my object?
« on: May 24, 2012, 05:50:40 am »
please help me,
            sphere=Loader.load3DS(res.openRawResource(R.raw.skin), 1f)[0];

            sphere.translate(0, 25, 0);//want to move
            sphere.rotateX((float) -Math.PI / 2);
            sphere.setTexture("texture");
my code is like this,when i call sphere.translate(0, 25, 0);the sphere  rotate a little,it didnot move,why?
but i can move object: thing1 = Loader.loadMD2(res.openRawResource(R.raw.thingmd), 0.4f);
                                                        thing1.translate(0, -25, 0);
            thing1.rotateY((float) Math.PI / 3);
            thing1.setTexture("thing1T");
it's so confused me. :'(

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: why i cannot move my object?
« Reply #1 on: May 24, 2012, 08:08:55 am »
There's no reason why the upper code shouldn't move the object. The only difference is the move direction and the rotation. A translation will always be applied...it has to be something different. Either you are doing something else in your code that compensates this translation or your expectations of the outcome are wrong. Try to play around with the 25 and see what happens then.

Offline mike_pu

  • byte
  • *
  • Posts: 6
    • View Profile
Re: why i cannot move my object?
« Reply #2 on: May 24, 2012, 09:02:47 am »
please check my code,thanks a lot
Code: [Select]
public class HelloWorld extends Activity {

   // Used to handle pause and resume...
   private static HelloWorld master = null;

   private GLSurfaceView mGLView;
   private MyRenderer renderer = null;
   private FrameBuffer fb = null;
   private World world = null;
   private RGBColor back = new RGBColor(0, 0, 0);

   private float touchTurn = 0;
   private float touchTurnUp = 0;

   private float xpos = -1;
   private float ypos = -1;

   private Object3D sphere = null;
   private Object3D thing1 = null;
   private Object3D sky = null;
   private int fps = 0;

   private Light sun = null;
   private float ind = 0;
   protected void onCreate(Bundle savedInstanceState) {

      Logger.log("onCreate");

      if (master != null) {
         copy(master);
      }

      super.onCreate(savedInstanceState);
      mGLView = new GLSurfaceView(getApplication());

      mGLView.setEGLConfigChooser(new GLSurfaceView.EGLConfigChooser() {
         public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) {
            // Ensure that we get a 16bit framebuffer. Otherwise, we'll fall
            // back to Pixelflinger on some device (read: Samsung I7500)
            int[] attributes = new int[] { EGL10.EGL_DEPTH_SIZE, 16, EGL10.EGL_NONE };
            EGLConfig[] configs = new EGLConfig[1];
            int[] result = new int[1];
            egl.eglChooseConfig(display, attributes, configs, 1, result);
            return configs[0];
         }
      });

      renderer = new MyRenderer();
      mGLView.setRenderer(renderer);
      setContentView(mGLView);
   }

   @Override
   protected void onPause() {
      super.onPause();
      mGLView.onPause();
   }

   @Override
   protected void onResume() {
      super.onResume();
      mGLView.onResume();
   }

   @Override
   protected void onStop() {
      super.onStop();
   }

   private void copy(Object src) {
      try {
         Logger.log("Copying data from master Activity!");
         Field[] fs = src.getClass().getDeclaredFields();
         for (Field f : fs) {
            f.setAccessible(true);
            f.set(this, f.get(src));
         }
      } catch (Exception e) {
         throw new RuntimeException(e);
      }
   }

   public boolean onTouchEvent(MotionEvent me) {

      if (me.getAction() == MotionEvent.ACTION_DOWN) {
         xpos = me.getX();
         ypos = me.getY();
         return true;
      }

      if (me.getAction() == MotionEvent.ACTION_UP) {
         xpos = -1;
         ypos = -1;
         touchTurn = 0;
         touchTurnUp = 0;
         return true;
      }

      if (me.getAction() == MotionEvent.ACTION_MOVE) {
         float xd = me.getX() - xpos;
         float yd = me.getY() - ypos;

         xpos = me.getX();
         ypos = me.getY();

         touchTurn = xd / -100f;
         touchTurnUp = yd / -100f;
         return true;
      }

      try {
         Thread.sleep(15);
      } catch (Exception e) {
         // No need for this...
      }

      return super.onTouchEvent(me);
   }

   protected boolean isFullscreenOpaque() {
      return true;
   }

   class MyRenderer implements GLSurfaceView.Renderer {

      private long time = System.currentTimeMillis();
      private float rotateV=0;
      public MyRenderer() {
      }

      public void onSurfaceChanged(GL10 gl, int w, int h) {
         if (fb != null) {
            fb.dispose();
         }
         fb = new FrameBuffer(gl, w, h);

         if (master == null) {

            world = new World();
            world.setAmbientLight(20, 20, 20);

            sun = new Light(world);
            sun.setIntensity(250, 250, 250);
           
            Resources res = getResources();
            // Create a texture out of the icon...:-)
            //Texture texture = new Texture(BitmapHelper.rescale(BitmapHelper.convert(getResources().getDrawable(R.drawable.icon)), 64, 64));
            Texture texture = new Texture(res.openRawResource(R.raw.planetex));

            TextureManager.getInstance().addTexture("texture", texture);           
            TextureManager.getInstance().addTexture("sky", new Texture(res.openRawResource(R.raw.sky)));
            TextureManager.getInstance().addTexture("thing1T", new Texture(res.openRawResource(R.raw.disco)));
         
            sphere=Loader.load3DS(res.openRawResource(R.raw.skin), 1f)[0];

            //sphere.translate(0, 25, 0);
            sphere.rotateX((float) -Math.PI / 2);
            sphere.setTexture("texture");
            sphere.strip();
            sphere.build();
            world.addObject(sphere);
                     
            Camera cam = world.getCamera();
            cam.moveCamera(Camera.CAMERA_MOVEOUT,40);
            cam.lookAt(sphere.getTransformedCenter());
            SimpleVector sv = new SimpleVector();
            sv.set(sphere.getTransformedCenter());
            //sv.y -= 100;
            sv.x += 50;
            sv.z -= 200;
            sun.setPosition(sv);
            MemoryHelper.compact();

            if (master == null) {
               Logger.log("Saving master Activity!");
               master = HelloWorld.this;
            }
         }
      }

      public void onSurfaceCreated(GL10 gl, EGLConfig config) {
      }

      public void onDrawFrame(GL10 gl) {
//         animate();
         if (touchTurn != 0) {
            sphere.rotateY(touchTurn);
            touchTurn = 0;
         }

         //sphere.rotateY(0.02f);
         /*if (touchTurnUp != 0) {
            cube.rotateX(touchTurnUp);
            touchTurnUp = 0;
         }*/

         fb.clear(back);
         world.renderScene(fb);
         world.draw(fb);
         fb.display();

         if (System.currentTimeMillis() - time >= 1000) {
            Logger.log(fps + "fps");
            fps = 0;
            time = System.currentTimeMillis();
         }
         fps++;
      }
}

as this code if i use this //sphere.translate(0, 25, 0); ,the sphere will looks very strange.

and the skin.3DS is created by myshelf ,i do this first time .if skin.3DS file's content  also can affect translate?

Offline mike_pu

  • byte
  • *
  • Posts: 6
    • View Profile
Re: why i cannot move my object?
« Reply #3 on: May 24, 2012, 10:12:45 am »
i donnot how to post my skin.3DS file and preview file.otherwise,i can post these file,canyou show the step how to upload files? :'(

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: why i cannot move my object?
« Reply #4 on: May 24, 2012, 10:42:19 am »
Just send them to info@jpct.net

Offline mike_pu

  • byte
  • *
  • Posts: 6
    • View Profile
Re: why i cannot move my object?
« Reply #5 on: May 24, 2012, 11:41:34 am »
i check this code times,and find the reason
i set cam.lookAt(sphere.getTransformedCenter());
so when sphere move,it looks strange,sorry for my poor english :)and thanks a lot.
if i want the sphere light itself like sun,when i set light in the center of the sphere ,it cannot work ,how can i achieve that.