Ejemplo n.º 1
0
    def report_collision_vs_world(cls, px, py, dx, dy):
        p = DP(cls.pos)
        o = DP(cls.oldpos)

        # calc velocity
        vx = p.x - o.x
        vy = p.y - o.y

        # find component of velocity parallel to collision normal
        dp = vx * dx + vy * dy
        # project velocity onto collision normal
        # nx, ny is normal velocity
        nx = dp * dx
        ny = dp * dy

        # px, py is tangent velocity
        tx = vx - nx
        ty = vy - ny

        # we only want to apply collision response forces
        # if the object is travelling into, and not out of, the collision
        if dp < 0:
            # f = cls.settings.get('FRICTION')
            f = 0.05
            fx = tx * f
            fy = ty * f

            # self bounce constant should be else:where, i.e inside the object/tile/etc..
            # b = 1 + cls.settings.get('BOUNCE')
            b = 1 + 0.3
            bx = nx * b
            by = ny * b
        else:
            # moving out of collision, do not apply forces
            bx = by = fx = fy = 0

        # project object out of collision
        p.x = p.x + px
        p.y = p.y + py

        # apply bounce+friction impulses which alter velocity
        o.x = o.x + px + bx + fx
        o.y = o.y + py + by + fy

        cls.pos = DP(p)
        cls.oldpos = DP(o)
Ejemplo n.º 2
0
    def integrate_verlet(cls):
        d = 1.0
        g = 0.2

        p, o = DP(cls.pos), DP(cls.oldpos)

        ox, oy = o.x, o.y

        # integrate
        p.x = p.x + (d * p.x) - (d * ox)
        p.y = p.y + (d * p.y) - (d * oy) + g

        cls.oldpos = DP(cls.pos)
        cls.pos = p