mirror of
https://github.com/johndoe6345789/SDL3CPlusPlus.git
synced 2026-04-30 08:34:59 +00:00
feat(physics): Enhance physics bridge service with new functionalities
- Added SetGravity method to adjust the gravity in the physics world. - Introduced AddSphereRigidBody method for creating sphere rigid bodies. - Implemented RemoveRigidBody method to delete existing rigid bodies. - Added SetRigidBodyTransform method to update the position and rotation of rigid bodies. - Included ApplyForce and ApplyImpulse methods for applying forces and impulses to rigid bodies. - Added SetLinearVelocity method to set the linear velocity of rigid bodies. - Enhanced StepSimulation method to accept a maxSubSteps parameter. - Implemented GetBodyCount method to retrieve the number of rigid bodies in the world. - Added Clear method to remove all rigid bodies from the physics world. - Updated the script engine service to bind new physics methods to Lua. - Enhanced material configuration handling in shader script service. - Introduced MaterialXMaterialConfig structure for better material management. - Added texture binding support in ShaderPaths structure. - Included stb_image implementation for image loading support.
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
#include <glm/gtc/quaternion.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include <lua.hpp>
|
||||
#include <cmath>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace sdl3cpp::services::impl::lua {
|
||||
@@ -28,6 +29,25 @@ std::array<float, 3> ReadVector3(lua_State* L, int index) {
|
||||
return result;
|
||||
}
|
||||
|
||||
std::array<float, 2> ReadVector2(lua_State* L, int index) {
|
||||
std::array<float, 2> result{};
|
||||
int absIndex = lua_absindex(L, index);
|
||||
size_t len = lua_rawlen(L, absIndex);
|
||||
if (len != 2) {
|
||||
throw std::runtime_error("Expected vector with 2 components");
|
||||
}
|
||||
for (size_t i = 1; i <= 2; ++i) {
|
||||
lua_rawgeti(L, absIndex, static_cast<int>(i));
|
||||
if (!lua_isnumber(L, -1)) {
|
||||
lua_pop(L, 1);
|
||||
throw std::runtime_error("Vector component is not a number");
|
||||
}
|
||||
result[i - 1] = static_cast<float>(lua_tonumber(L, -1));
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::array<float, 4> ReadQuaternion(lua_State* L, int index) {
|
||||
std::array<float, 4> result{};
|
||||
int absIndex = lua_absindex(L, index);
|
||||
@@ -99,6 +119,70 @@ void PushMatrix(lua_State* L, const glm::mat4& matrix) {
|
||||
|
||||
} // namespace
|
||||
|
||||
int LuaGlmMatrixIdentity(lua_State* L) {
|
||||
glm::mat4 matrix(1.0f);
|
||||
PushMatrix(L, matrix);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int LuaGlmMatrixMultiply(lua_State* L) {
|
||||
std::array<float, 16> left = ReadMatrix(L, 1);
|
||||
std::array<float, 16> right = ReadMatrix(L, 2);
|
||||
glm::mat4 leftMat = glm::make_mat4(left.data());
|
||||
glm::mat4 rightMat = glm::make_mat4(right.data());
|
||||
glm::mat4 combined = leftMat * rightMat;
|
||||
PushMatrix(L, combined);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int LuaGlmMatrixTranslation(lua_State* L) {
|
||||
float x = static_cast<float>(luaL_checknumber(L, 1));
|
||||
float y = static_cast<float>(luaL_checknumber(L, 2));
|
||||
float z = static_cast<float>(luaL_checknumber(L, 3));
|
||||
glm::mat4 matrix = glm::translate(glm::mat4(1.0f), glm::vec3(x, y, z));
|
||||
PushMatrix(L, matrix);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int LuaGlmMatrixRotationX(lua_State* L) {
|
||||
float radians = static_cast<float>(luaL_checknumber(L, 1));
|
||||
glm::mat4 matrix = glm::rotate(glm::mat4(1.0f), -radians, glm::vec3(1.0f, 0.0f, 0.0f));
|
||||
PushMatrix(L, matrix);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int LuaGlmMatrixRotationY(lua_State* L) {
|
||||
float radians = static_cast<float>(luaL_checknumber(L, 1));
|
||||
glm::mat4 matrix = glm::rotate(glm::mat4(1.0f), -radians, glm::vec3(0.0f, 1.0f, 0.0f));
|
||||
PushMatrix(L, matrix);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int LuaGlmMatrixLookAt(lua_State* L) {
|
||||
std::array<float, 3> eye = ReadVector3(L, 1);
|
||||
std::array<float, 3> center = ReadVector3(L, 2);
|
||||
std::array<float, 3> up = ReadVector3(L, 3);
|
||||
glm::mat4 matrix = glm::lookAt(ToVec3(eye), ToVec3(center), ToVec3(up));
|
||||
PushMatrix(L, matrix);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int LuaGlmMatrixPerspective(lua_State* L) {
|
||||
float fov = static_cast<float>(luaL_checknumber(L, 1));
|
||||
float aspect = static_cast<float>(luaL_checknumber(L, 2));
|
||||
float zNear = static_cast<float>(luaL_checknumber(L, 3));
|
||||
float zFar = static_cast<float>(luaL_checknumber(L, 4));
|
||||
float tanHalf = std::tan(fov * 0.5f);
|
||||
glm::mat4 matrix(0.0f);
|
||||
matrix[0][0] = 1.0f / (aspect * tanHalf);
|
||||
matrix[1][1] = -1.0f / tanHalf;
|
||||
matrix[2][2] = zFar / (zNear - zFar);
|
||||
matrix[2][3] = -1.0f;
|
||||
matrix[3][2] = (zNear * zFar) / (zNear - zFar);
|
||||
PushMatrix(L, matrix);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int LuaGlmMatrixFromTransform(lua_State* L) {
|
||||
std::array<float, 3> translation = ReadVector3(L, 1);
|
||||
std::array<float, 4> rotation = ReadQuaternion(L, 2);
|
||||
|
||||
Reference in New Issue
Block a user