Allowed scene items cleanup to happen earlier to prevent access null weak pointers. Also added Sphere Mesh and Sphere Collider(s)

This commit is contained in:
2024-11-26 18:44:52 -06:00
parent e91b1983c8
commit f4120095ed
22 changed files with 380 additions and 79 deletions

View File

@ -1,14 +0,0 @@
// Copyright (c) 2024 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "BoxCollider.hpp"
using namespace Dawn;
std::shared_ptr<JPH::ShapeSettings> BoxCollider::getShapeSettings() {
return std::make_shared<JPH::BoxShapeSettings>(
JPH::Vec3(shape.x, shape.y, shape.z)
);
}

View File

@ -6,5 +6,6 @@
target_sources(${DAWN_TARGET_NAME}
PRIVATE
Collider.cpp
BoxCollider.cpp
CubeCollider.cpp
SphereCollider.cpp
)

View File

@ -11,6 +11,29 @@ using namespace Dawn;
using namespace JPH;
using namespace JPH::literals;
EMotionType Collider::getMotionType(const ColliderType colliderType) {
EMotionType motionType;
switch(colliderType) {
case ColliderType::DYNAMIC:
return EMotionType::Dynamic;
break;
case ColliderType::STATIC:
return EMotionType::Static;
break;
case ColliderType::KINEMATIC:
return EMotionType::Kinematic;
break;
}
assertUnreachable("Invalid ColliderType");
return EMotionType::Kinematic;
}
//
void Collider::onInit() {
assertNull(this->body, "Body is not NULL?");
@ -24,7 +47,7 @@ void Collider::onInit() {
shape,
RVec3(pos.x, pos.y, pos.z),
Quat::sIdentity(),
EMotionType::Dynamic,
Collider::getMotionType(this->colliderType),
layer
);
@ -40,10 +63,46 @@ void Collider::onDispose() {
getBodyInterface().DestroyBody(this->bodyId);
}
void Collider::notifyShapeChanged() {
if(!this->isColliderReady()) return;
auto settings = this->getShapeSettings();
auto shapeResult = settings->Create();
auto shape = shapeResult.Get();
getBodyInterface().SetShape(
this->bodyId,
shape,
// TODO: I may not always need to re-activate the body here.
true,
EActivation::Activate
);
}
bool_t Collider::isColliderReady() {
return this->body != nullptr;
}
BodyInterface & Collider::getBodyInterface() {
return getGame()->physicsManager->getBodyInterface();
}
ColliderType Collider::getColliderType() {
return colliderType;
}
BodyID Collider::getBodyId() {
return bodyId;
}
void Collider::setColliderType(const ColliderType type) {
this->colliderType = type;
if(!this->isColliderReady()) return;
getBodyInterface().SetMotionType(
this->bodyId,
Collider::getMotionType(type),
EActivation::Activate// TODO: Should be false on kinematics
);
}

View File

@ -16,10 +16,18 @@ namespace Dawn {
class Collider : public SceneComponent {
private:
JPH::Body *body = nullptr;
ColliderType colliderType = ColliderType::DYNAMIC;
/**
* Returns the JoltPhysics motion type for the collider type.
*
* @param colliderType The collider type.
* @return The JoltPhysics motion type.
*/
static JPH::EMotionType getMotionType(const ColliderType colliderType);
protected:
JPH::BodyID bodyId;
JPH::EMotionType emotionType = JPH::EMotionType::Dynamic;
JPH::ObjectLayer layer = 1;
/**
@ -36,17 +44,41 @@ namespace Dawn {
*/
JPH::BodyInterface & getBodyInterface();
public:
ColliderType type = ColliderType::KINEMATIC;
/**
* Callable by subclasses to notify that the shape has changed.
*/
void notifyShapeChanged();
public:
void onInit() override;
void onDispose() override;
/**
* Returns whether the collider is ready.
*
* @return Whether the collider is ready.
*/
bool_t isColliderReady();
/**
* Returns the collider type.
*
* @return The collider type.
*/
ColliderType getColliderType();
/**
* Returns the JoltPhysics body ID of the collider.
*
* @return The body ID of the collider.
*/
JPH::BodyID getBodyId();
/**
* Sets the collider type.
*
* @param colliderType The collider type.
*/
void setColliderType(ColliderType colliderType);
};
}

View File

@ -0,0 +1,23 @@
// Copyright (c) 2024 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "CubeCollider.hpp"
using namespace Dawn;
std::shared_ptr<JPH::ShapeSettings> CubeCollider::getShapeSettings() {
return std::make_shared<JPH::BoxShapeSettings>(
JPH::Vec3(shape.x, shape.y, shape.z)
);
}
glm::vec3 CubeCollider::getShape() {
return shape;
}
void CubeCollider::setShape(const glm::vec3 &shape) {
this->shape = shape;
this->notifyShapeChanged();
}

View File

@ -0,0 +1,32 @@
// Copyright (c) 2024 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "Collider.hpp"
namespace Dawn {
class CubeCollider : public Collider {
private:
glm::vec3 shape = glm::vec3(1, 1, 1);
protected:
std::shared_ptr<JPH::ShapeSettings> getShapeSettings() override;
public:
/**
* Returns the shape of the cube collider.
*
* @return The shape of the cube collider.
*/
glm::vec3 getShape();
/**
* Sets the shape of the cube collider.
*
* @param shape The shape of the cube collider.
*/
void setShape(const glm::vec3 &shape);
};
}

View File

@ -0,0 +1,12 @@
// Copyright (c) 2024 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "SphereCollider.hpp"
using namespace Dawn;
std::shared_ptr<JPH::ShapeSettings> SphereCollider::getShapeSettings() {
return std::make_shared<JPH::SphereShapeSettings>(radius);
}

View File

@ -7,11 +7,11 @@
#include "Collider.hpp"
namespace Dawn {
class BoxCollider : public Collider {
class SphereCollider : public Collider {
protected:
std::shared_ptr<JPH::ShapeSettings> getShapeSettings() override;
public:
glm::vec3 shape = glm::vec3(1, 1, 1);
float radius = 1.0f;
};
}