Hi, I’m trying to make an FPS style camera in panda, and everything seems to be going well except that whenever I click any mouse button, the camera moves to another spot and gets stuck, even though I’m using no mouse button events. I am assuming this is becuse I am on OSX and base.win.movePointer() requires MRelative to be on, because if I turn it off, the mouse button doesn’t do anything. I’m not sure why this is, and if anyone using OSX can tell me if it’s happening with them or fix it, that would be great. Here is my code, it uses the camera and a basic environment:
from direct.showbase.ShowBase import ShowBase
import direct.directbase.DirectStart
from pandac.PandaModules import *
from direct.gui.OnscreenText import OnscreenText
from direct.gui.OnscreenImage import OnscreenImage
from direct.actor.Actor import Actor
from direct.interval.IntervalGlobal import Sequence
from direct.filter.CommonFilters import CommonFilters
from panda3d.core import *
from math import *
import sys
class Game(object, ShowBase):
def __init__(self):
ShowBase.__init__(self)
base.accept( "escape" , sys.exit)
props = WindowProperties()
props.setCursorHidden(True)
base.disableMouse()
props.setMouseMode(WindowProperties.MRelative)
base.win.requestProperties(props)
# self.crosshair = OnscreenImage(image = 'gfx/crosshair.png', scale = (.03125,1,.03125))
# self.crosshair.setTransparency(TransparencyAttrib.MAlpha)
player = Player()
OnscreenText(text="The Game", style=1, fg=(1,1,1,1),pos=(1.3,-0.95), align=TextNode.ARight, scale = .07)
self.environ = self.loader.loadModel("models/environment")
self.environ.reparentTo(self.render)
self.environ.setScale(0.25, 0.25, 0.25)
self.environ.setPos(-8, 42, 0)
self.pandaActor = Actor("models/panda-model",{"walk": "models/panda-walk4"})
self.pandaActor.setScale(0.005,0.005,0.005)
self.pandaActor.reparentTo(self.render)
self.pandaActor.loop("walk")
pandaPos1 = self.pandaActor.posInterval(13, Point3(0, -10, 0), startPos = Point3(0, 10, 0))
pandaPos2 = self.pandaActor.posInterval(13, Point3(0, 10, 0))
pandaHpr1 = self.pandaActor.hprInterval(3, Point3(180, 0, 0), startHpr = Point3(0, 0, 0))
pandaHpr2 = self.pandaActor.hprInterval(3, Point3(0, 0, 0), startHpr = Point3(180, 0, 0))
self.pandaPace = Sequence(pandaPos1,pandaHpr1,pandaPos2,pandaHpr2,name="pandaPace")
self.pandaPace.loop()
class Player(object):
speed = 110
FORWARD = Vec3(0,2,0)
BACK = Vec3(0,-1,0)
LEFT = Vec3(-1,0,0)
RIGHT = Vec3(1,0,0)
STOP = Vec3(0)
walk = STOP
strafe = STOP
readyToJump = False
jump = 0
global playerNode
playerNode = NodePath('player')
def __init__(self):
self.loadModel()
self.setUpCamera()
self.attachControls()
taskMgr.add(self.mouseUpdate, 'mouse-task')
taskMgr.add(self.moveUpdate, 'move-task')
ItemHandling(playerNode)
def loadModel(self):
playerNode.reparentTo(render)
playerNode.setPos(0,0,4)
playerNode.setScale(.05)
def setUpCamera(self):
pl = base.cam.node().getLens()
pl.setFov(70)
base.cam.node().setLens(pl)
base.camera.reparentTo(playerNode)
def attachControls(self):
base.accept( "s" , self.__setattr__,["walk",self.STOP] )
base.accept( "w" , self.__setattr__,["walk",self.FORWARD])
base.accept( "s" , self.__setattr__,["walk",self.BACK] )
base.accept( "s-up" , self.__setattr__,["walk",self.STOP] )
base.accept( "w-up" , self.__setattr__,["walk",self.STOP] )
base.accept( "a" , self.__setattr__,["strafe",self.LEFT])
base.accept( "d" , self.__setattr__,["strafe",self.RIGHT] )
base.accept( "a-up" , self.__setattr__,["strafe",self.STOP] )
base.accept( "d-up" , self.__setattr__,["strafe",self.STOP] )
def mouseUpdate(self,task):
md = base.win.getPointer(0)
x = md.getX()
y = md.getY()
cameray = base.camera.getP() - (y - base.win.getYSize()/2)*0.12
if base.win.movePointer(0, base.win.getXSize()/2, base.win.getYSize()/2):
playerNode.setH(playerNode.getH() - (x - base.win.getXSize()/2)*0.12)
if cameray<-80:
cameray = -80
if cameray>90:
cameray = 90
base.camera.setP(cameray)
return task.cont
def moveUpdate(self,task):
playerNode.setPos(playerNode,self.walk*globalClock.getDt()*self.speed)
playerNode.setPos(playerNode,self.strafe*globalClock.getDt()*self.speed)
return task.cont
class ItemHandling(object):
def __init__(self, playerNode):
taskMgr.add(self.setItemPosition, 'ItemPosition')
def setItemPosition(self, task):
return task.cont
Game()
run()
Thanks in advance.