Im creating a simple space dog fighting game. im currently working on camera and ship controls i want the camera to follow the ship and also have its hpr be oriented to the ship. i figured reparenting the camera to the ship would do the trick but it puts it in the middle of the ship and i want it behind the ship and up a little so i added some camera.setX setY and setZ along with reparent to ship. it works but the camera either moves faster than the ship or the ship doesnt move. heres the code.
import direct.directbase.DirectStart
from pandac.PandaModules import *
from direct.gui.OnscreenText import OnscreenText
import sys
class Main(object):
def __init__(self):
base.disableMouse()
base.accept( "escape" , sys.exit)
self.loadPlayer = Player() # Calls up the Player() class
self.addlight() # load the light
def addlight(self): #create the light
ambientLight = AmbientLight('ambientLight')
ambientLight.setColor(Vec4(.5, .5, .5, 1))
ambientLightNP = render.attachNewNode(ambientLight)
render.setLight(ambientLightNP)
class Player(object): # controls the Player
speed = 5 # controls the speed of the ship
FORWARD = Vec3(0, 2, 0) # sets direction and speed of forward direction
STOP = Vec3(0, 0, 0)# sets the speed and direction of stop
walk = STOP # makes walk = stop
def __init__(self):
self.loadShip() #calls up the function to load the ship
self.loadWorld()
self.createControls()
self.setCamera()
taskMgr.add(self.updatePos, 'update position')
taskMgr.add(self.mouseUpdate, 'update mouse')
def loadShip(self): # just loads the ship model
self.ship = loader.loadModel('ship')
self.ship.reparentTo(render)
self.ship.setTwoSided(True)
def loadWorld(self): # loads world
self.world = loader.loadModel('models/box')
self.world.reparentTo(render)
self.world.setPos(-1, 30, 3)
def setCamera(self):
place = base.cam.node().getLens()
place.setFov(70)
base.cam.node().setLens(place)
def mouseUpdate(self,task):
""" this task updates the mouse """
md = base.win.getPointer(0)
x = md.getX()
y = md.getY()
if base.win.movePointer(0, base.win.getXSize()/2, base.win.getYSize()/2):
self.ship.setP(self.ship.getP() - (y - base.win.getYSize()/2)*0.1)
self.ship.setH(self.ship.getH() - (x - base.win.getXSize()/2)*0.1)
return task.cont
def createControls(self):
base.accept( "w" , self.__setattr__,["walk",self.FORWARD])
base.accept( "w-up" , self.__setattr__,["walk",self.STOP] )
def updatePos(self,task):
self.ship.setPos(self.ship,self.walk*globalClock.getDt()*self.speed)
base.camera.setZ(self.ship.getZ())
base.camera.setX(self.ship.getX())
base.camera.setY(self.ship.getY() - 10)
base.camera.reparentTo(self.ship)
return task.cont
Main()
run()
any help would be great!