Skip to content
Jonathan Schnee edited this page Jul 14, 2021 · 6 revisions

Tutorial 2 Input

📥

Tutorial 2 Input

Now we want to control the car's wheels interactively. To do this, we have to take user input and convert it into rotations.

In FUSEE we can access user input from various input devices via the Input class. This already has static fields (class variables) for the most common input devices, such as

  • Mouse
  • Keyboard
  • Touch

With the command

using static Fusee.Engine.Core.Input;

at the top of the file we can access the input devices in the code.

The following shows the most important properties that can be read out for the mouse and keyboard input devices. These can be read out in RenderAFrame() and the values ​​returned can be used for the interaction.

Mouse

Property Datatype Description
Mouse.LeftButton bool Indicates whether the left mouse button is currently pressed (true) or not ( false).
Mouse.MiddleButton bool Indicates whether the middle mouse button is currently pressed (true) or not ( false).
Mouse.RightButton bool Indicates whether the right mouse button is currently pressed (true) or not ( false).
Mouse.Position float2 Current position of the mouse cursor in pixels ((0, 0) = top left corner of the render window).
Mouse.Velocity float2 Current speed of the mouse cursor in pixels / second along the X and Y axes.

Keyboard

Property Datatype Description
Keyboard.GetKey(<KeyCode>) bool Indicates whether the transferred key is currently pressed (true) or not ( false).
Keyboard.IsKeyDown(<KeyCode>) bool Indicates whether the key was pressed in the current frame (true) or not ( false). The return value is false even if the key is still held down but has not been pressed in the current frame.
Keyboard.IsKeyUp(<KeyCode>) bool Indicates whether the given key was released in the current frame (true) or not ( false). The return value is also false if the key is not currently pressed, but not released in the current frame.
Keyboard.LeftRightAxis float 'Virtual' axis controlled by the left and right arrow. Value between -1 and 1.
Keyboard.UpDownAxis float 'Virtual 'axis controlled by the up and down arrow. Value between -1 and 1.
Keyboard.ADAxis float 'Virtual' axis controlled by the 'A' key and the 'D' key. Value between -1 and 1.
Keyboard.WSAxis float 'Virtual' axis controlled by the 'W' key and the 'S' key. Value between -1 and 1.

With the ... Axis, game-like control for the arrows and the 'WASD' keys can be implemented via the keyboard, without time-consuming individual queries via" GetKey ". The values of the axes in the interval [-1 ... 1] can be used as speed. The current axis value is also accelerated / decelerated with a certain amount of inertia when the key is changed.

We now want to use the current value of the WSAxis to control the rotation of the wheels on the car and simulate the car driving forward. We also want to turn the car with the ADAxis.

👨‍🔧 TODO

  • Add the following code to the beginning of the body of RenderAFrame()

     public override void RenderAFrame()
     {
        //Rotate both Axles forward or backwards
         _frontAxleTransform.Rotation.x = (wsSpeed + _frontAxleTransform.Rotation.x) % M.TwoPi;
         _rearAxleTransform.Rotation.x = (wsSpeed + _rearAxleTransform.Rotation.x) % M.TwoPi;
    
         //Rotate the right Wheels
         _frontRightWheelTransform.Rotation.x = (_frontRightWheelTransform.Rotation.x - 2 * adSpeed) % M.TwoPi;
         _rearRightWheelTransform.Rotation.x = (_rearRightWheelTransform.Rotation.x - 2 * adSpeed) % M.TwoPi;
    
        //Rotate the left Wheels
        _frontLeftWheelTransform.Rotation.x = (2 * adSpeed + _frontLeftWheelTransform.Rotation.x) % M.TwoPi;
         _rearLeftWheelTransform.Rotation.x = (2 * adSpeed + _rearLeftWheelTransform.Rotation.x) % M.TwoPi;
    
         //Rotate the car Left and Right (Tank-Controls)
         _baseTransform.Rotation.y = (adSpeed + _baseTransform.Rotation.y) % M.TwoPi;
    
         //Simulate car driving
         _baseTransform.Translation += new float3(wsSpeed * M.Sin(_baseTransform.Rotation.y), 0, wsSpeed * M.Cos(_baseTransform.Rotation.y));
         ...
Clone this wiki locally