FPS camera issues

Return to Scripting Issues

FPS camera issues

Postby ctr » Sun Oct 30, 2011 6:30 pm

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:
Code: Select all
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.
ctr
 
Posts: 4
Joined: Sun Oct 30, 2011 6:17 pm

Postby ctr » Mon Oct 31, 2011 8:50 pm

Anyone?
ctr
 
Posts: 4
Joined: Sun Oct 30, 2011 6:17 pm

Postby hobbit » Mon Oct 31, 2011 9:50 pm

It's only been about 3 hours since you first posted. I am not sure about the number of people that use OSX that could help you either. Just give it some time and check back tomorrow.
hobbit
 
Posts: 199
Joined: Fri May 22, 2009 3:41 pm
Location: USA

Postby ctr » Mon Oct 31, 2011 11:10 pm

Actually its been a day and three hours, but I guess there aren't too many people using OSX.
ctr
 
Posts: 4
Joined: Sun Oct 30, 2011 6:17 pm

Postby drwr » Tue Nov 01, 2011 12:23 pm

I just pasted in your code and had no difficulty with the mouse button--clicking the mouse button repeatedly had no effect. Tell me more about the precise problem you are seeing. The camera moves to another position and gets stuck? As if, perhaps, the mouse data changed to something strange and stopped coming, maybe because MRelative mode got switched off or something? Try printing the numbers you are getting back from the mouse position every frame.

Incidentally, you should not import DirectStart if you are inheriting from ShowBase.

David
drwr
 
Posts: 11253
Joined: Fri Feb 13, 2004 12:42 pm
Location: Glendale, CA

Postby ctr » Tue Nov 01, 2011 3:21 pm

Thanks David. These are y relative mouse movement values with button clicks marked, although it is obvious something is wrong:

Code: Select all
300 ## not moving
300
300
300
300
300
300
301 ## moving mouse
301
305
308
310
310
309
305
309
308
308
306
304
304
302
301
301
300
-2147483648 ## mouse button click
300
300
300
300
300
-2147483648 ## mouse button click
300
300
300
300
300
300

Nevermind about it getting stuck, I can still move the mouse afterwards.
ctr
 
Posts: 4
Joined: Sun Oct 30, 2011 6:17 pm

Same Problem? Did anyone fix this?

Postby liltimtim » Fri Jul 06, 2012 2:11 am

Hello,
I hate to revive an old topic, but I too have ran into the issue with OSX and the clicking problem.

Both my trackpad (MacBook Pro Late 2009 Glass no button track bad) and Magic Mouse (multi touch mouse) cause the program to stop working (getting stuck)

the getP() seems to stop working but the heading continues to work just fine.

If you keep the mouse still and just click the Pitch seems to jump randomly in a circle (as in it rotates to some random values)

If anyone has a fix for this I would greatly appreciate this! Thanks!
liltimtim
 
Posts: 15
Joined: Mon Dec 27, 2010 11:52 pm

Postby liltimtim » Fri Jul 06, 2012 4:47 pm

A small discovery, I'm running OSX Lion so keep that in mind. When NOT in fullscreen mode, I can use MAbsolute and the problem is solved with the inverted/random clicking issue however, if I use fullscreen mode the mouse stops working entirely as in my program does not receive input from the mouse at all!
liltimtim
 
Posts: 15
Joined: Mon Dec 27, 2010 11:52 pm

Postby kurohyou » Wed Jul 11, 2012 10:21 pm

For what it's worth, RMB doesn't seem to work as expected either on OSX in the p3d environment. Might want to add this to the list...
kurohyou
 
Posts: 206
Joined: Tue Jun 29, 2010 9:59 pm

Postby liltimtim » Fri Jul 20, 2012 5:55 pm

What do you mean 'add it to the list'?
liltimtim
 
Posts: 15
Joined: Mon Dec 27, 2010 11:52 pm


Return to Scripting Issues

Who is online

Users browsing this forum: Google [Bot] and 1 guest