# Third Person View

Hello.

I want to have a thrid person view.

I searched in this forum and found post that helped me a lot. I adapt it for my needs.

``````
global x, y
pandaposx = pandaActor.getX()
pandaposy = pandaActor.getY()
pandaposz = pandaActor.getZ()
dummyNode.setPos(pandaposx, pandaposy, pandaposz+5)
if base.mouseWatcherNode.hasMouse():
x = base.mouseWatcherNode.getMouseX()
y = base.mouseWatcherNode.getMouseY()
angledegrees1 = x * -180.0
angledegrees2 = y * -180.0
angleradians1 = angledegrees1 * (math.pi / 180.0)
angleradians2 = angledegrees2 * (math.pi / 180.0)
if camZ <= 1: camZ = 1
elif camZ >= 10: camZ = 10

base.camera.lookAt(dummyNode)

The problem with this function is that if i move the mouse on the top of the window the z-axis sinks. (I have no idea if this is the best way to explain my problem. English is not my mother tongue:) )
EDIT: I mean the position of z increases and decreases
z = 1 2 3 … 10 8 9 7 …

``````def anotherSpinCameraTask(task):
global X, Y, prevmouseX, prevmouseY
#Get the Actor position
actorPosX = actor.getX()
actorPosY = actor.getY()
actorPosZ = actor.getZ()
#set the position of the Dummy Node
# The Dummy Node has the same position as the actor but higher in the Z-axis
dummyNode.setPos(actorPosX, actorPosY, actorPosZ+5)
#Check if a mouse is inside the window
if base.mouseWatcherNode.hasMouse():
#get the Mouse position
mouseX = base.mouseWatcherNode.getMouseX()
mouseY = base.mouseWatcherNode.getMouseY()
#Calculate the differences of the previosu Mouse position and the new Mouse position
mouseXDiff = prevmouseX - mouseX
mouseYDiff = prevmouseY - mouseY
prevmouseX = mouseX
prevmouseY = mouseY
#Store the Mouse differences in X and Y
X += mouseXDiff
Y += mouseYDiff
#angledegrees = X * 180
if camZ <= actorPosZ: camZ = actorPosZ
if camZ >= 20: camZ = 20
#Set the position of the camera relative to the position of the actor
#Problem: If the mouse is not inside the window, then the camera will not move with the actor
base.camera.setPos(camX+actorPosX, camY+actorPosY,camZ)
#The camera looks at the dummyNode
base.camera.lookAt(dummyNode)

With this code it works good. But a new problem attract my attetion.
How can i catch the mouse in the window? Or how can i get an movement of the mouse when it is already on edge of the window (display)?

How can i catch the mouse in the window?
You can’t but you can set it to be in the center of the window every frame. When some one moves it you only get the change in mouse position and snap it back. Make sure you only do that when window is maximized or ill get to you!

Or how can i get an movement of the mouse when it is already on edge of the window (display)?
I don’t think you can can but first issue fixes it.

In the project i’m developing i found an ugly solution to “break” the screen borders: when the pointer is really near an edge, i put it at the other.
Some code:

``````winX=base.win.getXSize()
if base.mouseWatcherNode.hasMouse() and not self.isPaused:
mPos=base.mouseWatcherNode.getMouse()
if mPos[0]>=.997:
base.win.movePointer(0, 2, base.win.getPointer(0).getY())
elif mPos[0]<=-.999:
base.win.movePointer(0, winX-2,  base.win.getPointer(0).getY())
Hz=360*mPos[0]
Hy=45*mPos[1]``````

The limitations of this imlementation is that Hz must be mPos[0] by a multiple of 360 (else, the cam won’t do a full circle around the player).
I made it this way because i wanted to implement a cam similar to the one used in the Witcher game, and this code works pretty.
Please note the mPos[0]>=.997; initially i put .999, but i wasn’t working right!Don’t get why though.
I used a 2-pixel border from edges to make sure the pointer isn’t trapped in cycle of jumping left-right-left-right etcetera.