Hi everybody.
Im new here, new to coding, new to 3D.
I worked with GDevelop in 2D before, but found myself a little to limited, and ended up here.
I am trying to make a Submarine game, and struggle with, well, everything.
My first question here(for sure,not my last) is about my camera:
I try to get a smooth look when the player is turning, by positioning the camera in alternating X & Z positions, depending on direction, to have the movement emphathised (aka not just the stationary look at the back).
I somehow have it half working, but i fail to set the max. X/Z, the cam can move.
I tryed if self.camera.getX() > self.player.getX()+2 / if self.camera.getX() < self.player.getX()-2 and samefor Z, but somehow that locks my cam in only one direction, while the other is unlimited.
The code, i have so far:
class Game(ShowBase):
def __init__(self):
ShowBase.__init__(self)
self.lens = PerspectiveLens()
self.lens.setFov(60)
self.lens.setNear(0.01)
self.lens.setFar(1000.0)
self.cam.node().setLens(self.lens)
self.player = Actor('models/player')
self.player.reparentTo(render)
self.player.setScale(.2)
self.camera.reparentTo(self.player)
self.camera.setPos( 0, -10, 0)
self.floater = NodePath(PandaNode("floater"))
self.floater.reparentTo(self.player)
self.floater.setPos( 0, 5, 0)
self.disableMouse()
props = WindowProperties()
props.setCursorHidden(True)
self.win.requestProperties(props)
self.focus = LVector3(55, -55, 20)
self.heading = 180
self.pitch = 0
self.mousex = 0
self.mousey = 0
self.last = 0
self.mousebtn = [0, 0, 0]
taskMgr.add(self.controlCamera, "camera-task")
self.accept("escape", sys.exit, [0])
self.accept("mouse1", self.setMouseBtn, [0, 1])
self.accept("mouse1-up", self.setMouseBtn, [0, 0])
self.accept("mouse2", self.setMouseBtn, [1, 1])
self.accept("mouse2-up", self.setMouseBtn, [1, 0])
self.accept("mouse3", self.setMouseBtn, [2, 1])
self.accept("mouse3-up", self.setMouseBtn, [2, 0])
def setMouseBtn(self, btn, value):
self.mousebtn[btn] = value
def controlCamera(self, task):
# figure out how much the mouse has moved (in pixels)
angleDegrees = task.time * 6.0
delta = globalClock.getDt()
md = self.win.getPointer(0)
x = md.getX()
y = md.getY()
if self.win.movePointer(0, 100, 100):
self.heading = self.heading - (x - 100) * 0.2
self.pitch = self.pitch - (y - 100) * 0.2
self.camera.lookAt (self.floater)
self.player.setHpr(self.heading, self.pitch, 0)
dir = self.player.getMat().getRow3(1)
elapsed = task.time - self.last
if self.last == 0:
elapsed = 0
if self.mousebtn[0]:
self.focus = self.focus + dir * elapsed * 30
if self.mousebtn[1] or self.mousebtn[2]:
self.focus = self.focus - dir * elapsed * 30
self.player.setPos(self.focus - (dir * 5))
dir = self.player.getMat().getRow3(1)
elapsed = task.time - self.last
if self.last == 0:
elapsed = 0
if self.mousebtn[0]:
self.focus = self.focus + dir * elapsed * 30
if self.mousebtn[1] or self.mousebtn[2]:
self.focus = self.focus - dir * elapsed * 30
self.player.setPos(self.focus - (dir * 5))
self.focus = self.player.getPos() + (dir * 5)
"""troublemaker: if i do not include the if not statement, it works pretty well,
but the cam can run away after too many turns. but if i use this statement,
the cam gets locked in one direction, while the other still runs away!"""
if not self.camera.getX() < self.player.getX()-2:
if x < 100:
self.camera.setX(self.camera.getX()-5*delta)
if not self.camera.getX() > self.player.getX()+2:
if x > 100:
self.camera.setX(self.camera.getX()+5*delta)
if y < 100:
self.camera.setZ(self.camera.getZ()+5*delta)
if y > 100:
self.camera.setZ(self.camera.getZ()-5*delta)
self.last = task.time
return Task.cont
game = Game()
game.run()
i have to admit, that i still dont fully understand how the postioning here works for the player, i just copyed the cameracontrol from one of the examples.
I tryed
if self.camera.getX() > self.player.getX()-2:
if x < 100:
self.camera.setX(self.camera.getX()-5*delta)
wich, of cause has the same result, as the if not statement
what am i missing?