[Solved] Mouse speed and model follow

Ok so I currently have my character mapped to my mouse for movement. It’s a top-down game for simplicity.

The problem is. If I move the mouse really fast, the character can zoom across the screen.

Is there any way I can limit the character’s move speed?

Btw, there’s 4 mice so keep that in mind when looking at the code I’m using.

def update(self, task):
        if(self.gameEnded == False):
            for i in range(self.miceNum):
                # if there has been movement then get the movement
                if base.pointerWatcherNodes[i+1].hasMouse():
                    mX = base.pointerWatcherNodes[i+1].getMouseX()
                    mY = base.pointerWatcherNodes[i+1].getMouseY()
                    mX = 0
                    mY = 0
                self.movePlayer(i+1, mX, mY)
                mX2 = base.win.getPointer(i+1).getX()
                mY2 = base.win.getPointer(i+1).getY()
                if(i+1 == 1 or i+1 == 3):
                    if(mX2 < -30):
                        base.win.movePointer(i+1, -30, mY2)
                    if(mX2 > 400):
                        base.win.movePointer(i+1, 400, mY2)
                    if(mY2 < 90):
                        base.win.movePointer(i+1, mX2, 90)
                    if(mY2 > 470):
                        base.win.movePointer(i+1, mX2, 470)
                    if(mX2 < 400):
                        base.win.movePointer(i+1, 400, mY2)
                    if(mX2 > 830):
                        base.win.movePointer(i+1, 830, mY2)
                    if(mY2 < 90):
                        base.win.movePointer(i+1, mX2, 90)
                    if(mY2 > 470):
                        base.win.movePointer(i+1, mX2, 470)
                self.mouseX1 = base.win.getPointer(1).getX()
                self.mouseY1 = base.win.getPointer(1).getY()
                self.mouseX2 = base.win.getPointer(2).getX()
                self.mouseY2 = base.win.getPointer(2).getY()
                self.mouseX3 = base.win.getPointer(3).getX()
                self.mouseY3 = base.win.getPointer(3).getY()
                self.mouseX4 = base.win.getPointer(4).getX()
                self.mouseY4 = base.win.getPointer(4).getY()
            return Task.cont

Well, the simplest thing that comes to mind offhand is, instead of simply moving the object to the mouse’s mew coordinates, to move the object by at most a certain amount in the direction that the mouse moved.

I would suggest something like this:

  • Keep the last position of the mouse.
  • Each update, find the vector given by the difference of the current mouse position and the last mouse position.
  • If this has a length larger than the desired maximum movement, scale it down to that length
  • Move the object by the vector (that is, the object’s new position is the object’s old position plus the vector).
  • Set the mouse cursor’s position to the object’s position (this could perhaps be conditional on the original length of the vector).

If you find that offsetting the object in this way incurs too much inaccuracy, you can always set the object’s position to the mouse position in those cases in which the vector is not longer than the maximum desired offset.

You might also want to take time-since-last-frame into account with this method, specifically in determining the maximum allowed movement for the current update; after all, this maximum should surely be less when less time has elapsed between updates, not so?

Another, similar idea is to do much the same, but move the mouse to the object’s position plus the vector and then set the object’s position, instead of the other way around.

Worked well thanks.

My pleasure. :slight_smile:

As it happens, I’ve decided that a project of mine could benefit from this technique too, so I think that I’ll be implementing it too. :wink: