Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
MouseMovement Demo
#1
Code: (Select All)
SCREEN _NEWIMAGE(1024, 720, 32)

arrow = _NEWIMAGE(75, 100, 32)

font = _LOADFONT("courbd.ttf", 100, "monospaced")
_DEST arrow
_FONT font
_CONTROLCHR ON
PRINT "";
_DEST 0


DO
    WHILE _MOUSEINPUT
        angle = angle + _MOUSEMOVEMENTX
    WEND
    CLS , 0
    PRINT "Angle:"; angle
    DisplayImage arrow, 512, 360, 3, 3, angle, 0
    _LIMIT 15
    _DISPLAY
LOOP UNTIL _MOUSEBUTTON(2)



SUB DisplayImage (Image AS LONG, x AS INTEGER, y AS INTEGER, xscale AS SINGLE, yscale AS SINGLE, angle AS SINGLE, mode AS _BYTE)
    'Image is the image handle which we use to reference our image.
    'x,y is the X/Y coordinates where we want the image to be at on the screen.
    'angle is the angle which we wish to rotate the image.
    'mode determines HOW we place the image at point X,Y.
    'Mode 0 we center the image at point X,Y
    'Mode 1 we place the Top Left corner of oour image at point X,Y
    'Mode 2 is Bottom Left
    'Mode 3 is Top Right
    'Mode 4 is Bottom Right

    DIM AS INTEGER px(3), py(3), w, h, w1, h1
    DIM sinr AS SINGLE, cosr AS SINGLE, i AS _BYTE
    w = _WIDTH(Image): h = _HEIGHT(Image)
    w1 = w * xscale: h1 = h * yscale
    SELECT CASE mode
        CASE 0 'center
            px(0) = -w1 / 2: py(0) = -h1 / 2: px(3) = w1 / 2: py(3) = -h1 / 2
            px(1) = -w1 / 2: py(1) = h1 / 2: px(2) = w1 / 2: py(2) = h1 / 2
        CASE 1 'top left
            px(0) = 0: py(0) = 0: px(3) = w1: py(3) = 0
            px(1) = 0: py(1) = h1: px(2) = w1: py(2) = h1
        CASE 2 'bottom left
            px(0) = 0: py(0) = -h1: px(3) = w1: py(3) = -h1
            px(1) = 0: py(1) = 0: px(2) = w1: py(2) = 0
        CASE 3 'top right
            px(0) = -w1: py(0) = 0: px(3) = 0: py(3) = 0
            px(1) = -w1: py(1) = h1: px(2) = 0: py(2) = h1
        CASE 4 'bottom right
            px(0) = -w1: py(0) = -h1: px(3) = 0: py(3) = -h1
            px(1) = -w1: py(1) = 0: px(2) = 0: py(2) = 0
    END SELECT
    sinr = SIN(angle / 57.2957795131): cosr = COS(angle / 57.2957795131)
    FOR i = 0 TO 3
        x2 = (px(i) * cosr + sinr * py(i)) + x: y2 = (py(i) * cosr - px(i) * sinr) + y
        px(i) = x2: py(i) = y2
    NEXT
    _MAPTRIANGLE (0, 0)-(0, h - 1)-(w - 1, h - 1), Image TO(px(0), py(0))-(px(1), py(1))-(px(2), py(2))
    _MAPTRIANGLE (0, 0)-(w - 1, 0)-(w - 1, h - 1), Image TO(px(0), py(0))-(px(3), py(3))-(px(2), py(2))
END SUB

I think a lot of folks misunderstand what MouseMovementX and MouseMovementY are for.  

First thing to note:  They're NOT for tracking your mouse's absolute position on the screen.  There's _MouseX and _MouseY for that.

So what are they for??

Let's move our thinking from QB64PE out to just playing some game.  Now, in this game, you're in a spaceship flying through outer space.  To control your ship, it's set up to use a simple scroll ball as the navigation tool.

Scroll that ball right, and the ship turns right.
Scroll that ball left, and the ship turns left.
Scroll that ball up, and the ship flys down.  (The arse end goes up, the nose goes down.)
Scroll that ball down, and the ship flys up.  (The arse end goes down, the nose goes up.)

Now, tracking mouse position in this situation is just foolish.  You scroll right.... right... right.... right.... Hit the edge of the screen with the mouse, and BOOM!!  Your ship can no longer rotate to the right in outer space!!

See the problem with trying to use _MouseX in this situation?   You can turn right and go around in circles till the end of time.  Your mouse cursor, however, is going to stop at the edge of your screen.

So, how to track this "ball rotated right" event??    _MouseMovementX.    Even if the mouse is at the right edge of the screen, you can scroll that mouseball on your desk endlessly to the right.  It won't move the mouse pointer -- it's already at the limit -- but _MouseMovementX will record that rotation and report it to you.

MouseMovementX  simply tells you if your mouseball has rotated left or right.
MouseMovementY tells you if that mouseball has rotated up or down.

It has nothing to do with screen coordinates.  Just mouse wheel scrolling itself.   I hope the demo above helps to highlight what these commands are actually used for, for us.  Smile
Reply




Users browsing this thread: 2 Guest(s)