Rice-IIIP (1)

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值