Math's Trig Versus Basic's Trig Functions
#42
Well I think I learned something here. Working with the angular formula and mouse coordinates I was able to make a mouse mimic a joystick.

Hold right button to down to move in the direction of the mouse pointer or click left button to just move all at once.

Code: (Select All)
yy = 3: xx = 1
LOCATE yy, xx: PRINT "*";
DO
    _LIMIT 60
    WHILE _MOUSEINPUT: WEND
    mx = _MOUSEX
    my = _MOUSEY
    lb = _MOUSEBUTTON(1)
    rb = _MOUSEBUTTON(2)
    b$ = INKEY$
    IF LEN(b$) THEN
        SELECT CASE b$
            CASE CHR$(0) + "K"
                IF POS(0) > 1 THEN LOCATE , POS(0) - 1
            CASE CHR$(0) + "M"
                IF POS(0) < _WIDTH THEN LOCATE , POS(0) + 1
            CASE CHR$(0) + "H"
                IF CSRLIN > 1 THEN LOCATE CSRLIN - 1, POS(0)
            CASE CHR$(0) + "P"
                IF CSRLIN < _HEIGHT THEN LOCATE CSRLIN + 1, POS(0)
        END SELECT
    END IF
    IF ABS(z1 - TIMER) > .2 THEN
        z1 = TIMER
        IF rb THEN
            ' Angle formula by bplus ------------------------------
            cx = POS(0): cy = CSRLIN
            stepX = ABS(cx - mx): stepY = ABS(cy - my)

            dAng = INT(_R2D(_ATAN2(my - cy, mx - cx)) + .5)
            IF dAng < 0 THEN dAng = dAng + 360

            IF dAng <= 90 THEN
                startA = 0: endA = dAng: ra = dAng
            ELSEIF dAng <= 180 THEN
                startA = dAng: endA = 180: ra = 90 - (dAng - 90)
            ELSEIF dAng <= 270 THEN
                startA = 180: endA = dAng: ra = dAng - 180
            ELSEIF dAng <= 360 THEN
                startA = dAng: endA = 360: ra = 90 - (dAng - 270)
            END IF
            '-------------------------------------------------------
            m_y = 0: m_x = 0
            IF ra <= 90 AND ra >= 50 THEN
                IF my > CSRLIN THEN
                    x$ = "down": m_y = 1: m_x = 0
                ELSE
                    x$ = "up": m_y = -1: m_x = 0
                END IF
            ELSEIF ra < 50 AND ra >= 15 THEN
                IF mx > POS(0) AND my > CSRLIN THEN
                    x$ = "down right": m_y = 1: m_x = 2
                ELSEIF mx < POS(0) AND my > CSRLIN THEN
                    x$ = "down left": m_y = 1: m_x = -2
                ELSEIF mx > POS(0) AND my < CSRLIN THEN
                    x$ = "up right": m_y = -1: m_x = 2
                ELSEIF mx < POS(0) AND my < CSRLIN THEN
                    x$ = "up left": m_y = -1: m_x = -2
                END IF
            ELSEIF ra < 15 AND ra >= 0 THEN
                IF mx > POS(0) THEN
                    x$ = "right": m_y = 0: m_x = 2
                ELSE
                    x$ = "left": m_y = 0: m_x = -2
                END IF
            END IF
            y2 = CSRLIN: x2 = POS(0): LOCATE 1, 1: PRINT x1; " "; x2; "  "; ra; "  "; x$; "              ";: LOCATE y2, x2
            LOCATE yy, xx: PRINT " ";: yy = yy + m_y: xx = xx + m_x: LOCATE yy, xx: PRINT "*";
        END IF
    END IF
    IF lb THEN LOCATE yy, xx: PRINT " ";: LOCATE my, mx: PRINT "*";: yy = my: xx = mx
LOOP


I'm not sure I have the exact degrees worked out, but it seems to be pretty close.

Pete
Reply


Messages In This Thread
RE: Math's Trig Versus Basic's Trig Functions - by Pete - 10-08-2022, 04:33 AM



Users browsing this thread: 10 Guest(s)