Repository navigation
Expand file tree
/
Copy pathTransform.cpp
More file actions
117 lines (96 loc) · 2.05 KB
/
Copy pathTransform.cpp
File metadata and controls
117 lines (96 loc) · 2.05 KB
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
#include "Transform.h"
#include <cmath>
#define DEG_TO_RAD (3.14159f / 180.0f) // Convert degrees to radians
void Transform::Scale(sf::Vector2f scaleFactor)
{
scale.x *= scaleFactor.x;
scale.y *= scaleFactor.y;
matrixDirty = true;
}
void Transform::Rotate(float degrees)
{
rotation += degrees;
matrixDirty = true;
}
void Transform::Translate(sf::Vector2f delta)
{
position += delta;
matrixDirty = true;
}
void Transform::Translate(float x, float y)
{
position += sf::Vector2f(x, y);
matrixDirty = true;
}
void Transform::SetLocalPosition(sf::Vector2f pos)
{
position = pos;
matrixDirty = true;
}
void Transform::SetLocalPosition(float x, float y)
{
position = sf::Vector2f(x, y);
matrixDirty = true;
}
void Transform::SetLocalRotation(float rot)
{
rotation = rot;
matrixDirty = true;
}
void Transform::SetLocalScale(sf::Vector2f scl)
{
scale = scl;
matrixDirty = true;
}
sf::Vector2f Transform::GetLocalPosition() const
{
return { position.x, position.y };
}
float Transform::GetLocalRotation() const
{
return rotation;
}
sf::Vector2f Transform::GetLocalScale() const
{
return { scale.x, scale.y };
}
void Transform::Compose()
{
float rad = rotation * DEG_TO_RAD;
float sin = std::sin(rad), cos = std::cos(rad);
axes[0] = { cos,-sin };
axes[1] = { sin, cos };
matrix[0] = cos * scale.x;
matrix[1] = -sin * scale.y;
matrix[2] = position.x;
matrix[3] = sin * scale.x;
matrix[4] = cos * scale.y;
matrix[5] = -position.y;
matrix[6] = 0.0f;
matrix[7] = 0.0f;
matrix[8] = 1.0f;
}
float* Transform::GetMatrix()
{
if (matrixDirty)
{
Compose();
matrixDirty = false;
}
return matrix;
}
sf::Vector2f Transform::GetLocalAxis(int index) const
{
return axes[index];
}
sf::Transform Transform::GetSF()
{
if (matrixDirty)
{
Compose();
matrixDirty = false;
}
return sf::Transform(matrix[0], matrix[1], matrix[2],
matrix[3], matrix[4], matrix[5],
matrix[6], matrix[7], matrix[8]);
}