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
|
static const float pi = 3.14159265;
inline float rad(float degval)
{
return degval * pi / 180;
}
inline float deg(float degval)
{
return degval * 180 / pi;
}
template <typename T>
inline T abs(sf::Vector2<T> v)
{
return sqrt(v.x * v.x + v.y * v.y);
}
float get_rotation(sf::Vector2f p, sf::Vector2f o)
{
const sf::Vector2f op = p - o;
if ((op.x == 0 && op.y == 0))
return 0.f;
static const sf::Vector2f zero_rotation(1, 0);
return acos((op.x * zero_rotation.x + op.y * zero_rotation.y) / (abs(op) * abs(zero_rotation)));
}
const sf::Vector2f set_point_rotation(sf::Vector2f p, sf::Vector2f o, float angle)
{
const sf::Vector2f op = p - o;
if ((op.x == 0 && op.y == 0))
return p;
const float r = abs(op);
const sf::Vector2f zero_rotation(r, 0);
const float rotation_rad = get_rotation(p, o);
p.x = o.x + r * cos(rad(angle) - rotation_rad);
p.y = o.y - r * sin(rad(angle) - rotation_rad);
return p;
}
float min4(float a, float b, float c, float d)
{
return std::min(std::min(a, b), std::min(c, d));
}
float max4(float a, float b, float c, float d)
{
return std::max(std::max(a, b), std::max(c, d));
}
void TestExecutable::set_rotation(const int angle)
{
m_sprite.SetRotation(angle);
const float alpha = m_sprite.GetRotation();
// A+----------+D
// | |
// | |
// B+----------+C
const sf::Vector2f A = m_sprite.GetPosition();
sf::Vector2f C = m_sprite.GetPosition() + m_sprite.GetSize();
sf::Vector2f B (A.x, C.y);
sf::Vector2f D (C.x, A.y);
B = set_point_rotation(B, A, alpha);
C = set_point_rotation(C, A, alpha);
D = set_point_rotation(D, A, alpha);
const sf::Vector2f lt(min4(A.x, B.x, C.x, D.x), min4(A.y, B.y, C.y, D.y)); // Left-Top
const sf::Vector2f rb(max4(A.x, B.x, C.x, D.x), max4(A.y, B.y, C.y, D.y)); // Right-Bottom
sf::Vector2f P = lt - m_sprite.GetOrigin();
P = set_point_rotation(P, A, get_rotation(P, A));
const sf::FloatRect bounding_box(
lt.x - m_sprite.GetOrigin().x,
lt.y - m_sprite.GetOrigin().y,
rb.x - lt.x,
rb.y - lt.y
);
m_bounding_box = sf::Shape::Rectangle(bounding_box, sf::Color(0, 0, 0, 0), 1.f, sf::Color::White);
}
|