def projectile_velocity(start, dest, move_force): start, dest = Vec2d(start), Vec2d(dest) dx, dy = (dest.x - start.x), (dest.y - start.y) if dx == 0: dx += 1 if dy == 0: dy += 1 slope = abs(dy / dx) vel = Vec2d(1, 1) if dx < 0: vel.x *= -1 if dy < 0: vel.y *= -1 vel.y *= (slope / (slope + 1)) vel.x *= (1 / (slope + 1)) vel = Vec2d.normalized(vel) vel *= move_force return vel
def move(self): if self.up and self.down: return if self.right and self.left: return force = Vec2d(0.0, 0.0) if self.up: force.y = -1 elif self.down: force.y = 1 if self.left: force.x = -1 elif self.right: force.x = 1 force *= self.move_force if force.x != 0 and force.y != 0: # Normalize diagonal force force = self.move_force * Vec2d.normalized(force) if force.x != 0 or force.y != 0: self.body.apply_force_at_local_point(force, (0, 0)) else: self._toggle_sprinting(False)