示例#1
0
    def update(self, dt):
        """Updates the position and the velocity of the shell.

        Moves the shell based on it's current velocity and the time passed (`dt`).
        Updates the velocity based on `self.gravity`, `self.wind`, `self.drag_coef` and the time passed.

        Args:
            dt: Delta t, the change of time the shell should be updated by.
        """
        # move the shell based on the current velocity and change of time
        self.center = Vector(*self.center) + Vector(*self.velocity) * dt
        # apply gravitational acceleration
        self.velocity_y -= self.gravity * dt

        air_vel = Vector(self.velocity_x - self.wind, self.velocity_y)

        # based on https://en.wikipedia.org/wiki/Drag_equation
        # hides the density, area and other constants for the shell into the drag coefficient
        drag = air_vel.normalize() * self.drag_coef * air_vel.length2()
        vel_vec = Vector(*self.velocity) - (drag / self.mass)
        self.velocity = (vel_vec.x, vel_vec.y)

        # bounce off the walls
        if (self.x < 0) or (self.right > self.parent.width):
            self.velocity_x *= -1
            self.right = clamp(self.right, 0, self.parent.width)
            self.x = clamp(self.x, 0, self.parent.width)

        if self.top > self.parent.height:
            self.velocity_y *= -1
            self.top = clamp(self.top, 0, self.parent.height)
示例#2
0
文件: widgets.py 项目: wilsaj/flingy
 def gravitate_towards(self, body):
     velocity_v = Vector(self.velocity)
     gravity_v = Vector(body.pos) - Vector(self.pos)
     self.velocity = velocity_v + \
                     (gravity_v * 1. / gravity_v.length2()) * body.mass
示例#3
0
 def gravitate_towards(self, body):
     velocity_v = Vector(self.velocity)
     gravity_v = Vector(body.pos) - Vector(self.pos)
     self.velocity = velocity_v + (gravity_v * 1.0 / gravity_v.length2()) * body.mass