### 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
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849`` ``````#ifndef DEFS_H #define DEFS_H #include #include #include #include #include #include #include #include #include #include #include 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
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465`` ``````#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
 ``123456789101112131415161718`` ``````#include #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
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293`` ``````#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.