Without too much logic.
from direct.showbase.ShowBase import ShowBase
class Game(ShowBase):
def __init__(self):
ShowBase.__init__(self)
base.disableMouse()
self.mouse_sens = 0.05
scene = self.loader.loadModel("models/environment")
scene.reparentTo(render)
self.taskMgr.add(self.update, "update")
def update(self, task):
md = base.win.getPointer(0)
x = md.getX()
y = md.getY()
if base.win.movePointer(0, base.win.getXSize()//2, base.win.getYSize()//2):
camera.setH(camera.getH() - (x - base.win.getXSize()/2)*self.mouse_sens)
camera.setP(camera.getP() - (y - base.win.getYSize()/2)*self.mouse_sens)
return task.cont
game = Game()
game.run()
3 Likes
Thanks for the snippet!
I was about to create a thread to ask how to do so, and you already gave the code.
Thanks again!
EDIT: Is there any way that we can disconnect the mouse from the camera view?
There is no concept to disconnect the mouse from the camera, you can do it simply, either update or don’t update the position and rotation. Or stop performing the update task.
One could perhaps add a flag that determined whether the camera’s orientation should be updated. I could see that being useful when doing things like operating menus.
More generally, this is a nice, elegantly simple example of operating a camera, I think!
I got it.
self.accept("some key", self.toggleCamera)
def toggleCamera(self):
self.camera_disconnected = not self.camera_disconnected
# In the task
def camera_view(self, task):
if not camera_disconnected:
# Serega-kkz's code
1 Like
In fact, the flag idea would be a good one if you have a global update method for everything. And you want to insert a camera update into it.
However, if you have allocated a separate task for this, of course, it makes sense to do so. To save CPU resources.
from direct.showbase.ShowBase import ShowBase
class Game(ShowBase):
def __init__(self):
ShowBase.__init__(self)
base.disableMouse()
self.camera_status = False
self.mouse_sens = 0.05
scene = self.loader.loadModel("models/environment")
scene.reparentTo(render)
self.accept("space", self.camera_state)
def camera_state(self):
if self.camera_status:
taskMgr.remove('camera_update')
else:
base.win.movePointer(0, base.win.getXSize()//2, base.win.getYSize()//2)
taskMgr.add(self.update, "camera_update")
self.camera_status = not self.camera_status
def update(self, task):
md = base.win.getPointer(0)
x = md.getX()
y = md.getY()
if base.win.movePointer(0, base.win.getXSize()//2, base.win.getYSize()//2):
camera.setH(camera.getH() - (x - base.win.getXSize()/2)*self.mouse_sens)
camera.setP(camera.getP() - (y - base.win.getYSize()/2)*self.mouse_sens)
return task.cont
game = Game()
game.run()
2 Likes
I took the liberty of adding another line to keep the camera from twitching towards the cursor position. Previously, this was a big problem for me, but it turned out to be simple.
2 Likes
I am having an issue where the camera is not moving up and down not sure if that is the bug you fixed tho
If you noticed, I added a button to simulate entering and exiting the menu. Press the key space and the camera will move, then press the key space again to disable the camera update.
1 Like