简体   繁体   中英

Rotation around 3d object on touch using min3d/Rajawali framework (android)

I am working on an algorithm for rotating the camera around a 3D object using the Min3d/Rajawali framework.

With my implementation, the rotation around axis X is not working properly. I think the method setLookAt() is not working properly.

The problem:

When I rotate the sphere vertically, I can't fully see it. For example, turning the planet Earth, I can not fully see the Antarctic, because the algorithm resets the camera down.

Is it possible to realize the camera rotation around an object without using the method "setLookAt"?

I have tried different solutions, but have not been able to get it working correctly.

Below is my code:

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;
    }

Number3d target = scene.camera.target;
Number3d cp = scene.camera.position.clone();
// move position like target is (0,0,0)
cp.x -= target.x;
cp.y -= target.y;
cp.z -= target.z;

cp.roateX(angle);

// restore offset
cp.x += target.x;
cp.y += target.y;
cp.z += target.z;
scene.camera.position.setAllFrom(cp);

A bit late but in case anyone has trouble with that, like me.

Rajawali offers a camera called ArcballCamera , which does exactly what you/we are trying to do.

In your Renderer add the following:

 ArcballCamera arcball = new ArcballCamera(mContext, ((Activity)mContext).findViewById(R.id.contentView)); 
 arcball.setTarget(mObjectGroup); //your 3D Object

 arcball.setPosition(0,0,4); //optional

 getCurrentScene().replaceAndSwitchCamera(getCurrentCamera(), arcball);

now you can rotate and zoom the object without a dozen lines of code.

setLookAt() needs to be called onDrawFrame if you want the camera to update regularly. But you need to care more about creating a "RotateAroundAnimation". See here for more info: http://www.rozengain.com/blog/2012/03/26/rajawali-tutorial-12-animation-classes/

You'll have to make yourAnimation.setTransformable3D(mCamera), and then it should control your main camera. I often use this methodology and then call the "yourAnimation.start()" on touch, or other external stimulus.

This is my way to rotate 3d model according as x and y

in Render class

public boolean left, right;
public boolean up, down;

and in onDrawFrame

@Override
public void onDrawFrame(GL10 glUnused) {
    super.onDrawFrame(glUnused);
    // getCurrentCamera().setRotY(getCurrentCamera().getRotY() + 1);
    if (left) {
        getCurrentCamera().setRotX(getCurrentCamera().getRotX() - 1);
    }
    if (right) {
        getCurrentCamera().setRotX(getCurrentCamera().getRotX() + 1);
    }
    if (up) {
        getCurrentCamera().setRotY(getCurrentCamera().getRotY() - 1);
    }
    if (down) {
        getCurrentCamera().setRotY(getCurrentCamera().getRotY() + 1);
    }
}

and in MainActivity

@Override
public boolean onTouchEvent(MotionEvent event) {

    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        xpos = event.getX();
        ypos = event.getY();
    }

    if (event.getAction() == MotionEvent.ACTION_UP) {
        xpos = -1;
        ypos = -1;
        mRender.left = false;
        mRender.right = false;
        mRender.up = false;
        mRender.down = false;

    }

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

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

        if (xd < 0) {
            mRender.up = true;
        } else {
            mRender.down = true;
        }
        if (yd < 0) {
            mRender.left = true;
        } else {
            mRender.right = true;
        }

    }

    return super.onTouchEvent(event);
}

by this way, i can rotate my model :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM