[Tutor] Math Function and Python

Greg Nielsen gollumgreg407366 at gmail.com
Mon Jul 2 06:21:11 CEST 2012


Hello Tutor,

     I'm having some trouble with a mathematical function in my code. I am
attempting to create a realistic acceleration in an object, slower to speed
up at its min and max speeds and fastest in the middle. To keep the math
simpl(er), I just wrote this function for a parabola, which models the
acceleration of the object. Y is the acceleration and X is the current
speed of the object.

Y = -.01X^2 * 1.45X

Before I came up with this formula, I was using a very inefficient list
method to do this. The old code is in italics. I still have it in the code
because it works (sort of). The function is commented out because it
doesn't work. It's also in bold, so you can see it easily.

        if keys[pygame.K_UP] and self.dy < (15 + self.speedMod):
            if self.dy <= 0:
               * self.accelPoints += self.accelList[0]*
                *#self.dy += 1*
            else:
                *self.accelPoints += self.accelList[self.dy]*
                *#self.accelPoints += ((-.1 * (self.dy * self.dy)) + (1.45
* self.dy))*
            if self.accelPoints >= self.accelGoal:
                self.dy += 1
                self.nety = self.dy
                self.accelPoints = 0

I did a test, and for some reason, it seems like the output of the function
is never saved into the self.accelPoints variable, because it always prints
out as 0. (For those who don't want to do the math, when dy = 1, the
function should output something close to 1.55) My best guess is that the
accelPoints doesn't like the fact that the function outputs a double, but
even when i make accelPoints a double (made it equal 0.01 instead of 0) it
still didn't work. If you have any thoughts on the matter, please let me
know. I most likely broke it in the most silly way imaginable. Thanks for
reading.

Greg
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120701/40584a4f/attachment.html>


More information about the Tutor mailing list