def execute(self, obj): maxforward = obj.maxspeed * obj.heading * 0.5 pressed = pygame.key.get_pressed() if pressed[K_LEFT]: desired_velocity = V.rotate(maxforward, -5) elif pressed[K_RIGHT]: desired_velocity = V.rotate(maxforward, 5) else: desired_velocity = obj.velocity obj.steering = 0.0 obj.velocity = desired_velocity obj.heading = V.normalize(obj.velocity)
def execute(self, obj): maxforward = obj.maxspeed*obj.heading*0.5 pressed = pygame.key.get_pressed() if pressed[K_LEFT]: desired_velocity = V.rotate(maxforward, -5) elif pressed[K_RIGHT]: desired_velocity = V.rotate(maxforward, 5) else: desired_velocity = obj.velocity obj.steering = 0.0 obj.velocity = desired_velocity obj.heading = V.normalize(obj.velocity)
def move(self, env): #physics of forces on sail here #src:https://en.wikipedia.org/wiki/Forces_on_sails#Effect_of_coefficients_of_lift_and_drag_on_forces #f: (angle between wind and sail) -> (total force on sail) # function returns matrix: Matrix * Wind = Total Force def force_on_sails_matrix(wind, sail): #1. get angle between wind and sail orientation = np.sign(np.cross(wind, sail)) angle = v.angle(wind, sail) if np.pi/2 < angle <= np.pi: angle = abs(np.pi - angle) orientation *= -1 #2. get forces on sail (for 0 <= angle <= pi/2) #every ship has different parameters #so this is just game model boat #to test control self-education ability drag = np.exp(abs(angle)/2) - 1 lift = -orientation * np.exp(-20*(angle - 0.6)**2) return np.array([[drag, -lift], [lift, drag]]) #1. rotation k_rotate = 0.05 * v.length(self.speed) #wheel -> rotate coefficient self.speed = v.rotate(self.speed, (-1) * self.wheel * k_rotate) #2. friction k_friction = 0.05 self.speed = self.speed * (1 - k_friction) #3. acceleration = forces on sails -> projection on boat direction k_acceleration = 0.1 wind_apparent = env.wind - self.speed total_force_matrix = force_on_sails_matrix(wind_apparent, v.rotate(self.speed, self.sail)) total_force = np.dot(total_force_matrix, wind_apparent) self.speed += k_acceleration * v.projection(total_force, self.speed) #4. move self.location += self.speed
plt.show() return True #draw_vectors([np.array([1,5]), (2, 3)]) def force_on_sails_matrix(wind, sail): #get angle between wind and sail orientation = np.sign(np.cross(wind, sail)) angle = v.angle(wind, sail) if np.pi/2 < angle <= np.pi: angle = abs(np.pi - angle) orientation *= -1 #get forces on sail (for 0 <= angle <= pi/2) drag = np.exp(abs(angle)/2) - 1 lift = -orientation * np.exp(-20*(angle - 0.6)**2) return np.array([[drag, -lift], [lift, drag]]) wind_apparent = np.array([1, 0]) speed = np.array([1, 0.5]) sail_angle = np.pi/180 * (110) total_matrix = force_on_sails_matrix(wind_apparent, v.rotate(speed, sail_angle)) total = np.dot(total_matrix, wind) print('wind=', wind) draw_vectors([wind, speed, total])