njin
Engine 2D gọn nhẹ để tự tay làm game theo ý mình. Vibe coding cũng ổn luôn
Loading...
Searching...
No Matches
_tween.h
1#pragma once
2#include "_math.h"
3
4namespace njin {
7
12struct timer {
13 f32 duration = 1.0f;
14 bool repeat = false;
15 f32 time = 0.0f;
16 bool finished = false;
17
22 bool tick(f32 dt) {
23 if (finished || duration <= 0.0f)
24 return false;
25 time += dt;
26 if (time < duration)
27 return false;
28 if (repeat) {
29 while (time >= duration)
30 time -= duration;
31 } else {
32 time = duration;
33 finished = true;
34 }
35 return true;
36 }
37
40 f32 progress() const {
41 return duration > 0.0f ? clamp(time / duration, 0.0f, 1.0f) : 1.0f;
42 }
43
45 void reset() {
46 time = 0.0f;
47 finished = false;
48 }
49};
50
70
75inline f32 ease_apply(ease curve, f32 t) {
76 t = clamp(t, 0.0f, 1.0f);
77 switch (curve) {
78 case ease::linear:
79 return t;
80 case ease::in_quad:
81 return t * t;
82 case ease::out_quad:
83 return t * (2.0f - t);
85 return t < 0.5f ? 2.0f * t * t : 1.0f - 2.0f * (1.0f - t) * (1.0f - t);
86 case ease::in_cubic:
87 return t * t * t;
88 case ease::out_cubic: {
89 const f32 u = 1.0f - t;
90 return 1.0f - u * u * u;
91 }
92 case ease::in_out_cubic: {
93 const f32 u = 1.0f - t;
94 return t < 0.5f ? 4.0f * t * t * t : 1.0f - 4.0f * u * u * u;
95 }
96 case ease::in_sine:
97 return 1.0f - std::cos(t * pi * 0.5f);
98 case ease::out_sine:
99 return std::sin(t * pi * 0.5f);
101 return 0.5f - 0.5f * std::cos(t * pi);
102 case ease::in_back: {
103 const f32 s = 1.70158f;
104 return t * t * ((s + 1.0f) * t - s);
105 }
106 case ease::out_back: {
107 const f32 s = 1.70158f;
108 const f32 u = t - 1.0f;
109 return 1.0f + u * u * ((s + 1.0f) * u + s);
110 }
111 case ease::out_bounce: {
112 const f32 n = 7.5625f;
113 const f32 d = 2.75f;
114 if (t < 1.0f / d)
115 return n * t * t;
116 if (t < 2.0f / d) {
117 t -= 1.5f / d;
118 return n * t * t + 0.75f;
119 }
120 if (t < 2.5f / d) {
121 t -= 2.25f / d;
122 return n * t * t + 0.9375f;
123 }
124 t -= 2.625f / d;
125 return n * t * t + 0.984375f;
126 }
128 if (t <= 0.0f || t >= 1.0f)
129 return t;
130 return std::pow(2.0f, -10.0f * t) *
131 std::sin((t * 10.0f - 0.75f) * (2.0f * pi / 3.0f)) +
132 1.0f;
133 }
134 return t;
135}
136
141template <class T> struct tween {
142 T from{};
143 T to{};
144 f32 duration = 1.0f;
146 f32 time = 0.0f;
147
150 T value() const {
151 const f32 t = duration > 0.0f ? time / duration : 1.0f;
152 return lerp(from, to, ease_apply(curve, t));
153 }
154
158 T tick(f32 dt) {
159 time = time + dt > duration ? duration : time + dt;
160 return value();
161 }
162
165 bool done() const { return time >= duration; }
166
168 void reset() { time = 0.0f; }
169};
170
171} // namespace njin
constexpr f32 clamp(f32 v, f32 lo, f32 hi)
Giới hạn v trong khoảng [lo, hi].
Definition _math.h:165
constexpr vec3 lerp(vec3 a, vec3 b, f32 t)
Nội suy tuyến tính giữa hai vector.
Definition _math.h:158
constexpr f32 pi
Số pi.
Definition _math.h:10
f32 ease_apply(ease curve, f32 t)
Áp đường cong lên tiến độ t.
Definition _tween.h:75
ease
Đường cong chuyển động của tween.
Definition _tween.h:54
@ out_elastic
Rung như lò xo ở cuối.
Definition _tween.h:68
@ out_cubic
Giảm tốc mạnh.
Definition _tween.h:60
@ out_sine
Giảm tốc theo hình sin.
Definition _tween.h:63
@ in_out_quad
Tăng rồi giảm tốc nhẹ.
Definition _tween.h:58
@ in_cubic
Tăng tốc mạnh.
Definition _tween.h:59
@ in_out_cubic
Tăng rồi giảm tốc mạnh.
Definition _tween.h:61
@ in_out_sine
Tăng rồi giảm tốc theo hình sin.
Definition _tween.h:64
@ out_back
Vượt quá đích một chút rồi quay về.
Definition _tween.h:66
@ linear
Đều.
Definition _tween.h:55
@ out_quad
Giảm tốc nhẹ.
Definition _tween.h:57
@ in_back
Lùi lại một chút trước khi đi.
Definition _tween.h:65
@ in_sine
Tăng tốc theo hình sin.
Definition _tween.h:62
@ out_bounce
Nảy ở cuối như quả bóng rơi.
Definition _tween.h:67
@ in_quad
Tăng tốc nhẹ.
Definition _tween.h:56
float f32
Số thực 32 bit.
Definition _types.h:28
Bộ đếm thời gian: báo khi hết giờ.
Definition _tween.h:12
f32 time
Thời gian đã trôi qua.
Definition _tween.h:15
bool finished
Đã hết giờ (chỉ dùng khi không lặp).
Definition _tween.h:16
bool tick(f32 dt)
Cho thời gian trôi.
Definition _tween.h:22
void reset()
Bắt đầu lại từ 0.
Definition _tween.h:45
f32 progress() const
Tiến độ từ 0 đến 1.
Definition _tween.h:40
f32 duration
Thời lượng, tính bằng giây.
Definition _tween.h:13
bool repeat
Hết giờ thì tự bắt đầu lại.
Definition _tween.h:14
Chuyển mượt một giá trị từ from đến to trong duration giây.
Definition _tween.h:141
f32 time
Thời gian đã trôi qua.
Definition _tween.h:146
T from
Giá trị lúc đầu.
Definition _tween.h:142
f32 duration
Thời lượng, tính bằng giây.
Definition _tween.h:144
T value() const
Giá trị hiện tại.
Definition _tween.h:150
T to
Giá trị lúc cuối.
Definition _tween.h:143
ease curve
Đường cong chuyển động.
Definition _tween.h:145
void reset()
Bắt đầu lại từ from.
Definition _tween.h:168
bool done() const
Đã chạy hết chưa.
Definition _tween.h:165
T tick(f32 dt)
Cho thời gian trôi rồi trả về giá trị mới.
Definition _tween.h:158