MOGRE 1.7.1 simple mouse as joystick example

dannypb

11-07-2011 20:49:16

I want to give back, so here is a crappy example of using a mouse as a joystick. Feel free to offer suggestions for improvements, but I wanted to post this to help anyone else who may need an example.

This is in my unbuffered frame started listener:

MOIS.MouseState_NativePtr mouseState = mMouse.MouseState;
Degree cameraYaw = 0;
Degree cameraPitch = 0;

gCamNode.Translate(translateVector, Mogre.Node.TransformSpace.TS_LOCAL);

if (mouseState.Y.rel < 0.0f)
{
MouseYPos--;
if (MouseYPos < YCenter - ySpeed) { MouseYPos = YCenter - ySpeed; }
}
if (mouseState.Y.rel > 0.0f)
{
MouseYPos++;
if (MouseYPos > YCenter + ySpeed) { MouseYPos = YCenter + ySpeed; }
}
if (mouseState.X.rel < 0.0f)
{
MouseXPos--;
if (MouseXPos < XCenter - xSpeed) { MouseXPos = XCenter - xSpeed; }
}
if (mouseState.X.rel > 0.0f)
{
MouseXPos++;
if (MouseXPos > XCenter + xSpeed) { MouseXPos = XCenter + xSpeed; }
}

if (MouseYPos < YCenter - MouseDeadZone)
{
cameraPitch = (YCenter - MouseYPos - MouseDeadZone) * evt.timeSinceLastFrame * -1;
}
if (MouseYPos > YCenter + MouseDeadZone)
{
cameraPitch = (MouseYPos - YCenter + MouseDeadZone) * evt.timeSinceLastFrame;
}
if (MouseXPos < XCenter - MouseDeadZone)
{
cameraYaw = (XCenter - MouseXPos - MouseDeadZone) * evt.timeSinceLastFrame * -1;
}
if (MouseXPos > XCenter + MouseDeadZone)
{
cameraYaw = (MouseXPos - XCenter + MouseDeadZone) * evt.timeSinceLastFrame;
}

panel.Top = MouseYPos;
panel.Left = MouseXPos;
//Console.WriteLine(mouseState.Y.rel + ":" + MouseYPos + ":" + YCenter);
gCamNode.Pitch(-cameraYaw, Mogre.Node.TransformSpace.TS_LOCAL);
gCamNode.Yaw(-cameraPitch, Mogre.Node.TransformSpace.TS_LOCAL);


xSpeed and ySpeed should actually be called xMaxSpeed and yMaxSpeed. I use two panels to show the current position and the center. I haven't really figured out panels and billboards yet, so it's pretty basic there. Mogre>Node.TransformSpace.TS_LOCAL is very important. Without it you will move like a first person shooter entity on the ground, this will cause significant distortion of your movement in all directions. the code to initialize my basic panels is (from CreateScene()):


var overlay = OverlayManager.Singleton.Create("TestOverlay");
panel = (PanelOverlayElement)OverlayManager.Singleton.CreateOverlayElement("Panel", "PanelElement");
var centerPanel = (PanelOverlayElement)OverlayManager.Singleton.CreateOverlayElement("Panel", "CenterPanel");
// Set panel properties.
panel.MaterialName = "Core/StatsBlockCenter";
panel.MetricsMode = GuiMetricsMode.GMM_PIXELS;
centerPanel.MaterialName = "Core/StatsBlockCenter";
centerPanel.MetricsMode = GuiMetricsMode.GMM_PIXELS;

MouseYPos = mRenderWindow.Height / 2;
MouseXPos = mRenderWindow.Width / 2;
YCenter = mRenderWindow.Height / 2;
XCenter = mRenderWindow.Width / 2;
panel.Top = MouseYPos;
panel.Left = MouseXPos;
panel.Width = 3;
panel.Height = 3;

centerPanel.Top = MouseYPos;
centerPanel.Left = MouseXPos;
centerPanel.Width = 5;
centerPanel.Height = 5;


panel is a class private variable. in fact here are the class variables I use:

PanelOverlayElement panel;
UInt32 MouseYPos, MouseXPos, YCenter, XCenter;
Int32 MouseDeadZone = 5;
UInt32 xSpeed = 50;
UInt32 ySpeed = 30;


I hope this helps someone.