Mouse Movement Looping

Hi guys,
My problem seems to be a weird one, for I can't find it anywhere on the web. My problem is that I am trying to make it
so the mouse loops to the opposite side of the display when it reaches the edge of said display. For example,
if the mouse cursor was supposed to reach a Y position of -1, I want the mouse to loop to the other side of the display, in this case,
the bottom. I also need this to be translated as a linear movement, in other words, I don't the cursor movement to be translated as
up then INSTANT down then up again. I want a smooth up motion. I can make the linear movement, I just need the looping part. I have realized that this problem might also be with the OS, and my OS is Xubuntu 12.04 LTS I am also using QT 4.8 if that means anything.

Thank you in advance!
Last edited on
It's Friday and I'm not quite understanding your problem, but might it be something with your weird-on? I had one of those once: It really hurt! :)
I fixed the typo, now I feel pretty dumb.

An example of my problem is when you move your mouse cursor to the edge of your display, it stops right? I want to make it so the mouse continues to move that direction but is loops to the opposite side of the display.
For instance: If you move your mouse cursor left to right, when you hit the right-edge I want the cursor to "loop" to the left edge then continue to move left-to-right. I want this to happen indefinitely.
Let's look at some code...
Here is the code:
defs.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#ifndef DEFS_H
#define DEFS_H

#include <QMainWindow>
#include <QTimer>
#include <QLabel>
#include <QSlider>
#include <QtGui>
#include <math.h>
#include <vector>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;

#define DIM(x) (sizeof(x)/sizeof(*(x))) //This is a MACRO used to get the size of an array in memory.

enum EDirection
{
    EDirection_None,
    EDirection_Up,
    EDirection_Down,
    EDirection_Left,
    EDirection_Right
};

enum EGameState
{
    EGameState_Menu,
    EGameState_Playing,
    EGameState_Paused
};

struct SQuad
{
    float x1;
    float y1;
    float x2;
    float y2;
};

struct SVec2D
{
    float nAngle;
    float nMagnitude;
};

#endif // DEFS_H 


mainwindow.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include "defs.h"

class CMainWindow : public QMainWindow
{
    Q_OBJECT

public:
    CMainWindow();
    ~CMainWindow();

protected:
    // Re-implemented QT virtual functions.
    void paintEvent(QPaintEvent *);
    void resizeEvent(QResizeEvent *r);
    void keyPressEvent(QKeyEvent *k);
    void keyReleaseEvent(QKeyEvent *);
    void mousePressEvent(QMouseEvent *);
    void mouseMoveEvent(QMouseEvent *m);
    void mouseReleaseEvent(QMouseEvent *);

private slots:
    void Update();
    void ChangeMouse(int aValue);
    void ChangeSpeed(int aValue);

private:
    int Calculate();
    void AI();
    void DrawHUD();
    bool PointInQuad(float aX, float aY, SQuad aQuad);
    void Move();
    void SetupPause();
    void MouseMove();

    // Native C/C++ member types ;; NO CLASSES.
    float mnMouseSpeed; // This variable is used to keep track of the mouse speed.
    bool mbMouseClick; // This bit is set when the mouse button has been clicked.
    float mnBallSpeed; // This variable directly varies with the mouse speed.
    int miPrevMouseY; // The previous Y-position of the mouse cursor
    int miMouseY; // The current Y-position of the mouse cursor

    // Vectors of dynamic objects objects.
    SVec2D mqBall;
    SVec2D mqPlayer;
    SVec2D mqOpponent;

    // Native QT member types ;; CLASSES.
    QWidget *mpWid; // This is the Client Area Widget.
    QVBoxLayout *mpLay; // This is the game layout.
    QWidget *mpGl;
    QTimer *mpFrameTimer; // At each timeout() game is updated.
    QLabel *mpPlyrScoreLbl;
    QLabel *mpOppScoreLbl;

    // These objects are for the settings/pause screen.
    QVBoxLayout *mpPauseLayout; // This object describes the layout of the pause screen.
    QLabel *mpMouseSliderLbl;
    QLabel *mpMouseSpeedLbl;
    QSlider *mpMouseSpeed; // This slider object is used to control the mouse speed.
};

#endif // MAINWINDOW_H 


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
    // Seed the random number generator.
    srand(time(0));

    // Create the QApplication Object.
    QApplication app(argc, argv);

    //Create the MainWindow Object then show it.
    CMainWindow w;
    w.show();

    //Run the QApplication object, then return it's error code (if any).
    return app.exec();
}
mainwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "mainwindow.h"

..........

void CMainWindow::mousePressEvent(QMouseEvent *)
{
    mbMouseClick = true;
    setCursor(QCursor(Qt::BlankCursor));
}

void CMainWindow::mouseMoveEvent(QMouseEvent *m)
{
    if(mbMouseClick)
    {
        miMouseY = m->y();
        MouseMove();
        miPrevMouseY = m->y();
    }
}

void CMainWindow::mouseReleaseEvent(QMouseEvent *)
{
    mbMouseClick = false;
    setCursor(QCursor(Qt::ArrowCursor));
}

void CMainWindow::MouseMove()
{
    // Only move if the GameState is set to EGameState_Playing.
    if(g_eState == EGameState_Playing)
    {
        // Determine the hieght of the player's quad.
        float nHeight = g_qPlayer.y2 - g_qPlayer.y1;

        // Determine the length of the movement by the mouse.
        float nLength = 0.0f;

        if(miPrevMouseY == miMouseY)
        {
//            mqPlayer.nAngle = 0.0f;
            mqPlayer.nMagnitude = 0.0f;
        }

        else if(miPrevMouseY > miMouseY)
        {
            nLength = miPrevMouseY - miMouseY;

            // Now move according to the scale factor.
            nLength *= (mnMouseSpeed / 100.0f);

            g_qPlayer.y1 -= nLength;
            g_qPlayer.y2 = g_qPlayer.y1 + nHeight;

            // Set the Vector.
            mqPlayer.nMagnitude = (nLength / 10);
//            mqPlayer.nAngle = 270;
        }

        else if(miPrevMouseY < miMouseY)
        {
            nLength = miMouseY - miPrevMouseY;
            mqPlayer.nMagnitude = (nLength / 10);

            // Now move according to the scale factor.
            nLength *= (mnMouseSpeed / 100.0f);

            g_qPlayer.y1 += nLength;
            g_qPlayer.y2 = g_qPlayer.y1 + nHeight;

            // Set the Vector.
            mqPlayer.nMagnitude = (nLength / 10);
//            mqPlayer.nAngle = 90;
        }

        // Now do bounds checking.
        if(g_qPlayer.y1 <= (float)(mpGl->y() + 1.0f))
        {
            // Set the position of the player.
            g_qPlayer.y1 = (mpGl->y() + 1.0f);
            g_qPlayer.y2 = (mpGl->y() + 1.0f) + nHeight;
        }

        else if(g_qPlayer.y2 >= (float)(mpGl->size().height() - 1.0f))
        {
            // Set the position of the player.
            g_qPlayer.y1 = (mpGl->size().height() - 1.0f) - nHeight;
            g_qPlayer.y2 = (mpGl->size().height() - 1.0f);
        }
    }
}

..........


I can't place the whole file here, it is too big.
Sorry about the stoopid comment (I was in nine-year-old mode.)

In your example, you're saying that the cursor is moving upwards and is at position zero, the next value of y would be -1, and you want it to now be at the bottom of the screen, so y should be set to whatever the maximum value of y should be? Now I'm in 57-year-old-man mode and I don't understand the "smooth up motion" part, nor the "looping" part. Can you please explain that so an old man can understand?
Topic archived. No new replies allowed.