Code Monkey home page Code Monkey logo

xbox_controller_linux_interface's Introduction

Linux Interface for an XBox One Controller

This is a linux interface, to integrate the Xbox-Controller directly into your favorite project. If you require different mappings or axes, please see the linux programs: jscal and jstest-gtk to change the mapping on the controller.

It is also recommended, to calibrate the controller before using it. This is also done with: jscal or jstest-gtk.

This library possibly works with other joystick devices but is only tested with the XBox controller.

Library Dependencies

Because some vector and quaternion based math is required to calculate the joystick movements, I provide the appropriate libraries needed to use Xbox-Controller here:

Quaternion library: https://github.com/MauriceGit/Quaternion_Library.

Vector library: https://github.com/MauriceGit/Vector_Library.

Just place the .c and .h files in the same directory and compile them with your project.

Functionality

The only interface and functions you should use, are the documented as function heads in the mtXboxController.h file. More documentation about these functions are found above the apropriate function bodies in mtXboxController.c.

Function Description
int mtInitJoyControl(char* name) This function MUST be called before any other. It initialises the usb stream connection.
int mtFinishJoyControl() This function should be called after any interaction with the Controller is finished.
void mtCalcJoyCameraMovement(double interval) This function should be called in very short intervals, to allow a very smooth joystick handling. The parameter is the time since the last call to this method.
MTVec3D mtGetJoyPosition() The current position is retreived (of the camera or object, controlled via the Controller).
MTVec3D mtGetJoyUp() The current up-vector is retreived (of the camera or object, controlled via the Controller).
MTVec3D mtGetJoyCenter() The current center point is retreived (of the camera or object, controlled via the Controller).

Calibration

There are two constants, to change the sensitivity of the joystick input. They are found in the mtXboxController.h and can be changed at will. Please ensure, that these values are of floating point.

Changes might be necessary depending on your application and size of your virtual environment.

Name Default Value Description
MT_XBOX_TRANS_NORMALISATION 500000.0 The smaller this value, the faster the translation will get. Affected are: Right stick, LT Axis and RT Axis (See: Controller Assignment).
MT_XBOX_TURN_NORMALISATION 2000.0 The smaller this value, the faster Pitch and Yaw will be. Left stick is affected (See: Controller Assignment).

The following constants will affect the axis and give them an offset (if value > 0). The offset could be used, to for example ignore the negative axis values.

Name Default Value Assignment
MT_AXIS_2_OFFSET 32768 LT Axis
MT_AXIS_3_OFFSET 0 Left stick
MT_AXIS_4_OFFSET 0 Right stick
MT_AXIS_5_OFFSET 32768 RT Axis

See also: Controller Assignment.

What about the Buttons?

You might have noticed that we ignored any buttons from your Controller and the mtXboxController does in fact not give you any interface for buttons. Reason for that is, that this controller interface acts as an abstraction level for the transformation of axis movements to 3D orientation calculation, that internally uses Quaternions.

Buttons don't need fancy calculations and abstractions around them. So if you like to implement actions for your Xbox Controller buttons, just include the mtJoystick.h and call the appropriate function directly (int getButtonValue(int buttonNumber, short * value)).

Usage

This library can be used to control a camera or object in a 3D environment.

The following example uses the Controller to move the camera in an OpenGL context.

// This is done right at the start of your program, before entering the main loop.
char* jsSource = "/dev/input/js0";
if (!mtInitJoyControl(jsSource)) {
    // Error. Initialisation failed.
}

...

// In GLUT or GLFW, there is a timer, allowing us to get the time interval between
// two frames. We need this interval, to update the camera position via the Controller
float interval = ...
mtCalcJoyMovement(interval);

...

// Later, we want to set our camera position and direction with gluLookAt().
// The result of our functions fit exactly into this scheme.
MTVec3D cam = mtGetJoyPosition();
MTVec3D center = mtGetJoyCenter();
MTVec3D up = mtGetJoyUp();
gluLookAt (cam.x,    cam.y,    cam.z,
           center.x, center.y, center.z,
           up.x,     up.y,     up.z);

...

// Don't forget, to nicely kill the controller ;)
mtFinishJoyControl();

Controller Assignment

All code is tested with the XBox One controller (see following image).

XBox Controller

The following axes are assigned:

Axis Assignment
LT Axis Increases the height of the object/camera along the Y axis.
RT Axis Decreases the height of the object/camera along the Y axis.
Left Stick Controls the Pitch and Yaw axis. (Front/Back: Pitch, Left/Right: Yaw).
Right Stick Moves the object/camera on the X/Z plane relative to the current position and orientation. Front/Back/Left/Right accordingly.

xbox_controller_linux_interface's People

Contributors

mauricegit avatar mervyn-mccreight avatar ztbrown avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.