def execute(self, obj): pos = V.vector(obj.rect.center) targpos = obj.target.rect.center traveltime = V.length(pos-targpos)/obj.maxspeed targvel = obj.target.velocity futurepos = targpos + targvel*traveltime desired_velocity = obj.maxspeed*V.normalize(futurepos - pos) obj.steering = desired_velocity - obj.velocity V.truncate(obj.steering, obj.maxforce)
def update(self): self.prevpos = V.vector(self.rect.center).copy() self.state.execute(self) ## Add a tiny force toward the center of the field if False: #self.state != S.Wait(): center = V.vector(pygame.display.get_surface().get_rect().center) towardcenter = center - self.rect.center V.normalize_ip(towardcenter) self.steering += 0.5 * towardcenter self.velocity += FORCE_PER_TICK * self.steering / (self.mass) V.truncate(self.velocity, self.maxspeed) speed = V.length(self.velocity) if speed > 0.001: self.heading = V.normalize(self.velocity) self.rect.center += self.velocity self.depth = -self.rect.centery #determine which image direction to use, based on heading and velocity: if speed >= 0.001: small = 0.382 # sin(22.5 degrees) big = 0.923 # cos(22.5 degrees) x, y = self.heading if y >= big: self.dir = 's' elif small <= y: if x > 0: self.dir = 'se' else: self.dir = 'sw' elif -small <= y: if x > 0: self.dir = 'e' else: self.dir = 'w' elif -big <= y: if x > 0: self.dir = 'ne' else: self.dir = 'nw' else: self.dir = 'n' else: self.velocity[0] = self.velocity[1] = 0.0 #image action: stopped or moving? if speed < 0.001: self.aspeed = 0.5 action = 'looking' else: self.aspeed = 0.2 * speed action = 'walking' self.images = Images.get(self.name)[self.dir][action] self.nframes = len(self.images) #advance animation frame self.frame = self.aspeed + self.frame while self.frame >= self.nframes: self.frame -= self.nframes self.image = self.images[int(self.frame)]
def execute(self, obj): pos = V.vector(obj.rect.center) targpos = obj.target.rect.center traveltime = V.length(pos - targpos) / obj.maxspeed targvel = obj.target.velocity futurepos = targpos + targvel * traveltime desired_velocity = obj.maxspeed * V.normalize(pos - futurepos) obj.steering = desired_velocity - obj.velocity V.truncate(obj.steering, obj.maxforce)
def update(self): self.prevpos = V.vector(self.rect.center).copy() self.state.execute(self) ## Add a tiny force toward the center of the field if False:#self.state != S.Wait(): center = V.vector(pygame.display.get_surface().get_rect().center) towardcenter = center - self.rect.center V.normalize_ip(towardcenter) self.steering += 0.5*towardcenter self.velocity += FORCE_PER_TICK*self.steering/(self.mass) V.truncate(self.velocity, self.maxspeed) speed = V.length(self.velocity) if speed > 0.001: self.heading = V.normalize(self.velocity) self.rect.center += self.velocity self.depth = -self.rect.centery #determine which image direction to use, based on heading and velocity: if speed >= 0.001: small = 0.382 # sin(22.5 degrees) big = 0.923 # cos(22.5 degrees) x,y = self.heading if y >= big: self.dir = 's' elif small <= y: if x > 0: self.dir = 'se' else: self.dir = 'sw' elif -small <= y: if x > 0: self.dir = 'e' else: self.dir = 'w' elif -big <= y: if x > 0: self.dir = 'ne' else: self.dir = 'nw' else: self.dir = 'n' else: self.velocity[0] = self.velocity[1] = 0.0 #image action: stopped or moving? if speed < 0.001: self.aspeed = 0.5 action = 'looking' else: self.aspeed = 0.2*speed action = 'walking' self.images = Images.get(self.name)[self.dir][action] self.nframes = len(self.images) #advance animation frame self.frame = self.aspeed+self.frame while self.frame >= self.nframes: self.frame -= self.nframes self.image = self.images[int(self.frame)]
def execute(self, obj): pos = obj.rect.center wp = obj.wanderpoint hd = obj.heading angle = math.atan2(wp[1], wp[0]) angle += random.uniform(-0.5,0.5) wp[0] = math.cos(angle) wp[1] = math.sin(angle) seekvec = WANDER_OFFSET*hd + WANDER_RADIUS*wp desired_velocity = obj.maxspeed * V.normalize(seekvec) obj.steering = desired_velocity - obj.velocity V.truncate(obj.steering, obj.maxforce)
def execute(self, obj): pos = obj.rect.center wp = obj.wanderpoint hd = obj.heading angle = math.atan2(wp[1], wp[0]) angle += random.uniform(-0.5, 0.5) wp[0] = math.cos(angle) wp[1] = math.sin(angle) seekvec = WANDER_OFFSET * hd + WANDER_RADIUS * wp desired_velocity = obj.maxspeed * V.normalize(seekvec) obj.steering = desired_velocity - obj.velocity V.truncate(obj.steering, obj.maxforce)
def execute(self, obj): pos = V.vector(obj.rect.center) endpos = obj.target.rect.center desired_velocity = obj.maxspeed*V.normalize(pos - endpos) obj.steering = desired_velocity - obj.velocity V.truncate(obj.steering, obj.maxforce)
def execute(self, obj): pos = V.vector(obj.rect.center) endpos = obj.target.rect.center desired_velocity = obj.maxspeed * V.normalize(endpos - pos) obj.steering = desired_velocity - obj.velocity V.truncate(obj.steering, obj.maxforce)