Skip to content

Commit

Permalink
Fixed EventChange mistakes, added world raytest method.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattparks committed Nov 22, 2018
1 parent a7fb04a commit 777db1c
Show file tree
Hide file tree
Showing 17 changed files with 131 additions and 71 deletions.
2 changes: 1 addition & 1 deletion Sources/Acid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
#include "Particles/Spawns/SpawnPoint.hpp"
#include "Particles/Spawns/SpawnSphere.hpp"
#include "Physics/Collider.hpp"
#include "Physics/ColliderBox.hpp"
#include "Physics/ColliderCube.hpp"
#include "Physics/ColliderCapsule.hpp"
#include "Physics/ColliderCone.hpp"
#include "Physics/ColliderConvexHull.hpp"
Expand Down
33 changes: 0 additions & 33 deletions Sources/Events/EventChange.cpp

This file was deleted.

44 changes: 38 additions & 6 deletions Sources/Events/EventChange.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,58 @@ namespace acid
/// </summary>
/// @param <T> The type of value to find change with. </param>
template<typename T>
class ACID_EXPORT EventChange :
class EventChange :
public IEvent
{
private:
T *m_reference;
std::function<T()> m_reference;
T m_current;
std::function<void()> m_onEvent;
std::function<void(T)> m_onEvent;
bool m_repeat;
public:
/// <summary>
/// Creates a new change event.
/// </summary>
/// <param name="reference"> The reference function to get the value from. </param>
/// <param name="onEvent"> A function called when the event is triggered. </param>
/// <param name="repeat"> If the event will repeat after the first run. </param>
EventChange(const std::function<T()> &reference, const std::function<void(T)> &onEvent, const bool &repeat = true) :
m_reference(reference),
m_current(m_reference()),
m_onEvent(onEvent),
m_repeat(repeat)
{
}

/// <summary>
/// Creates a new change event.
/// </summary>
/// <param name="reference"> The reference to listen to. </param>
/// <param name="onEvent"> A function called when the event is triggered. </param>
/// <param name="repeat"> If the event will repeat after the first run. </param>
EventChange(T *reference, const std::function<void()> &onEvent, const bool &repeat = true);
EventChange(T *reference, const std::function<void(T)> &onEvent, const bool &repeat = true) :
m_reference([reference]() -> T
{
return *reference;
}),
m_current(m_reference()),
m_onEvent(onEvent),
m_repeat(repeat)
{
}

bool EventTriggered() override;
bool EventTriggered() override
{
auto newValue = m_reference();
bool triggered = newValue != m_current;
m_current = newValue;
return triggered;
}

void OnEvent() override;
void OnEvent() override
{
m_onEvent(m_current);
}

bool RemoveAfterEvent() override { return !m_repeat; }
};
Expand Down
4 changes: 2 additions & 2 deletions Sources/Objects/ComponentRegister.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "Materials/MaterialDefault.hpp"
#include "Meshes/MeshRender.hpp"
#include "Particles/ParticleSystem.hpp"
#include "Physics/ColliderBox.hpp"
#include "Physics/ColliderCube.hpp"
#include "Physics/ColliderCapsule.hpp"
#include "Physics/CharacterController.hpp"
#include "Physics/ColliderCone.hpp"
Expand All @@ -29,7 +29,7 @@ namespace acid
RegisterComponent<MeshAnimated>("MeshAnimated");
RegisterComponent<MeshRender>("MeshRender");
RegisterComponent<ParticleSystem>("ParticleSystem");
RegisterComponent<ColliderBox>("ColliderBox");
RegisterComponent<ColliderCube>("ColliderCube");
RegisterComponent<ColliderCapsule>("ColliderCapsule");
RegisterComponent<CharacterController>("CharacterController");
RegisterComponent<ColliderCone>("ColliderCone");
Expand Down
1 change: 1 addition & 0 deletions Sources/Physics/CharacterController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ namespace acid
m_shape = shape->GetCollisionShape();

m_ghostObject = CreateGhostObject(m_mass, worldTransform, m_shape);
m_ghostObject->setUserPointer(GetGameObject());

m_controller = std::make_unique<btKinematicCharacterController>(m_ghostObject.get(), (btConvexShape *)shape->GetCollisionShape(), 0.01666f);
Scenes::Get()->GetPhysics()->GetDynamicsWorld()->addCollisionObject(m_ghostObject.get(), btBroadphaseProxy::CharacterFilter, btBroadphaseProxy::AllFilter);
Expand Down
2 changes: 1 addition & 1 deletion Sources/Physics/Collider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace acid

btQuaternion rotation = transform.getRotation();
float pitch, yaw, roll;
rotation.getEulerZYX(yaw, pitch, roll);
rotation.getEulerZYX(roll, yaw, pitch);

return Transform(Collider::Convert(position), Vector3(pitch * RAD_TO_DEG, yaw * RAD_TO_DEG, roll * RAD_TO_DEG), scaling);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
#include "ColliderBox.hpp"
#include "ColliderCube.hpp"

#include <BulletCollision/CollisionShapes/btBoxShape.h>
#include "Scenes/Scenes.hpp"

namespace acid
{
ColliderBox::ColliderBox(const Vector3 &extents) :
ColliderCube::ColliderCube(const Vector3 &extents) :
m_shape(std::make_unique<btBoxShape>(Collider::Convert(extents / 2.0f))),
m_extents(extents)
{
}

ColliderBox::~ColliderBox()
ColliderCube::~ColliderCube()
{
}

void ColliderBox::Start()
void ColliderCube::Start()
{
}

void ColliderBox::Update()
void ColliderCube::Update()
{
// m_shape->setImplicitShapeDimensions(Collider::Convert(m_extents)); // TODO
}

void ColliderBox::Decode(const Metadata &metadata)
void ColliderCube::Decode(const Metadata &metadata)
{
m_extents = metadata.GetChild<Vector3>("Extents");
}

void ColliderBox::Encode(Metadata &metadata) const
void ColliderCube::Encode(Metadata &metadata) const
{
metadata.SetChild<Vector3>("Extents", m_extents);
}

btCollisionShape *ColliderBox::GetCollisionShape() const
btCollisionShape *ColliderCube::GetCollisionShape() const
{
return m_shape.get();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ class btBoxShape;

namespace acid
{
class ACID_EXPORT ColliderBox :
class ACID_EXPORT ColliderCube :
public Collider
{
private:
std::unique_ptr<btBoxShape> m_shape;
Vector3 m_extents;
public:
explicit ColliderBox(const Vector3 &extents = Vector3::ONE);
explicit ColliderCube(const Vector3 &extents = Vector3::ONE);

~ColliderBox();
~ColliderCube();

void Start() override;

Expand Down
8 changes: 1 addition & 7 deletions Sources/Physics/Rigidbody.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,7 @@ namespace acid
}

auto &transform = GetGameObject()->GetTransform();
btTransform worldTransform;
m_body->getMotionState()->getWorldTransform(worldTransform);
transform = Collider::Convert(worldTransform, transform.GetScaling());

// worldTransform->setIdentity();
// worldTransform->setOrigin(Collider::Convert(transform.GetPosition()));
// worldTransform->setRotation(Collider::Convert(transform.GetRotation()));
transform = Collider::Convert(m_body->getWorldTransform(), transform.GetScaling());

m_shape->setLocalScaling(Collider::Convert(m_localTransform.GetScaling() * transform.GetScaling()));
// m_body->getMotionState()->setWorldTransform(*worldTransform);
Expand Down
13 changes: 12 additions & 1 deletion Sources/Scenes/ScenePhysics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <BulletSoftBody/btSoftBodyRigidBodyCollisionConfiguration.h>
#include <BulletSoftBody/btSoftRigidDynamicsWorld.h>
#include "Engine/Engine.hpp"
#include "Objects/GameObject.hpp"
#include "Physics/Collider.hpp"

namespace acid
Expand All @@ -23,7 +24,7 @@ namespace acid
m_dynamicsWorld->getDispatchInfo().m_enableSatConvex = true;
m_dynamicsWorld->getSolverInfo().m_splitImpulse = true;

auto softDynamicsWorld = static_cast<btSoftRigidDynamicsWorld *>(m_dynamicsWorld.get());
auto softDynamicsWorld = dynamic_cast<btSoftRigidDynamicsWorld *>(m_dynamicsWorld.get());
softDynamicsWorld->getWorldInfo().air_density = 1.0f;
softDynamicsWorld->getWorldInfo().m_sparsesdf.Initialize();
}
Expand All @@ -49,6 +50,16 @@ namespace acid
m_dynamicsWorld->stepSimulation(Engine::Get()->GetDelta().AsSeconds());
}

Raycast ScenePhysics::Raytest(const Vector3 &start, const Vector3 &end)
{
auto startBt = Collider::Convert(start);
auto endBt = Collider::Convert(end);
btCollisionWorld::ClosestRayResultCallback result(startBt, endBt);
m_dynamicsWorld->getCollisionWorld()->rayTest(startBt, endBt, result);

return Raycast(result.hasHit(), Collider::Convert(result.m_hitPointWorld), result.m_collisionObject != nullptr ? static_cast<GameObject *>(result.m_collisionObject->getUserPointer()) : nullptr);
}

Vector3 ScenePhysics::GetGravity() const
{
return Collider::Convert(m_dynamicsWorld->getGravity());
Expand Down
36 changes: 36 additions & 0 deletions Sources/Scenes/ScenePhysics.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once

#include <memory>
#include <optional>
#include <BulletCollision/CollisionDispatch/btCollisionObject.h>
#include "Maths/Vector3.hpp"

class btCollisionConfiguration;
Expand All @@ -15,6 +17,38 @@ class btDiscreteDynamicsWorld;

namespace acid
{
class GameObject;

class ACID_EXPORT Raycast
{
private:
bool m_hasHit;
Vector3 m_pointWorld;
GameObject *m_gameObject;
public:
Raycast(bool m_hasHit, const Vector3 &m_pointWorld, GameObject *gameObject) :
m_hasHit(m_hasHit),
m_pointWorld(m_pointWorld),
m_gameObject(gameObject)
{
}

bool HasHit() const
{
return m_hasHit;
}

const Vector3 &GetPointWorld() const
{
return m_pointWorld;
}

GameObject *GetGameObject() const
{
return m_gameObject;
}
};

class ACID_EXPORT ScenePhysics
{
private:
Expand All @@ -30,6 +64,8 @@ namespace acid

void Update();

Raycast Raytest(const Vector3 &start, const Vector3 &end);

Vector3 GetGravity() const;

void SetGravity(const Vector3 &gravity);
Expand Down
2 changes: 1 addition & 1 deletion Tests/TestPBR/Scenes/Scene1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <Meshes/Mesh.hpp>
#include <Meshes/MeshRender.hpp>
#include <Models/Shapes/ModelSphere.hpp>
#include <Physics/ColliderBox.hpp>
#include <Physics/ColliderCube.hpp>
#include <Physics/ColliderSphere.hpp>
#include <Models/Shapes/ModelCube.hpp>
#include <Physics/ColliderConvexHull.hpp>
Expand Down
7 changes: 7 additions & 0 deletions Tests/TestPhysics/Configs/ConfigManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <Events/Events.hpp>
#include <Events/EventTime.hpp>
#include <Audio/Audio.hpp>
#include <Events/EventChange.hpp>

namespace test
{
Expand All @@ -19,6 +20,12 @@ namespace test
{
Save();
}, false);

// Events::Get()->AddEvent(new EventChange<uint32_t>([](){
// return Display::Get()->GetWidth();
// }, [](uint32_t value){
// Log::Out("New width: %i\n", value);
// }));
}

void ConfigManager::Load()
Expand Down
5 changes: 4 additions & 1 deletion Tests/TestPhysics/Scenes/FpsCamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ namespace test
m_projectionMatrix = Matrix4::PerspectiveMatrix(GetFov(), Display::Get()->GetAspectRatio(), GetNearPlane(), GetFarPlane());

m_viewFrustum.Update(m_viewMatrix, m_projectionMatrix);
m_viewRay.Update(m_position, Vector2(Mouse::Get()->GetPositionX(), Mouse::Get()->GetPositionY()), m_viewMatrix, m_projectionMatrix);
m_viewRay.Update(m_position, Vector2(0.5f, 0.5f), m_viewMatrix, m_projectionMatrix); // Mouse::Get()->GetPositionX(), Mouse::Get()->GetPositionY()

// auto raytest = Scenes::Get()->GetPhysics()->Raytest(m_viewRay.GetOrigin(), m_viewRay.GetPointOnRay(20.0f));
// Log::Out("%s: %f\n", raytest.HasHit() ? raytest.GetGameObject()->GetName().c_str() : "", raytest.GetPointWorld().Distance(m_viewRay.GetOrigin()));
}

void FpsCamera::CalculateHorizontalAngle()
Expand Down

0 comments on commit 777db1c

Please sign in to comment.