switching walkers to use inputState

This commit is contained in:
Dave Schuyler 2003-11-07 08:31:37 +00:00
parent 1ddbac215a
commit f86733e241
3 changed files with 359 additions and 342 deletions

View File

@ -1,5 +1,10 @@
"""instantiate global Messenger object""" """instantiate global Messenger object"""
import Messenger import Messenger
import InputState
messenger = Messenger.Messenger() messenger = Messenger.Messenger()
# inputState is an optional add-on for the messenger, and
# that is why it is created here (See Also: InputState.py):
inputState = InputState.InputState()

View File

@ -26,16 +26,16 @@ class NonPhysicsWalker(DirectObject.DirectObject):
# special methods # special methods
def __init__(self): def __init__(self):
DirectObject.DirectObject.__init__(self) DirectObject.DirectObject.__init__(self)
self.forwardButton=0 #self.forwardButton=0
self.reverseButton=0 #self.reverseButton=0
self.jumpButton=0 #self.jumpButton=0
self.leftButton=0 #self.leftButton=0
self.rightButton=0 #self.rightButton=0
self.speed=0.0 self.speed=0.0
self.rotationSpeed=0.0 self.rotationSpeed=0.0
self.vel=Vec3(0.0, 0.0, 0.0) self.vel=Vec3(0.0, 0.0, 0.0)
self.stopThisFrame = 0 self.stopThisFrame = 0
self.fSlide = 0 #self.fSlide = 0
def setWalkSpeed(self, forward, jump, reverse, rotate): def setWalkSpeed(self, forward, jump, reverse, rotate):
assert(self.debugPrint("setWalkSpeed()")) assert(self.debugPrint("setWalkSpeed()"))
@ -62,24 +62,24 @@ class NonPhysicsWalker(DirectObject.DirectObject):
# Set up the collision sphere # Set up the collision sphere
# This is a sphere on the ground to detect barrier collisions # This is a sphere on the ground to detect barrier collisions
self.cSphere = CollisionSphere(0.0, 0.0, 0.0, avatarRadius) self.cSphere = CollisionSphere(0.0, 0.0, 0.0, avatarRadius)
self.cSphereNode = CollisionNode('cSphereNode') cSphereNode = CollisionNode('cSphereNode')
self.cSphereNode.addSolid(self.cSphere) cSphereNode.addSolid(self.cSphere)
self.cSphereNodePath = avatarNodePath.attachNewNode(self.cSphereNode) self.cSphereNodePath = avatarNodePath.attachNewNode(cSphereNode)
self.cSphereBitMask = wallCollideMask self.cSphereBitMask = wallCollideMask
self.cSphereNode.setFromCollideMask(self.cSphereBitMask) cSphereNode.setFromCollideMask(self.cSphereBitMask)
self.cSphereNode.setIntoCollideMask(BitMask32.allOff()) cSphereNode.setIntoCollideMask(BitMask32.allOff())
# Set up the collison ray # Set up the collison ray
# This is a ray cast from your head down to detect floor polygons # This is a ray cast from your head down to detect floor polygons
# A toon is about 4.0 feet high, so start it there # A toon is about 4.0 feet high, so start it there
self.cRay = CollisionRay(0.0, 0.0, 4.0, 0.0, 0.0, -1.0) self.cRay = CollisionRay(0.0, 0.0, 4.0, 0.0, 0.0, -1.0)
self.cRayNode = CollisionNode('cRayNode') cRayNode = CollisionNode('cRayNode')
self.cRayNode.addSolid(self.cRay) cRayNode.addSolid(self.cRay)
self.cRayNodePath = avatarNodePath.attachNewNode(self.cRayNode) self.cRayNodePath = avatarNodePath.attachNewNode(cRayNode)
self.cRayBitMask = floorCollideMask self.cRayBitMask = floorCollideMask
self.cRayNode.setFromCollideMask(self.cRayBitMask) cRayNode.setFromCollideMask(self.cRayBitMask)
self.cRayNode.setIntoCollideMask(BitMask32.allOff()) cRayNode.setIntoCollideMask(BitMask32.allOff())
# set up wall collision mechanism # set up wall collision mechanism
self.pusher = CollisionHandlerPusher() self.pusher = CollisionHandlerPusher()
@ -111,12 +111,10 @@ class NonPhysicsWalker(DirectObject.DirectObject):
del self.cTrav del self.cTrav
del self.cSphere del self.cSphere
del self.cSphereNode
self.cSphereNodePath.removeNode() self.cSphereNodePath.removeNode()
del self.cSphereNodePath del self.cSphereNodePath
del self.cRay del self.cRay
del self.cRayNode
self.cRayNodePath.removeNode() self.cRayNodePath.removeNode()
del self.cRayNodePath del self.cRayNodePath
@ -125,8 +123,8 @@ class NonPhysicsWalker(DirectObject.DirectObject):
def collisionsOff(self): def collisionsOff(self):
assert(self.debugPrint("collisionsOff")) assert(self.debugPrint("collisionsOff"))
self.cTrav.removeCollider(self.cSphereNode) self.cTrav.removeCollider(self.cSphereNodePath)
self.cTrav.removeCollider(self.cRayNode) self.cTrav.removeCollider(self.cRayNodePath)
# Now that we have disabled collisions, make one more pass # Now that we have disabled collisions, make one more pass
# right now to ensure we aren't standing in a wall. # right now to ensure we aren't standing in a wall.
@ -134,8 +132,8 @@ class NonPhysicsWalker(DirectObject.DirectObject):
def collisionsOn(self): def collisionsOn(self):
assert(self.debugPrint("collisionsOn")) assert(self.debugPrint("collisionsOn"))
self.cTrav.addCollider(self.cSphereNode, self.pusher) self.cTrav.addCollider(self.cSphereNodePath, self.pusher)
self.cTrav.addCollider(self.cRayNode, self.lifter) self.cTrav.addCollider(self.cRayNodePath, self.lifter)
def oneTimeCollide(self): def oneTimeCollide(self):
""" """
@ -144,24 +142,31 @@ class NonPhysicsWalker(DirectObject.DirectObject):
have been disabled. have been disabled.
""" """
tempCTrav = CollisionTraverser() tempCTrav = CollisionTraverser()
tempCTrav.addCollider(self.cSphereNode, self.pusher) tempCTrav.addCollider(self.cSphereNodePath, self.pusher)
tempCTrav.addCollider(self.cRayNode, self.lifter) tempCTrav.addCollider(self.cRayNodePath, self.lifter)
tempCTrav.traverse(render) tempCTrav.traverse(render)
def handleAvatarControls(self, task): def handleAvatarControls(self, task):
""" """
Check on the arrow keys and update the avatar. Check on the arrow keys and update the avatar.
""" """
# get the button states:
forward = inputState.isSet("forward")
reverse = inputState.isSet("reverse")
turnLeft = inputState.isSet("turnLeft")
turnRight = inputState.isSet("turnRight")
slide = inputState.isSet("slide")
#jump = inputState.isSet("jump")
# Determine what the speeds are based on the buttons: # Determine what the speeds are based on the buttons:
self.speed=(self.forwardButton and self.avatarControlForwardSpeed or self.speed=(forward and self.avatarControlForwardSpeed or
self.reverseButton and -self.avatarControlReverseSpeed) reverse and -self.avatarControlReverseSpeed)
# Should fSlide be renamed slideButton? # Should fSlide be renamed slideButton?
self.slideSpeed=self.fSlide and ( self.slideSpeed=slide and (
(self.leftButton and -self.avatarControlForwardSpeed) or (turnLeft and -self.avatarControlForwardSpeed) or
(self.rightButton and self.avatarControlForwardSpeed)) (turnRight and self.avatarControlForwardSpeed))
self.rotationSpeed=not self.fSlide and ( self.rotationSpeed=not slide and (
(self.leftButton and self.avatarControlRotateSpeed) or (turnLeft and self.avatarControlRotateSpeed) or
(self.rightButton and -self.avatarControlRotateSpeed)) (turnRight and -self.avatarControlRotateSpeed))
# How far did we move based on the amount of time elapsed? # How far did we move based on the amount of time elapsed?
dt=min(ClockObject.getGlobalClock().getDt(), 0.1) dt=min(ClockObject.getGlobalClock().getDt(), 0.1)
# Check to see if we're moving at all: # Check to see if we're moving at all:
@ -197,23 +202,23 @@ class NonPhysicsWalker(DirectObject.DirectObject):
""" """
assert(self.debugPrint("enableAvatarControls")) assert(self.debugPrint("enableAvatarControls"))
print id(self), "NPW.enableAvatarControls()" print id(self), "NPW.enableAvatarControls()"
self.accept("control-arrow_left", self.moveTurnLeft, [1]) #self.accept("control-arrow_left", self.moveTurnLeft, [1])
self.accept("control-arrow_left-up", self.moveTurnLeft, [0]) #self.accept("control-arrow_left-up", self.moveTurnLeft, [0])
self.accept("control-arrow_right", self.moveTurnRight, [1]) #self.accept("control-arrow_right", self.moveTurnRight, [1])
self.accept("control-arrow_right-up", self.moveTurnRight, [0]) #self.accept("control-arrow_right-up", self.moveTurnRight, [0])
self.accept("control-arrow_up", self.moveForward, [1]) #self.accept("control-arrow_up", self.moveForward, [1])
self.accept("control-arrow_up-up", self.moveForward, [0]) #self.accept("control-arrow_up-up", self.moveForward, [0])
self.accept("control-arrow_down", self.moveInReverse, [1]) #self.accept("control-arrow_down", self.moveInReverse, [1])
self.accept("control-arrow_down-up", self.moveInReverse, [0]) #self.accept("control-arrow_down-up", self.moveInReverse, [0])
self.accept("arrow_left", self.moveTurnLeft, [1]) #self.accept("arrow_left", self.moveTurnLeft, [1])
self.accept("arrow_left-up", self.moveTurnLeft, [0]) #self.accept("arrow_left-up", self.moveTurnLeft, [0])
self.accept("arrow_right", self.moveTurnRight, [1]) #self.accept("arrow_right", self.moveTurnRight, [1])
self.accept("arrow_right-up", self.moveTurnRight, [0]) #self.accept("arrow_right-up", self.moveTurnRight, [0])
self.accept("arrow_up", self.moveForward, [1]) #self.accept("arrow_up", self.moveForward, [1])
self.accept("arrow_up-up", self.moveForward, [0]) #self.accept("arrow_up-up", self.moveForward, [0])
self.accept("arrow_down", self.moveInReverse, [1]) #self.accept("arrow_down", self.moveInReverse, [1])
self.accept("arrow_down-up", self.moveInReverse, [0]) #self.accept("arrow_down-up", self.moveInReverse, [0])
self.collisionsOn() self.collisionsOn()
@ -228,89 +233,89 @@ class NonPhysicsWalker(DirectObject.DirectObject):
Ignore the arrow keys, etc. Ignore the arrow keys, etc.
""" """
assert(self.debugPrint("disableAvatarControls")) assert(self.debugPrint("disableAvatarControls"))
print id(self), "NPW.enableAvatarControls()" print id(self), "NPW.disableAvatarControls()"
taskName = "AvatarControls%s"%(id(self),) taskName = "AvatarControls%s"%(id(self),)
taskMgr.remove(taskName) taskMgr.remove(taskName)
self.ignore("control") #self.ignore("control")
self.ignore("control-up") #self.ignore("control-up")
self.ignore("control-arrow_left") #self.ignore("control-arrow_left")
self.ignore("control-arrow_left-up") #self.ignore("control-arrow_left-up")
self.ignore("control-arrow_right") #self.ignore("control-arrow_right")
self.ignore("control-arrow_right-up") #self.ignore("control-arrow_right-up")
self.ignore("control-arrow_up") #self.ignore("control-arrow_up")
self.ignore("control-arrow_up-up") #self.ignore("control-arrow_up-up")
self.ignore("control-arrow_down") #self.ignore("control-arrow_down")
self.ignore("control-arrow_down-up") #self.ignore("control-arrow_down-up")
self.ignore("arrow_left") #self.ignore("arrow_left")
self.ignore("arrow_left-up") #self.ignore("arrow_left-up")
self.ignore("arrow_right") #self.ignore("arrow_right")
self.ignore("arrow_right-up") #self.ignore("arrow_right-up")
self.ignore("arrow_up") #self.ignore("arrow_up")
self.ignore("arrow_up-up") #self.ignore("arrow_up-up")
self.ignore("arrow_down") #self.ignore("arrow_down")
self.ignore("arrow_down-up") #self.ignore("arrow_down-up")
self.collisionsOff() self.collisionsOff()
# reset state # reset state
self.moveTurnLeft(0) #self.moveTurnLeft(0)
self.moveTurnRight(0) #self.moveTurnRight(0)
self.moveForward(0) #self.moveForward(0)
self.moveInReverse(0) #self.moveInReverse(0)
self.moveJumpLeft(0) #self.moveJumpLeft(0)
self.moveJumpRight(0) #self.moveJumpRight(0)
self.moveJumpForward(0) #self.moveJumpForward(0)
self.moveJumpInReverse(0) #self.moveJumpInReverse(0)
self.moveJump(0) #self.moveJump(0)
def moveTurnLeft(self, isButtonDown): #def moveTurnLeft(self, isButtonDown):
self.leftButton=isButtonDown # self.leftButton=isButtonDown
#
#def moveTurnRight(self, isButtonDown):
# self.rightButton=isButtonDown
#
#def moveForward(self, isButtonDown):
# self.forwardButton=isButtonDown
def moveTurnRight(self, isButtonDown): #def moveInReverse(self, isButtonDown):
self.rightButton=isButtonDown # self.reverseButton=isButtonDown
#
#def moveJumpLeft(self, isButtonDown):
# self.jumpButton=isButtonDown
# self.leftButton=isButtonDown
#
#def moveJumpRight(self, isButtonDown):
# self.jumpButton=isButtonDown
# self.rightButton=isButtonDown
#
#def moveJumpForward(self, isButtonDown):
# self.jumpButton=isButtonDown
# self.forwardButton=isButtonDown
#
#def moveJumpInReverse(self, isButtonDown):
# self.jumpButton=isButtonDown
# self.reverseButton=isButtonDown
#
#def moveJump(self, isButtonDown):
# self.jumpButton=isButtonDown
#
#def toggleSlide(self):
# self.fSlide = not self.fSlide
def moveForward(self, isButtonDown): #def enableSlideMode(self):
self.forwardButton=isButtonDown # self.accept("control-up", self.toggleSlide)
#
def moveInReverse(self, isButtonDown): #def disableSlideMode(self):
self.reverseButton=isButtonDown # self.fSlide = 0
# self.ignore("control-up")
def moveJumpLeft(self, isButtonDown): #
self.jumpButton=isButtonDown #def slideLeft(self, isButtonDown):
self.leftButton=isButtonDown # self.slideLeftButton=isButtonDown
#
def moveJumpRight(self, isButtonDown): #def slideRight(self, isButtonDown):
self.jumpButton=isButtonDown # self.slideRightButton=isButtonDown
self.rightButton=isButtonDown
def moveJumpForward(self, isButtonDown):
self.jumpButton=isButtonDown
self.forwardButton=isButtonDown
def moveJumpInReverse(self, isButtonDown):
self.jumpButton=isButtonDown
self.reverseButton=isButtonDown
def moveJump(self, isButtonDown):
self.jumpButton=isButtonDown
def toggleSlide(self):
self.fSlide = not self.fSlide
def enableSlideMode(self):
self.accept("control-up", self.toggleSlide)
def disableSlideMode(self):
self.fSlide = 0
self.ignore("control-up")
def slideLeft(self, isButtonDown):
self.slideLeftButton=isButtonDown
def slideRight(self, isButtonDown):
self.slideRightButton=isButtonDown
if __debug__: if __debug__:
def debugPrint(self, message): def debugPrint(self, message):

View File

@ -46,16 +46,16 @@ class PhysicsWalker(DirectObject.DirectObject):
self.__oldContact=None self.__oldContact=None
self.__oldPosDelta=Vec3(0) self.__oldPosDelta=Vec3(0)
self.__oldDt=0 self.__oldDt=0
self.__forwardButton=0 #self.__forwardButton=0
self.__reverseButton=0 #self.__reverseButton=0
self.__jumpButton=0 #self.__jumpButton=0
self.__leftButton=0 #self.__leftButton=0
self.__rightButton=0 #self.__rightButton=0
self.__speed=0.0 self.__speed=0.0
self.__rotationSpeed=0.0 self.__rotationSpeed=0.0
self.__slideSpeed=0.0 self.__slideSpeed=0.0
self.__vel=Vec3(0.0) self.__vel=Vec3(0.0)
self.__slideButton = 0 #self.__slideButton = 0
self.isAirborne = 0 self.isAirborne = 0
self.highMark = 0 self.highMark = 0
@ -129,13 +129,13 @@ class PhysicsWalker(DirectObject.DirectObject):
# Set up the collision sphere # Set up the collision sphere
# This is a sphere on the ground to detect barrier collisions # This is a sphere on the ground to detect barrier collisions
self.cSphere = CollisionSphere(0.0, 0.0, avatarRadius, avatarRadius) self.cSphere = CollisionSphere(0.0, 0.0, avatarRadius, avatarRadius)
self.cSphereNode = CollisionNode('cSphereNode') cSphereNode = CollisionNode('cSphereNode')
self.cSphereNode.addSolid(self.cSphere) cSphereNode.addSolid(self.cSphere)
self.cSphereNodePath = avatarNodePath.attachNewNode(self.cSphereNode) self.cSphereNodePath = avatarNodePath.attachNewNode(cSphereNode)
self.cSphereBitMask = wallBitmask|floorBitmask self.cSphereBitMask = wallBitmask|floorBitmask
self.cSphereNode.setFromCollideMask(self.cSphereBitMask) cSphereNode.setFromCollideMask(self.cSphereBitMask)
self.cSphereNode.setIntoCollideMask(BitMask32.allOff()) cSphereNode.setIntoCollideMask(BitMask32.allOff())
# set up collision mechanism # set up collision mechanism
self.pusher = PhysicsCollisionHandler() self.pusher = PhysicsCollisionHandler()
@ -192,7 +192,7 @@ class PhysicsWalker(DirectObject.DirectObject):
# activate the collider with the traverser and pusher # activate the collider with the traverser and pusher
self.collisionsOn() self.collisionsOn()
self.pusher.addColliderNode(self.cSphereNode, avatarNodePath.node()) self.pusher.addCollider(self.cSphereNodePath, avatarNodePath)
self.avatarNodePath = avatarNodePath self.avatarNodePath = avatarNodePath
@ -259,7 +259,6 @@ class PhysicsWalker(DirectObject.DirectObject):
del self.cTrav del self.cTrav
del self.cSphere del self.cSphere
del self.cSphereNode
self.cSphereNodePath.removeNode() self.cSphereNodePath.removeNode()
del self.cSphereNodePath del self.cSphereNodePath
@ -267,14 +266,14 @@ class PhysicsWalker(DirectObject.DirectObject):
def collisionsOff(self): def collisionsOff(self):
assert(self.debugPrint("collisionsOff()")) assert(self.debugPrint("collisionsOff()"))
self.cTrav.removeCollider(self.cSphereNode) self.cTrav.removeCollider(self.cSphereNodePath)
# Now that we have disabled collisions, make one more pass # Now that we have disabled collisions, make one more pass
# right now to ensure we aren't standing in a wall. # right now to ensure we aren't standing in a wall.
self.oneTimeCollide() self.oneTimeCollide()
def collisionsOn(self): def collisionsOn(self):
assert(self.debugPrint("collisionsOn()")) assert(self.debugPrint("collisionsOn()"))
self.cTrav.addCollider(self.cSphereNode, self.pusher) self.cTrav.addCollider(self.cSphereNodePath, self.pusher)
def oneTimeCollide(self): def oneTimeCollide(self):
""" """
@ -284,7 +283,7 @@ class PhysicsWalker(DirectObject.DirectObject):
""" """
assert(self.debugPrint("oneTimeCollide()")) assert(self.debugPrint("oneTimeCollide()"))
tempCTrav = CollisionTraverser() tempCTrav = CollisionTraverser()
tempCTrav.addCollider(self.cSphereNode, self.pusher) tempCTrav.addCollider(self.cSphereNodePath, self.pusher)
tempCTrav.traverse(render) tempCTrav.traverse(render)
def handleAvatarControls(self, task): def handleAvatarControls(self, task):
@ -309,15 +308,22 @@ class PhysicsWalker(DirectObject.DirectObject):
self.avatarNodePath.setZ(50.0) self.avatarNodePath.setZ(50.0)
self.resetPhys() self.resetPhys()
# get the button states:
forward = inputState.isSet("forward")
reverse = inputState.isSet("reverse")
turnLeft = inputState.isSet("turnLeft")
turnRight = inputState.isSet("turnRight")
slide = inputState.isSet("slide")
jump = inputState.isSet("jump")
# Determine what the speeds are based on the buttons: # Determine what the speeds are based on the buttons:
self.__speed=(self.__forwardButton and self.avatarControlForwardSpeed or self.__speed=(forward and self.avatarControlForwardSpeed or
self.__reverseButton and -self.avatarControlReverseSpeed) reverse and -self.avatarControlReverseSpeed)
self.__slideSpeed=self.__slideButton and ( self.__slideSpeed=slide and (
(self.__leftButton and -self.avatarControlForwardSpeed) or (turnLeft and -self.avatarControlForwardSpeed) or
(self.__rightButton and self.avatarControlForwardSpeed)) (turnRight and self.avatarControlForwardSpeed))
self.__rotationSpeed=not self.__slideButton and ( self.__rotationSpeed=not slide and (
(self.__leftButton and self.avatarControlRotateSpeed) or (turnLeft and self.avatarControlRotateSpeed) or
(self.__rightButton and -self.avatarControlRotateSpeed)) (turnRight and -self.avatarControlRotateSpeed))
# How far did we move based on the amount of time elapsed? # How far did we move based on the amount of time elapsed?
dt=min(ClockObject.getGlobalClock().getDt(), 0.1) dt=min(ClockObject.getGlobalClock().getDt(), 0.1)
@ -432,15 +438,15 @@ class PhysicsWalker(DirectObject.DirectObject):
messenger.send("jumpLand") messenger.send("jumpLand")
self.priorParent.setVector(Vec3.zero()) self.priorParent.setVector(Vec3.zero())
self.isAirborne = 0 self.isAirborne = 0
elif self.__jumpButton: elif jump:
#print "jump" #print "jump"
self.__jumpButton=0 #self.__jumpButton=0
messenger.send("jumpStart") messenger.send("jumpStart")
jump=Vec3(contact+Vec3.up()) jumpVec=Vec3(contact+Vec3.up())
#jump=Vec3(rotAvatarToPhys.xform(jump)) #jumpVec=Vec3(rotAvatarToPhys.xform(jumpVec))
jump.normalize() jumpVec.normalize()
jump*=self.avatarControlJumpForce jumpVec*=self.avatarControlJumpForce
physObject.addImpulse(Vec3(jump)) physObject.addImpulse(Vec3(jumpVec))
self.isAirborne = 1 # Avoid double impulse before fully airborne. self.isAirborne = 1 # Avoid double impulse before fully airborne.
onScreenDebug.add("isAirborne", "%d"%(self.isAirborne,)) onScreenDebug.add("isAirborne", "%d"%(self.isAirborne,))
else: else:
@ -459,9 +465,9 @@ class PhysicsWalker(DirectObject.DirectObject):
messenger.send("jumpHardLand") messenger.send("jumpHardLand")
else: else:
messenger.send("jumpLand") messenger.send("jumpLand")
elif self.__jumpButton: elif jump:
self.jumpCount+=1 self.jumpCount+=1
self.__jumpButton=0 #self.__jumpButton=0
messenger.send("jumpStart") messenger.send("jumpStart")
jump=Vec3(contact+Vec3.up()) jump=Vec3(contact+Vec3.up())
#jump=Vec3(rotAvatarToPhys.xform(jump)) #jump=Vec3(rotAvatarToPhys.xform(jump))
@ -524,103 +530,104 @@ class PhysicsWalker(DirectObject.DirectObject):
self.actorNode.setContactVector(Vec3.zero()) self.actorNode.setContactVector(Vec3.zero())
return Task.cont return Task.cont
def handleAvatarControls_wip(self, task): #def handleAvatarControls_wip(self, task):
""" # """
Check on the arrow keys and update the avatar. # Check on the arrow keys and update the avatar.
""" # """
#assert(self.debugPrint("handleAvatarControls(task=%s)"%(task,))) # #assert(self.debugPrint("handleAvatarControls(task=%s)"%(task,)))
physObject=self.actorNode.getPhysicsObject() # physObject=self.actorNode.getPhysicsObject()
#rotAvatarToPhys=Mat3.rotateMatNormaxis(-self.avatarNodePath.getH(), Vec3.up()) # #rotAvatarToPhys=Mat3.rotateMatNormaxis(-self.avatarNodePath.getH(), Vec3.up())
#rotPhysToAvatar=Mat3.rotateMatNormaxis(self.avatarNodePath.getH(), Vec3.up()) # #rotPhysToAvatar=Mat3.rotateMatNormaxis(self.avatarNodePath.getH(), Vec3.up())
contact=self.actorNode.getContactVector() # contact=self.actorNode.getContactVector()
#
# hack fix for falling through the floor: # # hack fix for falling through the floor:
if contact==Vec3.zero() and self.avatarNodePath.getZ()<-50.0: # if contact==Vec3.zero() and self.avatarNodePath.getZ()<-50.0:
# reset: # # reset:
self.avatarNodePath.setPos(Vec3(0.0, 0.0, 20.0)) # self.avatarNodePath.setPos(Vec3(0.0, 0.0, 20.0))
#
# Determine what the speeds are based on the buttons: # # Determine what the speeds are based on the buttons:
self.__speed=(self.__forwardButton and self.avatarControlForwardSpeed or # buttons = inputState.state
self.__reverseButton and -self.avatarControlReverseSpeed) # self.__speed=(buttons["forward"] and self.avatarControlForwardSpeed or
self.__slideSpeed=self.__slideButton and ( # self.__reverseButton and -self.avatarControlReverseSpeed)
(self.__leftButton and -self.avatarControlForwardSpeed) or # self.__slideSpeed=self.__slideButton and (
(self.__rightButton and self.avatarControlForwardSpeed)) # (self.__leftButton and -self.avatarControlForwardSpeed) or
self.__rotationSpeed=not self.__slideButton and ( # (self.__rightButton and self.avatarControlForwardSpeed))
(self.__leftButton and self.avatarControlRotateSpeed) or # self.__rotationSpeed=not self.__slideButton and (
(self.__rightButton and -self.avatarControlRotateSpeed)) # (inputState.isSet("turnLeft") and self.avatarControlRotateSpeed) or
# How far did we move based on the amount of time elapsed? # (self.__rightButton and -self.avatarControlRotateSpeed))
dt=min(ClockObject.getGlobalClock().getDt(), 0.1) # # How far did we move based on the amount of time elapsed?
# dt=min(ClockObject.getGlobalClock().getDt(), 0.1)
doPhysics=1 #
if not contact.almostEqual(Vec3.zero()): # doPhysics=1
contactLength = contact.length() # if not contact.almostEqual(Vec3.zero()):
contact.normalize() # contactLength = contact.length()
angle=contact.dot(Vec3.up()) # contact.normalize()
if angle>self.__standableGround: # angle=contact.dot(Vec3.up())
# ...avatar is on standable ground. # if angle>self.__standableGround:
#print "standableGround" # # ...avatar is on standable ground.
if self.__oldContact==Vec3.zero(): # #print "standableGround"
if contactLength>self.__hardLandingForce: # if self.__oldContact==Vec3.zero():
# ...avatar was airborne. # if contactLength>self.__hardLandingForce:
messenger.send("jumpHardLand") # # ...avatar was airborne.
else: # messenger.send("jumpHardLand")
messenger.send("jumpLand") # else:
if self.__jumpButton: # messenger.send("jumpLand")
self.__jumpButton=0 # if self.__jumpButton:
messenger.send("jumpStart") # self.__jumpButton=0
jump=Vec3(contact+Vec3.up()) # messenger.send("jumpStart")
#jump=Vec3(rotAvatarToPhys.xform(jump)) # jump=Vec3(contact+Vec3.up())
jump.normalize() # #jump=Vec3(rotAvatarToPhys.xform(jump))
jump*=self.avatarControlJumpForce # jump.normalize()
physObject.addImpulse(Vec3(jump)) # jump*=self.avatarControlJumpForce
else: # physObject.addImpulse(Vec3(jump))
physObject.setVelocity(Vec3(0.0)) # else:
self.__vel.set(0.0, 0.0, 0.0) # physObject.setVelocity(Vec3(0.0))
doPhysics=0 # self.__vel.set(0.0, 0.0, 0.0)
if contact!=self.__oldContact: # doPhysics=0
# We must copy the vector to preserve it: # if contact!=self.__oldContact:
self.__oldContact=Vec3(contact) # # We must copy the vector to preserve it:
#print "doPhysics", doPhysics # self.__oldContact=Vec3(contact)
#print "contact", contact # #print "doPhysics", doPhysics
if doPhysics: # #print "contact", contact
self.phys.doPhysics(dt) # if doPhysics:
# Check to see if we're moving at all: # self.phys.doPhysics(dt)
if self.__speed or self.__slideSpeed or self.__rotationSpeed: # # Check to see if we're moving at all:
distance = dt * self.__speed # if self.__speed or self.__slideSpeed or self.__rotationSpeed:
slideDistance = dt * self.__slideSpeed # distance = dt * self.__speed
rotation = dt * self.__rotationSpeed # slideDistance = dt * self.__slideSpeed
# rotation = dt * self.__rotationSpeed
#debugTempH=self.avatarNodePath.getH() #
assert self.avatarNodePath.getHpr().almostEqual(physObject.getOrientation().getHpr(), 0.0001) # #debugTempH=self.avatarNodePath.getH()
assert self.avatarNodePath.getPos().almostEqual(physObject.getPosition(), 0.0001) # assert self.avatarNodePath.getHpr().almostEqual(physObject.getOrientation().getHpr(), 0.0001)
# assert self.avatarNodePath.getPos().almostEqual(physObject.getPosition(), 0.0001)
# update pos: #
# Take a step in the direction of our previous heading. # # update pos:
self.__vel=Vec3(Vec3.forward() * distance + # # Take a step in the direction of our previous heading.
Vec3.right() * slideDistance) # self.__vel=Vec3(Vec3.forward() * distance +
# rotMat is the rotation matrix corresponding to # Vec3.right() * slideDistance)
# our previous heading. # # rotMat is the rotation matrix corresponding to
rotMat=Mat3.rotateMatNormaxis(self.avatarNodePath.getH(), Vec3.up()) # # our previous heading.
step=rotMat.xform(self.__vel) # rotMat=Mat3.rotateMatNormaxis(self.avatarNodePath.getH(), Vec3.up())
physObject.setPosition(Point3( # step=rotMat.xform(self.__vel)
physObject.getPosition()+step)) # physObject.setPosition(Point3(
# update hpr: # physObject.getPosition()+step))
o=physObject.getOrientation() # # update hpr:
r=LOrientationf() # o=physObject.getOrientation()
r.setHpr(Vec3(rotation, 0.0, 0.0)) # r=LOrientationf()
physObject.setOrientation(o*r) # r.setHpr(Vec3(rotation, 0.0, 0.0))
# sync the change: # physObject.setOrientation(o*r)
self.actorNode.updateTransform() # # sync the change:
# self.actorNode.updateTransform()
assert self.avatarNodePath.getHpr().almostEqual(physObject.getOrientation().getHpr(), 0.0001) #
assert self.avatarNodePath.getPos().almostEqual(physObject.getPosition(), 0.0001) # assert self.avatarNodePath.getHpr().almostEqual(physObject.getOrientation().getHpr(), 0.0001)
#assert self.avatarNodePath.getH()==debugTempH-rotation # assert self.avatarNodePath.getPos().almostEqual(physObject.getPosition(), 0.0001)
messenger.send("avatarMoving") # #assert self.avatarNodePath.getH()==debugTempH-rotation
else: # messenger.send("avatarMoving")
self.__vel.set(0.0, 0.0, 0.0) # else:
# Clear the contact vector so we can tell if we contact something next frame: # self.__vel.set(0.0, 0.0, 0.0)
self.actorNode.setContactVector(Vec3.zero()) # # Clear the contact vector so we can tell if we contact something next frame:
return Task.cont # self.actorNode.setContactVector(Vec3.zero())
# return Task.cont
def doDeltaPos(self): def doDeltaPos(self):
assert(self.debugPrint("doDeltaPos()")) assert(self.debugPrint("doDeltaPos()"))
@ -648,11 +655,11 @@ class PhysicsWalker(DirectObject.DirectObject):
self.highMark = 0 self.highMark = 0
self.actorNode.setContactVector(Vec3.zero()) self.actorNode.setContactVector(Vec3.zero())
def getForwardButton(self): #def getForwardButton(self):
return self.__forwardButton # return self.__forwardButton
def getReverseButton(self): #def getReverseButton(self):
return self.__reverseButton # return self.__reverseButton
def enableAvatarControls(self): def enableAvatarControls(self):
""" """
@ -660,25 +667,25 @@ class PhysicsWalker(DirectObject.DirectObject):
""" """
assert(self.debugPrint("enableAvatarControls()")) assert(self.debugPrint("enableAvatarControls()"))
print id(self), "PW.enableAvatarControls()" print id(self), "PW.enableAvatarControls()"
self.accept("control", self.moveJump, [1]) #self.accept("control", self.moveJump, [1])
self.accept("control-up", self.moveJump, [0]) #self.accept("control-up", self.moveJump, [0])
self.accept("control-arrow_left", self.moveJumpLeft, [1]) #self.accept("control-arrow_left", self.moveJumpLeft, [1])
self.accept("control-arrow_left-up", self.moveJumpLeft, [0]) #self.accept("control-arrow_left-up", self.moveJumpLeft, [0])
self.accept("control-arrow_right", self.moveJumpRight, [1]) #self.accept("control-arrow_right", self.moveJumpRight, [1])
self.accept("control-arrow_right-up", self.moveJumpRight, [0]) #self.accept("control-arrow_right-up", self.moveJumpRight, [0])
self.accept("control-arrow_up", self.moveJumpForward, [1]) #self.accept("control-arrow_up", self.moveJumpForward, [1])
self.accept("control-arrow_up-up", self.moveJumpForward, [0]) #self.accept("control-arrow_up-up", self.moveJumpForward, [0])
self.accept("control-arrow_down", self.moveJumpInReverse, [1]) #self.accept("control-arrow_down", self.moveJumpInReverse, [1])
self.accept("control-arrow_down-up", self.moveJumpInReverse, [0]) #self.accept("control-arrow_down-up", self.moveJumpInReverse, [0])
#
self.accept("arrow_left", self.moveTurnLeft, [1]) #self.accept("arrow_left", self.moveTurnLeft, [1])
self.accept("arrow_left-up", self.moveTurnLeft, [0]) #self.accept("arrow_left-up", self.moveTurnLeft, [0])
self.accept("arrow_right", self.moveTurnRight, [1]) #self.accept("arrow_right", self.moveTurnRight, [1])
self.accept("arrow_right-up", self.moveTurnRight, [0]) #self.accept("arrow_right-up", self.moveTurnRight, [0])
self.accept("arrow_up", self.moveForward, [1]) #self.accept("arrow_up", self.moveForward, [1])
self.accept("arrow_up-up", self.moveForward, [0]) #self.accept("arrow_up-up", self.moveForward, [0])
self.accept("arrow_down", self.moveInReverse, [1]) #self.accept("arrow_down", self.moveInReverse, [1])
self.accept("arrow_down-up", self.moveInReverse, [0]) #self.accept("arrow_down-up", self.moveInReverse, [0])
self.collisionsOn() self.collisionsOn()
@ -706,25 +713,25 @@ class PhysicsWalker(DirectObject.DirectObject):
taskName = "AvatarControlsIndicator%s"%(id(self),) taskName = "AvatarControlsIndicator%s"%(id(self),)
taskMgr.remove(taskName) taskMgr.remove(taskName)
self.ignore("control") #self.ignore("control")
self.ignore("control-up") #self.ignore("control-up")
self.ignore("control-arrow_left") #self.ignore("control-arrow_left")
self.ignore("control-arrow_left-up") #self.ignore("control-arrow_left-up")
self.ignore("control-arrow_right") #self.ignore("control-arrow_right")
self.ignore("control-arrow_right-up") #self.ignore("control-arrow_right-up")
self.ignore("control-arrow_up") #self.ignore("control-arrow_up")
self.ignore("control-arrow_up-up") #self.ignore("control-arrow_up-up")
self.ignore("control-arrow_down") #self.ignore("control-arrow_down")
self.ignore("control-arrow_down-up") #self.ignore("control-arrow_down-up")
#
self.ignore("arrow_left") #self.ignore("arrow_left")
self.ignore("arrow_left-up") #self.ignore("arrow_left-up")
self.ignore("arrow_right") #self.ignore("arrow_right")
self.ignore("arrow_right-up") #self.ignore("arrow_right-up")
self.ignore("arrow_up") #self.ignore("arrow_up")
self.ignore("arrow_up-up") #self.ignore("arrow_up-up")
self.ignore("arrow_down") #self.ignore("arrow_down")
self.ignore("arrow_down-up") #self.ignore("arrow_down-up")
self.collisionsOff() self.collisionsOff()
@ -733,60 +740,60 @@ class PhysicsWalker(DirectObject.DirectObject):
self.ignore("f3") self.ignore("f3")
# reset state # reset state
self.moveTurnLeft(0) #self.moveTurnLeft(0)
self.moveTurnRight(0) #self.moveTurnRight(0)
self.moveForward(0) #self.moveForward(0)
self.moveInReverse(0) #self.moveInReverse(0)
self.moveJumpLeft(0) #self.moveJumpLeft(0)
self.moveJumpRight(0) #self.moveJumpRight(0)
self.moveJumpForward(0) #self.moveJumpForward(0)
self.moveJumpInReverse(0) #self.moveJumpInReverse(0)
self.moveJump(0) #self.moveJump(0)
self.moveSlide(0) #self.moveSlide(0)
def moveTurnLeft(self, isButtonDown): #def moveTurnLeft(self, isButtonDown):
assert(self.debugPrint("moveTurnLeft(isButtonDown=%s)"%(isButtonDown,))) # assert(self.debugPrint("moveTurnLeft(isButtonDown=%s)"%(isButtonDown,)))
self.__leftButton=isButtonDown # self.__leftButton=isButtonDown
#
#def moveTurnRight(self, isButtonDown):
# assert(self.debugPrint("moveTurnRight(isButtonDown=%s)"%(isButtonDown,)))
# self.__rightButton=isButtonDown
#
#def moveForward(self, isButtonDown):
# assert(self.debugPrint("moveForward(isButtonDown=%s)"%(isButtonDown,)))
# self.__forwardButton=isButtonDown
#
#def moveInReverse(self, isButtonDown):
# assert(self.debugPrint("moveInReverse(isButtonDown=%s)"%(isButtonDown,)))
# self.__reverseButton=isButtonDown
#
#def moveJumpLeft(self, isButtonDown):
# assert(self.debugPrint("moveJumpLeft(isButtonDown=%s)"%(isButtonDown,)))
# self.__jumpButton=isButtonDown
# self.__leftButton=isButtonDown
def moveTurnRight(self, isButtonDown): #def moveJumpRight(self, isButtonDown):
assert(self.debugPrint("moveTurnRight(isButtonDown=%s)"%(isButtonDown,))) # assert(self.debugPrint("moveJumpRight(isButtonDown=%s)"%(isButtonDown,)))
self.__rightButton=isButtonDown # self.__jumpButton=isButtonDown
# self.__rightButton=isButtonDown
def moveForward(self, isButtonDown): #def moveJumpForward(self, isButtonDown):
assert(self.debugPrint("moveForward(isButtonDown=%s)"%(isButtonDown,))) # assert(self.debugPrint("moveJumpForward(isButtonDown=%s)"%(isButtonDown,)))
self.__forwardButton=isButtonDown # self.__jumpButton=isButtonDown
# self.__forwardButton=isButtonDown
def moveInReverse(self, isButtonDown): #def moveJumpInReverse(self, isButtonDown):
assert(self.debugPrint("moveInReverse(isButtonDown=%s)"%(isButtonDown,))) # assert(self.debugPrint("moveJumpInReverse(isButtonDown=%s)"%(isButtonDown,)))
self.__reverseButton=isButtonDown # self.__jumpButton=isButtonDown
# self.__reverseButton=isButtonDown
def moveJumpLeft(self, isButtonDown): #def moveJump(self, isButtonDown):
assert(self.debugPrint("moveJumpLeft(isButtonDown=%s)"%(isButtonDown,))) # assert(self.debugPrint("moveJump()"))
self.__jumpButton=isButtonDown # self.__jumpButton=isButtonDown
self.__leftButton=isButtonDown #
#def moveSlide(self, isButtonDown):
def moveJumpRight(self, isButtonDown): # assert(self.debugPrint("moveSlide(isButtonDown=%s)"%(isButtonDown,)))
assert(self.debugPrint("moveJumpRight(isButtonDown=%s)"%(isButtonDown,))) # self.__slideButton=isButtonDown
self.__jumpButton=isButtonDown
self.__rightButton=isButtonDown
def moveJumpForward(self, isButtonDown):
assert(self.debugPrint("moveJumpForward(isButtonDown=%s)"%(isButtonDown,)))
self.__jumpButton=isButtonDown
self.__forwardButton=isButtonDown
def moveJumpInReverse(self, isButtonDown):
assert(self.debugPrint("moveJumpInReverse(isButtonDown=%s)"%(isButtonDown,)))
self.__jumpButton=isButtonDown
self.__reverseButton=isButtonDown
def moveJump(self, isButtonDown):
assert(self.debugPrint("moveJump()"))
self.__jumpButton=isButtonDown
def moveSlide(self, isButtonDown):
assert(self.debugPrint("moveSlide(isButtonDown=%s)"%(isButtonDown,)))
self.__slideButton=isButtonDown
if __debug__: if __debug__:
def debugPrint(self, message): def debugPrint(self, message):