Example physics implemented.

This commit is contained in:
2024-11-26 14:26:53 -06:00
parent 98f2f3e955
commit e91b1983c8
19 changed files with 656 additions and 29 deletions

View File

@ -0,0 +1,14 @@
// 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

@ -0,0 +1,17 @@
// 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 BoxCollider : public Collider {
protected:
std::shared_ptr<JPH::ShapeSettings> getShapeSettings() override;
public:
glm::vec3 shape = glm::vec3(1, 1, 1);
};
}

View File

@ -0,0 +1,10 @@
# Copyright (c) 2023 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
target_sources(${DAWN_TARGET_NAME}
PRIVATE
Collider.cpp
BoxCollider.cpp
)

View File

@ -0,0 +1,49 @@
// Copyright (c) 2024 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "Collider.hpp"
#include "assert/assert.hpp"
#include "game/Game.hpp"
using namespace Dawn;
using namespace JPH;
using namespace JPH::literals;
void Collider::onInit() {
assertNull(this->body, "Body is not NULL?");
auto settings = this->getShapeSettings();
auto shapeResult = settings->Create();
auto shape = shapeResult.Get();
auto pos = getItem()->getLocalPosition();
BodyCreationSettings bodySettings(
shape,
RVec3(pos.x, pos.y, pos.z),
Quat::sIdentity(),
EMotionType::Dynamic,
layer
);
this->body = getBodyInterface().CreateBody(bodySettings);
assertNotNull(this->body, "Body failed to create?");
this->bodyId = this->body->GetID();
getBodyInterface().AddBody(this->bodyId, EActivation::Activate);
}
void Collider::onDispose() {
getBodyInterface().RemoveBody(this->bodyId);
getBodyInterface().DestroyBody(this->bodyId);
}
BodyInterface & Collider::getBodyInterface() {
return getGame()->physicsManager->getBodyInterface();
}
BodyID Collider::getBodyId() {
return bodyId;
}

View File

@ -0,0 +1,52 @@
// Copyright (c) 2024 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "scene/SceneItem.hpp"
namespace Dawn {
enum class ColliderType {
DYNAMIC,
STATIC,
KINEMATIC
};
class Collider : public SceneComponent {
private:
JPH::Body *body = nullptr;
protected:
JPH::BodyID bodyId;
JPH::EMotionType emotionType = JPH::EMotionType::Dynamic;
JPH::ObjectLayer layer = 1;
/**
* Returns the shape settings for the collider.
*
* @return The shape settings for the collider.
*/
virtual std::shared_ptr<JPH::ShapeSettings> getShapeSettings() = 0;
/**
* Returns the JoltPhysics body interface system.
*
* @return The JoltPhysics body interface system.
*/
JPH::BodyInterface & getBodyInterface();
public:
ColliderType type = ColliderType::KINEMATIC;
void onInit() override;
void onDispose() override;
/**
* Returns the JoltPhysics body ID of the collider.
*
* @return The body ID of the collider.
*/
JPH::BodyID getBodyId();
};
}