Point & Click Turning Bug!

Oops, I forgot to mention, I eventually plan to combine both playerTurn and playerMove into the one function and then play them in sequence. Hopefully this will make the player turn in the direction of the mouse click and then move to that position.

Something like the following code (this is Sandman’s example :smiley:):

  def moveToPosition(self):
    if self.playerMovement != None:
        self.playerMovement.pause()
        self.__stopWalkAnim()
       
    # This is the "start" position
    sPos = self.player.getPos()

    # Calculate the new hpr
    # Create a tmp NodePath and set it to the start position
    # Then make it "lookAt" the position we want to be at.
    # It will then create the hpr that we can turn to.
    a = NodePath('tmp')
    a.setPos(sPos)
    a.lookAt(position)
    newHpr = a.getHpr()

    # Create a turn animation from current hpr to the calculated new hpr.
    playerTurn = self.player.hprInterval(1,Point3(newHpr[0] + self.rotationOffset,newHpr[1],newHpr[2]),startHpr = self.player.getHpr())

    # Calculate the distance between the start and finish positions.
    # This is then used to calculate the duration it should take to
    # travel to the new coordinate base on self.movementSpeed
    if sPos[0] > position[0]:
      distanceX = sPos[0] - position[0]
    else:
      distanceX = position[0] - sPos[0]
   
    if sPos[1] > position[1]:
      distanceY = sPos[1] - position[1]
    else:
      distanceY = position[1] - sPos[1]

    distance = sqrt((distanceX * distanceX) + (distanceY * distanceY))
   
    # Create a movement animation using the 2 positions
    playerMove = self.player.posInterval( (distance / self.movementSpeed), position, startPos = sPos)

    # Put the animations into a sequence and create an event that will be
    # called when its finished.
    self.playerMovement = Sequence(playerTurn, playerMove, name = 'playerMove')
    self.playerMovement.setDoneEvent('player-stopped')

    # Start the walking animation, then start the turn+move anims
    self.player.loop('walk')
    self.playerMovement.start()

  def __stopWalkAnim(self):
    # This is called when the turn+move anim has finished.
    # We can then stop the walk anim.
    self.player.stop('walk')
    self.playerMovement = None 

Cheers