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"
20
22
23
24// This class' implementation is partly inspired by Doug Schilling's CS 230 Engine
25
26class Transform final : public Component
27{
28public:
29 Transform();
30 Transform(const Transform&);
31 Transform(Transform&&) noexcept;
32 auto operator=(const Transform& rhs) -> Transform& = default;
33 ~Transform() override = default;
34
35 auto parentTo(GameObject* newParent) -> bool override;
36
37 auto clone() const -> std::unique_ptr<GameObject> override;
38 auto componentShowMenu() -> void override;
39 auto onUpdate(float dt) -> void override;
40
41 auto endWindow() -> void override
42 {
43 }; // for debug ImGUI
44 auto onRender() -> void override;
45 auto load(const Stream& stream) -> void override;
46 auto save(Stream& stream) const -> void override;
47
48 auto move(float x, float y) -> void;
49 auto move(const Vector4D& vec) -> void;
50
51 auto setX(float x) -> void;
52 auto setY(float y) -> void;
53
55 auto scale() const -> const Vector4D&;
57 auto scale(const Vector4D& scale) -> void;
58
60 auto worldScale() -> Vector4D;
62 auto worldScale(const Vector4D& scale) -> void;
63
65 auto translation() const -> const Point2D&;
67 auto translation(const Vector4D& translation) -> void;
69 auto worldTranslation() -> Point2D;
71 auto worldTranslation(const Point2D& translation) -> void;
72
74 auto rotation() const -> float;
76 auto rotation(float rotation) -> void;
78 auto worldRotation() -> float;
80 auto worldRotation(float rotation) -> void;
81
83 auto matrix() -> AffineMatrix&;
84
85
86 auto setMatrix(AffineMatrix& newMat) -> void;
87 auto makeMat3Rotation(float rotation) -> Eigen::Matrix3f;
88
89 auto setRotationViaMatrix(AffineMatrix& newMat) -> void;
90
91 friend auto to_json(json& j, const Transform& p) -> void;
92 friend auto from_json(const json& j, Transform& p) -> void;
93
94private:
96 float _rotation;
98
99
100 // The isDirty & AffineMatrix idea is directly taken from Doug Schilling's 230 Engine
101
102 // True if the transformation matrix needs to be recalculated.
104
105 // The transformation matrix resulting from concatenating the matrices
106 // Formed by scale*rotation*translation matrices
113
117 auto recalculateMatrix() -> void;
122 auto internalMatrix() -> const AffineMatrix&;
123
128 auto getParentTransform() const -> Transform*;
129
134 auto calculateAbsoluteMatrix() -> bool;
135
140 auto getWorldRotationAndScale() -> Eigen::Matrix3f;
141
148 auto setGlobalRotationAndScale(const Eigen::Matrix3f&) -> void;
149
150};
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:21
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:23
Definition Transform.h:27
Point2D _translation
Definition Transform.h:97
bool isDirty
Definition Transform.h:103
auto scale() const -> const Vector4D &
Get the local scale ( this is normally what you want).
Definition Transform.cpp:285
auto setY(float y) -> void
Definition Transform.cpp:278
auto endWindow() -> void override
currently unused, but intended to but was assumed to be necessary for a proper ImGui editor
Definition Transform.h:41
auto makeMat3Rotation(float rotation) -> Eigen::Matrix3f
Definition Transform.cpp:530
auto translation() const -> const Point2D &
get the local translation (this is normally what you want)
Definition Transform.cpp:328
auto setGlobalRotationAndScale(const Eigen::Matrix3f &) -> void
helper for setting rotation and scale based on a world rotation and scale matrix
Definition Transform.cpp:570
auto setRotationViaMatrix(AffineMatrix &newMat) -> void
Definition Transform.cpp:511
Vector4D _scale
Definition Transform.h:95
auto parentTo(GameObject *newParent) -> bool override
Sets the GameObject as a child of another GameObject.
Definition Transform.cpp:55
auto worldRotation() -> float
get the world rotation in degrees
Definition Transform.cpp:376
auto rotation() const -> float
get the local rotation in degrees (this is what you normally want to do)
Definition Transform.cpp:365
auto setMatrix(AffineMatrix &newMat) -> void
Definition Transform.cpp:488
AffineMatrix _matrix
Definition Transform.h:107
auto onRender() -> void override
called every frame after update has been called for every object.
Definition Transform.cpp:224
auto load(const Stream &stream) -> void override
Implementations will load the state of a GameObject from a th::Json Object.
Definition Transform.cpp:249
auto operator=(const Transform &rhs) -> Transform &=default
float _rotation
Definition Transform.h:96
auto clone() const -> std::unique_ptr< GameObject > override
makes a copy a GameObject even if it's held polymorphically
Definition Transform.cpp:60
auto save(Stream &stream) const -> void override
Implementations will load the state of a GameObject to a th::Json object.
Definition Transform.cpp:254
auto componentShowMenu() -> void override
Definition Transform.cpp:179
auto getParentTransform() const -> Transform *
helper for grabbing parent transform
Definition Transform.cpp:429
Transform()
Definition Transform.cpp:27
auto worldScale() -> Vector4D
get the world scale (mostly accurate)
Definition Transform.cpp:296
auto onUpdate(float dt) -> void override
called once every frame.
Definition Transform.cpp:219
auto calculateAbsoluteMatrix() -> bool
computes the absolute matrix, by getting parent entity's transform
Definition Transform.cpp:456
~Transform() override=default
auto getWorldRotationAndScale() -> Eigen::Matrix3f
helper for getting approximate world scale
Definition Transform.cpp:542
AffineMatrix _absoluteMatrix
A matrix that holds the absolute transform of the component, after offsetting from parent Entities.
Definition Transform.h:112
auto setX(float x) -> void
Definition Transform.cpp:272
auto move(float x, float y) -> void
Definition Transform.cpp:259
auto recalculateMatrix() -> void
helper function for recalculating internal Matrix
Definition Transform.cpp:408
auto internalMatrix() -> const AffineMatrix &
Definition Transform.cpp:420
auto matrix() -> AffineMatrix &
get the world modeling matrix ( this is normally what you want )
Definition Transform.cpp:474
auto worldTranslation() -> Point2D
get the world translation
Definition Transform.cpp:339
Definition AffineMatrix.h:30
Definition Vector4D.h:23