Unigine心得之村长的野望1 - 键盘鼠标控制物体移动

    

前几日写了一个小DEMO用到键盘用ASDW等按键控制一个物体的上下左右移动和旋转等操作。

这个是一个非常基础的简单控制,但是查询官方文档也是很吃力。

因此,分享在此处,方便大家不用闭门造车,重复造车。

使用注意:

1:移动旋转速度,自行控制

2:我的代码操作的是mSelectedNode,这个节点对象请自行准备。

代码运行版本Unigine2.5 

但相信可以作用在几乎所有的Unigine版本中。因为都是基础操作函数。

int AppWorldLogic::update()

{

//设置旋转和移动的速度
    Unigine::App* app = Unigine::App::get();
    float movement_speed = 5.0;
    float rotation_speed = 5.0f;

    //有当前对象才操作
    if (mSelectedNode)
    {
        Vec3 oldPos = mSelectedNode->getPosition();
        // get the frame duration
        float ifps = Game::get()->getIFps();
        // get the current world transformation matrix of the mesh
        Mat4 transform = mSelectedNode->getWorldTransform();
        // get the direction vector of the mesh from the second column of the transformation matrix

        //用aswd控制上下左右的位置变化,参考游戏控制
        if (app->getKeyState('a') && !Console::get()->getActivity())
        {
            Vec3 direction = transform.getColumn3(0);
            // calculate the delta of movement
            Vec3 delta_movement = direction * movement_speed * ifps;
            // set a new position to the mesh
            mSelectedNode->setWorldPosition(mSelectedNode->getWorldPosition() - delta_movement);
        }
        if (app->getKeyState('w') && !Console::get()->getActivity())
        {
            Vec3 direction = transform.getColumn3(1);
            // calculate the delta of movement
            Vec3 delta_movement = direction * movement_speed * ifps;
            // set a new position to the mesh
            mSelectedNode->setWorldPosition(mSelectedNode->getWorldPosition() + delta_movement);
        }
        if (app->getKeyState('s') && !Console::get()->getActivity())
        {
            Vec3 direction = transform.getColumn3(1);
            // calculate the delta of movement
            Vec3 delta_movement = direction * movement_speed * ifps;
            // set a new position to the mesh
            mSelectedNode->setWorldPosition(mSelectedNode->getWorldPosition() - delta_movement);
        }
        if (app->getKeyState('d') && !Console::get()->getActivity())
        {
            Vec3 direction = transform.getColumn3(0);
            // calculate the delta of movement
            Vec3 delta_movement = direction * movement_speed * ifps;
            // set a new position to the mesh
            mSelectedNode->setWorldPosition(mSelectedNode->getWorldPosition() + delta_movement);
        }
        //用wr控制左侧旋转和右侧
        if (app->getKeyState('q') && !Console::get()->getActivity())
        {
            // set the node rotation along the Z axis
            mSelectedNode->setWorldRotation(mSelectedNode->getWorldRotation() * quat(rotateZ(rotation_speed * ifps)));

        }
        if (app->getKeyState('e') && !Console::get()->getActivity())
        {
            // set the node rotation along the Z axis
            mSelectedNode->setWorldRotation(mSelectedNode->getWorldRotation() * quat(rotateZ(-rotation_speed * ifps)));

        }

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

拉达曼迪斯II

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值