Okay so I’ve been reading the user contributed tutorial “Room Walking”. I’ve used his code and modified it a lot and incorporated his techniques into my own code.
Okay here is my question: on his code
def walkTask( self, task ): # accept a task as an argument
# ok now for some math particulary trigonometry
dist = 1.0 # the hypotenuse defined by r = sqrt( a^2 + b^2 )
# here we get the current heading of tiny and convert that degree measurement
# to radians because the math.sin/cos function needs it to be radians
angle = self.tiny.getH( ) * math.pi / 180.0 # convert degrees to radians
correction = math.pi / 2 # ninety degrees in radians
# this is needed for the cofunction formulas
# in trigonometry x is defined as x = r * cos( angle )
# y is defined as y = r * sin( angle )
# remember that tiny was turned 180 degrees? And would now be facing
# 270 degrees (3pi/2 for radians) on the unit circle
# so we need to use the cofuntion formulas defined as:
# sin(pi/2 - angle) = cos(angle)
# cos(pi/2 - angle) = sin(angle)
# now knowing these we substitute
# x = r * sin( angle )
# y = r * cos( angle)
# however I left it un-substituted for clearity
dx = dist * math.cos( correction - angle )
# one more thing, because we want tiny to walk into the scene we
# make it negative to walk into the scene
# remove the negative and she walks backwards
# remember this if you want to make your avatar walk backwards
dy = dist * -math.sin( correction - angle )
# now that we know x and y we just add it to the current values since last frame
self.tiny.setPos( Vec3( self.tiny.getX( ) + dx, self.tiny.getY( ) + dy, 0 ) )
# on a side note, if you had trouble with the math I recommend
# "Trigonometry the Easy Way" third edition by Douglas Downing
# easy read and you should be up and running with trig in no time
# and we continue on to the next frame
return Task.cont
he divides by a positive 180
angle = self.tiny.getH( ) * math.pi / 180.0
this is fine and hunky dory until you want to rotate or turn the character in any direction. When I incorporated it into my code…
Old Broken Code Walking Forward
def walking_forward_task(self, task):
distance = 0.025
[b]angle = self.pandaActor.getH()*math.pi/180[/b]
correction = math.pi/2
dx = distance * math.cos( correction + angle )
dy = distance * -math.sin( correction + angle )
self.pandaActor.setPos( Vec3( self.pandaActor.getX( ) + dx, self.pandaActor.getY( ) + dy, 0 ) )
return Task.cont
Turning Left
def turnLeftTask(self, direction):
self.pandaActor.setH(self.pandaActor.getH()-direction)
the character would turn in the correct direction but the heading would be in the opposite direction or in math terms say my heading (assuming we are in a unit circle and we are using degrees and not radians) my character (not heading) is turned 45º but my heading would be 135º.
my new corrected code divides by a -180 instead which produces the correct rotation both for the character and direction. I figured out the problem but I do not know how it is correct mathematically. I guess I would need to sit down and solve the problem by hand but I’m too lazy to do that. I know its probably an elementary thing that I am overlooking but that’s just how I am.
Thank you for any responses!