Example physics implemented.
This commit is contained in:
14
src/dawn/component/physics/BoxCollider.cpp
Normal file
14
src/dawn/component/physics/BoxCollider.cpp
Normal 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)
|
||||
);
|
||||
}
|
17
src/dawn/component/physics/BoxCollider.hpp
Normal file
17
src/dawn/component/physics/BoxCollider.hpp
Normal 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);
|
||||
};
|
||||
}
|
10
src/dawn/component/physics/CMakeLists.txt
Normal file
10
src/dawn/component/physics/CMakeLists.txt
Normal 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
|
||||
)
|
49
src/dawn/component/physics/Collider.cpp
Normal file
49
src/dawn/component/physics/Collider.cpp
Normal 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;
|
||||
}
|
52
src/dawn/component/physics/Collider.hpp
Normal file
52
src/dawn/component/physics/Collider.hpp
Normal 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();
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user