-
Notifications
You must be signed in to change notification settings - Fork 7
/
smooth_2d.cpp
167 lines (131 loc) · 4.34 KB
/
smooth_2d.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright (c) 2019 Lawnjelly
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "smooth_2d.h"
#include "core/engine.h"
#define SMOOTHCLASS Smooth2D
#define SMOOTHNODE Node2D
#include "smooth_body.inl"
// finish the bind with custom stuff
}
#undef SMOOTHCLASS
#undef SMOOTHNODE
////////////////////////////////
Smooth2D::Smooth2D() {
m_Flags = 0;
SetFlags(SF_ENABLED | SF_TRANSLATE | SF_ROTATE | SF_SCALE);
set_process_priority(100);
Engine::get_singleton()->set_physics_jitter_fix(0.0);
}
float Smooth2D::LerpAngle(float from, float to, float weight) const {
return from + ShortAngleDist(from, to) * weight;
}
float Smooth2D::ShortAngleDist(float from, float to) const {
float max_angle = (3.14159265358979323846264 * 2.0);
float difference = fmod(to - from, max_angle);
return fmod(2.0f * difference, max_angle) - difference;
}
void Smooth2D::RefreshTransform(Node2D *pTarget) {
//print_line("RefreshTransform");
ClearFlags(SF_DIRTY);
// keep the data flowing...
m_Prev = m_Curr;
// global or local?
bool bGlobal = TestFlags(SF_GLOBAL_IN);
if (bGlobal) {
// new transform for this tick
if (TestFlags(SF_TRANSLATE)) {
m_Curr.pos = pTarget->get_global_position();
m_ptTranslateDiff = m_Curr.pos - m_Prev.pos;
}
if (TestFlags(SF_ROTATE))
m_Curr.angle = pTarget->get_global_rotation();
if (TestFlags(SF_SCALE))
m_Curr.scale = pTarget->get_global_scale();
} else {
// new transform for this tick
if (TestFlags(SF_TRANSLATE)) {
m_Curr.pos = pTarget->get_position();
m_ptTranslateDiff = m_Curr.pos - m_Prev.pos;
}
if (TestFlags(SF_ROTATE))
m_Curr.angle = pTarget->get_rotation();
if (TestFlags(SF_SCALE))
m_Curr.scale = pTarget->get_scale();
}
}
void Smooth2D::FrameUpdate() {
//print_line("FrameUpdate");
Node2D *pTarget = GetTarget();
if (!pTarget)
return;
if (TestFlags(SF_DIRTY))
RefreshTransform(pTarget);
// interpolation fraction
float f = Engine::get_singleton()->get_physics_interpolation_fraction();
// global or local?
bool bGlobal = TestFlags(SF_GLOBAL_OUT);
if (bGlobal) {
if (TestFlags(SF_TRANSLATE)) {
Point2 ptNew = m_Prev.pos + (m_ptTranslateDiff * f);
set_global_position(ptNew);
}
if (TestFlags(SF_ROTATE)) {
// need angular interp
float r = LerpAngle(m_Prev.angle, m_Curr.angle, f);
set_global_rotation(r);
}
if (TestFlags(SF_SCALE)) {
Size2 s = m_Prev.scale + ((m_Curr.scale - m_Prev.scale) * f);
set_global_scale(s);
}
} else {
if (TestFlags(SF_TRANSLATE)) {
Point2 ptNew = m_Prev.pos + (m_ptTranslateDiff * f);
set_position(ptNew);
}
if (TestFlags(SF_ROTATE)) {
// need angular interp
float r = LerpAngle(m_Prev.angle, m_Curr.angle, f);
set_rotation(r);
}
if (TestFlags(SF_SCALE)) {
Size2 s = m_Prev.scale + ((m_Curr.scale - m_Prev.scale) * f);
set_scale(s);
}
}
}
void Smooth2D::teleport() {
Node2D *pTarget = GetTarget();
if (!pTarget)
return;
// refresh all components during teleport
int temp_flags = m_Flags;
SetFlags(SF_TRANSLATE | SF_ROTATE | SF_SCALE | SF_LERP);
RefreshTransform(pTarget);
// set previous equal to current
m_Prev = m_Curr;
m_ptTranslateDiff.x = 0.0f;
m_ptTranslateDiff.y = 0.0f;
// call frame upate to make sure all components of the node are set
FrameUpdate();
// restore flags
m_Flags = temp_flags;
}
//bool Smooth2D::FindVisibility() const
//{
// return is_visible_in_tree();
//}