collisions are working ok with user defined offsets

This commit is contained in:
Clément Le Bihan
2021-06-07 23:37:51 +02:00
parent 8203532d52
commit 65af11f3f9
7 changed files with 85 additions and 31 deletions
+1
View File
@@ -10,6 +10,7 @@
RAY::Camera::Camera3D::Camera3D(const RAY::Vector3 &position, const RAY::Vector3 &target, const RAY::Vector3 &up, float fovy, Projection projection):
_camera({position, target, up, fovy, projection})
{
SetCameraMode(_camera, CAMERA_FREE);
}
void RAY::Camera::Camera3D::setPosition(const Vector3 &Position)
@@ -18,34 +18,40 @@ namespace BBM
CollisionComponent::CollisionComponent(WAL::Entity &entity,
const WAL::Callback<WAL::Entity &, const WAL::Entity &> &onCollide,
const WAL::Callback<WAL::Entity &, const WAL::Entity &> &onCollided,
Vector3f positionOffset,
Vector3f bound)
: WAL::Component(entity),
onCollide(onCollide),
onCollided(onCollided),
bound(bound)
bound(bound),
positionOffset(positionOffset)
{}
CollisionComponent::CollisionComponent(WAL::Entity &entity,
const WAL::Callback<WAL::Entity &, const WAL::Entity &> &onCollide,
const WAL::Callback<WAL::Entity &, const WAL::Entity &> &onCollided,
float positionOffset,
float boundSize)
: WAL::Component(entity),
onCollide(onCollide),
onCollided(onCollided),
bound({boundSize, boundSize, boundSize})
bound({boundSize, boundSize, boundSize}),
positionOffset({positionOffset, positionOffset, positionOffset})
{}
CollisionComponent::CollisionComponent(WAL::Entity &entity, Vector3f bound)
CollisionComponent::CollisionComponent(WAL::Entity &entity, Vector3f positionOffset, Vector3f bound)
: WAL::Component(entity),
onCollide(),
onCollided(),
bound(bound)
bound(bound),
positionOffset(positionOffset)
{}
CollisionComponent::CollisionComponent(WAL::Entity &entity, float boundSize)
CollisionComponent::CollisionComponent(WAL::Entity &entity, float positionOffset, float boundSize)
: WAL::Component(entity),
onCollide(),
onCollided(),
bound({boundSize, boundSize, boundSize})
bound({boundSize, boundSize, boundSize}),
positionOffset({positionOffset, positionOffset, positionOffset})
{}
}
@@ -20,6 +20,8 @@ namespace BBM
WAL::Callback<WAL::Entity &, const WAL::Entity &> onCollided;
//! @brief Bound size on all axis
Vector3f bound;
//! @brief Offset from the position component
Vector3f positionOffset;
//! @inherit
WAL::Component *clone(WAL::Entity &entity) const override;
@@ -29,21 +31,23 @@ namespace BBM
//! @brief Constructor with a WAL::Callback
CollisionComponent(WAL::Entity &entity,
const WAL::Callback<WAL::Entity &, const WAL::Entity &>& onCollide,
const WAL::Callback<WAL::Entity &, const WAL::Entity &>& onCollided,
const WAL::Callback<WAL::Entity &, const WAL::Entity &> &onCollide,
const WAL::Callback<WAL::Entity &, const WAL::Entity &> &onCollided,
Vector3f positionOffset,
Vector3f bound);
//! @brief Constructor with a WAL::Callback, same boundSize for all axis
CollisionComponent(WAL::Entity &entity,
const WAL::Callback<WAL::Entity &, const WAL::Entity &>& onCollide,
const WAL::Callback<WAL::Entity &, const WAL::Entity &>& onCollided,
const WAL::Callback<WAL::Entity &, const WAL::Entity &> &onCollide,
const WAL::Callback<WAL::Entity &, const WAL::Entity &> &onCollided,
float positionOffset,
float boundSize = 0);
//! @brief Constructor of collider with no callback
CollisionComponent(WAL::Entity &entity, Vector3f bound);
CollisionComponent(WAL::Entity &entity, Vector3f positionOffset, Vector3f bound);
//! @brief Constructor no callback, same boundSize for all axis
CollisionComponent(WAL::Entity &entity, float boundSize);
CollisionComponent(WAL::Entity &entity, float positionOffset, float boundSize);
//! @brief Default copy constructor
CollisionComponent(const CollisionComponent &) = default;
+6 -1
View File
@@ -14,9 +14,14 @@ namespace BBM
void MapGenerator::wallCollide(WAL::Entity &entity, const WAL::Entity &wall)
{
auto *mov = entity.tryGetComponent<MovableComponent>();
auto posspec = entity.getComponent<PositionComponent>();
auto posspecwall = wall.getComponent<PositionComponent>();
if (!mov)
return;
mov->_velocity = BBM::Vector3f ();
std::cout << "collided coords " << posspec.position << " cube " << posspecwall.position << std::endl;
//mov->_velocity = BBM::Vector3f ();
return;
auto &pos = entity.getComponent<PositionComponent>();
const auto &wallPos = wall.getComponent<PositionComponent>();
+6 -6
View File
@@ -70,21 +70,21 @@ namespace BBM
.addComponent<AnimatorComponent>()
.addComponent<KeyboardComponent>()
.addComponent<AnimationsComponent>(RAY::ModelAnimations("assets/player/player.iqm"), 3)
.addComponent<CollisionComponent>(1)
.addComponent<CollisionComponent>(0, 1)
.addComponent<MovableComponent>()
.addComponent<HealthComponent>(1, [](WAL::Entity &entity) {
auto &animation = entity.getComponent<AnimationsComponent>();
animation.setAnimIndex(5);
});
scene->addEntity("camera")
.addComponent<PositionComponent>(8, 20, 7)
.addComponent<CameraComponent>(Vector3f(8, 0, 8));
.addComponent<PositionComponent>(10, 20, 10)
.addComponent<CameraComponent>(Vector3f(2, 0, 2));
scene->addEntity("cube")
.addComponent<PositionComponent>(5, 0, 5)
.addComponent<Drawable3DComponent, RAY3D::Cube>(Vector3f(-5, 0, -5), Vector3f(3, 3, 3), RED)
.addComponent<PositionComponent>(0, 0, 0)
.addComponent<Drawable3DComponent, RAY3D::Cube>(Vector3f(0, 0, 0), Vector3f(3, 3, 3), RED)
.addComponent<ControllableComponent>()
.addComponent<KeyboardComponent>()
.addComponent<CollisionComponent>(WAL::Callback<WAL::Entity &, const WAL::Entity &>(), &MapGenerator::wallCollide, 3);
.addComponent<CollisionComponent>(WAL::Callback<WAL::Entity &, const WAL::Entity &>(), &MapGenerator::wallCollide, -1, 3);
std::srand(std::time(nullptr));
//MapGenerator::loadMap(16, 16, MapGenerator::createMap(16, 16), scene);
return scene;
+22 -8
View File
@@ -26,19 +26,33 @@ namespace BBM
void CollisionSystem::onFixedUpdate(WAL::ViewEntity<PositionComponent, CollisionComponent> &entity)
{
auto &posA = entity.get<PositionComponent>();
auto &col = entity.get<CollisionComponent>();
Vector3f position = posA.position;
auto &colA = entity.get<CollisionComponent>();
Vector3f pointA = posA.position + colA.positionOffset;
if (auto *movable = entity->tryGetComponent<MovableComponent>())
position += movable->getVelocity();
Vector3f minA = Vector3f::min(position, position + col.bound);
Vector3f maxA = Vector3f::max(position, position + col.bound);
pointA += movable->getVelocity();
Vector3f minA = Vector3f::min(pointA, pointA + colA.bound);
Vector3f maxA = Vector3f::max(pointA, pointA + colA.bound);
for (auto &[other, posB, colB] : this->getView()) {
if (other.getUid() == entity->getUid())
continue;
Vector3f minB = Vector3f::min(posB.position, posB.position + colB.bound);
Vector3f maxB = Vector3f::max(posB.position, posB.position + colB.bound);
auto pointB = posB.position + colB.positionOffset;
// TODO if B is also a movable we don't check with it's changing position
Vector3f minB = Vector3f::min(pointB, pointB + colB.bound);
Vector3f maxB = Vector3f::max(pointB, pointB + colB.bound);
if (collide(minA, maxA, minB, maxB)) {
col.onCollide(entity, other);
std::cout << "collided" << std::endl
<< "minA " << minA << std::endl
<< "maxA " << maxA << std::endl
<< "minB " << minB << std::endl
<< "maxB " << maxB << std::endl;
return;
colA.onCollide(entity, other);
colB.onCollided(entity, other);
}
}
+28 -4
View File
@@ -10,13 +10,16 @@
#include "Component/Renderer/Drawable2DComponent.hpp"
#include "Drawables/ADrawable3D.hpp"
#include "Component/Collision/CollisionComponent.hpp"
namespace BBM
{
RenderSystem::RenderSystem(WAL::Wal &wal, RAY::Window &window, bool debugMode)
: System(wal),
_window(window),
_camera(Vector3f(), Vector3f(), Vector3f(0, 1, 0), 50, CAMERA_PERSPECTIVE),
_debugMode(debugMode)
_window(window),
_camera(Vector3f(), Vector3f(), Vector3f(0, 1, 0), 50, CAMERA_PERSPECTIVE),
_debugMode(debugMode)
{
this->_window.setFPS(this->FPS);
}
@@ -29,6 +32,26 @@ namespace BBM
this->_window.useCamera(this->_camera);
for (auto &[_, pos, drawable] : this->_wal.scene->view<PositionComponent, Drawable3DComponent>()) {
if (_.getName() == "cube") {
auto col = _.getComponent<CollisionComponent>();
DrawCubeWires({pos.position.x, pos.position.y, pos.position.z},
col.bound.x,
col.bound.y,
col.bound.z,
WHITE);
DrawPoint3D({pos.position.x, pos.position.y, pos.position.z}, BLUE);
DrawPoint3D({pos.position.x + col.bound.x, pos.position.y + col.bound.y, pos.position.z + col.bound.z}, BLUE);
}
if (_.getName() == "player") {
auto col = _.getComponent<CollisionComponent>();
DrawCubeWires({pos.position.x, pos.position.y, pos.position.z},
col.bound.x,
col.bound.y,
col.bound.z,
WHITE);
DrawPoint3D({pos.position.x, pos.position.y, pos.position.z}, BLUE);
DrawPoint3D({pos.position.x + col.bound.x, pos.position.y + col.bound.y, pos.position.z + col.bound.z}, BLUE);
}
drawable.drawable->setPosition(pos.position);
drawable.drawable->drawOn(this->_window);
}
@@ -44,7 +67,8 @@ namespace BBM
this->_window.endDrawing();
}
void RenderSystem::onUpdate(WAL::ViewEntity<CameraComponent, PositionComponent> &entity, std::chrono::nanoseconds dtime)
void
RenderSystem::onUpdate(WAL::ViewEntity<CameraComponent, PositionComponent> &entity, std::chrono::nanoseconds dtime)
{
const auto &pos = entity.get<PositionComponent>();
const auto &cam = entity.get<CameraComponent>();