Skip to content

Commit e121f01

Browse files
committed
Fixed editor rotation errors, updated entity to use Vec3 rotation
1 parent 5e385e9 commit e121f01

File tree

7 files changed

+50
-19
lines changed

7 files changed

+50
-19
lines changed

engine/core/ResourceSystem.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright (c) 2022-2023 Jonathan Moallem (@J-Mo63) & Aryeh Zinn (@Raelr)
2+
// Copyright (c) 2022 Jonathan Moallem (@J-Mo63) & Aryeh Zinn (@Raelr)
33
//
44
// This code is released under an unmodified zlib license.
55
// For conditions of distribution and use, please see:

engine/core/entity/Entity.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ const Xform& Entity::GetTransform() const
6161
return transform;
6262
}
6363

64+
Xform& Entity::GetTransform()
65+
{
66+
return transform;
67+
}
68+
6469
int Entity::GetZIndex() const
6570
{
6671
return zIndex;
@@ -71,25 +76,29 @@ const Vec3& Entity::GetPosition() const
7176
return transform.GetPosition();
7277
}
7378

74-
float Entity::GetRotation() const
79+
const Vec3& Entity::GetRotation() const
7580
{
76-
return transform.GetRotation().y;
81+
return transform.GetRotation();
7782
}
7883

7984
const Vec3& Entity::GetScale() const
8085
{
8186
return transform.GetScale();
8287
}
8388

89+
void Entity::SetTransform(const Xform& xform)
90+
{
91+
transform = xform;
92+
}
93+
8494
void Entity::SetPosition(const Vec3& position)
8595
{
8696
transform.SetPosition(position);
8797
}
8898

89-
void Entity::SetRotation(float rotation)
99+
void Entity::SetRotation(const Vec3& rotation)
90100
{
91-
transform.SetRotation(
92-
{transform.GetRotation().x, fmod(rotation, 360.f), transform.GetRotation().z});
101+
transform.SetRotation(rotation);
93102
}
94103

95104
void Entity::SetScale(const Vec3& scale)

engine/core/entity/Entity.h

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,9 @@ class Entity
142142

143143
/**
144144
* Getter method for the entity's rotation attribute
145-
* @return the entity's rotation as a float
145+
* @return the entity's rotation as a Vec3
146146
*/
147-
float GetRotation() const;
147+
const Vec3& GetRotation() const;
148148

149149
/**
150150
* Getter method for the entity's scale attribute
@@ -159,6 +159,13 @@ class Entity
159159
*/
160160
const Xform& GetTransform() const;
161161

162+
/**
163+
* Getter method for the entity's transform
164+
* @return a reference to the entity's
165+
* transform values
166+
*/
167+
Xform& GetTransform();
168+
162169
/**
163170
* Getter method for the entity's z-index attribute
164171
* @return the entity's z-index as an int
@@ -176,26 +183,32 @@ class Entity
176183
*/
177184
void SetIndex(GenerationalIndex idx);
178185

186+
/**
187+
* Setter method for the entity's transform
188+
* @param xform - a transform to set to the entity
189+
*/
190+
void SetTransform(const Xform& xform);
191+
179192
/**
180193
* Setter method for the entity's position attribute
181-
* @param position - a Vector3 to set as the entity's
194+
* @param position - a Vec3 to set as the entity's
182195
* position
183196
*/
184197
void SetPosition(const Vec3& position);
185198

186199
/**
187200
* Setter method for the entity's rotation attribute
188-
* @param rotation - a float to set as the entity's
201+
* @param rotation - a Vec3 to set as the entity's
189202
* rotation
190203
* @note While negative rotations are accepted,
191204
* rotations beyond 360 degrees are
192205
* automatically wrapped
193206
*/
194-
void SetRotation(float rotation);
207+
void SetRotation(const Vec3& rotation);
195208

196209
/**
197210
* Setter method for the entity's scale attribute
198-
* @param scale - a Vector3 to set as the entity's
211+
* @param scale - a Vec3 to set as the entity's
199212
* scale
200213
*/
201214
void SetScale(const Vec3& scale);

engine/core/scene/SceneFile.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ String SceneFile::SerialiseToString(const std::vector<Entity*>& entities)
4444
// Serialise the general entity information
4545
fileData += (entity->GetName() + SEP);
4646
fileData += DefineField("POSITION", ToString(entity->GetPosition()));
47-
fileData += DefineField("ROTATION", String::FromFloat(entity->GetRotation()));
47+
fileData += DefineField("ROTATION", String::FromFloat(entity->GetRotation().y));
4848
fileData += DefineField("Z-INDEX", String::FromInt(entity->GetZIndex()));
4949

5050
// Apply its serialiser if it

engine/utils/math/Xform.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ struct Xform
3636
return position;
3737
}
3838

39-
Vec3 GetRotation() const
39+
const Vec3& GetRotation() const
4040
{
4141
return rotation;
4242
}
@@ -74,21 +74,27 @@ struct Xform
7474
void SetRotation(const Vec3& newRotation)
7575
{
7676
rotation = newRotation;
77+
rotation.x = fmod(rotation.x, 360.f);
78+
rotation.y = fmod(rotation.y, 360.f);
79+
rotation.z = fmod(rotation.z, 360.f);
7780
}
7881

7982
void SetRotationX(const float& newRotation)
8083
{
8184
rotation.x = newRotation;
85+
rotation.x = fmod(rotation.x, 360.f);
8286
}
8387

8488
void SetRotationY(const float& newRotation)
8589
{
8690
rotation.y = newRotation;
91+
rotation.y = fmod(rotation.y, 360.f);
8792
}
8893

8994
void SetRotationZ(const float& newRotation)
9095
{
9196
rotation.z = newRotation;
97+
rotation.z = fmod(rotation.z, 360.f);
9298
}
9399

94100
void SetScale(const Vec3& newScale)

examples/game/src/tools/EditorController.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ void EditorController::OnUpdate()
157157
float rotation =
158158
precision * (float) (Siege::Input::IsKeyDown(Siege::Key::KEY_LEFT) +
159159
-Siege::Input::IsKeyDown(Siege::Key::KEY_RIGHT));
160-
selectedEntity->SetRotation(selectedEntity->GetRotation() + rotation);
160+
Siege::Xform& xform = selectedEntity->GetTransform();
161+
xform.SetRotationY(selectedEntity->GetRotation().y + rotation);
161162
break;
162163
}
163164
}
@@ -181,7 +182,7 @@ void EditorController::OnDraw2D()
181182
selectedEntity->GetPosition().y,
182183
selectedEntity->GetPosition().z);
183184
Siege::String rotLabel =
184-
Siege::String("Rotation: %.2f").Formatted(selectedEntity->GetRotation());
185+
Siege::String("Rotation: %.2f").Formatted(selectedEntity->GetRotation().y);
185186

186187
// Draw display text just above the entity in world-space
187188
auto& pixel = ServiceLocator::GetRenderResources()->GetFont();
@@ -292,7 +293,9 @@ bool EditorController::TrySetPos(Siege::Vec3 position)
292293
bool EditorController::TrySetRot(float rotation)
293294
{
294295
if (!selectedEntity) return false;
295-
selectedEntity->SetRotation(rotation);
296+
Siege::Xform xform = selectedEntity->GetTransform();
297+
xform.SetRotationY(rotation);
298+
selectedEntity->SetTransform(xform);
296299
return true;
297300
}
298301

tests/core/test_SceneFile.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ UTEST_F(test_SceneFile, SerialiseSingleEntity)
111111

112112
// Modifying its fields should result in the same transforms being applied
113113
e1.SetPosition({1, 2, 3});
114-
e1.SetRotation(25.f);
114+
e1.SetRotation({0.f, 25.f, 0.f});
115115
e1.SetZIndex(-3);
116116
sceneData = SceneFile::SerialiseToString({&e1});
117117
ASSERT_STREQ("TestEntity1|"
@@ -244,7 +244,7 @@ UTEST_F(test_SceneFile, DeserialiseSingleEntity)
244244
ASSERT_EQ(1.f, pos.x);
245245
ASSERT_EQ(2.f, pos.y);
246246
ASSERT_EQ(3.f, pos.z);
247-
ASSERT_EQ(25.f, entities[0]->GetRotation());
247+
ASSERT_EQ(25.f, entities[0]->GetRotation().y);
248248
ASSERT_EQ(-3, entities[0]->GetZIndex());
249249

250250
// It should retain its custom data

0 commit comments

Comments
 (0)