fixed layer problem, remove 'default' layer to serialize, remove Transform class, using translation rotation scale instead

This commit is contained in:
2025-12-31 23:56:49 +08:00
parent 960eeaf94b
commit faecfe21ab
23 changed files with 330 additions and 452 deletions

View File

@ -3,11 +3,12 @@
//
#include "EditorLayer.h"
#include "ImGuizmo.h"
#define GLM_ENABLE_EXPERIMENTAL
#include <ImGuizmo.h>
#include <filesystem>
#define GLM_ENABLE_EXPERIMENTAL
#include "glm/gtx/matrix_decompose.hpp"
#include "Prism/Core/Input.h"
#include "Prism/Editor/PhysicsSettingsWindow.h"
#include "Prism/Physics/Physics3D.h"
@ -218,7 +219,7 @@ namespace Prism
auto viewProj = m_EditorCamera.GetViewProjection();
Renderer2D::BeginScene(viewProj, false);
glm::vec4 color = (m_SelectionMode == SelectionMode::Entity) ? glm::vec4{ 1.0f, 1.0f, 1.0f, 1.0f } : glm::vec4{ 0.2f, 0.9f, 0.2f, 1.0f };
Renderer::DrawAABB(selection.Mesh->BoundingBox, selection.Entity.GetComponent<TransformComponent>().Transformation.GetMatrix() * selection.Mesh->Transform, color);
Renderer::DrawAABB(selection.Mesh->BoundingBox, selection.Entity.GetComponent<TransformComponent>().GetTransform() * selection.Mesh->Transform, color);
Renderer2D::EndScene();
Renderer::EndRenderPass();
}
@ -231,7 +232,7 @@ namespace Prism
if (selection.Entity.HasComponent<BoxCollider2DComponent>())
{
const auto& size = selection.Entity.GetComponent<BoxCollider2DComponent>().Size;
auto [translation, rotationQuat, scale] = GetTransformDecomposition(selection.Entity.GetComponent<TransformComponent>().Transformation.GetMatrix());
auto [translation, rotationQuat, scale] = GetTransformDecomposition(selection.Entity.GetComponent<TransformComponent>().GetTransform());
const glm::vec3 rotation = glm::eulerAngles(rotationQuat);
Renderer::BeginRenderPass(SceneRenderer::GetFinalRenderPass(), false);
@ -786,12 +787,12 @@ namespace Prism
bool snap = Input::IsKeyPressed(Key::LEFT_CONTROL);
Transform& entityTransform = selection.Entity.Transformation();
TransformComponent& entityTransform = selection.Entity.Transform();
float snapValue = GetSnapValue();
float snapValues[3] = { snapValue, snapValue, snapValue };
if (m_SelectionMode == SelectionMode::Entity)
{
glm::mat4 transform = entityTransform.GetMatrix();
glm::mat4 transform = entityTransform.GetTransform();
ImGuizmo::Manipulate(glm::value_ptr(m_EditorCamera.GetViewMatrix()),
glm::value_ptr(m_EditorCamera.GetProjectionMatrix()),
(ImGuizmo::OPERATION)m_GizmoType,
@ -801,12 +802,12 @@ namespace Prism
snap ? snapValues : nullptr);
auto [translation, rotation, scale] = GetTransformDecomposition(transform);
entityTransform.SetTranslation(translation);
entityTransform.SetRotation(rotation);
entityTransform.SetScale(scale);
entityTransform.Translation = translation;
entityTransform.Rotation = glm::degrees(glm::eulerAngles(rotation));
entityTransform.Scale = scale;
}else
{
glm::mat4 transformBase = entityTransform.GetMatrix() * selection.Mesh->Transform;
glm::mat4 transformBase = entityTransform.GetTransform() * selection.Mesh->Transform;
ImGuizmo::Manipulate(glm::value_ptr(m_EditorCamera.GetViewMatrix()),
glm::value_ptr(m_EditorCamera.GetProjectionMatrix()),
(ImGuizmo::OPERATION)m_GizmoType,
@ -815,7 +816,7 @@ namespace Prism
nullptr,
snap ? snapValues : nullptr);
selection.Mesh->Transform = glm::inverse(entityTransform.GetMatrix()) * transformBase;
selection.Mesh->Transform = glm::inverse(entityTransform.GetTransform()) * transformBase;
}
}
@ -951,9 +952,10 @@ namespace Prism
for (uint32_t i = 0; i < submeshes.size(); i++)
{
auto& submesh = submeshes[i];
const auto transform = entity.Transform().GetTransform();
Ray ray = {
glm::inverse(entity.Transformation().GetMatrix() * submesh.Transform) * glm::vec4(origin, 1.0f),
glm::inverse(glm::mat3(entity.Transformation().GetMatrix()) * glm::mat3(submesh.Transform)) * direction
glm::inverse(transform * submesh.Transform) * glm::vec4(origin, 1.0f),
glm::inverse(glm::mat3(transform) * glm::mat3(submesh.Transform)) * direction
};
float t = 0;

View File

@ -6,130 +6,6 @@ Environment:
Radiance: [1, 1, 1]
Multiplier: 0.515
Entities:
- Entity: 18306113171518048249
TagComponent:
Tag: Box
TransformComponent:
Position: [0, 0, 0]
Rotation: [0, 0, 0]
Scale: [50, 1, 50]
MeshComponent:
AssetPath: assets/meshes/Cube1m.fbx
RigidBodyComponent:
BodyType: 0
Mass: 1
IsKinematic: false
Layer: 1
Constraints:
LockPositionX: false
LockPositionY: false
LockPositionZ: false
LockRotationX: false
LockRotationY: false
LockRotationZ: false
PhysicsMaterialComponent:
StaticFriction: 1
DynamicFriction: 1
Bounciness: 0
BoxColliderComponent:
Offset: [0, 0, 0]
Size: [1, 1, 1]
IsTrigger: false
- Entity: 5178862374589434728
TagComponent:
Tag: Camera
TransformComponent:
Position: [2.808, 2.25, 0]
Rotation: [0, 0, 0]
Scale: [1, 1, 1]
CameraComponent:
Camera: some camera data...
Primary: true
- Entity: 14057422478420564497
TagComponent:
Tag: Sphere
TransformComponent:
Position: [-3.9876995, 1, -1.9669533e-06]
Rotation: [0, 0, 0]
Scale: [1, 1, 1]
MeshComponent:
AssetPath: assets/meshes/Sphere1m.fbx
RigidBodyComponent:
BodyType: 1
Mass: 1
IsKinematic: false
Layer: 1
Constraints:
LockPositionX: false
LockPositionY: false
LockPositionZ: false
LockRotationX: false
LockRotationY: false
LockRotationZ: false
PhysicsMaterialComponent:
StaticFriction: 0.1
DynamicFriction: 0.1
Bounciness: 0.1
SphereColliderComponent:
Radius: 0.5
IsTrigger: false
- Entity: 10169503531257462571
TagComponent:
Tag: Box
TransformComponent:
Position: [0, 1.5, 0]
Rotation: [0, 0, 0]
Scale: [2, 2, 2]
MeshComponent:
AssetPath: assets/meshes/Cube1m.fbx
RigidBodyComponent:
BodyType: 1
Mass: 0.5
IsKinematic: false
Layer: 1
Constraints:
LockPositionX: false
LockPositionY: false
LockPositionZ: false
LockRotationX: false
LockRotationY: false
LockRotationZ: false
PhysicsMaterialComponent:
StaticFriction: 1
DynamicFriction: 1
Bounciness: 0
BoxColliderComponent:
Offset: [0, 0, 0]
Size: [1, 1, 1]
IsTrigger: false
- Entity: 11149966982516343187
TagComponent:
Tag: Mesh Collider
TransformComponent:
Position: [-2.6045518, 1, -0.0017139912]
Rotation: [0, 0, 0]
Scale: [1, 1, 1]
MeshComponent:
AssetPath: assets/meshes/Sphere1m.fbx
RigidBodyComponent:
BodyType: 1
Mass: 0.1
IsKinematic: false
Layer: 1
Constraints:
LockPositionX: false
LockPositionY: false
LockPositionZ: false
LockRotationX: false
LockRotationY: false
LockRotationZ: false
PhysicsMaterialComponent:
StaticFriction: 1
DynamicFriction: 1
Bounciness: 0.1
MeshColliderComponent:
AssetPath: assets/meshes/Sphere1m.fbx
IsTrigger: false
- Entity: 3247025703490125974
TagComponent:
Tag: Player
@ -176,10 +52,131 @@ Entities:
MeshColliderComponent:
AssetPath: assets/meshes/Capsule.fbx
IsTrigger: false
- Entity: 11149966982516343187
TagComponent:
Tag: Mesh Collider
TransformComponent:
Position: [-2.6045518, 1, -0.0017139912]
Rotation: [0, 0, 0]
Scale: [1, 1, 1]
MeshComponent:
AssetPath: assets/meshes/Sphere1m.fbx
RigidBodyComponent:
BodyType: 1
Mass: 0.1
IsKinematic: false
Layer: 1
Constraints:
LockPositionX: false
LockPositionY: false
LockPositionZ: false
LockRotationX: false
LockRotationY: false
LockRotationZ: false
PhysicsMaterialComponent:
StaticFriction: 1
DynamicFriction: 1
Bounciness: 0.1
MeshColliderComponent:
AssetPath: assets/meshes/Sphere1m.fbx
IsTrigger: false
- Entity: 10169503531257462571
TagComponent:
Tag: Box
TransformComponent:
Position: [0, 1.5, 0]
Rotation: [0, 0, 0]
Scale: [2, 2, 2]
MeshComponent:
AssetPath: assets/meshes/Cube1m.fbx
RigidBodyComponent:
BodyType: 1
Mass: 0.5
IsKinematic: false
Layer: 1
Constraints:
LockPositionX: false
LockPositionY: false
LockPositionZ: false
LockRotationX: false
LockRotationY: false
LockRotationZ: false
PhysicsMaterialComponent:
StaticFriction: 1
DynamicFriction: 1
Bounciness: 0
BoxColliderComponent:
Offset: [0, 0, 0]
Size: [1, 1, 1]
IsTrigger: false
- Entity: 14057422478420564497
TagComponent:
Tag: Sphere
TransformComponent:
Position: [-3.9876995, 1, -1.9669533e-06]
Rotation: [0, 0, 0]
Scale: [1, 1, 1]
MeshComponent:
AssetPath: assets/meshes/Sphere1m.fbx
RigidBodyComponent:
BodyType: 1
Mass: 1
IsKinematic: false
Layer: 1
Constraints:
LockPositionX: false
LockPositionY: false
LockPositionZ: false
LockRotationX: false
LockRotationY: false
LockRotationZ: false
PhysicsMaterialComponent:
StaticFriction: 0.1
DynamicFriction: 0.1
Bounciness: 0.1
SphereColliderComponent:
Radius: 0.5
IsTrigger: false
- Entity: 5178862374589434728
TagComponent:
Tag: Camera
TransformComponent:
Position: [2.808, 2.25, 0]
Rotation: [0, 0, 0]
Scale: [1, 1, 1]
CameraComponent:
Camera: some camera data...
Primary: true
- Entity: 18306113171518048249
TagComponent:
Tag: Box
TransformComponent:
Position: [0, 0, 0]
Rotation: [0, 0, 0]
Scale: [50, 1, 50]
MeshComponent:
AssetPath: assets/meshes/Cube1m.fbx
RigidBodyComponent:
BodyType: 0
Mass: 1
IsKinematic: false
Layer: 1
Constraints:
LockPositionX: false
LockPositionY: false
LockPositionZ: false
LockRotationX: false
LockRotationY: false
LockRotationZ: false
PhysicsMaterialComponent:
StaticFriction: 1
DynamicFriction: 1
Bounciness: 0
BoxColliderComponent:
Offset: [0, 0, 0]
Size: [1, 1, 1]
IsTrigger: false
PhysicsLayers:
- Name: Default
CollidesWith:
- Name: Box
- Name: Box
CollidesWith:
- Name: Default

View File

@ -4,11 +4,11 @@ namespace FPSExample
{
public class FPSPlayer : Entity
{
public float WalkingSpeed = 10.0F;
public float RunSpeed = 20.0F;
public float JumpForce = 50.0F;
public float MouseSensitivity = 10.0F;
public float Distance = 5.0F;
public float WalkingSpeed = 10.0f;
public float RunSpeed = 20.0f;
public float JumpForce = 50.0f;
public float MouseSensitivity = 10.0f;
public float Distance = 5.0f;
private bool m_Colliding = false;
private float m_CurrentSpeed;
@ -20,7 +20,8 @@ namespace FPSExample
private Entity m_CameraEntity;
private Vec2 m_LastMousePosition;
private float m_CurrentYMovement = 0.0f;
void OnCreate()
{
m_Transform = GetComponent<TransformComponent>();
@ -39,6 +40,7 @@ namespace FPSExample
Input.SetCursorMode(Input.CursorMode.Locked);
}
Collider[] colliders = new Collider[10];
void OnUpdate(float ts)
{
if (Input.IsKeyPressed(KeyCode.Escape) && Input.GetCursorMode() == Input.CursorMode.Locked)
@ -49,35 +51,6 @@ namespace FPSExample
m_CurrentSpeed = Input.IsKeyPressed(KeyCode.LeftControl) ? RunSpeed : WalkingSpeed;
UpdateRotation(ts);
UpdateMovement();
UpdateCameraTransform();
}
private void UpdateRotation(float ts)
{
Vec2 currentMousePosition = Input.GetMousePosition();
Vec2 delta = m_LastMousePosition - currentMousePosition;
float yRotation = delta.X * MouseSensitivity * ts;
float xRotation = delta.Y * MouseSensitivity * ts;
m_RigidBody.Rotate(new Vec3(0.0f, yRotation, 0.0f));
if (delta.X != 0 || delta.Y != 0)
{
m_CameraTransform.Rotation += new Vec3(xRotation, yRotation, 0.0f);
}
m_CameraTransform.Rotation = new Vec3(Mathf.Clamp(m_CameraTransform.Rotation.X, -89.0f, 89.0f), m_CameraTransform.Rotation.YZ);
m_LastMousePosition = currentMousePosition;
}
Collider[] colliders = new Collider[10];
private void UpdateMovement()
{
RaycastHit hitInfo;
if (Input.IsKeyPressed(KeyCode.H) &&
Physics.Raycast(m_CameraTransform.Position + (m_CameraTransform.Transform.Forward * 5.0f),
@ -91,7 +64,7 @@ namespace FPSExample
// NOTE: The NonAlloc version of Overlap functions should be used when possible since it doesn't allocate a new array
// whenever you call it. The normal versions allocates a brand new array every time.
int numColliders = Physics.OverlapBoxNonAlloc(m_Transform.Position, new Vec3(1.0F), colliders);
int numColliders = Physics.OverlapBoxNonAlloc(m_Transform.Position, new Vec3(1.0f), colliders);
Console.WriteLine("Colliders: {0}", numColliders);
// When using NonAlloc it's not safe to use a foreach loop since some of the colliders may not exist
@ -100,7 +73,37 @@ namespace FPSExample
Console.WriteLine(colliders[i]);
}
}
UpdateRotation(ts);
UpdateMovement();
UpdateCameraTransform();
}
private void UpdateRotation(float ts)
{
Vec2 currentMousePosition = Input.GetMousePosition();
Vec2 delta = m_LastMousePosition - currentMousePosition;
float m_CurrentYMovement = delta.X * MouseSensitivity * ts;
float xRotation = delta.Y * MouseSensitivity * ts;
m_RigidBody.Rotate(new Vec3(0.0f, m_CurrentYMovement, 0.0f));
if (delta.X != 0 || delta.Y != 0)
{
m_CameraTransform.Rotation += new Vec3(xRotation, m_CurrentYMovement, 0.0f);
}
m_CameraTransform.Rotation = new Vec3(Mathf.Clamp(m_CameraTransform.Rotation.X, -89.0f, 89.0f), m_CameraTransform.Rotation.YZ);
m_LastMousePosition = currentMousePosition;
}
private void UpdateMovement()
{
m_RigidBody.Rotate(new Vec3(0.0f, m_CurrentYMovement, 0.0f));
if (Input.IsKeyPressed(KeyCode.W))
m_RigidBody.AddForce(m_Transform.Transform.Forward * m_CurrentSpeed);
else if (Input.IsKeyPressed(KeyCode.S))

View File

@ -1,39 +0,0 @@
//
// Created by sfd on 25-12-29.
//
#include "Transform.h"
namespace Prism
{
Transform::Transform()
: m_Translation(glm::vec3(0.0f)), m_Rotation(glm::vec3(0.0f)), m_Scale(glm::vec3(1.0f))
{
RecalculateMatrix();
}
Transform::Transform(const glm::vec3& translation, const glm::vec3& rotation, const glm::vec3& scale)
: m_Translation(translation), m_Rotation(rotation), m_Scale(scale)
{
RecalculateMatrix();
}
const glm::mat4& Transform::GetMatrix()
{
if (m_HasChanged) RecalculateMatrix();
return m_Matrix;
}
void Transform::RecalculateMatrix()
{
const auto rotation = glm::quat(glm::radians(m_Rotation));
m_Up = glm::normalize(glm::rotate(rotation, glm::vec3(0.0f, 1.0f, 0.0f)));
m_Right = glm::normalize(glm::rotate(rotation, glm::vec3(1.0f, 0.0f, 0.0f)));
m_Forward = glm::normalize(glm::rotate(rotation, glm::vec3(0.0f, 0.0f, -1.0f)));
m_Matrix = glm::translate(glm::mat4(1.0f), m_Translation)
* glm::toMat4(rotation)
* glm::scale(glm::mat4(1.0f), m_Scale);
}
}

View File

@ -1,60 +0,0 @@
//
// Created by sfd on 25-12-29.
//
#ifndef TRANSFORM_H
#define TRANSFORM_H
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/quaternion.hpp>
#include <glm/gtx/common.hpp>
#include "glm/gtc/type_ptr.hpp"
namespace Prism
{
class PRISM_API Transform
{
public:
Transform();
Transform(const glm::vec3& translation, const glm::vec3& rotation = glm::vec3(0.0f), const glm::vec3& scale = glm::vec3(1.0f));
void Translate(const glm::vec3& translation) { m_Translation += translation; m_HasChanged = true;}
void Rotate(const glm::vec3& rotation) { m_Rotation += rotation; m_HasChanged = true;}
void SetTranslation(const glm::vec3& position) { m_Translation = position; m_HasChanged = true;}
void SetRotation(const glm::vec3& rotation) { m_Rotation = rotation; m_HasChanged = true;}
void SetRotation(const glm::quat& rotation) { m_Rotation = glm::degrees(glm::eulerAngles(rotation)); m_HasChanged = true;}
void SetScale(const glm::vec3& scale) { m_Scale = scale; m_HasChanged = true;}
const glm::vec3& GetTranslation() const { return m_Translation; }
const glm::vec3& GetRotation() const { return m_Rotation; }
const glm::vec3& GetScale() const { return m_Scale; }
const glm::vec3& GetUpDirection() const { return m_Up; }
const glm::vec3& GetRightDirection() const { return m_Right; }
const glm::vec3& GetForwardDirection() const { return m_Forward; }
const glm::mat4& GetMatrix();
float* Data() { return glm::value_ptr(m_Matrix); }
private:
void RecalculateMatrix();
private:
glm::vec3 m_Translation;
glm::vec3 m_Rotation;
glm::vec3 m_Scale;
glm::vec3 m_Forward, m_Right, m_Up;
glm::mat4 m_Matrix = glm::mat4(1.0f);
bool m_HasChanged = false;
};
}
#endif //TRANSFORM_H

View File

@ -57,6 +57,9 @@ namespace Prism
Property("World Bounds (Max)", settings.WorldBoundsMax);
Property("Grid Subdivisions", settings.WorldBoundsSubdivisions, 1, 10000);
}
Property("Solver Iterations", settings.SolverIterations, 1, 512);
Property("Solver Velocity Iterations", settings.SolverVelocityIterations, 1, 512);
}
void PhysicsSettingsWindow::RenderLayerList()

View File

@ -5,12 +5,15 @@
#include "SceneHierachyPanel.h"
#include "imgui.h"
#include "assimp/matrix4x4.h"
#define GLM_ENABLE_EXPERIMENTAL
#include "imgui_internal.h"
#include "assimp/scene.h"
#include "glm/gtx/matrix_decompose.hpp"
#include "glm/gtx/quaternion.hpp"
#include "assimp/matrix4x4.h"
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/quaternion.hpp>
#include <glm/gtx/matrix_decompose.hpp>
#include "Prism/Core/Application.h"
#include "Prism/Physics/PhysicsLayer.h"
#include "Prism/Physics/PhysicsWrappers.h"
@ -644,22 +647,16 @@ namespace Prism
if (entity.HasComponent<TransformComponent>())
{
Transform& transform = entity.GetComponent<TransformComponent>();
TransformComponent& transform = entity.GetComponent<TransformComponent>();
if (ImGui::TreeNodeEx((void*)((uint32_t)entity | typeid(TransformComponent).hash_code()), ImGuiTreeNodeFlags_DefaultOpen, "Transform"))
{
glm::vec3 translation = transform.GetTranslation();
glm::vec3 rotation = transform.GetRotation();
glm::vec3 scale = transform.GetScale();
ImGui::Columns(2);
ImGui::Text("Translation");
ImGui::NextColumn();
ImGui::PushItemWidth(-1);
if (ImGui::DragFloat3("##translation", glm::value_ptr(translation), 0.25f))
{
transform.SetTranslation(translation);
}
ImGui::DragFloat3("##translation", glm::value_ptr(transform.Translation), 0.25f);
ImGui::PopItemWidth();
ImGui::NextColumn();
@ -668,10 +665,7 @@ namespace Prism
ImGui::NextColumn();
ImGui::PushItemWidth(-1);
if (ImGui::DragFloat3("##rotation", glm::value_ptr(rotation), 0.25f))
{
transform.SetRotation(rotation);
}
ImGui::DragFloat3("##rotation", glm::value_ptr(transform.Rotation), 0.25f);
ImGui::PopItemWidth();
ImGui::NextColumn();
@ -680,14 +674,7 @@ namespace Prism
ImGui::NextColumn();
ImGui::PushItemWidth(-1);
if (ImGui::DragFloat3("##scale", glm::value_ptr(scale), 0.05f))
{
if (scale.x == 0) scale.x = 0.01f;
if (scale.y == 0) scale.y = 0.01f;
if (scale.z == 0) scale.z = 0.01f;
transform.SetScale(scale);
}
ImGui::DragFloat3("##scale", glm::value_ptr(transform.Scale), 0.05f);
ImGui::PopItemWidth();
ImGui::NextColumn();

View File

@ -9,11 +9,9 @@
#include "glm/gtx/matrix_decompose.hpp"
#include "Prism/Scene/Entity.h"
#define PX_PHYSX_STATIC_LIB
#include <PxPhysicsAPI.h>
#include "PhysicsLayer.h"
#include "PhysicsWrappers.h"
#include "Prism/Script/ScriptEngine.h"
namespace Prism
@ -87,7 +85,9 @@ namespace Prism
RigidBodyComponent& rigidbody = e.GetComponent<RigidBodyComponent>();
physx::PxRigidActor* actor = PhysicsWrappers::CreateActor(rigidbody, e.Transformation());
const auto transform = e.Transform();
physx::PxRigidActor* actor = PhysicsWrappers::CreateActor(rigidbody, transform);
if (rigidbody.BodyType == RigidBodyComponent::Type::Dynamic)
s_SimulatedEntities.push_back(e);
@ -100,33 +100,31 @@ namespace Prism
const physx::PxMaterial* material = PhysicsWrappers::CreateMaterial(e.GetComponent<PhysicsMaterialComponent>());
const auto& transform = e.Transformation();
const auto& scale = transform.GetScale();
// Add all colliders
if (e.HasComponent<BoxColliderComponent>())
{
const BoxColliderComponent& collider = e.GetComponent<BoxColliderComponent>();
PhysicsWrappers::AddBoxCollider(*actor, *material, collider, scale);
PhysicsWrappers::AddBoxCollider(*actor, *material, collider, transform.Scale);
}
if (e.HasComponent<SphereColliderComponent>())
{
const SphereColliderComponent& collider = e.GetComponent<SphereColliderComponent>();
PhysicsWrappers::AddSphereCollider(*actor, *material, collider, scale);
PhysicsWrappers::AddSphereCollider(*actor, *material, collider, transform.Scale);
}
if (e.HasComponent<CapsuleColliderComponent>())
{
const CapsuleColliderComponent& collider = e.GetComponent<CapsuleColliderComponent>();
PhysicsWrappers::AddCapsuleCollider(*actor, *material, collider, scale);
PhysicsWrappers::AddCapsuleCollider(*actor, *material, collider, transform.Scale);
}
if (e.HasComponent<MeshColliderComponent>())
{
auto& collider = e.GetComponent<MeshColliderComponent>();
PhysicsWrappers::AddMeshCollider(*actor, *material, collider, scale);
PhysicsWrappers::AddMeshCollider(*actor, *material, collider, transform.Scale);
}
if (!PhysicsLayerManager::IsLayerValid(rigidbody.Layer))
@ -148,19 +146,24 @@ namespace Prism
s_SimulationTime -= s_Settings.FixedTimestep;
for (Entity& e : s_SimulatedEntities)
{
if (ScriptEngine::IsEntityModuleValid(e))
ScriptEngine::OnPhysicsUpdateEntity(e, s_Settings.FixedTimestep);
}
s_Scene->simulate(s_Settings.FixedTimestep);
s_Scene->fetchResults(true);
for (Entity& e : s_SimulatedEntities)
{
auto& transform = e.Transformation();
auto& transform = e.Transform();
const auto& rb = e.GetComponent<RigidBodyComponent>();
const auto actor = static_cast<physx::PxRigidActor*>(rb.RuntimeActor);
physx::PxTransform actorPose = actor->getGlobalPose();
transform.SetTranslation(FromPhysXVector(actorPose.p));
transform.SetRotation(FromPhysXQuat(actorPose.q));
transform.Translation = FromPhysXVector(actorPose.p);
transform.Rotation = glm::degrees(glm::eulerAngles(FromPhysXQuat(actorPose.q)));
}
}

View File

@ -4,7 +4,7 @@
#ifndef PHYSXMANAGER_H
#define PHYSXMANAGER_H
#include "glm/glm.hpp"
#include "Prism/Scene/Components.h"
#include "Prism/Scene/Entity.h"
@ -58,7 +58,8 @@ namespace Prism
glm::vec3 WorldBoundsMin = glm::vec3(0.0f);
glm::vec3 WorldBoundsMax = glm::vec3(1.0f);
uint32_t WorldBoundsSubdivisions = 2;
// FrictionType FrictionModel = FrictionType::Patch;
uint32_t SolverIterations = 6;
uint32_t SolverVelocityIterations = 1;
};

View File

@ -21,11 +21,16 @@ namespace Prism
return false;
}
bool PhysicsLayer::IsValid() const
{
return LayerID >= 0 && !Name.empty() && BitValue > 0;
}
uint32_t PhysicsLayerManager::AddLayer(const std::string& name, bool setCollisions)
{
const uint32_t layerId = GetNextLayerID();
const PhysicsLayer layer = { layerId, name, static_cast<uint32_t>(BIT(layerId)), BIT(layerId)};
s_Layers.push_back(layer);
s_Layers.insert(s_Layers.begin() + layerId, layer);
if (setCollisions)
{
@ -40,9 +45,6 @@ namespace Prism
void PhysicsLayerManager::RemoveLayer(uint32_t layerId)
{
if (!IsLayerValid(layerId))
return;
const PhysicsLayer& layerInfo = GetLayer(layerId);
for (auto& otherLayer : s_Layers)
@ -96,17 +98,9 @@ namespace Prism
return layers;
}
PhysicsLayer& PhysicsLayerManager::GetLayer(uint32_t layerId)
PhysicsLayer& PhysicsLayerManager::GetLayer(const uint32_t layerId)
{
for (auto& layer : s_Layers)
{
if (layer.LayerID == layerId)
{
return layer;
}
}
return s_NullLayer;
return layerId >= s_Layers.size() ? s_NullLayer : s_Layers[layerId];
}
PhysicsLayer& PhysicsLayerManager::GetLayer(const std::string& layerName)
@ -129,18 +123,15 @@ namespace Prism
bool PhysicsLayerManager::IsLayerValid(uint32_t layerId)
{
for (const auto& layer : s_Layers)
{
if (layer.LayerID == layerId)
return true;
}
return false;
const PhysicsLayer& layer = GetLayer(layerId);
return layer.LayerID != s_NullLayer.LayerID && layer.IsValid();
}
void PhysicsLayerManager::ClearLayers()
{
s_Layers.clear();
if (s_Layers.size() > 1) {
s_Layers.erase(s_Layers.begin() + 1, s_Layers.end());
}
}
uint32_t PhysicsLayerManager::GetNextLayerID()

View File

@ -15,6 +15,8 @@ namespace Prism
std::string Name;
uint32_t BitValue;
int32_t CollidesWith = 0;
bool IsValid() const;
};
class PhysicsLayerManager
@ -34,17 +36,15 @@ namespace Prism
static bool ShouldCollide(uint32_t layer1, uint32_t layer2);
static bool IsLayerValid(uint32_t layerId);
static void ClearLayers();
private:
static void ClearLayers();
static uint32_t GetNextLayerID();
private:
static std::vector<PhysicsLayer> s_Layers;
static PhysicsLayer s_NullLayer;
private:
friend class SceneSerializer;
};
}

View File

@ -4,21 +4,12 @@
#include "PhysicsUtils.h"
#define GLM_ENABLE_EXPERIMENTAL
#include <filesystem>
#include "glm/gtx/quaternion.hpp"
#include "Prism/Script/ScriptEngine.h"
namespace Prism
{
physx::PxTransform ToPhysXTransform(const Transform& transform)
{
const physx::PxQuat r = ToPhysXQuat(glm::normalize(glm::quat(glm::radians(transform.GetRotation()))));
const physx::PxVec3 p = ToPhysXVector(transform.GetTranslation());
return physx::PxTransform{p, r};
}
physx::PxTransform ToPhysXTransform(const glm::mat4& matrix)
{

View File

@ -9,12 +9,10 @@
#include <PxPhysicsAPI.h>
#include "glm/glm.hpp"
#include "Prism/Core/Math/Transform.h"
namespace Prism
{
physx::PxTransform ToPhysXTransform(const Transform& matrix);
physx::PxTransform ToPhysXTransform(const glm::mat4& matrix);
physx::PxMat44 ToPhysXMatrix(const glm::mat4& matrix);
physx::PxVec3 ToPhysXVector(const glm::vec3& vector);

View File

@ -2,15 +2,15 @@
// Created by sfd on 25-12-20.
//
#include "PhysicsWrappers.h"
#include "Physics3D.h"
#include <cooking/PxCooking.h>
#include "PhysicsWrappers.h"
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/rotate_vector.hpp>
#include "PhysicsLayer.h"
#include "Prism/Script/ScriptEngine.h"
#define PHYSX_DEBUGGER
namespace Prism
@ -177,17 +177,19 @@ namespace Prism
return s_Physics->createScene(sceneDesc);
}
physx::PxRigidActor* PhysicsWrappers::CreateActor(const RigidBodyComponent& rigidbody, const Transform& transform)
physx::PxRigidActor* PhysicsWrappers::CreateActor(const RigidBodyComponent& rigidbody, const TransformComponent& transformComponent)
{
physx::PxRigidActor* actor = nullptr;
const auto transform = ToPhysXTransform(transformComponent.GetTransform());
const PhysicsSettings& settings = Physics3D::GetSettings();
if (rigidbody.BodyType == RigidBodyComponent::Type::Static)
{
actor = s_Physics->createRigidStatic(ToPhysXTransform(transform));
actor = s_Physics->createRigidStatic(transform);
}
else if (rigidbody.BodyType == RigidBodyComponent::Type::Dynamic)
{
physx::PxRigidDynamic* dynamicActor = s_Physics->createRigidDynamic(ToPhysXTransform(transform));
physx::PxRigidDynamic* dynamicActor = s_Physics->createRigidDynamic(transform);
dynamicActor->setRigidBodyFlag(physx::PxRigidBodyFlag::eKINEMATIC, rigidbody.IsKinematic);
@ -202,6 +204,8 @@ namespace Prism
dynamicActor->setAngularVelocity({1,1,1});
dynamicActor->setActorFlag(physx::PxActorFlag::eDISABLE_SIMULATION, false);
dynamicActor->setSolverIterationCounts(settings.SolverIterations, settings.SolverVelocityIterations);
// using OnWake and OnSleep callback should enable eSEND_SLEEP_NOTIFIES
// https://nvidia-omniverse.github.io/PhysX/physx/5.6.1/_api_build/classPxSimulationEventCallback.html#_CPPv4N25PxSimulationEventCallback6onWakeEPP7PxActor5PxU32
dynamicActor->setActorFlag(physx::PxActorFlag::eSEND_SLEEP_NOTIFIES, true);
@ -297,6 +301,7 @@ namespace Prism
{
const auto& vertices = collider.CollisionMesh->GetStaticVertices();
const physx::PxCookingParams cookingParams(s_Physics->getTolerancesScale());
physx::PxConvexMeshDesc convexDesc;

View File

@ -6,7 +6,6 @@
#define PHYSICSWRAPPERS_H
#include "PhysicsUtils.h"
#include "Prism/Scene/Components.h"
#define OVERLAP_MAX_COLLIDERS 10
@ -37,7 +36,7 @@ namespace Prism
{
public:
static physx::PxScene* CreateScene();
static physx::PxRigidActor* CreateActor(const RigidBodyComponent& rigidbody, const Transform& transform);
static physx::PxRigidActor* CreateActor(const RigidBodyComponent& rigidbody, const TransformComponent& transformComponent);
static void SetCollisionFilters(const physx::PxRigidActor& actor, uint32_t physicsLayer);
static void AddBoxCollider(physx::PxRigidActor& actor, const physx::PxMaterial& material, const BoxColliderComponent& collider, const glm::vec3& scale = glm::vec3(0.0f));

View File

@ -9,12 +9,15 @@
#include "SceneCamera.h"
#include "box2d/id.h"
#include "glm/glm.hpp"
#include "Prism/Core/Ref.h"
#include "Prism/Core/UUID.h"
#include "Prism/Core/Math/Transform.h"
#include "Prism/Renderer/Mesh.h"
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/quaternion.hpp>
namespace Prism
{
struct IDComponent
@ -38,15 +41,22 @@ namespace Prism
struct TransformComponent
{
Transform Transformation;
glm::vec3 Translation = { 0.0f, 0.0f, 0.0f };
glm::vec3 Rotation = { 0.0f, 0.0f, 0.0f };
glm::vec3 Scale = { 1.0f, 1.0f, 1.0f };
TransformComponent() = default;
TransformComponent(const TransformComponent& other) = default;
TransformComponent(const Transform& transform)
: Transformation(transform) {}
TransformComponent(const glm::vec3& translation)
: Translation(translation) {}
operator Transform& () { return Transformation; }
operator const Transform& () const { return Transformation; }
glm::mat4 GetTransform() const
{
return glm::translate(glm::mat4(1.0f), Translation)
* glm::toMat4(glm::quat(glm::radians(Rotation)))
* glm::scale(glm::mat4(1.0f), Scale);
}
};

View File

@ -50,8 +50,8 @@ namespace Prism
}
const std::string& Tag() const { return m_Scene->m_Registry.get<TagComponent>(m_EntityHandle).Tag; }
Transform& Transformation() { return m_Scene->m_Registry.get<TransformComponent>(m_EntityHandle); }
const Transform& Transformation() const { return m_Scene->m_Registry.get<TransformComponent>(m_EntityHandle); }
TransformComponent& Transform() { return m_Scene->m_Registry.get<TransformComponent>(m_EntityHandle); }
glm::mat4 Transform() const { return m_Scene->m_Registry.get<TransformComponent>(m_EntityHandle).GetTransform(); }
operator uint32_t () const { return (uint32_t)m_EntityHandle; }
operator entt::entity() const{ return m_EntityHandle; }

View File

@ -188,17 +188,16 @@ namespace Prism
for (const auto entity : view)
{
Entity e = { entity, this };
auto& transform = e.Transformation();
const auto& rb2d = e.GetComponent<RigidBody2DComponent>();
const auto& position = b2Body_GetPosition(rb2d.RuntimeBodyID);
const auto& rotation = transform.GetRotation();
const float angle = b2Rot_GetAngle(b2Body_GetRotation(rb2d.RuntimeBodyID));
float angle = b2Rot_GetAngle(b2Body_GetRotation(rb2d.RuntimeBodyID));
auto& transform = e.GetComponent<TransformComponent>();
transform.Translation.x = position.x;
transform.Translation.y = position.y;
transform.Rotation.z = angle;
transform.SetRotation({rotation.x, rotation.y, angle});
transform.SetScale(transform.GetScale());
transform.SetTranslation({position.x, position.y, transform.GetTranslation().z});
}
}
@ -214,7 +213,7 @@ namespace Prism
if (!cameraEntity)
return;
const glm::mat4 cameraViewMatrix = glm::inverse(cameraEntity.Transformation().GetMatrix());
const glm::mat4 cameraViewMatrix = glm::inverse(cameraEntity.Transform().GetTransform());
PM_CORE_ASSERT(cameraEntity, "Scene does not contain any cameras!");
SceneCamera& camera = cameraEntity.GetComponent<CameraComponent>();
camera.SetViewportSize(m_ViewportWidth, m_ViewportHeight);
@ -231,7 +230,7 @@ namespace Prism
meshComponent.Mesh->OnUpdate(ts);
// TODO: Should we render (logically)
SceneRenderer::SubmitMesh(meshComponent, transformComponent.Transformation.GetMatrix(), nullptr);
SceneRenderer::SubmitMesh(meshComponent, transformComponent.GetTransform(), nullptr);
}
}
SceneRenderer::EndScene();
@ -273,7 +272,7 @@ namespace Prism
meshComponent.Mesh->OnUpdate(ts);
// TODO: Should we render (logically)
SceneRenderer::SubmitMesh(meshComponent, transformComponent.Transformation.GetMatrix());
SceneRenderer::SubmitMesh(meshComponent, transformComponent.GetTransform());
}
}
@ -285,7 +284,7 @@ namespace Prism
auto& collider = e.GetComponent<BoxColliderComponent>();
if (m_SelectedEntity == entity)
SceneRenderer::SubmitColliderMesh(collider, e.GetComponent<TransformComponent>().Transformation.GetMatrix());
SceneRenderer::SubmitColliderMesh(collider, e.GetComponent<TransformComponent>().GetTransform());
}
}
@ -297,7 +296,7 @@ namespace Prism
auto& collider = e.GetComponent<SphereColliderComponent>();
if (m_SelectedEntity == entity)
SceneRenderer::SubmitColliderMesh(collider, e.GetComponent<TransformComponent>().Transformation.GetMatrix());
SceneRenderer::SubmitColliderMesh(collider, e.GetComponent<TransformComponent>().GetTransform());
}
}
@ -309,7 +308,7 @@ namespace Prism
auto& collider = e.GetComponent<CapsuleColliderComponent>();
if (m_SelectedEntity == entity)
SceneRenderer::SubmitColliderMesh(collider, e.GetComponent<TransformComponent>().Transformation.GetMatrix());
SceneRenderer::SubmitColliderMesh(collider, e.GetComponent<TransformComponent>().GetTransform());
}
}
@ -322,7 +321,7 @@ namespace Prism
if (m_SelectedEntity == entity)
{
SceneRenderer::SubmitColliderMesh(collider, e.GetComponent<TransformComponent>().Transformation.GetMatrix());
SceneRenderer::SubmitColliderMesh(collider, e.GetComponent<TransformComponent>().GetTransform());
}
}
}
@ -377,9 +376,7 @@ namespace Prism
for (auto entity : view)
{
Entity e = { entity, this };
auto& transform = e.Transformation();
const auto& position = transform.GetTranslation();
const auto& rotation = transform.GetRotation();
auto& transform = e.Transform();
auto& rigidBody2D = m_Registry.get<RigidBody2DComponent>(entity);
b2BodyDef bodyDef = b2DefaultBodyDef();
@ -389,9 +386,9 @@ namespace Prism
bodyDef.type = b2_dynamicBody;
else if (rigidBody2D.BodyType == RigidBody2DComponent::Type::Kinematic)
bodyDef.type = b2_kinematicBody;
bodyDef.position = {position.x, position.y};
bodyDef.position = {transform.Translation.x, transform.Translation.y};
float rotationZ = rotation.z;
float rotationZ = transform.Rotation.z;
bodyDef.rotation = b2Rot{cos(rotationZ), sin(rotationZ)};
// box2D fixRotation renamed to motionlocks
@ -525,7 +522,7 @@ namespace Prism
auto& idComponent = entity.AddComponent<IDComponent>();
idComponent.ID = {};
entity.AddComponent<TransformComponent>(Transform());
entity.AddComponent<TransformComponent>();
if (!name.empty())
entity.AddComponent<TagComponent>(name);
@ -539,7 +536,7 @@ namespace Prism
auto& idComponent = entity.AddComponent<IDComponent>();
idComponent.ID = uuid;
entity.AddComponent<TransformComponent>(Transform());
entity.AddComponent<TransformComponent>();
if (!name.empty())
entity.AddComponent<TagComponent>(name);

View File

@ -4,20 +4,9 @@
#include "SceneSerializer.h"
#include "SceneSerializer.h"
#include "Entity.h"
#include "Components.h"
#include "Prism/Script/ScriptEngine.h"
#include "yaml-cpp/yaml.h"
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/quaternion.hpp>
#include <glm/gtx/matrix_decompose.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <fstream>
#include "Prism/Script/ScriptEngine.h"
#include "Prism/Physics/PhysicsLayer.h"
#include "Prism/Physics/PhysicsWrappers.h"
@ -185,10 +174,10 @@ namespace Prism
out << YAML::Key << "TransformComponent";
out << YAML::BeginMap; // TransformComponent
auto& transform = entity.GetComponent<TransformComponent>().Transformation;
out << YAML::Key << "Position" << YAML::Value << transform.GetTranslation();
out << YAML::Key << "Rotation" << YAML::Value << transform.GetRotation();
out << YAML::Key << "Scale" << YAML::Value << transform.GetScale();
auto& transform = entity.GetComponent<TransformComponent>();
out << YAML::Key << "Position" << YAML::Value << transform.Translation;
out << YAML::Key << "Rotation" << YAML::Value << transform.Rotation;
out << YAML::Key << "Scale" << YAML::Value << transform.Scale;
out << YAML::EndMap; // TransformComponent
}
@ -450,9 +439,10 @@ namespace Prism
out << YAML::Key << "PhysicsLayers";
out << YAML::Value << YAML::BeginSeq;
for (uint32_t i = 0; i < PhysicsLayerManager::GetLayerCount(); i++)
for (const auto& layer : PhysicsLayerManager::GetLayers())
{
const PhysicsLayer& layer = PhysicsLayerManager::GetLayer(i);
if (layer.LayerID == 0)
continue;
out << YAML::BeginMap;
out << YAML::Key << "Name" << YAML::Value << layer.Name;
@ -531,20 +521,16 @@ namespace Prism
if (transformComponent)
{
// Entities always have transforms
auto& transform = deserializedEntity.GetComponent<TransformComponent>().Transformation;
auto& transform = deserializedEntity.GetComponent<TransformComponent>();
auto translation = transformComponent["Position"].as<glm::vec3>();
auto rotation = transformComponent["Rotation"].as<glm::vec3>();
auto scale = transformComponent["Scale"].as<glm::vec3>();
transform.SetTranslation(translation);
transform.SetRotation(rotation);
transform.SetScale(scale);
transform.Translation = transformComponent["Position"].as<glm::vec3>();
transform.Rotation = transformComponent["Rotation"].as<glm::vec3>();
transform.Scale = transformComponent["Scale"].as<glm::vec3>();
PM_CORE_INFO("Entity Transform:");
PM_CORE_INFO(" Translation: {0}, {1}, {2}", translation.x, translation.y, translation.z);
PM_CORE_INFO(" Rotation: {0}, {1}, {2}", rotation.x, rotation.y, rotation.z);
PM_CORE_INFO(" Scale: {0}, {1}, {2}", scale.x, scale.y, scale.z);
PM_CORE_INFO(" Translation: {0}, {1}, {2}", transform.Translation.x, transform.Translation.y, transform.Translation.z);
PM_CORE_INFO(" Rotation: {0}, {1}, {2}", transform.Rotation.x, transform.Rotation.y, transform.Rotation.z);
PM_CORE_INFO(" Scale: {0}, {1}, {2}", transform.Scale.x, transform.Scale.y, transform.Scale.z);
}
auto scriptComponent = entity["ScriptComponent"];

View File

@ -8,7 +8,6 @@
#include "ScriptEngineRegistry.h"
#include <iostream>
#include <mono/jit/jit.h>
#include <mono/metadata/assembly.h>
#include <mono/metadata/debug-helpers.h>
@ -56,6 +55,7 @@ namespace Prism
MonoMethod* OnCreateMethod = nullptr;
MonoMethod* OnDestroyMethod = nullptr;
MonoMethod* OnUpdateMethod = nullptr;
MonoMethod* OnPhysicsUpdateMethod = nullptr;
// Physics
MonoMethod* OnCollisionBeginMethod = nullptr;
@ -70,6 +70,7 @@ namespace Prism
Constructor = GetMethod(s_CoreAssemblyImage, "Prism.Entity:.ctor(ulong)");
OnCreateMethod = GetMethod(image, FullName + ":OnCreate()");
OnUpdateMethod = GetMethod(image, FullName + ":OnUpdate(single)");
OnPhysicsUpdateMethod = GetMethod(image, FullName + ":OnPhysicsUpdate(single)");
// Physics (Entity class)
OnCollision2DBeginMethod = GetMethod(s_CoreAssemblyImage, "Prism.Entity:OnCollision2DBegin(single)");
@ -188,9 +189,9 @@ namespace Prism
MonoAssembly* assembly = LoadAssemblyFromFile(path.c_str()); //mono_domain_assembly_open(s_MonoDomain, path.c_str());
if (!assembly)
std::cout << "Could not load assembly: " << path << std::endl;
PM_CORE_ERROR("Could not load assembly: ", path);
else
std::cout << "Successfully loaded assembly: " << path << std::endl;
PM_CORE_INFO("Successfully loaded assembly: ", path);
return assembly;
}
@ -199,7 +200,7 @@ namespace Prism
{
MonoImage* image = mono_assembly_get_image(assembly);
if (!image)
std::cout << "mono_assembly_get_image failed" << std::endl;
PM_CORE_FATAL("mono_assembly_get_image failed");
return image;
}
@ -208,7 +209,7 @@ namespace Prism
{
MonoClass* monoClass = mono_class_from_name(image, scriptClass.NamespaceName.c_str(), scriptClass.ClassName.c_str());
if (!monoClass)
std::cout << "mono_class_from_name failed" << std::endl;
PM_CORE_FATAL("mono_class_from_name failed");
return monoClass;
}
@ -217,10 +218,10 @@ namespace Prism
{
MonoObject* instance = mono_object_new(s_MonoDomain, scriptClass.Class);
if (!instance)
std::cout << "mono_object_new failed" << std::endl;
PM_CORE_FATAL("mono_object_new failed");
mono_runtime_object_init(instance);
uint32_t handle = mono_gchandle_new(instance, false);
const uint32_t handle = mono_gchandle_new(instance, false);
return handle;
}
@ -505,6 +506,16 @@ namespace Prism
}
}
void ScriptEngine::OnPhysicsUpdateEntity(Entity entity, float fixedTimeStep)
{
EntityInstance& entityInstance = GetEntityInstanceData(entity.GetSceneUUID(), entity.GetUUID()).Instance;
if (entityInstance.ScriptClass->OnPhysicsUpdateMethod)
{
void* args[] = { &fixedTimeStep };
CallMethod(entityInstance.GetInstance(), entityInstance.ScriptClass->OnPhysicsUpdateMethod, args);
}
}
void ScriptEngine::OnCollision2DBegin(Entity entity)
{
@ -646,7 +657,7 @@ namespace Prism
MonoClass* monoClass = mono_class_from_name(s_CoreAssemblyImage, namespaceName.c_str(), className.c_str());
if (!monoClass)
std::cout << "mono_class_from_name failed" << std::endl;
PM_CORE_FATAL("mono_class_from_name failed");
s_Classes[fullName] = monoClass;

View File

@ -117,6 +117,7 @@ namespace Prism
static void OnCreateEntity(Entity entity);
static void OnUpdateEntity(Entity entity, TimeStep ts);
static void OnPhysicsUpdateEntity(Entity entity, float fixedTimeStep);
static void OnCollision2DBegin(Entity entity);
static void OnCollision2DEnd(Entity entity);

View File

@ -6,24 +6,15 @@
#include "ScriptEngine.h"
#include "box2d/box2d.h"
#include "mono/metadata/metadata.h"
#include "mono/metadata/object.h"
#include "mono/metadata/reflection.h"
#include <mono/metadata/object.h>
#include <mono/metadata/reflection.h>
#include <mono/metadata/appdomain.h>
#include "Prism/Core/Input.h"
#include "Prism/Core/Math/Noise.h"
#include "Prism/Scene/Entity.h"
#include "Prism/Scene/Scene.h"
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/quaternion.hpp>
#include <glm/gtx/matrix_decompose.hpp>
#include "PxRigidActor.h"
#include "PxRigidDynamic.h"
#include "Prism/Physics/Physics3D.h"
#include "Prism/Physics/PhysicsUtils.h"
#include "Prism/Physics/PhysicsWrappers.h"
#include <mono/metadata/appdomain.h>
namespace Prism {
extern std::unordered_map<MonoType*, std::function<bool(Entity&)>> s_HasComponentFuncs;
@ -385,12 +376,16 @@ namespace Prism { namespace Script {
PM_CORE_ASSERT(entityMap.find(entityID) != entityMap.end(), "Invalid entity ID or entity doesn't exist in scene!");
Entity entity = entityMap.at(entityID);
const Transform& transform = entity.GetComponent<TransformComponent>();
const auto& transform = entity.GetComponent<TransformComponent>();
const glm::quat rotation = glm::quat(glm::radians(transform.Rotation));
const glm::vec3 right = glm::normalize(glm::rotate(rotation, glm::vec3(1.0f, 0.0f, 0.0f)));
const glm::vec3 up = glm::normalize(glm::rotate(rotation, glm::vec3(0.0f, 1.0f, 0.0f)));
const glm::vec3 forward = glm::normalize(glm::rotate(rotation, glm::vec3(0.0f, 0.0f, -1.0f)));
*outTransform = {
transform.GetTranslation(), transform.GetRotation(), transform.GetScale(),
transform.GetUpDirection(), transform.GetRightDirection(), transform.GetForwardDirection()
transform.Translation, transform.Rotation, transform.Scale,
up, right, forward
};
}
@ -402,11 +397,11 @@ namespace Prism { namespace Script {
PM_CORE_ASSERT(entityMap.find(entityID) != entityMap.end(), "Invalid entity ID or entity doesn't exist in scene!");
Entity entity = entityMap.at(entityID);
Transform& transform = entity.GetComponent<TransformComponent>();
auto& transform = entity.GetComponent<TransformComponent>();
transform.SetTranslation(inTransform->Translation);
transform.SetRotation(inTransform->Rotation);
transform.SetScale(inTransform->Scale);
transform.Translation = inTransform->Translation;
transform.Rotation = inTransform->Rotation;
transform.Scale = inTransform->Scale;
}
void* Prism_MeshComponent_GetMesh(const uint64_t entityID)
@ -647,9 +642,9 @@ namespace Prism { namespace Script {
PM_CORE_ASSERT(dynamicActor);
physx::PxTransform transform = dynamicActor->getGlobalPose();
transform.q *= (physx::PxQuat(glm::radians(rotation->x), { 1.0F, 0.0F, 0.0F })
* physx::PxQuat(glm::radians(rotation->y), { 0.0F, 1.0F, 0.0F })
* physx::PxQuat(glm::radians(rotation->z), { 0.0F, 0.0F, 1.0F }));
transform.q *= (physx::PxQuat(glm::radians(rotation->x), { 1.0f, 0.0f, 0.0f })
* physx::PxQuat(glm::radians(rotation->y), { 0.0f, 1.0f, 0.0f })
* physx::PxQuat(glm::radians(rotation->z), { 0.0f, 0.0f, 1.0f }));
dynamicActor->setGlobalPose(transform);
}

View File

@ -4,13 +4,10 @@
#ifndef SCRIPTWRAPPERS_H
#define SCRIPTWRAPPERS_H
#include "Prism/Core/Input.h"
#include "Prism/Core/KeyCodes.h"
#include "Prism/Core/Ref.h"
#include "Prism/Physics/Physics3D.h"
#include "Prism/Renderer/Material.h"
#include "Prism/Renderer/Mesh.h"
#include "Prism/Renderer/Texture.h"
extern "C" {
typedef struct _MonoString MonoString;