在Irrlicht中移动并旋转SceneNode(Sphere)

时间:2013-09-11 21:19:56

标签: c++ 3d rotation geometry irrlicht

我正在尝试使用WASD在Irrlicht中为3D球体设置动画。移动部件正常工作,但旋转不起作用。只要我按下W / S或A / D就行了。如果我将它们混合或从不同的点开始,旋转会旋转。

声明

scene::ISceneNode* ballSceneNode = sceneManager->addSphereSceneNode(5);
if (ballSceneNode) {
    ballSceneNode->setPosition(core::vector3df(0, 0, 100));
    ballSceneNode->setMaterialTexture(0, driver->getTexture("media/ball.bmp"));
    ballSceneNode->setMaterialFlag(video::EMF_LIGHTING, false);
}

主循环

core::vector3df ballRotation = ballSceneNode->getRotation();
core::vector3df ballRotation = ballSceneNode->getPosition();

if(receiver.isKeyDown(irr::KEY_KEY_W)) {
    ballPosition.Z += movement;
    ballRotation.X++;
} else if(receiver.isKeyDown(irr::KEY_KEY_S)) {
    ballPosition.Z -= movement;
    ballRotation.X--;
}

if(receiver.isKeyDown(irr::KEY_KEY_A)) {
    ballPosition.X -= movement;
    ballRotation.Z++;
} else if(receiver.isKeyDown(irr::KEY_KEY_D)) {
    ballPosition.X += movement;
    ballRotation.Z--;
}

ballSceneNode->setPosition(ballPosition);
ballSceneNode->setRotation(ballRotation);

我已经读过您需要core::matrix4进行轮换或更好地说它会有用/更容易也许也是正确的,但我不知道如何使用它。

修改

if(receiver.isKeyDown(irr::KEY_KEY_W)) {
    ballPosition.Z += movement;
    cameraPosition.Z += movement;

    core::quaternion test;
    test.fromAngleAxis(xAxisAngle, core::vector3df(1,0,0));
    xAxisAngle += 0.1f;
    test.normalize();
    core::vector3df rot;
    test.toEuler(rot);
    ballSceneNode->setRotation(rot * core::RADTODEG);
} else if(receiver.isKeyDown(irr::KEY_KEY_S)) {
    ballPosition.Z -= movement;
    cameraPosition.Z -= movement;

    core::quaternion test;
    test.fromAngleAxis(xAxisAngle, core::vector3df(1,0,0));
    xAxisAngle -= 0.1f;
    test.normalize();
    core::vector3df rot;
    test.toEuler(rot);
    ballSceneNode->setRotation(rot * core::RADTODEG);
}

if(receiver.isKeyDown(irr::KEY_KEY_A)) {
    ballPosition.X -= movement;
    cameraPosition.X -= movement;

    core::quaternion test;
    test.fromAngleAxis(zAxisAngle, core::vector3df(0,0,1));
    zAxisAngle += 0.1f;
    test.normalize();
    core::vector3df rot;
    test.toEuler(rot);
    ballSceneNode->setRotation(rot * core::RADTODEG);
} else if(receiver.isKeyDown(irr::KEY_KEY_D)) {
    ballPosition.X += movement;
    cameraPosition.X += movement;

    core::quaternion test;
    test.fromAngleAxis(zAxisAngle, core::vector3df(0,0,1));
    zAxisAngle -= 0.1f;
    test.normalize();
    core::vector3df rot;
    test.toEuler(rot);
    ballSceneNode->setRotation(rot * core::RADTODEG);
}

xAxisAnglezAxisAngle只是f32,默认值为0。

现在的问题是: W W D(跳到原始状态并且失去2 W)

1 个答案:

答案 0 :(得分:1)

使用欧拉角混合旋转总是很奇怪,因为应用旋转的顺序很重要。研究使用四元数或从正交基础构建旋转矩阵。有关在互联网和图形相关教科书上执行这些任务的大量信息。