Brunot
Loading...
Searching...
No Matches
Transform.h
Go to the documentation of this file.
1// File: Transform.h
2// Description: Component that handles kinematics of the object. Updates its position + rotation every frame
3// Author(s): Ori Balashov (ori.balashov@digipen.edu) Base foundation
4// Aidan Hartman (aidan.hartman@digipen.edu) Inherited parent functionality
5// Marcelo Escamilla(marcelo.escamilla@digipen.edu) JSON serialization, and ImGui implementation
6// 2025 / 09 / 17
7// (C) Digipen 2025
8// ____ __ __ __
9// /\__ _\/\ \ /\ \/\ \
10// \/_/\ \/\ \ \___ __ \ \ \_\ \ ___ __ __ ____ __
11// \ \ \ \ \ _ `\ /'__`\ \ \ _ \ / __`\/\ \/\ \ /',__\ /'__`\
12// \ \ \ \ \ \ \ \/\ __/ \ \ \ \ \/\ \L\ \ \ \_\ \/\__, `\/\ __/
13// \ \_\ \ \_\ \_\ \____\ \ \_\ \_\ \____/\ \____/\/\____/\ \____\
14// \/_/ \/_/\/_/\/____/ \/_/\/_/\/___/ \/___/ \/___/ \/____/
15#pragma once
16
17#include "Framework/Component.h"
18#include "Framework/Component.h"
21
23
24
25// This class' implementation is partly inspired by Doug Schilling's CS 230 Engine
26
27class Transform final : public Component
28{
29public:
30 static auto getEnum() -> ComponentTypeEnum
31 {
32 return cTransform;
33 }
34 static auto bindLuaTypes(sol::state& lua) -> void;
35
36 Transform();
37 Transform(const Transform&);
38 Transform(Transform&&) noexcept;
39 auto operator=(const Transform& rhs) -> Transform& = default;
40 ~Transform() override = default;
41
42 auto parentTo(GameObject* newParent) -> bool override;
43
44 auto clone() const -> std::unique_ptr<GameObject> override;
49 void markDirty();
50 auto componentShowMenu() -> void override;
51 auto onUpdate(float dt) -> void override;
52
53 auto endWindow() -> void override
54 {
55 }; // for debug ImGUI
56 auto onRender() -> void override;
57 auto load(const Stream& stream) -> void override;
58 auto save(Stream& stream) const -> void override;
59
60 auto move(float x, float y) -> void;
61 auto move(const Vector4D& vec) -> void;
62
63 auto x() const -> float;
64 auto y() const -> float;
65 auto z() const -> float;
66
67 auto setX(float x) -> void;
68 auto setY(float y) -> void;
69 auto setZ(float z) -> void;
70
71 auto adjustScale(float x, float y) -> void;
72
74 auto scale() const -> const Vector4D&;
76 auto scale(const Vector4D& scale) -> void;
78 auto scale(float x, float y) -> void;
79
81 auto worldScale() -> Vector4D;
83 auto worldScale(const Vector4D& scale) -> void;
84
86 auto translation() const -> const Point2D&;
88 auto translation(const Vector4D& translation) -> void;
90 auto translation(float x, float y) -> void;
92 auto worldTranslation() -> Point2D;
94 auto worldTranslation(const Point2D& translation) -> void;
95
97 auto rotation() const -> float;
99 auto rotation(float rotation) -> void;
101 auto worldRotation() -> float;
103 auto worldRotation(float rotation) -> void;
104
106 auto matrix() -> AffineMatrix&;
107
108
109 auto setMatrix(AffineMatrix& newMat) -> void;
110 auto makeMat3Rotation(float rotation) -> Eigen::Matrix3f;
111
112 auto setRotationViaMatrix(AffineMatrix& newMat) -> void;
113
114 friend auto to_json(json& j, const Transform& p) -> void;
115 friend auto from_json(const json& j, Transform& p) -> void;
116
117private:
121
122
123 // The isDirty & AffineMatrix idea is directly taken from Doug Schilling's 230 Engine
124
127
132
136
142
146 auto recalculateMatrix() -> void;
147
152 auto getParentTransform() const -> Transform*;
153
159 auto calculateAbsoluteMatrix() -> bool;
160
165 auto getWorldRotationAndScale() -> Eigen::Matrix3f;
166
173 auto setGlobalRotationAndScale(const Eigen::Matrix3f&) -> void;
174
175};
auto to_json(json &j, const AudioObject &a) -> void
Definition AudioObject.cpp:376
auto from_json(const json &j, AudioObject &a) -> void
Definition AudioObject.cpp:387
sys::Json Stream
Definition AudioObject.h:20
The base class for components, holding all of their shared All components should inherit from this.
nlohmann::json json
Definition Json.cpp:19
Vector4D Point2D
Definition Transform.h:22
ComponentTypeEnum
This enum lists out every type of component we have.
Definition Component.h:39
Component(ComponentTypeEnum type, const char *typeName)
Definition Component.cpp:72
GameObject(std::string typeName, gobj::Type parentType, gobj::Type type)
constructor for gameobject.
Definition GameObject.cpp:24
Definition Transform.h:28
Point2D _translation
Definition Transform.h:119
bool isDirty
True if the transformation matrix needs to be recalculated.
Definition Transform.h:126
auto scale() const -> const Vector4D &
Get the local scale ( this is normally what you want).
Definition Transform.cpp:407
auto setY(float y) -> void
Definition Transform.cpp:387
auto endWindow() -> void override
currently unused, but intended to but was assumed to be necessary for a proper ImGui editor
Definition Transform.h:53
auto makeMat3Rotation(float rotation) -> Eigen::Matrix3f
Definition Transform.cpp:654
auto translation() const -> const Point2D &
get the local translation (this is normally what you want)
Definition Transform.cpp:457
auto setGlobalRotationAndScale(const Eigen::Matrix3f &) -> void
helper for setting rotation and scale based on a world rotation and scale matrix
Definition Transform.cpp:694
static auto bindLuaTypes(sol::state &lua) -> void
Definition Transform.cpp:28
auto setRotationViaMatrix(AffineMatrix &newMat) -> void
Definition Transform.cpp:634
auto adjustScale(float x, float y) -> void
Definition Transform.cpp:399
Vector4D _scale
Definition Transform.h:118
auto parentTo(GameObject *newParent) -> bool override
Sets the GameObject as a child of another GameObject.
Definition Transform.cpp:101
auto worldRotation() -> float
get the world rotation in degrees
Definition Transform.cpp:512
auto rotation() const -> float
get the local rotation in degrees (this is what you normally want to do)
Definition Transform.cpp:501
auto setMatrix(AffineMatrix &newMat) -> void
Definition Transform.cpp:610
void markDirty()
top ten ways to make the engine do extra work
Definition Transform.cpp:225
AffineMatrix _matrix
The transformation matrix resulting from concatenating the matrices Formed by scale*rotation*translat...
Definition Transform.h:135
auto onRender() -> void override
called every frame after update has been called for every object.
Definition Transform.cpp:314
auto load(const Stream &stream) -> void override
Implementations will load the state of a GameObject from a th::Json Object.
Definition Transform.cpp:343
auto z() const -> float
Definition Transform.cpp:376
float _rotation
Definition Transform.h:120
auto clone() const -> std::unique_ptr< GameObject > override
makes a copy a GameObject even if it's held polymorphically
Definition Transform.cpp:106
auto save(Stream &stream) const -> void override
Implementations will load the state of a GameObject to a th::Json object.
Definition Transform.cpp:348
auto componentShowMenu() -> void override
Definition Transform.cpp:247
auto y() const -> float
Definition Transform.cpp:371
auto getParentTransform() const -> Transform *
helper for grabbing parent transform
Definition Transform.cpp:557
auto setZ(float z) -> void
Definition Transform.cpp:393
Transform()
Definition Transform.cpp:70
auto worldScale() -> Vector4D
get the world scale (mostly accurate)
Definition Transform.cpp:425
auto onUpdate(float dt) -> void override
called once every frame.
Definition Transform.cpp:309
auto calculateAbsoluteMatrix() -> bool
computes the absolute matrix, by getting parent entity's transform @precondition calculateInternalMat...
Definition Transform.cpp:582
auto x() const -> float
Definition Transform.cpp:366
auto getWorldRotationAndScale() -> Eigen::Matrix3f
helper for getting approximate world scale
Definition Transform.cpp:666
AffineMatrix _absoluteMatrix
A matrix that holds the absolute transform of the component, after offsetting from parent Entities.
Definition Transform.h:141
static auto getEnum() -> ComponentTypeEnum
Definition Transform.h:30
auto setX(float x) -> void
Definition Transform.cpp:381
auto move(float x, float y) -> void
Definition Transform.cpp:353
auto recalculateMatrix() -> void
helper function for recalculating internal Matrix
Definition Transform.cpp:544
auto matrix() -> AffineMatrix &
get the world modeling matrix ( this is normally what you want )
Definition Transform.cpp:595
bool lockedScale
Whether to lock x and y scale when editing it in the editor.
Definition Transform.h:131
auto worldTranslation() -> Point2D
get the world translation
Definition Transform.cpp:475
Definition AffineMatrix.h:30
Definition Vector4D.h:28