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.
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
base.camera.setPos(pandaposx+(20*math.cos(angleradians2)*math.cos(angleradians1)), \
(20*math.cos(angleradians2)*math.sin(angleradians1))+pandaposy, camZ+pandaposz)
base.camera.lookAt(dummyNode)
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
base.camera.setPos(camX+actorPosX, camY+actorPosY,camZ)
#The camera looks at the dummyNode
base.camera.lookAt(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)?