Knowledge Base » How to enable keyboard and mouse support

The Untold Engine supports keyboard and mouse actions.

For your reference, the required code to enable keyboard and mouse are implemented in the GameViewController class. Simply make sure that the following method is set to false in the viewDidLoad method in the GameViewController class.

//If using the keyboard, then set it to false. If using a controller then set it to true
director->setGamePadControllerPresent(false);

Detecting Absolute Mouse Movement

The snippet below shows how you can rotate a character by utilizing the absolute motion of the mouse.

//...other actions here

// Action detected on mouse

case U4DEngine::mouse:
{
    //SNIPPET TO USE FOR MOUSE ABSOLUTE POSITION
    if(controllerInputMessage.inputElementAction==U4DEngine::mouseActive){
        
    //get the current and previous mouse position
    U4DEngine::U4DVector3n p1(controllerInputMessage.inputPosition.x,controllerInputMessage.inputPosition.y,0.0);
    U4DEngine::U4DVector3n p2(controllerInputMessage.previousMousePosition.x,controllerInputMessage.previousMousePosition.y,0.0);
    
    //The following code will compute the correct orientation to rotate the character about an axis.
    float p1z=1.0-(p1.x*p1.x+p1.y*p1.y);
    p1.z=p1z>0.0 ? std::sqrt(p1z) :0.0;

    float p2z=1.0-(p2.x*p2.x+p2.y*p2.y);
    p2.z=p2z>0.0 ? std::sqrt(p2z) :0.0;


    U4DEngine::U4DVector3n axis;

    U4DEngine::U4DVector3n mouseDirection=p1-p2;

    mouseDirection.normalize();

    U4DEngine::U4DVector3n upVector(0.0,1.0,0.0);
    U4DEngine::U4DVector3n xVector(1.0,0.0,0.0);

    //get the dot product
    float upDot, xDot;

    upDot=mouseDirection.dot(upVector);
    xDot=mouseDirection.dot(xVector);

    U4DEngine::U4DVector3n v=pPlayer->getViewInDirection();
    v.normalize();
    
    if(mouseDirection.magnitude()>0){
        //if direction is closest to upvector
        if(std::abs(upDot)>=std::abs(xDot)){
            //rotate about x axis

            if(upDot>0.0){
                axis=v.cross(upVector);

            }else{
                axis=v.cross(upVector)*-1.0;

            }
        }else{

            //rotate about y axis
            if(xDot>0.0){

                axis=upVector;

            }else{

                axis=upVector*-1.0;
            }

        }

        //Once we know the angle and axis of rotation, we can rotate the model using interpolation as shown below
        float angle=p1.angle(p2);

        float biasAngleAccumulator=0.90;

        angleAccumulator=angleAccumulator*biasAngleAccumulator+angle*(1.0-biasAngleAccumulator);

        U4DEngine::U4DQuaternion newOrientation(angleAccumulator,axis);

        U4DEngine::U4DQuaternion modelOrientation=pPlayer->getAbsoluteSpaceOrientation();

        U4DEngine::U4DQuaternion p=modelOrientation.slerp(newOrientation,1.0);

        pPlayer->rotateBy(p);

    }
        
        
    }else if(controllerInputMessage.inputElementAction==U4DEngine::mouseInactive){
        
        
    }
    
}
    break;

The illustration below shows the absolute movement of the mouse rotating the charater.

Image

Detecting Relative Mouse Movement

To detect the relative mouse movement, you will need to anchor the mouse in the scene initialization method. To do so, simply calle the setAnchorMouse function as shown below:

void DemoScene::init(){
    
//create view component
demoWorld=new DemoWorld();

//create loading screen
loadingScene=new DemoLoading();

//create model component
demoLogic=new DemoLogic();

loadComponents(demoWorld, loadingScene, demoLogic);

//Anchor the mouse to the screen
setAnchorMouse(true);
    
}

The snippet below shows how you can rotate a character by utilizing the relative (delta) motion of the mouse.

//...other actions here

// Action detected on mouse
case U4DEngine::mouse:
            {
                
        if(controllerInputMessage.inputElementAction==U4DEngine::mouseActive){
        
            
            //USE THIS SNIPPET WHEN YOU ONLY WANT THE MOUSE DELTA LOCATION
            
            //Get the delta movement of the mouse
            U4DEngine::U4DVector2n delta=controllerInputMessage.mouseDeltaPosition;
            //the y delta should be flipped
            delta.y*=-1.0;
            
            //The following snippet will determine which way to rotate the model depending on the motion of the mouse
            float deltaMagnitude=delta.magnitude();
            delta.normalize();

            U4DEngine::U4DVector3n axis;

            U4DEngine::U4DVector3n mouseDirection(delta.x,delta.y,0.0);
            U4DEngine::U4DVector3n upVector(0.0,1.0,0.0);
            U4DEngine::U4DVector3n xVector(1.0,0.0,0.0);

            //get the dot product
            float upDot, xDot;

            upDot=mouseDirection.dot(upVector);
            xDot=mouseDirection.dot(xVector);

            U4DEngine::U4DVector3n v=pPlayer->getViewInDirection();
            v.normalize();

            if(mouseDirection.magnitude()>0){
                //if direction is closest to upvector
                if(std::abs(upDot)>=std::abs(xDot)){
                    //rotate about x axis

//                            if(upDot>0.0){
//                                axis=v.cross(upVector);
//
//                            }else{
//                                axis=v.cross(upVector)*-1.0;
//
//                            }
                }else{

                    //rotate about y axis
                    if(xDot>0.0){

                        axis=upVector;

                    }else{

                        axis=upVector*-1.0;
                    }

                }

                //Once we know the angle and axis of rotation, we can rotate the model using interpolation as shown below
                
                float angle=0.03*deltaMagnitude;
                
                float biasAngleAccumulator=0.90;

                angleAccumulator=angleAccumulator*biasAngleAccumulator+angle*(1.0-biasAngleAccumulator);
                
                U4DEngine::U4DQuaternion newOrientation(angleAccumulator,axis);

                U4DEngine::U4DQuaternion modelOrientation=pPlayer->getAbsoluteSpaceOrientation();

                U4DEngine::U4DQuaternion p=modelOrientation.slerp(newOrientation,1.0);

                pPlayer->rotateBy(p);
                    
            }

        }else if(controllerInputMessage.inputElementAction==U4DEngine::mouseInactive){
            
            //std::cout<<"Mouse stopped"<<std::endl;
        }
        
    }
        
        break;

The illustration below shows the relative movement of the mouse rotating the charater.

Image