zvepb
@zvepb

Как анимировать функции в ursina?

Как я могу добавить анимацию в данный пример, чтобы ось вращалась без моего участия и клавиш по заданной траектории ?

from ursina import *


app = Ursina()

origin = Entity(model='cube', scale=(0.2, 0.2, 0.2))

Entity(model='cube', parent=origin, color=color.red  , position=(5, 0, 0), scale=(10, 0.5, 0.5))
Entity(model='cube', parent=origin, color=color.green, position=(0, 5, 0), scale=(0.5, 10, 0.5))
Entity(model='cube', parent=origin, color=color.blue , position=(0, 0, 5), scale=(0.5, 0.5, 10))

x_handle = Entity()
y_handle = Entity()
z_handle = Entity()


def switch_parent(entity, parent):
    old_world_rotation = entity.world_rotation
    entity.parent = parent
    entity.world_rotation = old_world_rotation


def update():
    old_parent = origin.parent
    switch_parent(origin, x_handle)
    x_handle.rotation_x += held_keys['q'] * 50 * time.dt
    x_handle.rotation_x -= held_keys['a'] * 50 * time.dt

    switch_parent(origin, y_handle)
    y_handle.rotation_y += held_keys['w'] * 50 * time.dt
    y_handle.rotation_y -= held_keys['s'] * 50 * time.dt

    switch_parent(origin, z_handle)
    z_handle.rotation_z += held_keys['e'] * 50 * time.dt
    z_handle.rotation_z -= held_keys['d'] * 50 * time.dt

    switch_parent(origin, old_parent)


app.run()
  • Вопрос задан
  • 120 просмотров
Решения вопроса 1
Rodja
@Rodja
from ursina import *


app = Ursina()

origin = Entity(model='cube', scale=(0.2, 0.2, 0.2))

Entity(model='cube', parent=origin, color=color.red  , position=(5, 0, 0), scale=(10, 0.5, 0.5))
Entity(model='cube', parent=origin, color=color.green, position=(0, 5, 0), scale=(0.5, 10, 0.5))
Entity(model='cube', parent=origin, color=color.blue , position=(0, 0, 5), scale=(0.5, 0.5, 10))

x_handle = Entity()
y_handle = Entity()
z_handle = Entity()

# определяем точки на траектории вращения каждой из осей
x_trajectory = [0, 90, 180, 270, 360]
y_trajectory = [0, 90, 180, 270, 360]
z_trajectory = [0, 90, 180, 270, 360]

# определяем время анимации
animation_time = 5

def switch_parent(entity, parent):
    old_world_rotation = entity.world_rotation
    entity.parent = parent
    entity.world_rotation = old_world_rotation


def update():
    old_parent = origin.parent
    switch_parent(origin, x_handle)

    # вращение оси X по траектории
    x_start = x_trajectory[0]
    x_end = x_trajectory[-1]
    x_time = animation_time
    x_current = x_handle.rotation_x
    x_target = x_trajectory[int(time.time() * 0.5 % len(x_trajectory))]
    x_handle.rotation_x = lerp(x_current, x_target, time.dt / x_time)

    switch_parent(origin, y_handle)

    # вращение оси Y по траектории
    y_start = y_trajectory[0]
    y_end = y_trajectory[-1]
    y_time = animation_time
    y_current = y_handle.rotation_y
    y_target = y_trajectory[int(time.time() * 0.3 % len(y_trajectory))]
    y_handle.rotation_y = lerp(y_current, y_target, time.dt / y_time)

    switch_parent(origin, z_handle)

    # вращение оси Z по траектории
    z_start = z_trajectory[0]
    z_end = z_trajectory[-1]
    z_time = animation_time
    z_current = z_handle.rotation_z
    z_target = z_trajectory[int(time.time() * 0.1 % len(z_trajectory))]
    z_handle.rotation_z = lerp(z_current, z_target, time.dt / z_time)

    switch_parent(origin, old_parent)


app.run()
Ответ написан
Комментировать
Пригласить эксперта
Ответы на вопрос 1
@pavelpressf
rotation_animation = animation.sequence(
    animation.create_animation('rotation_x', 0, 360, duration=2, curve=animation.curve.linear),
    animation.create_animation('rotation_x', 360, 0, duration=2, curve=animation.curve.linear),
    loop=True
)

x_handle.animate(rotation_animation)
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы