problem with exam task for college

Oscar Benjamin oscar.j.benjamin at gmail.com
Sun Jan 13 18:28:42 CET 2013


On 13 January 2013 13:57,  <jeltedeproft at hotmail.com> wrote:
> this is again a newer version, right now the velocity does in fact turn, but the view doesn't follow, it keeps the ship vertical.
>
> i'm also having trouble letting the flame appear when pressing the "up" button
>
> and when the ship rotates the horizontal velocity keeps getting bigger and bigger
>
> i also have to make the game end when the ship hits the moon on the wrong place
>
> i'm kinda stressed out because this has to be done by the 15th i've been studying like crazy the past week. If anyone could help me out i will deeply appreciate it, here is the code
>
[SNIP]
>
>     def update(self,dt):
>         self.velocity = self.velocity + (self.acceleration * dt)
>         self.pos = self.pos + self.velocity * dt
>         print self.velocity
>
>         if scene.kb.keys:
>             key = scene.kb.getkey()
>             if key == "left":
>                 # linkerafwijking
>                 self.frame.axis -= (0, 0, 0.05)
>                 self.gas = vector(-sin(self.angle), cos(self.angle))
>                 self.vlam.visible = True
>                 self.updatemeter = True
>
>             elif key == "right":
>                 # rechterafwijking
>                 self.frame.axis += (0, 0, 0.05)
>                 self.gas = vector(sin(self.angle), cos(self.angle))
>
>             elif key == "up":
>                 self.velocity += self.gas
>                 self.frame.pos += self.velocity
>         else:
>             self.vlam.visible = False
>             self.updatemeter = False
>             self.velocity -= self.gas
>
>         if self.pos.x > 250:
>             self.pos.x = -250
>         if self.pos.x < -250:
>             self.pos.x = 250
>         self.view.update(self)
[SNIP]

The function above is poorly organised. You should break this into
several logical steps:
1) Process keypresses to update discrete state variables (such as
whether or not the ship is thrusting.
2) Compute from the discrete state variables of the system what the
acceleration will be, e.g.:
    acceleration = gravity
    if thrusting:
        acceleration += gas
3) Only once the acceleration is known apply the formulas to update
the continuous state (position/velocity) of your system.

If you rearrange it like this then you will probably be able to fix
the logic bugs in the function above. In particular it is important
that there should be only one place where you actually change each
kind of state variable (velocity is modified in three places in the
function above and I'm sure that this is related to the bugs you are
having). Similar considerations apply to updating the angle of the
ship.

Also the update formula for the position is wrong. If I've understood
your physics correctly, you should be using the equations of constant
acceleration (my students know these as the "suvat" equations). In
that case the formula should look like:

    newposition = oldposition + oldvelocity*dt + acceleration*dt**2
    newvelocity = oldvelocity + acceleration*dt

Your code is missing the quadratic term at the end of the first line
above. (Actually the situation would be more complicated than this
when the ship is also rotating but I don't understand exactly what
your rotation physics is supposed to be).


Oscar



More information about the Python-list mailing list