njin
Engine 2D gọn nhẹ để tự tay làm game theo ý mình. Vibe coding cũng ổn luôn
Toggle main menu visibility
Loading...
Searching...
No Matches
_tween.h
1
#pragma once
2
#include "_math.h"
3
4
namespace
njin {
7
12
struct
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
54
enum class
ease
{
55
linear
,
56
in_quad
,
57
out_quad
,
58
in_out_quad
,
59
in_cubic
,
60
out_cubic
,
61
in_out_cubic
,
62
in_sine
,
63
out_sine
,
64
in_out_sine
,
65
in_back
,
66
out_back
,
67
out_bounce
,
68
out_elastic
,
69
};
70
75
inline
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);
84
case
ease::in_out_quad
:
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);
100
case
ease::in_out_sine
:
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
}
127
case
ease::out_elastic
:
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
141
template
<
class
T>
struct
tween
{
142
T
from
{};
143
T
to
{};
144
f32
duration
= 1.0f;
145
ease
curve
=
ease::linear
;
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
njin::clamp
constexpr f32 clamp(f32 v, f32 lo, f32 hi)
Giới hạn v trong khoảng [lo, hi].
Definition
_math.h:165
njin::lerp
constexpr vec3 lerp(vec3 a, vec3 b, f32 t)
Nội suy tuyến tính giữa hai vector.
Definition
_math.h:158
njin::pi
constexpr f32 pi
Số pi.
Definition
_math.h:10
njin::ease_apply
f32 ease_apply(ease curve, f32 t)
Áp đường cong lên tiến độ t.
Definition
_tween.h:75
njin::ease
ease
Đường cong chuyển động của tween.
Definition
_tween.h:54
njin::ease::out_elastic
@ out_elastic
Rung như lò xo ở cuối.
Definition
_tween.h:68
njin::ease::out_cubic
@ out_cubic
Giảm tốc mạnh.
Definition
_tween.h:60
njin::ease::out_sine
@ out_sine
Giảm tốc theo hình sin.
Definition
_tween.h:63
njin::ease::in_out_quad
@ in_out_quad
Tăng rồi giảm tốc nhẹ.
Definition
_tween.h:58
njin::ease::in_cubic
@ in_cubic
Tăng tốc mạnh.
Definition
_tween.h:59
njin::ease::in_out_cubic
@ in_out_cubic
Tăng rồi giảm tốc mạnh.
Definition
_tween.h:61
njin::ease::in_out_sine
@ in_out_sine
Tăng rồi giảm tốc theo hình sin.
Definition
_tween.h:64
njin::ease::out_back
@ out_back
Vượt quá đích một chút rồi quay về.
Definition
_tween.h:66
njin::ease::linear
@ linear
Đều.
Definition
_tween.h:55
njin::ease::out_quad
@ out_quad
Giảm tốc nhẹ.
Definition
_tween.h:57
njin::ease::in_back
@ in_back
Lùi lại một chút trước khi đi.
Definition
_tween.h:65
njin::ease::in_sine
@ in_sine
Tăng tốc theo hình sin.
Definition
_tween.h:62
njin::ease::out_bounce
@ out_bounce
Nảy ở cuối như quả bóng rơi.
Definition
_tween.h:67
njin::ease::in_quad
@ in_quad
Tăng tốc nhẹ.
Definition
_tween.h:56
njin::f32
float f32
Số thực 32 bit.
Definition
_types.h:28
njin::timer
Bộ đếm thời gian: báo khi hết giờ.
Definition
_tween.h:12
njin::timer::time
f32 time
Thời gian đã trôi qua.
Definition
_tween.h:15
njin::timer::finished
bool finished
Đã hết giờ (chỉ dùng khi không lặp).
Definition
_tween.h:16
njin::timer::tick
bool tick(f32 dt)
Cho thời gian trôi.
Definition
_tween.h:22
njin::timer::reset
void reset()
Bắt đầu lại từ 0.
Definition
_tween.h:45
njin::timer::progress
f32 progress() const
Tiến độ từ 0 đến 1.
Definition
_tween.h:40
njin::timer::duration
f32 duration
Thời lượng, tính bằng giây.
Definition
_tween.h:13
njin::timer::repeat
bool repeat
Hết giờ thì tự bắt đầu lại.
Definition
_tween.h:14
njin::tween
Chuyển mượt một giá trị từ from đến to trong duration giây.
Definition
_tween.h:141
njin::tween::time
f32 time
Thời gian đã trôi qua.
Definition
_tween.h:146
njin::tween::from
T from
Giá trị lúc đầu.
Definition
_tween.h:142
njin::tween::duration
f32 duration
Thời lượng, tính bằng giây.
Definition
_tween.h:144
njin::tween::value
T value() const
Giá trị hiện tại.
Definition
_tween.h:150
njin::tween::to
T to
Giá trị lúc cuối.
Definition
_tween.h:143
njin::tween::curve
ease curve
Đường cong chuyển động.
Definition
_tween.h:145
njin::tween::reset
void reset()
Bắt đầu lại từ from.
Definition
_tween.h:168
njin::tween::done
bool done() const
Đã chạy hết chưa.
Definition
_tween.h:165
njin::tween::tick
T tick(f32 dt)
Cho thời gian trôi rồi trả về giá trị mới.
Definition
_tween.h:158
_tween.h
Được biên soạn bởi
1.18.0