Skip to content

Commit

Permalink
Add physics system ray casting, fix viewDir, fix dbg render
Browse files Browse the repository at this point in the history
  • Loading branch information
cfnptr committed Feb 9, 2025
1 parent 82ca682 commit ab898bf
Show file tree
Hide file tree
Showing 4 changed files with 241 additions and 75 deletions.
88 changes: 71 additions & 17 deletions include/garden/system/physics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,7 @@ class PhysicsSystem final : public ComponentSystem<RigidbodyComponent>,
void* contactListener = nullptr;
void* bodyInterface = nullptr;
const void* lockInterface = nullptr;
const void* narrowPhaseQuery = nullptr;
ID<Entity> thisBody = {};
ID<Entity> otherBody = {};
float deltaTimeAccum = 0.0f;
Expand Down Expand Up @@ -820,12 +821,82 @@ class PhysicsSystem final : public ComponentSystem<RigidbodyComponent>,
*/
ID<Shape> createSharedCustomShape(void* shapeInstance);

/*******************************************************************************************************************
* @brief Returns shape instance view.
* @param shape target shape instance
*/
View<Shape> get(ID<Shape> shape) const noexcept { return shapes.get(shape); }

/**
* Destroys shape instance.
* @param shape target shape instance or null
*/
void destroy(ID<Shape> shape) { shapes.destroy(shape); }
/**
* Destroys shared shape if it's the last one.
* @param shape target shape instance or null
*/
void destroyShared(ID<Shape> shape);

/*******************************************************************************************************************
* @brief Improves collision detection performance. (Expensive operation!)
*/
void optimizeBroadPhase();

/**
* @brief Casts a ray to find the closest object hit.
* @return True if found an object hit.
*
* @param[in] ray target ray to cast
* @param maxDistance maximum ray cast distance
*/
bool castRay(const Ray& ray, float maxDistance = 1000.0f);
/**
* @brief Casts a ray to find the closest object hit.
* @return True if found an object hit.
*
* @param[in] ray target ray to cast
* @param[out] entity hit object entity
* @param maxDistance maximum ray cast distance
*/
bool castRay(const Ray& ray, ID<Entity>& entity, float maxDistance = 1000.0f);
/**
* @brief Casts a ray to find the closest object hit.
* @return True if found an object hit.
*
* @param[in] ray target ray to cast
* @param[out] entity hit object entity
* @param[out] hitPoint object hit point
* @param maxDistance maximum ray cast distance
*/
bool castRay(const Ray& ray, ID<Entity>& entity, float3& hitPoint, float maxDistance = 1000.0f);
/**
* @brief Casts a ray to find the closest object hit.
* @return True if found an object hit.
*
* @param[in] ray target ray to cast
* @param[out] entity hit object entity
* @param[out] subShapeID hit object sub shape ID
* @param[out] hitPoint object hit point
* @param maxDistance maximum ray cast distance
*/
bool castRay(const Ray& ray, ID<Entity>& entity, uint32& subShapeID,
float3& hitPoint, float maxDistance = 1000.0f);
/**
* @brief Casts a ray to find the closest object hit.
* @return True if found an object hit.
*
* @param[in] ray target ray to cast
* @param[out] hitPoint object hit point
* @param[out] entity hit object entity
* @param[out] subShapeID hit object sub shape ID
* @param[out] normal hit surface normal vector
* @param maxDistance maximum ray cast distance
*/
bool castRay(const Ray& ray, ID<Entity>& entity, uint32& subShapeID,
float3& hitPoint, float3& normal, float maxDistance = 1000.0f);

/*******************************************************************************************************************
* @brief Wakes up rigidbody if it's sleeping.
* @details It also wakes up all rigidbody descendants.
*/
Expand Down Expand Up @@ -864,23 +935,6 @@ class PhysicsSystem final : public ComponentSystem<RigidbodyComponent>,
void setBroadPhaseLayerName(uint8 broadPhaseLayer, const string& debugName);
#endif

/*******************************************************************************************************************
* @brief Returns shape instance view.
* @param shape target shape instance
*/
View<Shape> get(ID<Shape> shape) const noexcept { return shapes.get(shape); }

/**
* Destroys shape instance.
* @param shape target shape instance or null
*/
void destroy(ID<Shape> shape) { shapes.destroy(shape); }
/**
* Destroys shared shape if it's the last one.
* @param shape target shape instance or null
*/
void destroyShared(ID<Shape> shape);

/**
* @brief Returns physics system internal instance.
* @warning Use only if you know what you are doing!
Expand Down
2 changes: 2 additions & 0 deletions source/editor/system/physics-renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ void PhysicsDebugRenderer::drawLines(const float4x4& viewProj)
pushConstants->mvp = viewProj;

pipelineView->bind();
pipelineView->setViewportScissor();
pipelineView->pushConstants();
pipelineView->draw(linesBuffer, (uint32)lines.size() * 2);
graphicsSystem->destroy(linesBuffer);
Expand All @@ -94,6 +95,7 @@ void PhysicsDebugRenderer::drawTriangles(const float4x4& viewProj)
pushConstants->mvp = viewProj;

pipelineView->bind();
pipelineView->setViewportScissor();
pipelineView->pushConstants();
pipelineView->draw(trianglesBuffer, (uint32)triangles.size());
graphicsSystem->destroy(trianglesBuffer);
Expand Down
2 changes: 1 addition & 1 deletion source/system/graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ static void prepareCameraConstants(ID<Entity> camera, ID<Entity> directionalLigh
cameraConstants.inverseView = inverse(cameraConstants.view);
cameraConstants.inverseProj = inverse(cameraConstants.projection);
cameraConstants.invViewProj = inverse(cameraConstants.viewProj);
auto viewDir = cameraConstants.view * float4(float3::front, 1.0f);
auto viewDir = cameraConstants.inverseView * float4(float3::front, 1.0f);
cameraConstants.viewDir = float4(normalize((float3)viewDir), 0.0f);

if (directionalLight)
Expand Down
Loading

0 comments on commit ab898bf

Please sign in to comment.