Beispiel #1
0
    def update(self, timestep):
        positions = np.stack([planet.position for planet in self._objects], axis=0)
        masses = np.array([np.array(planet.mass) for planet in self._objects]) * kg
        forces = gravity(positions, masses)
        force_tot = np.sum(forces, axis=1)

        for i, planet in enumerate(self._objects):
            planet.update_position(timestep)
            planet.update_velocity(timestep, force_tot[i])
Beispiel #2
0
    def update(self, delta):

        tmp = self.dy
        self.dy = self.y

        if self.y > 550:
            self.onGround = True

        if not self.onGround:
            self.y -= physics.gravity(self.y, tmp, delta)
Beispiel #3
0
 def update(self, dt):
     """ update method"""
     
     if not self._grounded:
         # call the gravity function to move the player in a 
         # kinda realistic way
         gravity_calc = physics.gravity(self._velocity_y, dt)
         self._ypos += gravity_calc[0]
         self._velocity_y = gravity_calc[1]
         
         # move the player in on the x-axis if there
         # is a velocity
         self._xpos += self._velocity_x
     else:
         # set both x and y velocity to 0 if the player is grounded
         self._velocity_y = 0
         self._velocity_x = 0
         
     self._portal_rect[0], self._portal_rect[1] = self._xpos, self._ypos
Beispiel #4
0
    # Event: Holding button
    hero.float(pressed_keys[K_LSHIFT])
    hero.movement(pressed_keys[K_a], pressed_keys[K_d])

    # Event change in mouse pos
    facing = (mouse_pos - (hero.pos + hero.size * 0.5)).normalize()
    hero.set_facing(facing)

    # Update, unit:
    for i in all_unit:
        if not i.update_all(my_map, time_passed):
            all_unit.remove(i)

    # Update, physics
    physics.gravity(hero, time_passed, g)

    # blit
    screen.blit(background, (0, 0))

    for i in my_map.block:
        temp = pygame.Surface(i.size)
        temp.fill((0, 200, 0))
        screen.blit(temp, i.topleft)

    hero_image = pygame.Surface(hero.size.value())
    hero_image.fill(hero.color)
    hero.set_image(hero_image)

    for i in all_unit:
        screen.blit(i.image, i.pos.value())
Beispiel #5
0
    def step(self, dt, push=False, train=False):
        """step once by dt seconds"""
        self.time_elapsed += dt
        """update positions"""
        self.state[:2] += dt * self.state[2:]
        """friction"""
        #self.state[2:]
        """calculate gravity"""
        r = physics.pythagoras(self.state[0], self.state[1], 0, 0)
        gravity_F = physics.gravity(self.G, self.m_1, self.m_2, r)

        a = gravity_F / self.m_1

        norm = physics.norm_vector(0 - self.state[:2])

        gravity_vector = norm * a * dt
        """add gravity"""
        self.state[2:] += gravity_vector
        """ calculate air resistance """
        dist = r - self.r_2
        resis_F = physics.a_resis(dist, np.linalg.norm(self.state[2:]),
                                  self.drag, self.area)

        resis_a = resis_F / self.m_1
        resis_norm = (0 - self.state[2:]) / np.linalg.norm(0 - self.state[2:])
        resis_vector = resis_norm * resis_a * dt
        self.state[2:] += resis_vector

        push_vector = [0, 0]
        if push:
            push_force = self.pulse_strength

            if push % 2 == 0:
                norm = (0 - self.state[2:]) / np.linalg.norm(0 -
                                                             self.state[2:])
            else:
                norm = (self.state[2:]) / np.linalg.norm(self.state[2:])

            push_vector = norm * push_force / self.m_1 * dt
            """add gravity"""
            self.state[2:] += push_vector

        x1 = self.state[0]
        y1 = self.state[1]

        x2 = np.cos(2 * np.pi / 86400 * self.time_elapsed)
        y2 = np.sin(2 * np.pi / 86400 * self.time_elapsed)

        h = physics.pythagoras(self.state[0], self.state[1], 0, 0) - self.r_2

        a = np.arctan2(x1 * y2 - y1 * x2, x1 * x2 + y1 * y2)

        v = np.linalg.norm(self.state[2:])

        if h < 0:
            x_2 = np.cos(2 * np.pi / 86400 * self.time_elapsed)
            y_2 = np.sin(2 * np.pi / 86400 * self.time_elapsed)
            score = 180 - physics.angle_between_vectors(
                self.state[:2], [x_2, y_2])
            if train:
                return np.array([a, h, v]), score, True
            else:
                return self.state[:2]

        if train:
            test_model = OrbitModel(init_state=self.state,
                                    m_1=self.m_1,
                                    drag=self.drag,
                                    pulse_strength=self.pulse_strength)

            if self.time_elapsed % 600 == 1:

                i = 0
                while True:
                    pos = test_model.step(dt)
                    test_h = physics.pythagoras(pos[0], pos[1], 0,
                                                0) - self.r_2
                    if test_h < 0:
                        x_2 = np.cos(2 * np.pi / 86400 *
                                     test_model.time_elapsed)
                        y_2 = np.sin(2 * np.pi / 86400 *
                                     test_model.time_elapsed)
                        self.score = 180 - physics.angle_between_vectors(
                            pos, [x_2, y_2])
                        break
                    # elif i == 43200 - 1:
                    #     self.score = 0
                    #     break
                    # print(i, end="\r")
                    i += 1
                # print(i)
                # print(str(test_h) + "                 " + str(self.score), end="\r")

            return np.array([a, h, v]), self.score, False
        else:
            return self.state[:2], push_vector
Beispiel #6
0
    movex = 0
    movey = 0

    # Handle input
    if keystate.state('LEFT'):
        movex += -2.0
    elif keystate.state('RIGHT'):
        movex += 2.0
    if keystate.state('DOWN'):
        ''' nothing currently '''
        #movey += 2.0
    if keystate.state('UP'):
        if player.onGround:
            movey += -5.5
    
    movey -= physics.gravity(player.y, player.dy, delta)

    bound = collision.calcBound(player.nextModel(movex,movey), (player.x+movex, player.y+movey), player.size)
    static = collision.calcBound(*som.getStaticGrid((player.x, player.y), 4, 4))

    cols = collision.collision(bound, static)
    collision.moveable(player, cols, movex, movey, delta)

    # Draw stuff
    screen.fill((0,0,0))
    som.blitBackground()
    som.blitGround()
    player.draw(screen)
    som.blitForeground()

    fpsClock.tick(60)