joseantunez / min3d

Automatically exported from code.google.com/p/min3d
1 stars 0 forks source link

How to rotate around an Object by using touchscreen? #70

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
hi,
how can i rotate the camera around an Object by using the touchscreen? I tried 
to do this with the code below, but the camera rotates weird when the object 
its moving. 

initScene:
        scene.camera().position.z = 90;
        scene.camera().target = raketeOBJ.position();

onTouchEvent:
    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 / -200f;
            touchTurnUp = yd / -200f;
            return true;
        }

        try {
            Thread.sleep(15);
        } catch (Exception e) {

        }

        return super.onTouchEvent(me);
    }

UpdateScene:
        if (touchTurn != 0) {
            scene.camera().position.rotateY(touchTurn);
            touchTurn = 0;
        }

        if (touchTurnUp != 0) {
            scene.camera().position.rotateX(touchTurnUp);
            touchTurnUp = 0;
        }

Original issue reported on code.google.com by der.appb...@googlemail.com on 3 Jan 2012 at 2:41

GoogleCodeExporter commented 9 years ago
I suggest you start with 1 rotation and work your way up.
Don't forget that the pivot points are different (ObjectX = event.getY() and 
ObjectY = event.getX())
Here is my code that I used to rotate the object with your finger.

// Touch event to control the object
// Stop the rotation rended during manual control.
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            rotating = false;
            break;
        case MotionEvent.ACTION_UP:
            rotating = true;
            prevSwipeY = swipeY = 0;
            break;
        case MotionEvent.ACTION_MOVE:
            float directionY = (prevSwipeY - event.getX()) * -1;
            swipeY = directionY * ROTATION_SPEED;
            break;
        }

        // Camera pivot point is different 
        prevSwipeY = event.getX();
        return super.onTouchEvent(event);
    }

    @Override
    public void updateScene() {
        if (monster != null) {  // If an object is loaded
            if (swipeY != 0)  // and we did swiped in the correct direction
                monster.rotation().y += swipeY;

            swipeY = 0; // Reset the pointer

            if (rotating) { // continue the roation
                monster.rotation().y += ROTATION_SPEED;
                monster.rotation().x = 0;
            }
        }
    }

Original comment by jens.deblieck on 28 Jan 2012 at 9:17

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
hi

code that u provided is not working.monster is not moving at all.I am sending 
you the modified java file.can you tell me
where i need to change to work it fine.

Original comment by goyal.ar...@gmail.com on 5 Dec 2012 at 6:59

Attachments:

GoogleCodeExporter commented 9 years ago
Hi~ did you solved the problem now?! can you help me? thanks!!

Original comment by chenlire...@gmail.com on 21 Apr 2013 at 7:20

GoogleCodeExporter commented 9 years ago
Please tell me also how can rotates objects in all direction ...........my code 
is but not working well.......

package ezeelearning.tablet.min3d.sampleProject1;

import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import ezeelearning.tablet.R;
import ezeelearning.tablet.min3d.core.Object3dContainer;
import ezeelearning.tablet.min3d.core.RendererActivity;
import ezeelearning.tablet.min3d.parser.IParser;
import ezeelearning.tablet.min3d.parser.Parser;
import ezeelearning.tablet.min3d.vos.Light;
public class ExampleLoadObjFlower extends RendererActivity{
    private Object3dContainer objModel;
    Button b;
    protected GLSurfaceView _glSurfaceView;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        _glSurfaceView = new GLSurfaceView(this);
        }

       protected void onPause()
        {
            super.onPause();
            _glSurfaceView.onPause();
        }
        protected void onResume()
        {
            super.onResume();
            // The following call resumes a paused rendering thread.
            // If you de-allocated graphic objects for onPause()
            // this is a good place to re-allocate them.
            _glSurfaceView.onResume();
        }
    public boolean onTouchEvent(MotionEvent event) {

        int action = event.getAction() & MotionEvent.ACTION_MASK;

          switch(action) {
           case MotionEvent.ACTION_DOWN : {

               objModel.rotation().x=objModel.rotation().x+10;
            Log.d("CV","ACTION_DOWN");

            break;
           }
         case MotionEvent.ACTION_UP : {

           objModel.rotation().x=objModel.rotation().x-10;
        Log.d("CV","ACTION_DOWN");

        break;
           }

          }

          return true;
    }
    public void initScene() {

        scene.lights().add(new Light());
        IParser parser = Parser.createParser(Parser.Type.MAX_3DS, getResources(),
                "ezeelearning.tablet:drawable/monster_high", true);

        parser.parse(); 
        objModel = parser.getParsedObject();
        objModel.scale().x = objModel.scale().y = objModel.scale().z = .7f;
        scene.addChild(objModel);

    }

    public void updateScene() {
        //objModel.rotation().x++;
        //objModel.rotation().z++;
    }

}

Original comment by bsanga...@gmail.com on 27 Apr 2013 at 11:25

GoogleCodeExporter commented 9 years ago
Hello dude I solve that question chek:

@Override
    public boolean onTouchEvent(MotionEvent event){ 

        if (event.getAction() == MotionEvent.ACTION_DOWN)
        {

            touchedX = event.getX();
            touchedY = event.getY();

        } else if (event.getAction() == MotionEvent.ACTION_MOVE)
        {
            objModel.rotation().z -= (touchedX - event.getX())/2f;
            objModel.rotation().x -= (touchedY - event.getY())/2f;

            try {

                float ok=spacing(event);                
                planePosition += (event.getY() - touchedY) * 0.01f;

                if(planePosition <= maxScal){
                objModel.scale().x = objModel.scale().y = objModel.scale().z = planePosition;
                }else{
                    objModel.scale().x = objModel.scale().y = objModel.scale().z = 1f;
                }

                if(planePosition >= minScal){
                objModel.scale().x = objModel.scale().y = objModel.scale().z = planePosition;
                }else{                  
                    System.out.println("Se ha salido del rango");
                    objModel.scale().x = objModel.scale().y = objModel.scale().z = .1f;
                }               
            } catch (Exception e) {

            }

            touchedX = event.getX();
            touchedY = event.getY();

        }           
        return true;    
    }

    private float spacing(MotionEvent event) {
           float x = event.getX(0) - event.getX(1);
           float y = event.getY(0) - event.getY(1);
           return FloatMath.sqrt(x * x + y * y);
        }

In other class where you call RenderActivity.java you need to put this code and 
it works for me very well.

Original comment by Armando....@gmail.com on 1 May 2013 at 6:13

GoogleCodeExporter commented 9 years ago
Hello dude  I solved the problem of zooming in on the basis of u
-------------------------------------------------------
package com.quan.airdisplay.activity.mini3d;

import min3d.core.Object3dContainer;
import min3d.core.RendererActivity;
import min3d.parser.IParser;
import min3d.parser.Parser;
import min3d.vos.Light;
import android.util.FloatMath;
import android.view.MotionEvent;

public class Load3DSFile extends RendererActivity {
    private Object3dContainer objModel;
    boolean rotating;
    float swipeY;
    float prevSwipeY;

    @Override
    public void initScene() {

        scene.lights().add(new Light());

        IParser parser = Parser.createParser(Parser.Type.MAX_3DS,
                getResources(), "com.quan.airdisplay:raw/monster_high", false);
        parser.parse();

        objModel = parser.getParsedObject();
        objModel.scale().x = objModel.scale().y = objModel.scale().z = .5f;
        // objModel.position().y = -10;
        objModel.position().y = -20;
        scene.addChild(objModel);

        scene.camera().target = objModel.position();
    }

    //单触的时候记录按下时的X、Y坐标
    float touchedX, touchedY;
    //lastplanePosition记录双按下的两点间的距离,planePosition记录滑动时两点间的距离
    float lastplanePosition,planePosition;
    //最大、最小缩放比
    float maxScal = 1f,minScal = .1f;
    //缩放的速度比
    float scalSpeed = .5f;
    //当前缩放比
    float totalRatio = .9f;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getActionMasked()) {
        case MotionEvent.ACTION_DOWN:
            touchedX = event.getX();
            touchedY = event.getY();
            break;
        case MotionEvent.ACTION_POINTER_DOWN:
            if (event.getPointerCount() == 2) {
                // 当有两个手指按在屏幕上时,计算两指之间的距离
                lastplanePosition = spacing(event);
            }
            break;

        case MotionEvent.ACTION_MOVE:
            switch (event.getPointerCount()) {
            //单触 用来 旋转
            case 1:
                objModel.rotation().z += (touchedX - event.getX()) / 2f;
                objModel.rotation().x -= (touchedY - event.getY()) / 2f;
                touchedX = event.getX();
                touchedY = event.getY();
                break;
            //双触 用来缩放
            case 2:
                planePosition = spacing(event);
                float scaledRatio = planePosition/ lastplanePosition;
                totalRatio = totalRatio *scaledRatio;

                if(totalRatio >maxScal) totalRatio = maxScal;
                if(totalRatio<minScal) totalRatio = minScal;

                objModel.scale().x = objModel.scale().y = objModel.scale().z = totalRatio;
                lastplanePosition = planePosition;
                break;
            }
            break;
        }
        return true;
    }

    private float spacing(MotionEvent event) {
        float x = event.getX(0) - event.getX(1);
        float y = event.getY(0) - event.getY(1);
        return FloatMath.sqrt(x * x + y * y);
    }

}

Original comment by weiyidea...@gmail.com on 18 Jan 2014 at 7:10