Re: [pygame] Why does my ball vibrate?

2007-12-06 Thread Ken Seehart

Ian Mallett wrote:
On Dec 5, 2007 4:03 PM, Greg Ewing <[EMAIL PROTECTED] 
> wrote:


Actually, it does -- a photon is an example of an object
with no mass. Such an object always travels at the speed
of light -- it doesn't even need a push to get it going.
It's fundamentally incapable of standing still. 


Heh heh.  Try hitting that with a paddle.

While it has no mass, it does have both energy and
momentum, both of which are proportional to its frequency.

Momentum is defined as mass*velocity.  If mass is zero, how does a 
photon have momentum?
When a physicists say that a photon has zero mass, what they generally 
mean is that photons have zero /rest mass/ (a rather hypothetical notion 
since a photon can't be at rest).  A photon that is moving (the only 
kind of photon there is) has a mass of *h/c**?*.  Since particles have 
ever increasing mass as their velocity increases, approaching infinity 
as velocity approaches *c*.  You can loosely imagine a photon as having 
it's rest mass multiplied by infinity, which would be m = 0*?, which 
doesn't help us much.  The actual equations are:


Energy of a photon is inversely proportional to wavelength* **?*, thats: 
*E = hc/?*, where *h* is Planck's  constant
We also have *E = mc^2*, so dividing both sides by *c^2*, we get mass:  
*m = h/c**?*
Momentum is *p = mv = hv/**c**? = h/? *(because *v=c* since it is a 
photon)



These are conserved in any collision, so when it
bounces off a wall, the wall gains some momentum, just
as it would if a massive particle with the same
momentum bounced off it. And if the wall starts to
move as a result, then it has also gained some energy,
which must have come from the photon, so the reflected
photon must be red-shifted slightly (longer wavelength
= lower frequency = less energy). 

All this is true, but /how/ exactly does a massless particle have 
momentum?


--
Greg

Ian 




Re: [pygame] Why does my ball vibrate?

2007-12-06 Thread Greg Ewing

Luke Paireepinart wrote:
I don't pretend to understand the implications of this discussion, but 
it sounds to me like, at the speed of light, frequency is analogous to 
mass at lower speeds?


Not really. The relationship between momentum and wavelength
applies at all speeds, and to all particles, both massive
and massless. Electrons have a wavelength too, related to
their momentum in the same way as photons. Fast-moving
electrons have a short wavelength, slow-moving ones have
a long wavelength. Pass them through a diffraction grating
and you get interference patterns. Sounds weird, but it
really happens.

In quantum mechanics, there's really very little difference
between massive and massless particles. Also, the word
"particle" doesn't exactly mean what it sounds like it
means. Rather than a little ball, it's more like a
mode of vibration in a string or a drumhead. As a
vibration, it has a frequency, and there is energy
associated with that vibration. The higher the frequency,
the greater the energy.

(Like all things in quantum mechanics, you shouldn't
push that analogy too far. But the mathematics involved
are very reminiscent of what you get from dealing with
waves in a vibrating medium.)

--
Greg


Re: [pygame] Why does my ball vibrate?

2007-12-05 Thread Greg Ewing

Ian Mallett wrote:


Of course, there is a paradox in all this, that the ball can
retain kinetic energy, yet have no mass.  It could be viewed that, by
Physics, it could not impart energy with no mass, because energy
transfer is proportional to the mass and speed of an object.


That's only the case in Newtonian physics. It's true that
Newtonian physics doesn't include the concept of an object
with zero mass, but things are different in relativity.

One of the differences is the meaning of the word "mass".
Nowadays it's used to mean "rest energy", i.e. the energy
an object has even when it's not moving. When it is
moving, it has additional energy as well, calculated from
its rest energy and its speed.

But a photon is never at rest, so the concept of rest
energy doesn't really apply. Its energy is calculated a
different way, based on its frequency.


But photons accelerate, at least, so ???


No, they don't. They're born moving at c, and they do so
until they die.

--
Greg


Re: [pygame] Why does my ball vibrate?

2007-12-05 Thread Jason Ward
a while ago someone said that the ball speeding up and slowing down
"randomly" was because it was based on the time elapsed every frame and that
time varies because if other tasks are being run then the cpu is executing
both threads so it doesn't spend all of its time in your game.

But then what workaround is there for physics simulations that are based on
time?


Re: [pygame] Why does my ball vibrate?

2007-12-05 Thread Luke Paireepinart

Ian Mallett wrote:
On Dec 5, 2007 4:03 PM, Greg Ewing <[EMAIL PROTECTED] 
> wrote:


Actually, it does -- a photon is an example of an object
with no mass. Such an object always travels at the speed
of light -- it doesn't even need a push to get it going.
It's fundamentally incapable of standing still. 


Heh heh.  Try hitting that with a paddle.

While it has no mass, it does have both energy and
momentum, both of which are proportional to its frequency.

Momentum is defined as mass*velocity.  If mass is zero, how does a 
photon have momentum?


These are conserved in any collision, so when it
bounces off a wall, the wall gains some momentum, just
as it would if a massive particle with the same
momentum bounced off it. And if the wall starts to
move as a result, then it has also gained some energy,
which must have come from the photon, so the reflected
photon must be red-shifted slightly (longer wavelength
= lower frequency = less energy). 

All this is true, but /how/ exactly does a massless particle have 
momentum?
I don't pretend to understand the implications of this discussion, but 
it sounds to me like, at the speed of light, frequency is analogous to 
mass at lower speeds?


Re: [pygame] Why does my ball vibrate?

2007-12-05 Thread Ian Mallett
On Dec 5, 2007 4:03 PM, Greg Ewing <[EMAIL PROTECTED]> wrote:

> Actually, it does -- a photon is an example of an object
> with no mass. Such an object always travels at the speed
> of light -- it doesn't even need a push to get it going.
> It's fundamentally incapable of standing still.

Heh heh.  Try hitting that with a paddle.

> While it has no mass, it does have both energy and
> momentum, both of which are proportional to its frequency.

Momentum is defined as mass*velocity.  If mass is zero, how does a photon
have momentum?

> These are conserved in any collision, so when it
> bounces off a wall, the wall gains some momentum, just
> as it would if a massive particle with the same
> momentum bounced off it. And if the wall starts to
> move as a result, then it has also gained some energy,
> which must have come from the photon, so the reflected
> photon must be red-shifted slightly (longer wavelength
> = lower frequency = less energy).

All this is true, but *how* exactly does a massless particle have momentum?

> --
> Greg
>
Ian


Re: [pygame] Why does my ball vibrate?

2007-12-05 Thread Greg Ewing

Michael George wrote:
I've thought of writing a game with physics operating at the quantum 
scale, but I couldn't think of anything interesting.


Some things you might be able to exploit:

* Non-commuting observables - an object that you can determine
  two different things about, but not both at the same time.
  Observing one makes the other indeterminate, so by making
  alternating observations you can change its state.

* Interference - something can happen two mutually exclusive
  ways, but to achieve a desired outcome it has to happen
  both ways at the same time. You achieve this by closing
  your eyes and not watching what happens.

* Entanglement - two distant objects are connected in such
  a way that observing something about one of them has an
  effect on the other.

--
Greg


Re: [pygame] Why does my ball vibrate?

2007-12-05 Thread Greg Ewing

Ian Mallett wrote:

However, Physics, as far as I know, does not say what
happens with a particle of no mass.


Actually, it does -- a photon is an example of an object
with no mass. Such an object always travels at the speed
of light -- it doesn't even need a push to get it going.
It's fundamentally incapable of standing still.

While it has no mass, it does have both energy and
momentum, both of which are proportional to its frequency.
These are conserved in any collision, so when it
bounces off a wall, the wall gains some momentum, just
as it would if a massive particle with the same
momentum bounced off it. And if the wall starts to
move as a result, then it has also gained some energy,
which must have come from the photon, so the reflected
photon must be red-shifted slightly (longer wavelength
= lower frequency = less energy).

--
Greg


Re: [pygame] Why does my ball vibrate?

2007-12-05 Thread Ian Mallett
On 12/5/07, Patrick Mullen <[EMAIL PROTECTED]> wrote:
> Well pong is viewed from above, so downward acceleration makes no
> sense.  (ping pong)
So it would be a case where the bounce is sideways, like hockey or soccer.
> As for bouncing indefinitely, pong takes place in
> a magical universe where no energy is ever lost :)
And now, for your random amusement:

Well, some energy must be lost from the ball, and transferred into
the wall, (so the walls should move as well as the ball).  That way,
the energy would be temporarily lost to the ball, but then given back
to it when it hits the other wall, which is moving towards it.
Actually, that's sort of what happens.  The energy of the ball goes
into the Earth, accelerating it VERY slightly.  If collisions were
totally elastic, and friction was zero, the ball would bounce forever.

Either that or the ball must be infinitely light, in which case,
it would be accelerated to the speed of light by a touch.  However, as
long as the object it comes into contact with remains motionless, the
bounce will just be a bounce, and not a speed change.  (Velocity
changes because the direction does).  On the other hand, since the
ball is massless, the ball's velocity can be changed instantaneously.
Because the contact time is nonexistent, the speed of the paddle per
unit of time is irrelevant, so it would always bounce and never
accelerate.  Even friction would have no effect.  (Air friction,
caused by air molecules, for example, would cause the ball to bounce
rapidly between the atoms- no energy would even be lost as heat, or
would it?).

Note that these two cases only work if the ball has a set speed to
begin with.  If it was at rest, a single atom of gas would accelerate
it to light speed.  If it was going at light speed, there is no way to
stop it because it would retain the speed of any collision.  If it hit
another atom of gas, it could not transfer energy because it does not
have any mass to carry the energy.  Or would it?  Physics says that
the same force is required to stop a particle as took to accelerate
it.  So identical atoms at identical speeds could both stop and start
the ball?  However, Physics, as far as I know, does not say what
happens with a particle of no mass.

Now, according to Wikipedia, the mass of a photon is zero.  Yet we
know that photons carry energy (shine this:
http://www.youtube.com/watch?v=vXDYCGVEMac into your face (don't)).
Since energy is mass (the conversion factor is the speed of light
squared), photons must have effective mass.  Yet they don't!  Somehow,
they can obviously be created and accelerated.  So, there are your
magic particles, but we don't often see them going slower than the
speed of light, not to mention small, so they're useless for Pong.

Of course, there is a paradox in all this, that the ball can
retain kinetic energy, yet have no mass.  It could be viewed that, by
Physics, it could not impart energy with no mass, because energy
transfer is proportional to the mass and speed of an object.  But
photons accelerate, at least, so ???  Incidentally, ask someone what
(1/infinity)*(infinity)
is.  I want to know.

Best not to analyse it too much. :-)

Ian


Re: [pygame] Why does my ball vibrate?

2007-12-05 Thread Patrick Mullen
On Dec 5, 2007 9:59 AM, Ian Mallett <[EMAIL PROTECTED]> wrote:
> On 12/4/07, Greg Ewing <[EMAIL PROTECTED]> wrote:
> > That's true. Probably not something you need to take into
> > account in a Pong game, but if you're simulating space
> > flight or something like that, you certainly do!
> I always wondered how the ball in Pong could bounce indefinitely, and
> not accelerate when traveling downwards.
>

Well pong is viewed from above, so downward acceleration makes no
sense.  (ping pong)  As for bouncing indefinitely, pong takes place in
a magical universe where no energy is ever lost :)  It's funny how you
don't ever have to worry about going over the net.  Why they didn't
just name it hock (air hockey) we'll never know.


Re: [pygame] Why does my ball vibrate?

2007-12-05 Thread Ian Mallett
On 12/4/07, Greg Ewing <[EMAIL PROTECTED]> wrote:
> That's true. Probably not something you need to take into
> account in a Pong game, but if you're simulating space
> flight or something like that, you certainly do!
I always wondered how the ball in Pong could bounce indefinitely, and
not accelerate when traveling downwards.


Re: [pygame] Why does my ball vibrate?

2007-12-04 Thread Greg Ewing

Ian Mallett wrote:

However, the thing that will change significantly is gravitational
pull.  The further an object is away, the less pull it exerts by an
inverse square function.  (On the roof, you do weigh slightly less).


That's true. Probably not something you need to take into
account in a Pong game, but if you're simulating space
flight or something like that, you certainly do!

--
Greg


Re: [pygame] Why does my ball vibrate?

2007-12-04 Thread Ian Mallett
Like we calculated, a small object must be moving several trillion
times the speed of light to accelerate the Earth that much.  My point
was that the Earth's motion is insignificant.  I think we already knew
that...

However, the thing that will change significantly is gravitational
pull.  The further an object is away, the less pull it exerts by an
inverse square function.  (On the roof, you do weigh slightly less).

Ian


Re: [pygame] Why does my ball vibrate?

2007-12-03 Thread David Muffley
On Dec 4, 2007 12:45 AM, Greg Ewing <[EMAIL PROTECTED]> wrote:

> To do this calculation properly you'd need to use the
> appropriate relativistic formulas. No doubt you could
> get enough kinetic energy if the object was moving at
> a speed close enough to c, but the more likely result
> would be vaporisation of the object together with a
> sizeable chunk of the earth...
>
> --
> Greg


Not to mention that you'd have to modify the size of the ball every frame,
and somehow manage the changing looks of the ball too, aie


Re: [pygame] Why does my ball vibrate?

2007-12-03 Thread Greg Ewing

Ian Mallett wrote:

So, if our object masses 1kg., to move the Earth 1m/s., the object
must move 
199,120,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,00

0,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000

times the speed of light!


To do this calculation properly you'd need to use the
appropriate relativistic formulas. No doubt you could
get enough kinetic energy if the object was moving at
a speed close enough to c, but the more likely result
would be vaporisation of the object together with a
sizeable chunk of the earth...

--
Greg


Re: [pygame] Why does my ball vibrate?

2007-12-03 Thread Michael George
At those speeds you need to use relativity to get the right answer 
anyway.  A 1kg mass needs to be moving at roughly the speed of light to 
accelerate the earth to 1m/s.


Ian Mallett wrote:

So, if our object masses 1kg., to move the Earth 1m/s., the object
must move 
199,120,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000
times the speed of light!  That's 199120*1trillion^84!  Had to do this
mostly by hand, my calc doesn't handle this sort of thing, so it might
not be terribly accurate...
Ian
  





Re: [pygame] Why does my ball vibrate?

2007-12-03 Thread David Gowers
On Dec 4, 2007 7:02 AM, David Gowers <[EMAIL PROTECTED]> wrote:
> Hi Ian,
>
>
> On Dec 4, 2007 6:18 AM, Ian Mallett <[EMAIL PROTECTED]> wrote:
> > So, if our object masses 1kg., to move the Earth 1m/s., the object
> > must move 
> > 199,120,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000
> > times the speed of light!  That's 199120*1trillion^84!  Had to do this
> > mostly by hand, my calc doesn't handle this sort of thing, so it might
> > not be terribly accurate...
> > Ian
> >
> :)
> This is a python related mailing list, ya know..
>
>
> /usr/lib/python2.6/site-packages/IPython/Extensions/path.py:32:
> DeprecationWarning: the md5 module is deprecated; use hashlib instead
>   import sys, warnings, os, fnmatch, glob, shutil, codecs, md5
> /usr/lib/python2.6/site-packages/IPython/iplib.py:58:
> DeprecationWarning: the sets module is deprecated
>   from sets import Set
> IPython 0.8.2   [on Py 2.6a0]
> [~]|1> a = 199120
> [~]|2> c = 84
> [~]|3> b = 1
> [~]|4> a * pow(b, c)
><4> 199120
> 000
> 000
> 000
> 000
> 000
> 000
> 000
> 000
> 000
> 000
> 000
> 000
> 000
> 000
> 000
> 000
> 000
> 000
> 000
> 000
> 000
> 000
> 000
> 000
> 000
> 0L
>
>
> Which is considerably larger than the figure you gave.
>
Oops, of course it is. silly me. My earlier point stands, though.


Re: [pygame] Why does my ball vibrate?

2007-12-03 Thread David Gowers
Hi Ian,

On Dec 4, 2007 6:18 AM, Ian Mallett <[EMAIL PROTECTED]> wrote:
> So, if our object masses 1kg., to move the Earth 1m/s., the object
> must move 
> 199,120,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000
> times the speed of light!  That's 199120*1trillion^84!  Had to do this
> mostly by hand, my calc doesn't handle this sort of thing, so it might
> not be terribly accurate...
> Ian
>
:)
This is a python related mailing list, ya know..


/usr/lib/python2.6/site-packages/IPython/Extensions/path.py:32:
DeprecationWarning: the md5 module is deprecated; use hashlib instead
  import sys, warnings, os, fnmatch, glob, shutil, codecs, md5
/usr/lib/python2.6/site-packages/IPython/iplib.py:58:
DeprecationWarning: the sets module is deprecated
  from sets import Set
IPython 0.8.2   [on Py 2.6a0]
[~]|1> a = 199120
[~]|2> c = 84
[~]|3> b = 1
[~]|4> a * pow(b, c)
   <4> 199120
000
000
000
000
000
000
000
000
000
000
000
000
000
000
000
000
000
000
000
000
000
000
000
000
000
0L


Which is considerably larger than the figure you gave.


RE: [pygame] Why does my ball vibrate?

2007-12-03 Thread Boer Kees

#ok, I think I have it broken down into pieces now... check out this code:
# 
import sys, pygame, mathpygame.init()pixelspercm=10.0gravity=9.80 * 10.0 # 
meter per second per second (juiced up a little :) )framerate=50.0 # per 
secondlossperbounce=0.33 # lose energy per bounceframestosilence=3#if a bounce 
occurs AGAIN within the next 3 frames, consider the ball dead
 
class Ball(object):def __init__(self, id,pos=(0,0)):self.speed=0.0 
# meter per secondself.diameter=0.025 # meterself.pos=pos   
 self.previous=0#count how many frames ago we had the previous bounce
self.name=id#just some name to show for feedback
image=pygame.Surface((int(self.diameter*2*pixelspercm*100),int(self.diameter*2*pixelspercm*100)))
image.set_colorkey((0,0,0))image.fill((0,0,0))
pygame.draw.circle(image, (255,255,255), 
(int(self.diameter*pixelspercm*100),int(self.diameter*pixelspercm*100)), 
int(self.diameter*pixelspercm*100))self.image=imagedef 
updatepos(self,ms):if not (self.speed==0 and self.pos[1]==300): 
distance=self.DeltaX2(self.speed, gravity,ms/1000.0)if 
distance+self.pos[1]<300:
self.speed=self.FinalVelocity1(self.speed,gravity,ms/1000.0)
self.pos=(self.pos[0],distance+self.pos[1])if self.previous>0:  
  self.previous=self.previous-1print 
self.name, 'normal cycle',self.previouselse:print 
self.namedis2=300-self.pos[1]# distance to ground   
 print 'distance to ground=',dis2
ms2=self.MsTillGround(dis2,self.speed,self.FinalVelocity1(self.speed,gravity,ms/1000.0))
 #ms till groundprint 'ms till ground=',ms2
endvel=self.FinalVelocity1(self.speed, gravity, (ms-ms2)/1000.0)#speed at 
groundspeedatground=-endvel*(1-lossperbounce)#lose energy   
 print 'speed at ground=',speedatground
dis3=self.DeltaX2(speedatground, gravity,(ms-ms2)/1000.0)#deltaX after all ms   
 print 'deltaX after all ms=',dis3
endvel2=self.FinalVelocity1(speedatground, gravity, (ms-ms2)/1000.0)#speed 
after timeprint 'speed after time=',endvel2if 
self.previous>1:self.pos=(self.pos[0],300)  
  self.speed=0.0print 'self.speed=',self.speed  
  print 'self.pos=',self.posprint 'SILENCE!'
else:self.speed=endvel2
self.pos=(self.pos[0],300+dis3)print 
'self.speed=',self.speedprint 'self.pos=',self.pos  
  self.previous=framestosilence
 
def MsTillGround(self, Dx, Vi, Vf):#Dx=1/2*(Vi+Vf)*t
#t=Dx/(1/2*(Vi+Vf))t=Dx/(0.5*(Vi+Vf))return t*1000.0 # t is in 
seconds, but we want the amount of millisecondsdef 
FinalVelocity1(self,Vi,A,t):# ### 1# Vf=Vi+A*t  
  # Final Velocity = Initial Velocity + Acceleration * time# 
meter/second = meter/second + meter/second/second * secondreturn Vi+A*t 
   def AverageVelocity(self,Vi,Vf):# ### 2# 
Va=(Vi+Vf)/2# Average Velocity = ( Initial Velocity + Final Velocity 
)/2# meter/second = ( meter/second + meter/second )/2return 
(Vi+Vf)/2def DeltaX1(self,Vi,Vf,t):# ### 3# 
Dx=1/2*(Vi+Vf)*t# DeltaX = 1/2 * ( Initial Velocity + Final Velocity ) 
* time# meter = 1/2 * ( meter/second + meter/second )* second
return 0.5*(Vi+Vf)*tdef DeltaX2(self,Vi,A,t):# ### 4
# Dx=Vi*t+ 1/2*a* t^2 # notice: t^2 in python is: t**2# DeltaX = 
Initial Velocity * time+1/2*Acceleration* time*time# meter = 
meter/second * second+1/2*meter/second/second * second*secondreturn 
Vi*t+0.5*A*t**2def FinalVelocity2(self,Vi,A,Dx):# ### 5 
   # Vf^2=Vi^2+2*a*Dx# Final Velocity^2=Initial 
Velocity^2+2*Acceleration*DeltaX# meter/second*meter/second= 
meter/second*meter/second+2*meter/second/second*meterVf2=Vi**2+2*A*Dx   
 return math.sqrt(Vf2)
 
screen = pygame.display.set_mode((200, 400), 0, 32)balls = 
[]balls.append(Ball('Ball 1',(78,15)))balls.append(Ball('Ball 
2',(12,158)))balls.append(Ball('Ball 3',(122,56)))newtime=oldtime=0
while True:newtime=pygame.time.get_ticks() # milliseconds since start of 
programif newtime-oldtime>(1000.0/framerate)*2:print 'LAGGING!'
if newtime-oldtime>1000.0/framerate:for event in pygame.event.get():
if event.type == pygame.QUIT:exit()
screen.fill((0, 0, 0))for ball in balls:
ball.updatepos(newtime-oldtime)for ball in balls:
screen.

Re: [pygame] Why does my ball vibrate?

2007-12-03 Thread Ian Mallett
So, if our object masses 1kg., to move the Earth 1m/s., the object
must move 
199,120,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000
times the speed of light!  That's 199120*1trillion^84!  Had to do this
mostly by hand, my calc doesn't handle this sort of thing, so it might
not be terribly accurate...
Ian


Re: [pygame] Why does my ball vibrate?

2007-12-03 Thread Michael George
Lenard Lindstrom wrote: 


This is less than the Planck length of 1.6 * 10 ** -35 meters so 
becomes moot. The Planck length would make a natural cutoff for any 
game physics. :-)




I've thought of writing a game with physics operating at the quantum 
scale, but I couldn't think of anything interesting.  Maybe your 
character gets to resolve quantum choices but you lose if your 
improbability goes too high or something (like a life meter).


Re: [pygame] Why does my ball vibrate?

2007-12-03 Thread Lenard Lindstrom

Ian Mallett wrote:



If the ground were mobile, that gravity force down would cause the
earth to move down with that same force.  Since the earth can't move,
it pushes back with the force of gravity.


But the Earth does move.  If you drop a 1kg. ball from a height of one
meter, then both objects (the ball and the Earth) accelerate towards
each other.  The reason we see the ball fall is because 1), we move
with it, and 2) the Earth is more massive and so is accelerated very
little by the force.  (In fact, the Earth accelerates:
(1 kg.)/(5.9736×1024 kg.)
= .1674^ -1024th as much.  Not even a trillion trillion trillionth of
a millimeter.  Still much smaller than even that.  As soon as I get a
minute, I'll do some more demos...
  


This is less than the Planck length of 1.6 * 10 ** -35 meters so becomes 
moot. The Planck length would make a natural cutoff for any game 
physics. :-)


--
Lenard Lindstrom
<[EMAIL PROTECTED]>



Re: [pygame] Why does my ball vibrate?

2007-12-03 Thread Ian Mallett
On 12/2/07, Patrick Mullen <[EMAIL PROTECTED]> wrote:
> But there IS an upward force, at least as far as my memory of the
> physics I learned.  The whole every action has a reaction business.
Right.  Newton's third law.  Pushing on a table should move it, if the
force is unbalanced, but it doesn't, so the force is balanced by
another force, thus action = reaction.
> If the ground were mobile, that gravity force down would cause the
> earth to move down with that same force.  Since the earth can't move,
> it pushes back with the force of gravity.
But the Earth does move.  If you drop a 1kg. ball from a height of one
meter, then both objects (the ball and the Earth) accelerate towards
each other.  The reason we see the ball fall is because 1), we move
with it, and 2) the Earth is more massive and so is accelerated very
little by the force.  (In fact, the Earth accelerates:
(1 kg.)/(5.9736×1024 kg.)
= .1674^ -1024th as much.  Not even a trillion trillion trillionth of
a millimeter.  Still much smaller than even that.  As soon as I get a
minute, I'll do some more demos...
> It has been quite a while since I was in physics, but that's how I remember 
> it.
You remember much rightly.  I'm currently in Physics.

Ian


Re: [pygame] Why does my ball vibrate?

2007-12-02 Thread Patrick Mullen
On Dec 2, 2007 7:00 PM, David Gowers <[EMAIL PROTECTED]> wrote:
> I believe this is the most reliable solution.
> (AFAIK there is no upward force though; it's more to do with the
> pressure the object exerts being insufficient to penetrate the ground.
> So for a simple physics simulation, this solution is also more
> correct.)

But there IS an upward force, at least as far as my memory of the
physics I learned.  The whole every action has a reaction business.
If the ground were mobile, that gravity force down would cause the
earth to move down with that same force.  Since the earth can't move,
it pushes back with the force of gravity.

It has been quite a while since I was in physics, but that's how I remember it.

>
> Matt: I assume you didn't mean to use a suggestive subject line, Every
> time I look at my mail I see this conversation, read the subject line
> rather differently than you intended, and delete it. And then someone
> posts another reply, so it appears again.. ._.
>

I never once read the subject line in that way, but now I can't help
it.  Thanks a lot.

LOL


Re: [pygame] Why does my ball vibrate?

2007-12-02 Thread David Gowers
Hi,

On Nov 30, 2007 9:30 AM, Greg Ewing <[EMAIL PROTECTED]> wrote:
> Matt Smith wrote:
> > I can't quite get my head around WHY the ball sinks into the ground if I
> > don't set ypos back to 384 but it certainly works.
>
> It's probably because you're applying gravity to the ball
> even when it's touching the ground. So it repeatdly gets
> accelerated downwards for one time step, then moved back
> up to ground level.
>
> In real life, when the ball is touching the ground, the
> downward force of gravity is balanced by an upward force
> from the ground, so the net acceleration is zero. You
> can model that by only applying gravity if the ball is
> above ground level.
>

I believe this is the most reliable solution.
(AFAIK there is no upward force though; it's more to do with the
pressure the object exerts being insufficient to penetrate the ground.
So for a simple physics simulation, this solution is also more
correct.)

Matt: I assume you didn't mean to use a suggestive subject line, Every
time I look at my mail I see this conversation, read the subject line
rather differently than you intended, and delete it. And then someone
posts another reply, so it appears again.. ._.


Re: [pygame] Why does my ball vibrate?

2007-12-01 Thread Matt Smith

Douglas Bagnall wrote:

FLOOR = 384

if ypos >= FLOOR and velocity > 0:
overstep = ypos - FLOOR
ypos = FLOOR - overstep * bounce
velocity = -velocity * bounce


I tried using this code and I go back to the situation where the ball continues 
to bounce when it should have come to rest. I would imagine this is because each 
time the ball passes below the floor we are giving it a little bit of upwards 
velocity.





Re: [pygame] Why does my ball vibrate?

2007-11-29 Thread Douglas Bagnall
Greg Ewing wrote:

> Matt Smith wrote:
>> I can't quite get my head around WHY the ball sinks into the ground if
>> I don't set ypos back to 384 but it certainly works.
> 
> It's probably because you're applying gravity to the ball
> even when it's touching the ground. So it repeatdly gets
> accelerated downwards for one time step, then moved back
> up to ground level.
> 
> In real life, when the ball is touching the ground, the
> downward force of gravity is balanced by an upward force
> from the ground, so the net acceleration is zero. You
> can model that by only applying gravity if the ball is
> above ground level.
> 


I have a slightly different take on it: assuming an instantaneous
bounce, the ball should have travelled up in proportion to the distance
it would have sunk below, like so:

FLOOR = 384

if ypos >= FLOOR and velocity > 0:
overstep = ypos - FLOOR
ypos = FLOOR - overstep * bounce
velocity = -velocity * bounce

This is unrealistic, of course, since a ball spends a period of time
deforming before bouncing back up, but at least it is consistent, while
just truncating gives you a different error each time.

But, anyway, the reason it sinks is it always goes down further than it
can come up, because bounce < 1 and gravity points down, so the upward
velocity is always less than the downward. For a period towards the end
of the journey, the upward velocity will not be enough to lift the ball
above the floor before it turns to drop again. Rounding might exacerbate
this.


douglas


Re: [pygame] Why does my ball vibrate?

2007-11-29 Thread Greg Ewing

Matt Smith wrote:
I can't quite get my head around WHY the ball sinks into the ground if I 
don't set ypos back to 384 but it certainly works.


It's probably because you're applying gravity to the ball
even when it's touching the ground. So it repeatdly gets
accelerated downwards for one time step, then moved back
up to ground level.

In real life, when the ball is touching the ground, the
downward force of gravity is balanced by an upward force
from the ground, so the net acceleration is zero. You
can model that by only applying gravity if the ball is
above ground level.

--
Greg


Re: [pygame] Why does my ball vibrate?

2007-11-29 Thread Ian Mallett
For your amusement (and envy), here's some beautiful simulations.  On their
bouncing ball: the ball is rendered in 3D and actually deforms noticeably
and oscillates after bouncing.  I think it was here:
http://www.gamedev.net/community/forums/topic.asp?topic_id=465499
You'll have to go to the project page and download the demo(s), but it's
worth it.
Ian


Re: [pygame] Why does my ball vibrate?

2007-11-29 Thread Ian Mallett
I actually got interested in the problem, and tried to recreate your code,
even though you practically gave it to me.  Had to sleep though :) I'll
finish when I get home.

Anyway, as long as the physics time engine is based on your fps, any point
when the window is moved will cause a drop in the fps.  I recommend timing
the loop and getting an average speed going.  Then, pause the simulation if
the loop time is longer than that.  That way, when you release-click, the
ball will move from where it started.

You could just forget timing and estimate, which I don't recommend if you
want accuracy.

Ian


Re: [pygame] Why does my ball vibrate?

2007-11-29 Thread Matt Smith

Thanks for all the help.

I have modified the code for determining the position of the ball so 
that the pixel co-ordinates are always integers:


ypos = ypos + int(((velocity + newvelocity) / 2) * time_passed * 160)

This has cured the vibrating problem and the ball comes to rest quite 
realistically. I tried consolidating the code checking for the floor but 
found that the ball sinks into the floor unless I always correct an over 
shoot of the floor to the co-ordinates of the floor.


# Reverse velocity taking into account bounciness if we hit the ground
if ypos >= 384 and velocity > 0:
# Avoid the ball sinking into the ground
ypos = 384
velocity = -velocity * bounce

I can't quite get my head around WHY the ball sinks into the ground if I 
don't set ypos back to 384 but it certainly works.


I am interested by the idea of separating the physics calculations from 
the frame rate calculations but I really don't know where to start.


Thanks again,

Matt



Re: [pygame] Why does my ball vibrate?

2007-11-28 Thread Ian Mallett
On Nov 28, 2007 3:38 PM, Kevin <[EMAIL PROTECTED]> wrote:

> A somewhat simple fix to get the ball to stop bouncing would be like this
> (put after velocity is set to newvelocity):
> if abs(velocity) < 0.01:
>   gravity = 0
>   velocity = 0

I'm not sure that would work.  At the top of a parabolic arc, Physics says
that the ball's velocity is temporarily zero.  So if you're starting from
rest, it won't fall at all.  If it starts with some velocity, it will stop
at the maximum of the parabola.  A solution would be to have it also check
for the ground level.  When I wrote Marble Madness, (
http://www.pygame.org/project/471/), for example, exploding particles hung
disturbingly in the air until I checked for the ground level (mercifully
simple, because it was flat).

Incidentally, I noticed your parameter for gravity was 9.8.  As in 9.8m/s.
I'm probably stating the obvious, but I'm guessing you're using the
framerate to calculate to motion because it's a simulation, which requires
accurate time.

Also, I suggest that you change the collision detection to: "
if ypos => 384 and velocity > 0:
velocity = -velocity * bounce
" ("==" becomes "=>") so that you don't have the extra code.

Hope this helps,
Ian


Re: [pygame] Why does my ball vibrate?

2007-11-28 Thread Kevin
A somewhat simple fix to get the ball to stop bouncing would be like this
(put after velocity is set to newvelocity):
if abs(velocity) < 0.01:
  gravity = 0
  velocity = 0

This is just a quick fix though (and alters gravity, which is bad), you
probably want to use Patrick's or James'.

On Nov 28, 2007 1:57 PM, James Paige <[EMAIL PROTECTED]> wrote:

> On Wed, Nov 28, 2007 at 08:26:19PM +, Matt Smith wrote:
> > Hi,
> >
> > I am beginning to learn Pygame and I have written a short program to
> > simulate a bouncing ball. The motion of the ball is pretty realistic
> > until it has almost come to rest. The ball continues to vibrate long
> > after you would have expected it to come to a complete rest (or be
> > moving less than 1 pile each time as it will never stop moving 100%).
> > Also, the ball can start to bounce higher again if I click somewhere
> > else on the desktop. I can't work out why this happens so can anyone
> > shed some light on it and suggest how I can prevent it.Here's my code:
>
> I have two suggestions, see below:
>
> > #! /usr/bin/python
> >
> > import sys, pygame
> > pygame.init()
> >
> > xpos = 92
> > ypos = 0
> > gravity = 9.8
> > velocity = 0
> > # How much of the velocity of the ball is retained on a bounce
> > bounce = 0.8
> >
> > screen = pygame.display.set_mode((200, 400), 0, 32)
> > # The ball is a 16px sprite
> > ball = pygame.image.load('ball.png')
> > clock = pygame.time.Clock()
> >
> > # The main loop
> > while True:
> >
> > # Test for exit
> > for event in pygame.event.get():
> > if event.type == pygame.QUIT:
> > exit()
> >
> > # The physics
> > # Reverse velocity taking into account bounciness if we hit the
> ground
> > if ypos == 384 and velocity > 0:
>
> Here you probably want "if ypos >= 384 ..." rather than checking the
> precise pixel position, since your ball could be falling fast enough to
> skip over pixel 384. I notice you have a check for that later, but why
> not put it here?
>
> > velocity = -velocity * bounce
> > time_passed = clock.tick(60) / 1000.0
> > newvelocity = velocity + (gravity * time_passed)
> > # Use the average velocity over the period of the frame to change
> > position
> > ypos = ypos + (((velocity + newvelocity) / 2) * time_passed * 160)
>
> Your velocity is a floating point number, and floating point math has
> limited precision. Because your ball sprite is being displayed only at
> integer pixel coordinates, you ought not to let your xpos and ypos have
> decmals. I suggest you change the above line to this:
>
>  ypos = ypos + int(((velocity + newvelocity) / 2) * time_passed * 160)
>
> This throws away the decmal on the ypos, but preserves it for your
> velocity. This fix alone, even without the ypos == 384 thing I mentioned
> above, is enough to solve your bouncing problem.
>
> > # Prevent the ball from sinking into the ground
> > if ypos >= 384:
> > ypos = 384
> > velocity = newvelocity
> >
> > # Update the screen
> > screen.fill((0, 0, 0))
> > screen.blit(ball, (xpos, ypos))
> > pygame.display.update()
> >
> > Thanks for looking.
> >
> > Matt
>
> You are welcome :)
>
> ---
> James Paige
>



-- 
This, from Jach.

How many programmers does it take to change a light bulb?
None. It's a hardware problem.

How many Microsoft programmers does it take to change a light bulb?
None. Microsoft just declared darkness as the newest innovation in
cutting-edge technology.


Re: [pygame] Why does my ball vibrate?

2007-11-28 Thread James Paige
On Wed, Nov 28, 2007 at 08:26:19PM +, Matt Smith wrote:
> Hi,
> 
> I am beginning to learn Pygame and I have written a short program to 
> simulate a bouncing ball. The motion of the ball is pretty realistic 
> until it has almost come to rest. The ball continues to vibrate long 
> after you would have expected it to come to a complete rest (or be 
> moving less than 1 pile each time as it will never stop moving 100%). 
> Also, the ball can start to bounce higher again if I click somewhere 
> else on the desktop. I can't work out why this happens so can anyone 
> shed some light on it and suggest how I can prevent it.Here's my code:

I have two suggestions, see below:

> #! /usr/bin/python
> 
> import sys, pygame
> pygame.init()
> 
> xpos = 92
> ypos = 0
> gravity = 9.8
> velocity = 0
> # How much of the velocity of the ball is retained on a bounce
> bounce = 0.8
> 
> screen = pygame.display.set_mode((200, 400), 0, 32)
> # The ball is a 16px sprite
> ball = pygame.image.load('ball.png')
> clock = pygame.time.Clock()
> 
> # The main loop
> while True:
> 
> # Test for exit
> for event in pygame.event.get():
> if event.type == pygame.QUIT:
> exit()
> 
> # The physics
> # Reverse velocity taking into account bounciness if we hit the ground
> if ypos == 384 and velocity > 0:

Here you probably want "if ypos >= 384 ..." rather than checking the 
precise pixel position, since your ball could be falling fast enough to 
skip over pixel 384. I notice you have a check for that later, but why 
not put it here?

> velocity = -velocity * bounce
> time_passed = clock.tick(60) / 1000.0
> newvelocity = velocity + (gravity * time_passed)
> # Use the average velocity over the period of the frame to change 
> position
> ypos = ypos + (((velocity + newvelocity) / 2) * time_passed * 160)

Your velocity is a floating point number, and floating point math has 
limited precision. Because your ball sprite is being displayed only at 
integer pixel coordinates, you ought not to let your xpos and ypos have 
decmals. I suggest you change the above line to this:

  ypos = ypos + int(((velocity + newvelocity) / 2) * time_passed * 160)

This throws away the decmal on the ypos, but preserves it for your 
velocity. This fix alone, even without the ypos == 384 thing I mentioned 
above, is enough to solve your bouncing problem.

> # Prevent the ball from sinking into the ground
> if ypos >= 384:
> ypos = 384
> velocity = newvelocity
> 
> # Update the screen
> screen.fill((0, 0, 0))
> screen.blit(ball, (xpos, ypos))
> pygame.display.update()
> 
> Thanks for looking.
> 
> Matt

You are welcome :)

---
James Paige


Re: [pygame] Why does my ball vibrate?

2007-11-28 Thread Patrick Mullen
When I click and hold on the window of the demo, the simulation
"pauses".  This accounts for the ball bouncing high into the sky.
Since the game has paused for a while, the framerate has gone wy
down, resulting in a very large time_passed amount.  When put into the
rest of the system, this very large time_passed makes the new velocity
very large, resulting in it shooting high above.  A better method of
handling physics, is to run a certain number of physics steps
depending on how much time has passed, so that variable time doesn't
cause issues such as this.  So it would be like:

def physics_step(blah):
"""Physics to be run for 1/30th of a second"""
update_velocity()
collision_detection()

As to the bouncy problem, you see this happen in even established
physics engines such as ODE.  What you have to do is have some sort of
threshold in which to stop the physics and "freeze" the moving
objects, when they collide with something at some small amount of
speed.  The inaccuracies have to do with floating point math, which is
never going to be exactly 0, in addition to your allowance of the ball
falling through the floor before calculating the velocity.  Here's my
quick and dirty modification:

# The physics
# Reverse velocity taking into account bounciness if we hit the ground
newvelocity = velocity + (gravity * time_passed)
# Use the average velocity over the period of the frame to change position
ypos = ypos + (((velocity + newvelocity) / 2) * time_passed * 160)
if ypos >= 384:
ypos = 384
newvelocity = -newvelocity * bounce
if velocity < 1:   #Alter this value for the "freeze" to
happen at different speeds
ypos = 384
newvelocity = 0
# Prevent the ball from sinking into the ground
velocity = newvelocity

On Nov 28, 2007 12:26 PM, Matt Smith <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I am beginning to learn Pygame and I have written a short program to
> simulate a bouncing ball. The motion of the ball is pretty realistic
> until it has almost come to rest. The ball continues to vibrate long
> after you would have expected it to come to a complete rest (or be
> moving less than 1 pile each time as it will never stop moving 100%).
> Also, the ball can start to bounce higher again if I click somewhere
> else on the desktop. I can't work out why this happens so can anyone
> shed some light on it and suggest how I can prevent it.Here's my code:
>
> #! /usr/bin/python
>
> import sys, pygame
> pygame.init()
>
> xpos = 92
> ypos = 0
> gravity = 9.8
> velocity = 0
> # How much of the velocity of the ball is retained on a bounce
> bounce = 0.8
>
> screen = pygame.display.set_mode((200, 400), 0, 32)
> # The ball is a 16px sprite
> ball = pygame.image.load('ball.png')
> clock = pygame.time.Clock()
>
> # The main loop
> while True:
>
>  # Test for exit
>  for event in pygame.event.get():
>  if event.type == pygame.QUIT:
>  exit()
>
>  # The physics
>  # Reverse velocity taking into account bounciness if we hit the ground
>  if ypos == 384 and velocity > 0:
>  velocity = -velocity * bounce
>  time_passed = clock.tick(60) / 1000.0
>  newvelocity = velocity + (gravity * time_passed)
>  # Use the average velocity over the period of the frame to change
> position
>  ypos = ypos + (((velocity + newvelocity) / 2) * time_passed * 160)
>  # Prevent the ball from sinking into the ground
>  if ypos >= 384:
>  ypos = 384
>  velocity = newvelocity
>
>  # Update the screen
>  screen.fill((0, 0, 0))
>  screen.blit(ball, (xpos, ypos))
>  pygame.display.update()
>
> Thanks for looking.
>
> Matt
>
>
>
>
>


[pygame] Why does my ball vibrate?

2007-11-28 Thread Matt Smith

Hi,

I am beginning to learn Pygame and I have written a short program to 
simulate a bouncing ball. The motion of the ball is pretty realistic 
until it has almost come to rest. The ball continues to vibrate long 
after you would have expected it to come to a complete rest (or be 
moving less than 1 pile each time as it will never stop moving 100%). 
Also, the ball can start to bounce higher again if I click somewhere 
else on the desktop. I can't work out why this happens so can anyone 
shed some light on it and suggest how I can prevent it.Here's my code:


#! /usr/bin/python

import sys, pygame
pygame.init()

xpos = 92
ypos = 0
gravity = 9.8
velocity = 0
# How much of the velocity of the ball is retained on a bounce
bounce = 0.8

screen = pygame.display.set_mode((200, 400), 0, 32)
# The ball is a 16px sprite
ball = pygame.image.load('ball.png')
clock = pygame.time.Clock()

# The main loop
while True:

# Test for exit
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()

# The physics
# Reverse velocity taking into account bounciness if we hit the ground
if ypos == 384 and velocity > 0:
velocity = -velocity * bounce
time_passed = clock.tick(60) / 1000.0
newvelocity = velocity + (gravity * time_passed)
# Use the average velocity over the period of the frame to change 
position

ypos = ypos + (((velocity + newvelocity) / 2) * time_passed * 160)
# Prevent the ball from sinking into the ground
if ypos >= 384:
ypos = 384
velocity = newvelocity

# Update the screen
screen.fill((0, 0, 0))
screen.blit(ball, (xpos, ypos))
pygame.display.update()

Thanks for looking.

Matt