Jump to content
  • Advertisement
Sign in to follow this  
GarrickW

Python weirding out?

This topic is 2593 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I must be making some kind of embarassingly simple mistake, here, but I just can't see it.

I've got a very basic equation in my program that looks like this:

self.xProportion = 1.0 * self.xDistance / self.totalDistance

self.xDistance is a negative integer, such as -87 (literally, that is the value it had when I last run the program). self.totalDistance is a positive integer, in this case, 183. I stuck the 1.0 in there because I want a float; this technique has worked for me in the past.

Yet no matter what the values for the two variables are, I get an end result of 0. I can't think of that many other ways to do this. The following permutation gives me a result of -1.

self.xProportion = float(self.xDistance / self.totalDistance)

The following gives me a result of 0.

self.xProportion = float(self.xDistance) / float(self.totalDistance)

Alone in the console, however, when I ask Python what 1.0 * -87 / 183 gives me, I get -0.47540983606557374, which is exactly what I want.

So what's going on here? Am I missing something?

For reference, the larger part of the program in which the code appears:


def moveOrder(self, point):
#Used setting up movement directly towards a particular node..
self.xDistance = self.mapPosX - point[0]
self.yDistance = self.mapPosY - point[1]
print "xDistance: %i" % (self.xDistance)
print "yDistance: %i" % (self.yDistance)
#Establish a total distance.
self.totalDistance = abs(self.xDistance) + abs(self.yDistance)

#If the distance is too small, don't bother moving.
if self.totalDistance <= 2:
self.moving = False
return

#The amount of total moveSpeed dedicated to each direction
#is proportional to the weight that direction has in the distance.

#Establish which proportion of that distance is X, which Y
self.xProportion = 1.0 * self.xDistance / self.totalDistance
self.yProportion = 1.0 * self.yDistance / self.totalDistance
print "xProportion: %i" % (self.xProportion)
print "yProportion: %i" % (self.yProportion)
print "Equation: %i = 1.0 * %i / %i" % (self.xProportion, self.xDistance, self.yDistance)
#Proportionalize movement speed.
self.xSpeed = self.moveSpeed * self.xProportion
self.ySpeed = self.moveSpeed * self.yProportion
print "xSpeed: %i" % (self.xSpeed)
print "ySpeed: %i" % (self.ySpeed)

self.moving = True

Share this post


Link to post
Share on other sites
Advertisement
The problem is in your print statement. You're telling print to format the value as an integer (%i). Try %f instead.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!