简体   繁体   English

Android min3D使用触摸旋转对象

[英]Android min3D using touch to rotate around an object

I'm trying to rotate the camera around an object by using the touchscreen. 我正在尝试使用触摸屏围绕对象旋转相机。 The rotation around Y axis works fine (The X axis is disabled). 围绕Y轴的旋转工作正常(X轴被禁用)。 Rotation around the X axis is really weird. 绕X轴旋转真的很奇怪。 when the Object (its a rocket) gets higher rocket.position().y++ & scene.camera().position.y++ , the rotation around the x axis gets bigger and weird. 当Object(它的火箭)变得更高时,使用了rocket.position().y++ & scene.camera().position.y++ ,围绕x轴的旋转变得越来越大。 If the rockets stops rocket.position().y = 500; & scene.camera().position.y = 500; 如果火箭停止火箭。位置rocket.position().y = 500; & scene.camera().position.y = 500; rocket.position().y = 500; & scene.camera().position.y = 500; , I can't rotate around x axis, I zoom in or out the object instead. ,我不能围绕x轴旋转,而是放大或缩小对象。 With both axis enabled its weird as hell. 两个轴都启用了它的奇怪地狱。

In initScene I set the camera to look at the center of the rocket. initScene我将相机设置为观察火箭的中心。

Here's 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;
    }

I think it is a view-orientation-issue. 我认为这是一个以观点为导向的问题。 try basic android opengl example "rotating cubes", i think it is a similar behaviour. 尝试基本的android opengl示例“旋转立方体”,我认为这是一个类似的行为。

To avoid this you have to transform display-coordinates into min3D object rotation-coordinates. 为避免这种情况,您必须将显示坐标转换为min3D对象旋转坐标。

Think about moving your finger 1cm on display to the left, with 3D-object following your finger. 考虑将手指向左移动1厘米左右,用手指按住3D对象。 This means x_display = x_object. 这意味着x_display = x_object。 If you rotate your object, there is still a fixed connection between display and object. 如果旋转对象,则显示和对象之间仍然存在固定连接。 (x_display = - 1cm) equals (x_object = -1cm) (x_display = - 1cm)等于(x_object = -1cm)
at 90° & 270° your object will move xy-inverted, at 180° your object will move x-inverted. 在90°和270°时,您的物体将以xy反转方式移动,在180°时,您的物体将向x方向移动。

use some sin/cos to overcome this. 使用一些sin / cos来克服这个问题。

edit: also rotationangle (object) = - rotationangle (camera) 编辑:还是rotationangle(对象)= - rotationangle(相机)

One option is to rotate the container itself instead of the camera. 一种选择是旋转容器本身而不是相机。 In min3d you often work with an Object3dContainer as a class variable in your activity that displays the model. 在min3d中,您经常使用Object3dContainer作为显示模型的活动中的类变量。 When you get your MotionEvent you can use the method rotation() from your Object3dContainer class and set x or y properties as appropriate. 获取MotionEvent后,可以使用Object3dContainer类中的rotation()方法,并根据需要设置x或y属性。

You keep saying wierd, but you know that it doesn't describe what you see. 你一直说很奇怪,但你知道它并没有描述你所看到的。 Rotate is actually method of Number3d. Rotate实际上是Number3d的方法。 It doesn't know about the target , so I guess rotation is done according the center, not according the rocket (Didn't quite check that, just a guess). 它不知道目标 ,所以我猜旋转是根据中心完成的,而不是根据火箭(没有完全检查,只是一个猜测)。 There might be 2 solutions: 可能有两种解决方案:

  1. The easiest thing is to set the rocket to (0,0,0) and instead moving the rocket, move evertything else. 最简单的方法是将火箭设置为(0,0,0),然后移动火箭,移动其他东西。 Well that would be a stupid suggestion. 那将是一个愚蠢的建议。 Forget it. 算了吧。
  2. The other solution is to calculate camera rotation traectory using sin() and cos() 另一个解决方案是使用sin()和cos()计算相机旋转轨迹
  3. There might be a third solution, but I didn't think aa lot about it and it might be wrong. 可能有第三种解决方案,但我没有想太多,这可能是错误的。 What you have to do is move the camera like target is at (0,0,0), rotate and then return the camera. 你需要做的是移动相机,如目标位于(0,0,0),旋转然后返回相机。

Would look like something like this: 看起来像这样:

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

You can try 3 but don't rely it'll do the job. 你可以尝试3但不要依赖它就能完成这项工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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