Re: [Tutor] Math Function and Python

2012-07-02 Thread Alan Gauld

On 02/07/12 05:21, Greg Nielsen wrote:


models the acceleration of the object. Y is the acceleration and X is
the current speed of the object.
Y = -.01X^2 * 1.45X


That's not the same as the formula you have implemented.


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))*


Are you sure the else is being executed?
Have you tried inserting a print statement to check the value right 
after the block?



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.


If your dy is very close to zero the value of your formula will be very 
close to zero so accelPoints never increases. But you also commented out 
the dy increment line so dy never increases which migfht suggest the 
else never gets called??


Can you send us the actual code that fails rather than something else 
that nearly works?
Its much easier to debug the thing that's actually broken that something 
that looks vaguely like it.




outputs a double, but even when i make accelPoints a double (made it
equal 0.01 instead of 0) it still didn't work.


Provided they are both numbers Python will adapt. That's not likely to 
be the problem.


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Math Function and Python

2012-07-01 Thread Steven D'Aprano
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
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor