Sunday, August 29, 2021

kv 45 perspective transform shift 2


#galaxy.py

from kivy.app import App
from kivy.graphics import Line
from kivy.metrics import dp
from kivy.properties import NumericProperty, Clock
from kivy.uix.widget import Widget


class Galaxy(Widget):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.n_vlines = 11
        self.n_hlines = 9
        self.space_vlines = dp(100)
        self.speed_hl = 10
        self.step_hl = 0
        self.clock = 0.2
        self.speed_vl = 10
        self.step_vl = 0
        self.direction_vl = 1

        with self.canvas:
            self.vlines = []
            self.hlines = []
            for i in range(self.n_vlines):
                vline = Line(points=(0,0,0,100))
                self.vlines.append(vline)
            for j in range(self.n_hlines):
                hline = Line(points=(0,0,100,0))
                self.hlines.append(hline)

        Clock.schedule_interval(self.update, self.clock)

    def on_size(self, *args):
        pass

    def perspective_transform(self, x, y):
        #return x, y
        ratio = (self.height-y)/self.height
        tx = ratio*(self.center_x-x)
        ty = ratio**2
        return self.center_x-tx, 0.8*self.height*(1-ty)

    def update(self, dt):
        start_x = self.center_x - int(self.n_vlines / 2) * self.space_vlines
        shift_x = self.speed_vl * self.step_vl * dt / self.clock
        if shift_x > self.width or shift_x < -self.width:
            self.direction_vl = -self.direction_vl
        self.step_vl += self.direction_vl

        for i in range(self.n_vlines):
            x = start_x + self.space_vlines * i + shift_x
            x1, y1, x2, y2 = x, 0, x, self.height

            x1, y1 = self.perspective_transform(x1, y1)
            x2, y2 = self.perspective_transform(x2, y2)

            self.vlines[i].points = (x1, y1, x2, y2)

        space_hlines = self.height / self.n_hlines
        shift = self.speed_hl * self.step_hl * dt / self.clock
        if shift > space_hlines:
            self.step_hl = 0
            shift = 0
        self.step_hl += 1

        for j in range(self.n_hlines):
            y = j * space_hlines - shift
            x1, y1, x2, y2 = start_x + shift_x, y, self.width - start_x + shift_x, y

            _, y1 = self.perspective_transform(x1, y1)
            x1, _ = self.perspective_transform(x1, y1 / 0.8)
            _, y2 = self.perspective_transform(x2, y2)
            x2, _ = self.perspective_transform(x2, y2 / 0.8)

            self.hlines[j].points = (x1, y1, x2, y2)


class GalaxyApp(App):
    pass


GalaxyApp().run()

reference:

No comments:

Post a Comment