Пример #1
0
    def solve_collision(self, b1, b2):
        # Changes the direction of non-static moving bodies, and separates overlapping bodies

        def penetration(normal, movable_body, fixed_body):
            if normal.x < -0.0001:
                return abs(movable_body.rect.right() - fixed_body.rect.left())
            elif normal.x > 0.0001:
                return abs(fixed_body.rect.right() - movable_body.rect.left())
            if normal.y < -0.0001:
                return abs(fixed_body.rect.top() - movable_body.rect.bottom())
            else:
                return abs(movable_body.rect.top() - fixed_body.rect.bottom())

        if b1.is_static and not (b2.is_static):
            normal = self.calculate_normal(b2, b1)
            pen_distance = penetration(normal, b2, b1)
            b2.rect.position = vector.sum(b2.rect.position, vector.mul(normal, pen_distance))
            b2.direction = vector.reflect(normal, b2.direction)
            return normal
        elif not (b1.is_static) and b2.is_static:
            normal = self.calculate_normal(b1, b2)
            pen_distance = penetration(normal, b1, b2)
            b1.rect.position = vector.sum(b1.rect.position, vector.mul(normal, pen_distance))
            b1.direction = vector.reflect(normal, b1.direction)
            return normal
        elif not (b1.is_static) and not (b2.is_static):
            normal = self.calculate_normal(b1, b2)
            normal_inv = vector.minus(normal)
            pen_distance = penetration(normal, b1, b2)
            b1.rect.set_pos(vector.sum(b1.rect.position, vector.mul(normal, 0.5 * pen_distance)))
            b1.direction = vector.reflect(normal, b1.direction)
            b2.rect.position = vector.sum(b2.rect.position, vector.mul(normal_inv, 0.5 * pen_distance))
            b2.direction = vector.reflect(normal_inv, b2.get_direction())
            return normal
Пример #2
0
    def __backward(self, dout):
        dbeta = dout.sum(axis=0)
        dgammax = dout

        dgamma = ve.sum(ve.mul(self.xhat, dgammax), axis=0)
        dxhat = dgammax * self.gamma

        divar = ve.sum(ve.mul(dxhat, self.xmu), axis=0)
        dxmu1 = dxhat * self.ivar

        dsqrtvar = divar * (1 / (self.sqrtvar**2)) * (-1)

        dvar = (1 / ve.sqrt(self.var + 10e-7)) * dsqrtvar * 0.5

        dsq = ve.arange([
            1 for i in range(reduce(lambda x, y: x * y, dout.get_demension()))
        ]).reshape(dout.get_demension()) * (dvar / dout.get_demension()[1])

        dxmu2 = ve.mul(self.xmu, dsq) * 2

        dx1 = dxmu1 + dxmu2
        dmu = ve.sum(dxmu1 + dxmu2, axis=0) * (-1)

        dx2 = ve.arange([
            1 for i in range(reduce(lambda x, y: x * y, dout.get_demension()))
        ]).reshape(dout.get_demension()) * (dmu / dout.get_demension()[1])

        dx = dx1 + dx2

        return dx
Пример #3
0
    def update(self, step_time):
        def change_dir_vel(entities, direction, velocity):
            for entity in entities:
                entity.body.direction = direction
                entity.body.set_velocity(velocity)

        if (self.moving_left or self.moving_right):
            if self.moving_left:
                change_dir_vel(self.paddles, Vector2(-1, 0), PADDLE_VELOCITY)
                if self.game_status == GameLayer.INITIALIZATION:
                    change_dir_vel(self.balls, Vector2(-1, 0), PADDLE_VELOCITY)
            else:
                change_dir_vel(self.paddles, Vector2(1, 0), PADDLE_VELOCITY)
                if self.game_status == GameLayer.INITIALIZATION:
                    change_dir_vel(self.balls, Vector2(1, 0), PADDLE_VELOCITY)
        else:
            change_dir_vel(self.paddles, ZERO2, magnitude(ZERO2))
            if self.game_status == GameLayer.INITIALIZATION:
                change_dir_vel(self.balls, ZERO2, magnitude(ZERO2))

        if self.push_balls and self.game_status == GameLayer.INITIALIZATION:
            for ball in self.balls:
                if ball.body.is_static:
                    ball.body.is_static = False
            v = Vector2(BALL_VELOCITY_X, BALL_VELOCITY_Y)
            change_dir_vel(self.balls, normalize(v), magnitude(v))
            self.push_balls = False
            self.game_status = GameLayer.GAME_LOOP

        # Remove bricks that have been destroyed
        free_brick_list = []
        for brick in self.bricks:
            if brick.health_points == 0:
                self.unregister_entity(brick)
                free_brick_list.append(brick)
        self.bricks = [b for b in self.bricks if free_brick_list.count(b) == 0]

        for paddle in self.paddles:
            # Integrate paddle
            paddle.body.rect.position = sum(
                paddle.body.rect.position,
                mul(paddle.body.direction, paddle.body.velocity * step_time))

            # Relocate paddle position to a valid position range
            paddle.body.rect.position.x = utils.clamp(
                paddle.body.rect.position.x, 0, WINDOW_WIDTH - PADDLE_WIDTH)
            paddle.body.rect.position.y = WINDOW_HEIGHT - PADDLE_HEIGHT - PADDLE_LINE_SPACING

        for ball in self.balls:
            if ball.body.is_static:
                pos_r = Vector2((PADDLE_WIDTH - BALL_WIDTH) * 0.5,
                                -BALL_HEIGHT)
                ball.body.rect.position = sum(
                    self.paddles[0].body.rect.position, pos_r)
Пример #4
0
    def update(self, step_time):
        def change_dir_vel(entities, direction, velocity):
            for entity in entities:
                entity.body.direction = direction
                entity.body.set_velocity(velocity)
           
        if(self.moving_left or self.moving_right):
            if self.moving_left:
                change_dir_vel(self.paddles, Vector2(-1,0), PADDLE_VELOCITY)
                if self.game_status == GameLayer.INITIALIZATION:
                    change_dir_vel(self.balls, Vector2(-1,0), PADDLE_VELOCITY)  
            else:
                change_dir_vel(self.paddles, Vector2(1,0), PADDLE_VELOCITY)
                if self.game_status == GameLayer.INITIALIZATION:
                    change_dir_vel(self.balls, Vector2(1,0), PADDLE_VELOCITY)    
        else:
            change_dir_vel(self.paddles, ZERO2, magnitude(ZERO2))
            if self.game_status == GameLayer.INITIALIZATION:
                change_dir_vel(self.balls, ZERO2, magnitude(ZERO2))
                    
        if self.push_balls and self.game_status == GameLayer.INITIALIZATION:                  
            for ball in self.balls:
                if ball.body.is_static:
                    ball.body.is_static = False
            v = Vector2(BALL_VELOCITY_X, BALL_VELOCITY_Y) 
            change_dir_vel(self.balls, normalize(v), magnitude(v))
            self.push_balls = False
            self.game_status = GameLayer.GAME_LOOP
             
        # Remove bricks that have been destroyed
        free_brick_list = []
        for brick in self.bricks:
            if brick.health_points == 0:
                self.unregister_entity(brick)
                free_brick_list.append(brick)
        self.bricks = [ b for b in self.bricks if free_brick_list.count(b) == 0 ] 
                
        for paddle in self.paddles:          
            # Integrate paddle
            paddle.body.rect.position = sum(paddle.body.rect.position,
                                            mul(paddle.body.direction, paddle.body.velocity * step_time))
    
            # Relocate paddle position to a valid position range
            paddle.body.rect.position.x = utils.clamp(paddle.body.rect.position.x, 0, 
                                                      WINDOW_WIDTH - PADDLE_WIDTH)
            paddle.body.rect.position.y = WINDOW_HEIGHT - PADDLE_HEIGHT - PADDLE_LINE_SPACING

        for ball in self.balls:
            if ball.body.is_static:
                pos_r = Vector2((PADDLE_WIDTH - BALL_WIDTH) * 0.5,  - BALL_HEIGHT)
                ball.body.rect.position = sum(self.paddles[0].body.rect.position, pos_r)
def identity_2(v1, v2, v3):

    #v1 ×(v2 + v3) = (v1 × v2) + (v1 × v3)

    v4 = vector.sum(v2, v3)
    v4 = vector.cross(v1, v4) #lhs
    v4 = [round(elm, 10) for elm in v4] #elements need to be rounded to be exactly equal
    v5 = vector.cross(v1,v2)
    v6 = vector.cross(v1, v3)
    v7 = vector.sum(v5, v6)
    v7 = [round(elm, 10) for elm in v7] #rhs

    if v4[0]==v7[0] and v4[1]==v7[1] and v4[2]==v7[2]:
        print('v1 ×(v2 + v3) = (v1 x v2) + (v1 × v3)')
    else:
        print('v1 ×(v2 + v3) does not equal (v1 × v2) + (v1 × v3)')
def identity_3(v1, v2, v3):

    #v1 ×(v2 × v3) = (v1 · v3)v2 − (v1 · v2)v3

    v4 = vector.cross(v2, v3)
    v4 = vector.cross(v1, v4) #lhs
    v4 = [round(elm, 10) for elm in v4] #lhs rounded
    s5 = vector.dot(v1, v3)
    s6 = vector.dot(v1, v2)
    s6 = -s6
    v5 = vector.scalar_mult(v2, s5)
    v6 = vector.scalar_mult(v3, s6)
    v7 = vector.sum(v5, v6) #rhs
    v7 = [round(elm, 10) for elm in v7] #rhs rounded

    if v4[0]==v7[0] and v4[1]==v7[1] and v4[2]==v7[2]:
        print('v1 ×(v2 × v3) = (v1 · v3)v2 − (v1 · v2)v3')
    else:
        print('v1 ×(v2 × v3) does not equal (v1 · v3)v2 − (v1 · v2)v3')
def main():

    # Create two random vectors

    v1 = [random.random(), random.random(), random.random()]
    v2 = [random.random(), random.random(), random.random()]
    v3 = [random.random(), random.random(), random.random()]

    #line break in output screen, clearer to read

    print()


    # Print out the vectors to terminal window

    print("v1 = ", v1)
    print("v2 = ", v2)
    print("v3 = ", v3)


    #line break in output screen, clearer to read

    print()

    #print basic vector manipulations

    #vector sum, scalar product and vector (cross) product

    print("v1 + v2 =", vector.sum(v1, v2))
    print('v1 · v2 =', vector.dot(v1, v2))
    print('v1 x v2 =', vector.cross(v1, v2))

    #line break in output screen, clearer to read

    print()


    #testing if the vector identites hold

    identity_1(v1, v2)
    identity_2(v1, v2, v3)
    identity_3(v1, v2, v3)
Пример #8
0
def mul(P, Q):
    w1, v1 = P
    w2, v2 = Q
    return (w1*w2 - _v.dot(v1, v2),_v.sum(_v.mul(w1, v2), _v.mul(w2, v1), _v.cross(v1, v2)))
Пример #9
0
 def integrate(self, elapsed_time):
     if not (self.is_static):
         self.rect.position = vector.sum(
             self.rect.position, vector.mul(self.direction, self.velocity * elapsed_time)
         )