class Vehicle(object): def __init__(self, location, velocity, target): self.location = location self.velocity = velocity self.acceleration = Vector(0.1, 0.1) self.max_speed = 1 self.max_force = 1 self.target = target def seek(self, target): # find vector to target desired = Vector.static_sub(target, self.location) desired.normalize() desired.mult(self.max_speed) # Reynold's formula for steering force steering_force = Vector.static_sub(desired, self.velocity) # apply force self.acceleration.add(steering_force) # recalc position self.velocity.add(self.acceleration) self.location.add(self.velocity) print(self.location.x, self.location.y) def update(self, canvas): self.seek(self.target) def render(self, canvas): # seeker canvas.create_circle(self.location.x, self.location.y, 7, fill='black') # target canvas.create_circle(self.target.x, self.target.y, 7, fill='red')
class Square(object): def __init__(self, position=None, velocity=None): self.img = Image.open(os.path.join('..', 'assets', 'black_square.png')) self.tk_img = ImageTk.PhotoImage(self.img.rotate(0)) self.position = Vector(60, 60) if position is None else position self.velocity = Vector(0, 0) if velocity is None else position self.acceleration = Vector(0.01, 0.01) self.side = 17 self.angle = 0 self.angular_velocity = 0 self.angular_acceleration = 0 def update(self, canvas): width = int(canvas['width']) height = int(canvas['height']) if self.position.x > width: self.position.x = 0 if self.position.y > height: self.position.y = 0 self.velocity.add(self.acceleration) self.position.add(self.velocity) self.angle = (math.degrees(math.atan2(self.velocity.y, self.velocity.x))) print(self.angle) def render(self, canvas): self.tk_img = ImageTk.PhotoImage(self.img.rotate(self.angle)) canvas.create_image(50, 50, image=self.tk_img) def event_left_pressed(self, event): self.velocity.y += 1 def event_right_pressed(self, event): self.velocity.y -= 1
from matplotlib import pyplot as plt from vectors.vector import Vector axes = plt.axes() v1 = Vector(0, 0, 3, 0) v2 = Vector(3, 0, 0, 4) v3 = Vector.add(v1, v2) axes.arrow(v1.x, v1.y, v1.dx, v1.dy, ec='r') axes.arrow(v2.x, v2.y, v2.dx, v2.dy, ec='g') axes.arrow(v3.x, v3.y, v3.dx, v3.dy, ec='b') plt.axis([0, 5, 0, 5]) plt.title('Adding Vectors') plt.show()