From 9b60deedc508c74b249fd1da0776b5a719322807 Mon Sep 17 00:00:00 2001 From: group1 Date: Sun, 9 Oct 2016 23:13:51 -0400 Subject: Realistic small fish movement --- fish_show.py | 50 ++++++++++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 22 deletions(-) (limited to 'fish_show.py') diff --git a/fish_show.py b/fish_show.py index f3ccf25..7cd8300 100644 --- a/fish_show.py +++ b/fish_show.py @@ -1,5 +1,6 @@ from __future__ import division import color +from color import Vector import random import constants from constants import * @@ -18,20 +19,23 @@ class Fish(object): self.speed = step self.step = step - self.speed = Vector(10, 0) - self.drag = -1 + self.speed = Vector(0.1, 0) + self.drag = -0.09 - self.tailAcceleration = color.Vector(5, 0) + self.tailAccelMag = 0.4 + self.tailAcceleration = Vector(self.tailAccelMag, 0) self.tailDuration = 2 self.tailMoving = False - self.tailThreshold = 5 + self.tailThreshold = 0.7 self.tailMoveCount = 0 def update(self): """Swim gently to the other side of the bridge.""" - self.speed += self.drag * self.speed / abs(self.speed) + self.speed += self.drag * self.speed if abs(self.speed) < self.tailThreshold: self.tailMoving = True + self.tailThreshold = rand_in_range(0.5, 1.4) + self.fix_direction() if self.tailMoveCount > self.tailDuration: self.tailMoveCount = 0 self.tailMoving = False @@ -47,22 +51,21 @@ class Fish(object): self.x = -self.width if self.x < -self.width: self.x = constants.BRIDGE_WIDTH - if self.y < -self.width: - self.y = constants.BRIDGE_HEIGHT + self.height - if self.y > -self.height: - self.y = constants.BRIDGE_HEIGHT - - def fix_direction(self, theta=None): - if theta is None: - theta = random.random * math.pi / 2 - math.pi / 4 - costheta = math.cos(theta) - sintheta = math.sin(theta) - speedx = self.tailAcceleration.components[0] - speedy = self.tailAcceleration.components[1] - data.tailAcceleration = color.Vector( - speedx * costheta - speedy * sintheta, - speedx * sintheta + speedy * costheta - ) + + print(self.x, self.y) + + def fix_direction(self): + min_theta = -0.32 + max_theta = 0.32 + if self.y < 0: + min_theta = 0 + if self.y > 1: + max_theta = 0 + + #theta = rand_in_range(min_theta, max_theta) + theta = random.gauss((max_theta+min_theta)/2, (max_theta - min_theta)/12) + self.tailAcceleration = Vector(self.tailAccelMag, 0).rot2d(theta) + class Light(object): """Represent a bridge light and its state.""" @@ -144,10 +147,13 @@ class FishShow(Show): self.lights = [Light(x, y, self.bridge.get_light(x, y)) for y in xrange(self.bridge.HEIGHT) for x in xrange(self.bridge.WIDTH)] - self.fish = Fish(0, 0, 23, 1.5, color.black, 1) + self.fish = Fish(0, 0.5, 23, 1.5, color.black, 1) def update(self): self.fish.update() for light in self.lights: light.update(self.fish) + +def rand_in_range(low, high): + return random.random() * (high - low) + low -- cgit v1.3.1