[Tutor] Math Function and Python
Steven D'Aprano
steve at pearwood.info
Mon Jul 2 07:14:15 CEST 2012
On Sun, Jul 01, 2012 at 11:21:11PM -0500, Greg Nielsen wrote:
> 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
Are you sure that's the expression you want to use for acceleration? It
certainly isn't a parabola. It simplifies to:
Y = -0.0145*X**3
which says that the DECELERATION is proportional to the CUBE of the
speed.
Also, that's not a function. This would be a function:
def acceleration(v):
"""Return the acceleration of the object given its current
velocity.
"""
return -0.0145*v**3
> 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.
Eh, no. There are no italics or bold in plain text emails.
> 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
Without explaining what "accelPoints", "nety", "dy" etc. mean, there is
no real way to tell whether this is a realistic model for a moving body.
Without more context, I can't tell what it actually does, let alone what
it is supposed to do.
I'm not an expert on PyGame, but I would expect that the way to model
movement of a sprite is to calculate the delta-X and delta-Y, where X
and Y are POSITIONS not speed and acceleration. I don't understand
what "accelList" is for. Then you call the move() method on the sprite's
rectangle, and redraw it.
> 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.
Of course it does. You set it to 0 with this line:
self.accelPoints = 0
> (For those who don't want to do the math, when dy = 1, the
> function should output something close to 1.55)
It certainly does not.
In your code, you have this expression:
(-.1 * (self.dy * self.dy)) + (1.45 * self.dy)
When self.dy = 1, that returns 1.35, not 1.55.
The other expression you give, early in this post, doesn't have a dy
term. But if I put Y = dy instead, it gives -0.0145.
> My best guess is that the
> accelPoints doesn't like the fact that the function outputs a double,
No.
By the way, in Python we don't talk about doubles. We have floats, which
are implemented as C doubles, but since we don't have C-singles, we just
call them floats.
--
Steven
More information about the Tutor
mailing list