Example scene loading
This commit is contained in:
7
src/dawnphysics/component/CMakeLists.txt
Normal file
7
src/dawnphysics/component/CMakeLists.txt
Normal file
@ -0,0 +1,7 @@
|
||||
# Copyright (c) 2024 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
# Subdirs
|
||||
add_subdirectory(physics)
|
11
src/dawnphysics/component/physics/CMakeLists.txt
Normal file
11
src/dawnphysics/component/physics/CMakeLists.txt
Normal file
@ -0,0 +1,11 @@
|
||||
# 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
|
||||
CubeCollider.cpp
|
||||
SphereCollider.cpp
|
||||
)
|
137
src/dawnphysics/component/physics/Collider.cpp
Normal file
137
src/dawnphysics/component/physics/Collider.cpp
Normal file
@ -0,0 +1,137 @@
|
||||
// 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;
|
||||
|
||||
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?");
|
||||
|
||||
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(),
|
||||
Collider::getMotionType(this->colliderType),
|
||||
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);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
glm::vec3 Collider::getVelocity() {
|
||||
if(!this->isColliderReady()) return glm::vec3(0.0f, 0.0f, 0.0f);
|
||||
auto vel = getBodyInterface().GetLinearVelocity(this->bodyId);
|
||||
return glm::vec3(vel.GetX(), vel.GetY(), vel.GetZ());
|
||||
}
|
||||
|
||||
void Collider::setVelocity(const glm::vec3 velocity) {
|
||||
if(!this->isColliderReady()) {
|
||||
assertUnreachable("Collider is not ready.");
|
||||
}
|
||||
|
||||
getBodyInterface().SetLinearVelocity(
|
||||
this->bodyId, RVec3(velocity.x, velocity.y, velocity.z)
|
||||
);
|
||||
}
|
||||
|
||||
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
|
||||
);
|
||||
}
|
||||
|
||||
void Collider::addForce(
|
||||
const glm::vec3 force,
|
||||
const glm::vec3 inPoint
|
||||
) {
|
||||
if(!this->isColliderReady()) assertUnreachable("Collider is not ready.");
|
||||
|
||||
getBodyInterface().AddForce(
|
||||
this->bodyId,
|
||||
RVec3(force.x, force.y, force.z),
|
||||
RVec3(0, 0, 0)
|
||||
);
|
||||
}
|
109
src/dawnphysics/component/physics/Collider.hpp
Normal file
109
src/dawnphysics/component/physics/Collider.hpp
Normal file
@ -0,0 +1,109 @@
|
||||
// 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;
|
||||
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::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();
|
||||
|
||||
/**
|
||||
* 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();
|
||||
|
||||
/**
|
||||
* Returns the velocity of the collider.
|
||||
*
|
||||
* @return The velocity of the collider.
|
||||
*/
|
||||
glm::vec3 getVelocity();
|
||||
|
||||
/**
|
||||
* Sets the collider type.
|
||||
*
|
||||
* @param colliderType The collider type.
|
||||
*/
|
||||
void setColliderType(ColliderType colliderType);
|
||||
|
||||
/**
|
||||
* Sets the velocity of the collider.
|
||||
*
|
||||
* @param velocity The velocity.
|
||||
*/
|
||||
void setVelocity(const glm::vec3 velocity);
|
||||
|
||||
/**
|
||||
* Adds a force to the collider.
|
||||
*
|
||||
* @param force Force to add.
|
||||
* @param inPoint Application point of the force.
|
||||
*/
|
||||
void addForce(
|
||||
const glm::vec3 force,
|
||||
const glm::vec3 inPoint = glm::vec3(0, 0, 0)
|
||||
);
|
||||
};
|
||||
}
|
23
src/dawnphysics/component/physics/CubeCollider.cpp
Normal file
23
src/dawnphysics/component/physics/CubeCollider.cpp
Normal 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();
|
||||
}
|
32
src/dawnphysics/component/physics/CubeCollider.hpp
Normal file
32
src/dawnphysics/component/physics/CubeCollider.hpp
Normal 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);
|
||||
};
|
||||
}
|
12
src/dawnphysics/component/physics/SphereCollider.cpp
Normal file
12
src/dawnphysics/component/physics/SphereCollider.cpp
Normal 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);
|
||||
}
|
17
src/dawnphysics/component/physics/SphereCollider.hpp
Normal file
17
src/dawnphysics/component/physics/SphereCollider.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 SphereCollider : public Collider {
|
||||
protected:
|
||||
std::shared_ptr<JPH::ShapeSettings> getShapeSettings() override;
|
||||
|
||||
public:
|
||||
float radius = 1.0f;
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user