Spaceship orientation
two fields
self.angle - ship orientation (scalar/float)
self.angle_vel - ship’s angular velocity(scalar/float)
Update method
self.angle += self.angle_vel
Draw method
canvas.draw_image(self.image, …, …, …, …, self.angle)
Relating position, velocity, and acceleration
Basic physics
position - point
velocity - vector
acceleration - vector
Postion update
position += velocity
Velocity update
velocity += acceleration
Adding acceleration to the spaceship
Ship class - four fields
self.pos - ship’s position (vector/pair of floats)
self.vel - ship’s velocity(vector/pair of floats)
self.angle - ship’s orientation(scalar/float)
self.thrust - whether ship is accelerating in forward direction(Boolean)
Position update
self.pos[0] += self.vel[0]
self.pos[1] += self.vel[1]
Velocity update - acceleration in direction of forward vector
forward = [math.cos(self.angle), math.sin(self.angle)]
if sefl.thrust:
self.vel[0] += forward[0]
self.vel[1] += forward[1]
Adding friction to the spaceship
Friction - let c be a small constant
friction = (-c)* velocity
acceleration = thrust + fricton
velocity = (1-c)*velocity + thrust
Postion update
self.pos[0] += self.vel[0]
self.pos[1] += self.vel[1]
Friction update
self.vel[0] *= (1-c)
self.vel[1] *= (1-c)
Thrust update - acceleration in direction of forward vector
forward = [math.cos(self.angle), math.sin(self.angle)]
if self.thrust:
self.vel[0] += forward[0]
self.vel[1] += forward[1]
Week 7 quiz-a question 7
Consider a spaceship where the ship’s thrusters can accelerate the ship by 10 pixels per second for each second that the thrust key is held down. If the friction induces a deceleration that is 10% of the ship’s velocity per second, what is the maximal velocity of the ship?
i = 0
velocity= 10
while i < 1000:
velocity = (1-0.1)*velocity + 10
i+=1
print velocity
Answer: 100