Third Person View


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.

def SpinCameraTask(task):
    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)
    camZ = 20*math.sin(angleradians2)
    if camZ <= 1: camZ = 1
    elif camZ >= 10: camZ = 10*math.cos(angleradians2)*math.cos(angleradians1)), \
        (20*math.cos(angleradians2)*math.sin(angleradians1))+pandaposy, camZ+pandaposz)
    return Task.cont

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 …

So i made an cylinder

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
        #angleradians = agledegrees * (math.pi/180.0)
        angleradiansXY = X * math.pi
        angleradiansZ = Y * math.pi
        camZ = 20*math.sin(angleradiansZ)+5
        camX = 20*math.sin(angleradiansXY)
        camY = -20*math.cos(angleradiansXY)
        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, camY+actorPosY,camZ)
        #The camera looks at the dummyNode
    return task.cont

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:
if base.mouseWatcherNode.hasMouse() and not self.isPaused:
            if mPos[0]>=.997:
      , 2,
            elif mPos[0]<=-.999:
      , winX-2,

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.