Python 2.7 - Pygame - intersecting two moving sprites -
i've got 2 sprites (well... 3). 1 sprite traveling in straight line @ steady speed @ random angle (in radians). other sprite shooting @ moving sprite. projectile sprite lands behind target because projectile given destination based on center of target when it's fired. have update, silly if projectile changed directions homing missile.
so, how on earth go this?
class projectile (pygame.sprite.sprite): container = pygame.sprite.group() def __init__ (self, pos, target): pygame.sprite.sprite.__init__ (self, self.container) self.image = fireball self.rect = self.image.get_rect() self.rect.center = pos self.true_loc = self.rect.center self.destination = target.rect.center self.speed = 200 diffx = self.destination[0]-self.rect.center[0] diffy = self.destination[1]-self.rect.center[1] self.angle = math.atan2(diffx, diffy) def combinecoords (self, coord1, coord2): return map(sum, zip(*[coord1, coord2])) def update (self, time_passed): new_move = (math.sin(self.angle) * time_passed * self.speed, math.cos(self.angle) * time_passed * self.speed) self.true_loc = self.combinecoords(self.true_loc, new_move) self.rect.center = self.true_loc
the target class instance same thing specific class attributes needs. specifically, don't know how calculate distance target travel based off it's angle , speed based on distance projectile has travel there. both have varying speeds , angles. should have paid attention in math class...
edit: dawned on me it's going trickier anticipated, dynamically move sprites based on player's framerate. attempting calculate distance between projectile , target.center, divide number how many frames pass until reaches it's destination, update self.destination moving target's angle multiplying it's movement , frames. method if player's screen hitches whatever reason (which common) projectile miss. great... i'm square one, haha.
if fix timestep, have stability in physics. can keep variable video framerate, constant timestep physics.
the famous fix-your timestep link http://gafferongames.com/game-physics/fix-your-timestep/ , pygame example: http://www.pygame.org/wiki/constantgamespeed?parent=cookbook
theres's bunch of info , sublinks @ https://gamedev.stackexchange.com/questions/4995/predicting-enemy-position-in-order-to-have-an-object-lead-its-target
Comments
Post a Comment