【问题标题】:How to rotate part of a 3D model about its center, not the model's centre with LIBGDX如何使用 LIBGDX 围绕其中心而不是模型的中心旋转 3D 模型的一部分
【发布时间】:2014-10-31 21:48:57
【问题描述】:

我在 Blender 中创建了一个带有一个炮塔的船的模型,炮塔有 1 门枪。 为了清楚起见,这些是搅拌机中的 3 个独立对象。 在 g3dj 文件中,这显示为 3 个节点:一个用于船,一个用于炮塔,一个用于火炮。

"nodes": [
    {
        "id": "gun1", 
        "rotation": [ 0.000000,  0.000000,  0.707107,  0.707107], 
        "scale": [ 0.100000,  0.984755,  0.100000], 
        "translation": [-3.526966,  1.428384,  0.191505], 
        "parts": [
            {
                "meshpartid": "Cylinder_part1", 
                "materialid": "gunMaterial"
            }
        ]
    }, 
    {
        "id": "turret1", 
        "scale": [ 1.000000,  0.300000,  0.395389], 
        "translation": [-1.603167,  1.406584,  0.000000], 
        "parts": [
            {
                "meshpartid": "Cube.001_part1", 
                "materialid": "turretMaterial"
            }
        ]
    }, 
    {
        "id": "ship", 
        "scale": [ 3.000000,  1.000000,  1.000000], 
        "parts": [
            {
                "meshpartid": "Cube_part1", 
                "materialid": "shipMaterial", 
                "uvMapping": [[]]
            }
        ]
    }
], 
"animations": []

}

使用 Libgdx,我尝试将炮塔旋转 30 度(rotAngle=30)。 所以我翻译成我认为是模型的起源(我认为那是船的起源?)。 然后我旋转。然后最后我会翻译回来。 但是我已经把那个翻译出来了,因为旋转不是围绕正确的点? 代码如下(基于一个xoppa教程):

        Vector3 dimensions = new Vector3();

    for (int i = 0; i < model.nodes.size; i++) {
        String id = model.nodes.get(i).id;
        ModelInstance instance = new ModelInstance(model, id);
        instance.transform.getTranslation(position);

        Node node = instance.getNode(id);
        node.calculateLocalTransform();
        node.localTransform.getTranslation(position);
        node.calculateBoundingBox(bounds).mul(node.localTransform);
        node.localTransform.getScale(scale);
        bounds.getDimensions(dimensions);
        centre=bounds.getCenter(centre);
        //float radius;
        //radius = dimensions.x / 2f;

        if (id.equals("ship")) {
            ///instance.transform.rotate(Vector3.Y,rotAngle);
            ship = instance;
            instances.add(ship);
            continue;
        }
        if (id.startsWith("gun")) {

            guns.add(instance);
            instances.add(instance);
            continue;
        }
        if (id.startsWith("turret")) {
            //movement on Y is based on Z from blender
            instance.transform.trn(-centre.x,-centre.z,0).
            rotate(Vector3.Y,rotAngle); // ???z???

            turrets.add(instance);
            instances.add(instance);
            continue;
        }
    }

怎么了?

【问题讨论】:

  • 你为什么要创建所有这些可渲染对象?
  • 然而,一旦你告诉他们这样做:stackoverflow.com/questions/26656334/…;关键是在您的枢轴点上平移,旋转并向后平移
  • @Xoppa 抱歉,刚刚看到回复。我当时正在关注您使用可渲染的教程!
  • @cfrick 谢谢。我知道这是理论,但我仍然有问题。我想我需要重申一下这个问题。
  • @xoppa 我已经重置了我的代码/问题。你能再看看吗?

标签: rotation libgdx coordinates nodes


【解决方案1】:

基本想法是正确的,但我需要使用节点的位置(平移)而不是中心进行平移,并考虑搅拌器的比例因子,以及一些 y/z 值的明显随机切换。

这是炮塔的修改代码:

Vector3 turretMove = new Vector3();                
turretMove.x = position.x*scale.x;
//scale.z is what was the blender scale.y and vice versa
turretMove.y = -position.y*scale.z; //using opposite sign of position
turretMove.z = position.z*scale.y;
instance.transform.translate(turretMove.x,turretMove.y,turretMove.z).
rotate(Vector3.Y,dt).
translate(-turretMove.x,-turretMove.y,-turretMove.z);

此代码现在包括翻译回原始位置。 经过几个小时的反复试验才到达这里!

在 Blender 中,Z-up 是从船的底部到顶部,Y 从船的左侧(和炮塔)到右侧。

在 Libgdx 中,Z 是从船的左侧到右侧(看屏幕),而 Y 是从船的底部到顶部(在屏幕上)。

从 Blender 导出到 FBX,使用 -Z forward 和 Y Up,只导出选定的对象。

我猜这部分解释了 Z 和 Y 比例值的翻转?

【讨论】:

  • 关注@Xoppa 链接。简而言之:在 Blender 中使用比例 0.01 导出到 FBX(默认为 1.0,导致模型大 100 倍)。然后,使用带有“-f”参数的 fbx-conv。这将修复 libGDX 的 Blender 轴。
猜你喜欢
  • 1970-01-01
  • 2012-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多