Point & Click Turning Bug!

Hi again. I’ve been playing around with this code all day, it’s just wonderful :smiley:. I’ve also been studying it to try and learn what I did wrong.

However, I’m a little bit puzzled, I don’t know why it works and this is something that I’d really love to understand. Anybody in the mood to give a quick programming lesson :smiley:?

You see, in the function below, ‘reducedH = self.player.getH()%360.0’ and ‘self.player.setH(reducedH)’ seem to be called/referenced (sorry, I don’t know what the right term is) before any player movement has even taken place.

To my newbie mind, it seems like they’re setting the player’s heading before he’s even started to move. It seems to me that they should be called after the player’s movement, or at least after ‘playerTurn’, but obviously that’s not the correct approach. How come it works when it’s done this way?

def moveToPosition(self):
        # Get the clicked position
        self.getPosition(base.mouseWatcherNode.getMouse())
       
        # Calculate the new hpr
        self.npLook.setPos(self.player.getPos())
        self.npLook.lookAt(self.position) # Look at the clicked position.
        reducedH = self.player.getH()%360.0
        self.player.setH(reducedH)
        currHpr = self.player.getHpr()
        #print "curr", currHpr
        newHpr = self.npLook.getHpr()
        #print "new", newHpr
        #print self.npLook.getPos(render)
        newH = closestDestAngle(currHpr[0], newHpr[0])
        #print newH
        # Create a turn animation from current hpr to the calculated new hpr.
        playerTurn = self.player.hprInterval(.2, Point3(newH, newHpr[1], newHpr[2]))
       
        # 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 coordinates based on self.movementSpeed
        travelVec = self.position - self.player.getPos()
        distance = travelVec.length()
       
        playerMove = self.player.posInterval((distance / self.movementSpeed), self.position)

        self.playerMovement = Sequence(playerTurn, playerMove)
        self.playerMovement.start()
        #playerTurn.start() 

Sorry to be a such a pest, I’m just really trying to learn and understand all this programming stuff.

Cheers