Hey everyone, so I am currently attempting a third person camera movement with mouse scrolling inputs and I am hoping some people could give some suggestions here.
To preface this post, I want to say that I have looked into multiple older forums posts here for suggestions but I cannot find a solution to the current problem I seem to be having, so perhaps an alternative way to code this would be beneficial. However, if you do know of a post/code snippet that would help, do not hesitate to post it! Additionally, I will first explain what my code is doing for a TLDR format and then also post my code below for others that want to take a deep dive into this. I am grateful for any and all suggestions and critiques!
Code Intention:
Basically, I am viewing my person from a third-person view with a dummy node positioned at the base of the character. When I move my mouse to the left or to the right, I want the to update the dummy node and so, the camera (the child node) to turn accordingly and obviously stop when the mouse stops, with the mouse always being at the center of the screen. That way it never leaves the screen and my character can turn indefinitely.
Code Application:
What the code currently does is it finds the center of the screen and saves that as a variable. Then, whenever it detects movement in the positive or negative direction from the center, it will rotate the dummy node, thus rotating my camera, and then at the end of the frame, it will restore the mouse pointer to the center of the screen.
Code Issue:
While it will turn, the turning is extremely choppy. So lets say I update the rotation by 1 degree, when it recognizes the mouse movement, this makes the turn only update my 1 degree each frame, then resets the mouse pointer, making the movement extremely slow.
Now, if I try and change it by more than 1 degree, if will be extremely choppy/jump and give no room for accuracy in a third person shooter e.g. I move left and my cursor/crosshairs jump way past the target.
So then I thought, āwell, what if I use a counter, and once it recognizes, say left movement, it continues to turn left through 15 frames, i.e. my counter set to 15.ā Well, as you would expect, although this makes the camera fluid, it also means I have to āwaitā until 15 frames until the camera comes to a stop. No good.
So my last thought was to make a loop that goes from X to a certain amount of degree turn to make it more fluid. But then again, if I execute the loop, it still goes through a single frame and is the same as just adding, say 15 degrees like setH(getH + 15)
Code:
#Initialized at the start of the program
self.MouseCounter = 3
self.LookLeft = False
self.LookRight = False
self.MouseSensitivity = 10
X = int(base.win.getXSize()/2)
Y = int(base.win.getYSize()/2)
base.win.movePointer(0, X, Y)
self.mouseCenter = [X,Y]
#Code Being updated:
if base.mouseWatcherNode.hasMouse():
XDiff = base.mouseWatcherNode.getMouseX()
#This is just for updating movement for more than one frame but is currently not in use
if XDiff == 0:
self.MouseCounter += 1
if self.MouseCounter > 1:
self.MouseCounter = 0
self.LookRight = False
self.LookLeft = False
if XDiff < 0:
self.LookLeft = True
self.LookRight = False
base.win.movePointer(0, self.mouseCenter[0], self.mouseCenter[1])
if XDiff > 0:
self.LookRight = True
self.LookLeft = False
base.win.movePointer(0, self.mouseCenter[0], self.mouseCenter[1])
if self.LookLeft:
self.dummyNode.setH(self.dummyNode.getH()+1)
self.Orientation = self.dummyNode.getH()
self.RealAxisX = sin(self.Orientation*pi/180+pi)
self.RealAxisY = cos(self.Orientation*pi/180)
self.RealAxisX2 = sin((self.Orientation+90)*pi/180+pi)
self.RealAxisY2 = cos((self.Orientation+90)*pi/180)
if self.LookRight:
self.dummyNode.setH(self.dummyNode.getH()-1)
self.Orientation = self.dummyNode.getH()
self.RealAxisX = sin(self.Orientation*pi/180+pi)
self.RealAxisY = cos(self.Orientation*pi/180)
self.RealAxisX2 = sin((self.Orientation+90)*pi/180+pi)
self.RealAxisY2 = cos((self.Orientation+90)*pi/180)
return Task.cont
Hereās a quick video of whatās happening: