-
Notifications
You must be signed in to change notification settings - Fork 0
/
ship.py
41 lines (32 loc) · 1.4 KB
/
ship.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import math
from entity import Entity
class Ship(Entity):
def __init__(self, surface, pixelsPerUnit):
Entity.__init__(self, surface, pixelsPerUnit)
self.topSpeed = 0.5
self.topAngularSpeed = 180.0
self.maxAcceleration = 1.0
self.maxAngularAcceleration = 180
def update(self, timestep, target):
displacementX = target[0]-self.position[0]
displacementY = target[1]-self.position[1]
distance = math.sqrt(displacementX**2 + displacementY**2)
if distance == 0: distance = 1
normalX = displacementX/distance
normalY = displacementY/distance
accelerationX = normalX*self.maxAcceleration
accelerationY = normalY*self.maxAcceleration
self.velocity = self.velocity[0]+accelerationX*timestep, self.velocity[1]+accelerationY*timestep
angleTo = math.atan2(normalY, normalX)*180/math.pi+180
angleDifference = angleTo - self.angle
if angleDifference > 180: angleDifference -= 360
angularAcceleration = angleDifference/180*self.maxAngularAcceleration
self.angularVelocity += angularAcceleration*timestep
relativeSpeed = self.speed()/self.topSpeed
if relativeSpeed > 1:
self.velocity = (self.velocity[0]/relativeSpeed, self.velocity[1]/relativeSpeed)
if self.angularVelocity > self.topAngularSpeed:
self.angularVelocity = self.topAngularSpeed
elif self.angularVelocity < -self.topAngularSpeed:
self.angularVelocity = -self.topAngularSpeed
Entity.update(self, timestep)