Mouse emulating Joystick

BlitzPlus Forums/BlitzPlus Programming/Mouse emulating Joystick

Cancerboy(Posted 2006) [#1]
Does anyone have a good routine for emulating joystick control with a a standard mouse? I am only looking to emulate the four basic directions: forward, back, left and right.

Thanks in advance!


Adam Novagen(Posted 2006) [#2]
Try replacing "JoyX()" and "JoyY()" with "MouseXSpeed()" and "MouseYSpeed()".


Cancerboy(Posted 2006) [#3]
You know that almost works. Thing is crazy sensitive though... I get repeat commands in a direction if I move the mouse fast at all. Trying to get my head around how I could make the speed adjustable and more controlled. Anyone dealt with this before?


Cancerboy(Posted 2006) [#4]
The problem I am facing now is that the mouse stops working when the mouse pointer reaches the extent of the screen. If I force a movemouse then the MouseXSpeed and MouseYSpeed jump and mix up my movement code. Anyone had to deal with this and can help? I am trying to enter a contest with my game in about 5 days and I wanted to get reliable mouse control emulating a joystick in the game.

Any help GREATLY appreciated!!


Adam Novagen(Posted 2006) [#5]
Okay, here's the thing: JoyX() and JoyY() only return a value between -1 and 1, while MouseXSpeed() and MouseYSpeed() return the literal X and Y speed of the mouse (in pixels.) Using MoveMouse in tandem with an If statement that tests the mouse's X/Y speed, you should be able to establish an actual "speed limit" which should cure the sensitivity problem. For instance:
;Try this in your main update loop
mx = MouseX()
my = MouseY()

If MouseXSpeed() > 1
    MoveMouse mx + 1,MouseY()
ElseIf MouseXSpeed() < -1
    MoveMouse mx - 1,MouseY()
EndIf
If MouseYSpeed() > 1
    MoveMouse MouseX(),my + 1
ElseIf MouseXSpeed() < -1
    MoveMouse MouseX(),my - 1
EndIf

As usual, this is just off the top of my head, (what can I say - I'm good at improv!) and I have no idea whether it will work properly - let me know!

By the way, cancerboy - you really like joysticks, doncha? ;)


Cancerboy(Posted 2006) [#6]
Thanks Adam (AKA all those other names),

It's not that I really like joysticks... it is that I am entering a competition at Retro Remakes and the theme is accessibility. This includes offering as many methods of controlling a game such as keyboard, mouse and joystick but also remapping of all keys and adding user assistance and visible audio cues for the hearing impaired. Also simple things like adding difficulty levels and making easy levels actually very easy to play.

I don't normaly add even joystick support but I am trying to go all out and add as many control methods as I can within the ever running out time frame of the competition. I have about 4 days left to get everything done. If I added this type of support to more things I would probably be better at it :).

Thanks for the tips... I was trying something like you suggest. I am aware that MouseXSpeed/MouseYSpeed returns the speed of the mouse in the dir from the last test of the mouse. Just from glancing at your code I think I could still have the problem that if the mouse pointer hits the edge of the screen I will lose the ability to move in that direction. I was hoping there was some way to move the mouse pointer to the middle of the screen when I am using it for control and never have to deal with the problem of running into the side of the screen. I will give it a shot and report my results.

Perhaps there is a good reason more people haven't implemented mouse movement for joystick control. The idea is that if the game can be controlled with a mouse then it likely could be controlled with a head tracker. This is ideal for someone without the physical ability to use a traditional control device. It would be great if I could get it working reasonably well.

Thanks again for your help!


Adam Novagen(Posted 2006) [#7]
Okay, new idea: a joystick's X/Y detection is based on how much it's handle (or stick) is offset from the centerpoint, right? So in other words, graphically speaking:

/---\ /---\ /---\ /---\ ./-o-\
| o | o.+.||..+.o|..+..||..+..|
\---/ \---/ \---/ \-o-/ .\---/
|X=0|X=-1|X=1 |X=0 | X=0
|Y=0|Y=0 |Y=0 |Y=-1| Y=1

The handle is represented by "o" and the center point is represented by "+" in my diagram. (Yeah I know it looks bad, but there's only so much you can do with text!) Now, imagine that the handle is the mouse. When your game loop starts, you set the centerpoint using MoveMouse, then use the same principle shown above. Your code might look something like this:
centerx = GraphicsWidth() / 2
centery = GraphicsHeight() / 2
sensitivity = 100;Raise this to decrease sensitivity, lower it to increase it

;The "MouseStick" (har har) test/update code
;jx & jy are the replacement vars for JoyX() & JoyY(), of course
jx = centerx - (MouseX() / sensitivity)
jy = centery - (MouseY() / sensitivity)

You may have to reverse the order of "centerx/y - (MouseX/Y() / sensitivity)" to "(MouseX/Y() / sensitivity) - centerx/y", I'm not sure. If this doesn't work, then I'm out of ideas - hey, I'm only 14, how much can you expect? Anyway, best of luck with the competition - L8r!


Cancerboy(Posted 2006) [#8]
This is almost exactly the first thing I tried a few weeks ago. I tried your version explicitly and it doesn't quite work. Oh well. I guess I will have to focus on making the joysticks and keyboards as friendly as possible. Thanks for trying!

If anyone can come up with a robust mouse method for emulating a 4 direction joystick before Wed the 30th of Aug I will be more than happy. Consider it a CHALLENGE. I will reward anyone who succeeds with a credit in my game.


Adam Novagen(Posted 2006) [#9]
Oh well. Hey, guess what! I check the Blitz forums regularly when on the PC, and when I checked this topic just now, I saw: "Last Post by Cancerboy (11 seconds ago)". WOAH! Talk about timing! Sorry the MouseStick theory didn't work.


Cancerboy(Posted 2006) [#10]
I am sure there is a way to get it to work but I have so much else to do and the mouse/joystick emulation is like a black hole on my time usage right now. I will hit it hard if I get everything else done before the deadline.