Merge remote-tracking branch 'origin/master' into FMOD

Conflicts:
	src/GameWorld.cpp
	src/Systems/SoundSystem.h
	vs11/Returngeance/Returngeance.vcxproj.filters
This commit is contained in:
Stiffly
2014-05-25 01:42:26 +02:00
58 changed files with 1860 additions and 533 deletions
+1 -1
View File
@@ -19,7 +19,7 @@ public:
void Update(double dt) override;
EventRelay<Events::KeyDown> m_EKeyDown;
EventRelay<DebugSystem, Events::KeyDown> m_EKeyDown;
bool OnKeyDown(const Events::KeyDown &event);
//void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
+7 -7
View File
@@ -60,27 +60,27 @@ void Systems::FreeSteeringSystem::UpdateEntity(double dt, EntityID entity, Entit
bool Systems::FreeSteeringSystem::FreeSteeringInputController::OnCommand(const Events::InputCommand &event)
{
// Movement
if (event.Command == "vertical")
if (event.Command == "cam_vertical")
{
Movement.z = -event.Value;
}
else if (event.Command == "horizontal")
else if (event.Command == "cam_horizontal")
{
Movement.x = event.Value;
}
else if (event.Command == "normal")
else if (event.Command == "cam_normal")
{
Movement.y = event.Value;
}
// Speed
else if (event.Command == "speed")
else if (event.Command == "cam_speed")
{
SpeedMultiplier = event.Value;
}
// Mouse click
else if (event.Command == "attack")
else if (event.Command == "cam_attack")
{
OrientationActive = event.Value > 0;
@@ -96,11 +96,11 @@ bool Systems::FreeSteeringSystem::FreeSteeringInputController::OnCommand(const E
}
}
else if (event.Command == "vertical2")
else if (event.Command == "cam_vertical2")
{
ControllerOrientation.x = event.Value;
}
else if (event.Command == "horizontal2")
else if (event.Command == "cam_horizontal2")
{
ControllerOrientation.y = -event.Value;
}
+1 -1
View File
@@ -27,7 +27,7 @@ private:
std::unique_ptr<FreeSteeringInputController> m_InputController;
};
class FreeSteeringSystem::FreeSteeringInputController : InputController
class FreeSteeringSystem::FreeSteeringInputController : InputController<FreeSteeringSystem>
{
public:
FreeSteeringInputController(std::shared_ptr<::EventBroker> eventBroker)
+67
View File
@@ -0,0 +1,67 @@
#include "PrecompiledHeader.h"
#include "HelicopterSteeringSystem.h"
#include "World.h"
void Systems::HelicopterSteeringSystem::RegisterComponents(ComponentFactory* cf)
{
cf->Register<Components::HelicopterSteering>([]() { return new Components::HelicopterSteering(); });
}
void Systems::HelicopterSteeringSystem::Initialize()
{
m_InputController = std::unique_ptr<HelicopterSteeringInputController>(new HelicopterSteeringInputController(EventBroker));
}
void Systems::HelicopterSteeringSystem::Update(double dt)
{
}
void Systems::HelicopterSteeringSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
{
auto transform = m_World->GetComponent<Components::Transform>(entity);
if (!transform)
return;
auto helicopterComponent = m_World->GetComponent<Components::HelicopterSteering>(entity);
if (helicopterComponent)
{
glm::vec3 controllerRotationEuler = m_InputController->Rotation * (float)dt;
transform->Orientation *= glm::quat(controllerRotationEuler);
Events::ApplyForce e;
e.Entity = entity;
e.DeltaTime = dt;
e.Force = glm::normalize(transform->Orientation * glm::vec3(0, 1, 0)) * (m_InputController->Power * 3000.f * 9.82f * 8.f);
EventBroker->Publish(e);
}
}
bool Systems::HelicopterSteeringSystem::HelicopterSteeringInputController::OnCommand(const Events::InputCommand &event)
{
if (event.Command == "horizontal")
{
Rotation.z = -event.Value;
}
else if (event.Command == "vertical")
{
Rotation.x = -event.Value;
}
else if (event.Command == "normal")
{
Power = event.Value;
}
return true;
}
bool Systems::HelicopterSteeringSystem::HelicopterSteeringInputController::OnMouseMove(const Events::MouseMove &event)
{
return true;
}
void Systems::HelicopterSteeringSystem::HelicopterSteeringInputController::Update(double dt)
{
}
+54
View File
@@ -0,0 +1,54 @@
#include "System.h"
#include "Components/Transform.h"
#include "Components/HelicopterSteering.h"
#include "Events/SetVelocity.h"
#include "Events/ApplyForce.h"
#include "InputController.h"
namespace Systems
{
class HelicopterSteeringSystem : public System
{
public:
HelicopterSteeringSystem(World* world, std::shared_ptr<::EventBroker> eventBroker)
: System(world, eventBroker) { }
void RegisterComponents(ComponentFactory* cf) override;
void Initialize() override;
void Update(double dt) override;
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
private:
class HelicopterSteeringInputController;
std::unique_ptr<HelicopterSteeringInputController> m_InputController;
std::map<EntityID, double> m_TimeSinceLastShot;
};
class HelicopterSteeringSystem::HelicopterSteeringInputController : InputController<HelicopterSteeringSystem>
{
public:
HelicopterSteeringInputController(std::shared_ptr<::EventBroker> eventBroker)
: InputController(eventBroker)
, Power(0.f)
{ }
float Power;
glm::vec3 Rotation;
void Update(double dt);
protected:
bool OnCommand(const Events::InputCommand &event) override;
bool OnMouseMove(const Events::MouseMove &event) override;
};
}
+4 -4
View File
@@ -53,7 +53,7 @@ bool Systems::InputSystem::OnKeyDown(const Events::KeyDown &event)
float value;
std::tie(command, value) = bindingIt->second;
m_CommandKeyboardValues[command][event.KeyCode] = value;
PublishCommand(0, command, GetCommandTotalValue(command));
PublishCommand(1, command, GetCommandTotalValue(command));
}
return true;
@@ -68,7 +68,7 @@ bool Systems::InputSystem::OnKeyUp(const Events::KeyUp &event)
float value;
std::tie(command, value) = bindingIt->second;
m_CommandKeyboardValues[command][event.KeyCode] = 0;
PublishCommand(0, command, GetCommandTotalValue(command));;
PublishCommand(1, command, GetCommandTotalValue(command));;
}
return true;
@@ -83,7 +83,7 @@ bool Systems::InputSystem::OnMousePress(const Events::MousePress &event)
float value;
std::tie(command, value) = bindingIt->second;
m_CommandMouseButtonValues[command][event.Button] = value;
PublishCommand(0, command, GetCommandTotalValue(command));
PublishCommand(1, command, GetCommandTotalValue(command));
}
return true;
@@ -98,7 +98,7 @@ bool Systems::InputSystem::OnMouseRelease(const Events::MouseRelease &event)
float value;
std::tie(command, value) = bindingIt->second;
m_CommandMouseButtonValues[command][event.Button] = 0;
PublishCommand(0, command, GetCommandTotalValue(command));
PublishCommand(1, command, GetCommandTotalValue(command));
}
return true;
+11 -11
View File
@@ -45,28 +45,28 @@ private:
std::unordered_map<Gamepad::Button, std::tuple<std::string, float>> m_GamepadButtonBindings; // Gamepad::Button -> command string
// Input events
EventRelay<Events::KeyDown> m_EKeyDown;
EventRelay<InputSystem, Events::KeyDown> m_EKeyDown;
bool OnKeyDown(const Events::KeyDown &event);
EventRelay<Events::KeyUp> m_EKeyUp;
EventRelay<InputSystem, Events::KeyUp> m_EKeyUp;
bool OnKeyUp(const Events::KeyUp &event);
EventRelay<Events::MousePress> m_EMousePress;
EventRelay<InputSystem, Events::MousePress> m_EMousePress;
bool OnMousePress(const Events::MousePress &event);
EventRelay<Events::MouseRelease> m_EMouseRelease;
EventRelay<InputSystem, Events::MouseRelease> m_EMouseRelease;
bool OnMouseRelease(const Events::MouseRelease &event);
EventRelay<Events::GamepadAxis> m_EGamepadAxis;
EventRelay<InputSystem, Events::GamepadAxis> m_EGamepadAxis;
bool OnGamepadAxis(const Events::GamepadAxis &event);
EventRelay<Events::GamepadButtonDown> m_EGamepadButtonDown;
EventRelay<InputSystem, Events::GamepadButtonDown> m_EGamepadButtonDown;
bool OnGamepadButtonDown(const Events::GamepadButtonDown &event);
EventRelay<Events::GamepadButtonUp> m_EGamepadButtonUp;
EventRelay<InputSystem, Events::GamepadButtonUp> m_EGamepadButtonUp;
bool OnGamepadButtonUp(const Events::GamepadButtonUp &event);
// Input binding events
EventRelay<Events::BindKey> m_EBindKey;
EventRelay<InputSystem, Events::BindKey> m_EBindKey;
bool OnBindKey(const Events::BindKey &event);
EventRelay<Events::BindMouseButton> m_EBindMouseButton;
EventRelay<InputSystem, Events::BindMouseButton> m_EBindMouseButton;
bool OnBindMouseButton(const Events::BindMouseButton &event);
EventRelay<Events::BindGamepadAxis> m_EBindGamepadAxis;
EventRelay<InputSystem, Events::BindGamepadAxis> m_EBindGamepadAxis;
bool OnBindGamepadAxis(const Events::BindGamepadAxis &event);
EventRelay<Events::BindGamepadButton> m_EBindGamepadButton;
EventRelay<InputSystem, Events::BindGamepadButton> m_EBindGamepadButton;
bool OnBindGamepadButton(const Events::BindGamepadButton &event);
float GetCommandTotalValue(std::string command);
+55 -66
View File
@@ -27,12 +27,16 @@
void Systems::PhysicsSystem::Initialize()
{
m_Accumulator = 0;
// Events
EVENT_SUBSCRIBE_MEMBER(m_ETankSteer, &Systems::PhysicsSystem::OnTankSteer);
EVENT_SUBSCRIBE_MEMBER(m_ESetVelocity, &Systems::PhysicsSystem::OnSetVelocity);
EVENT_SUBSCRIBE_MEMBER(m_EApplyForce, &Systems::PhysicsSystem::OnApplyForce);
EVENT_SUBSCRIBE_MEMBER(m_EApplyPointImpulse, &Systems::PhysicsSystem::OnApplyPointImpulse);
hkMemorySystem::FrameInfo finfo(6000 * 1024); // Allocate 6MB of Physics solver buffer
hkMemoryRouter* memoryRouter = hkMemoryInitUtil::initDefault(hkMallocAllocator::m_defaultMallocAllocator, finfo);
hkBaseSystem::init(memoryRouter, HavokErrorReport);
@@ -71,7 +75,7 @@ void Systems::PhysicsSystem::Initialize()
worldInfo.setupSolverInfo(hkpWorldCinfo::SOLVER_TYPE_4ITERS_MEDIUM);
worldInfo.m_gravity = hkVector4(0.0f, -9.82f, 0.0f);
worldInfo.m_broadPhaseBorderBehaviour = hkpWorldCinfo::BROADPHASE_BORDER_FIX_ENTITY; // just fix the entity if the object falls off too far
worldInfo.m_broadPhaseBorderBehaviour = hkpWorldCinfo::BROADPHASE_BORDER_DO_NOTHING;
// You must specify the size of the broad phase - objects should not be simulated outside this region
worldInfo.setBroadPhaseWorldSize(1000.0f);
@@ -102,6 +106,8 @@ void Systems::PhysicsSystem::Initialize()
SetupVisualDebugger(m_Context);
m_PhysicsWorld->unmarkForWrite();
m_collisionResolution = new MyCollisionResolution;
}
}
@@ -140,13 +146,13 @@ void Systems::PhysicsSystem::Update(double dt)
if (parent)
{
auto absoluteTransform = m_World->GetSystem<Systems::TransformSystem>()->AbsoluteTransform(entity);
position = ConvertPosition(absoluteTransform.Position);
rotation = ConvertRotation(absoluteTransform.Orientation);
position = GLMVEC3_TO_HKVECTOR4(absoluteTransform.Position);
rotation = GLMQUAT_TO_HKQUATERNION(absoluteTransform.Orientation);
}
else
{
position = ConvertPosition(transformComponent->Position);
rotation = ConvertRotation(transformComponent->Orientation);
position = GLMVEC3_TO_HKVECTOR4(transformComponent->Position);
rotation = GLMQUAT_TO_HKQUATERNION(transformComponent->Orientation);
}
m_PhysicsWorld->markForWrite();
m_RigidBodies[entity]->setPositionAndRotation(position, rotation);
@@ -172,10 +178,7 @@ void Systems::PhysicsSystem::Update(double dt)
// Clear accumulated timer data in this thread and all slave threads
hkMonitorStream::getInstance().reset();
m_ThreadPool->clearTimerData();
}
}
}
void Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
@@ -201,7 +204,7 @@ void Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID p
hkQuaternion steeringOrientation = m_Vehicles[car]->m_wheelsInfo[wheelComponent->ID].m_steeringOrientationChassisSpace;
hkReal spinAngle = -m_Vehicles[car]->m_wheelsInfo[wheelComponent->ID].m_spinAngle;
glm::quat orientation = ConvertRotation(steeringOrientation) * glm::angleAxis<float>(spinAngle, glm::vec3(1, 0, 0));
glm::quat orientation = HKQUATERNION_TO_GLMQUAT(steeringOrientation) * glm::angleAxis<float>(spinAngle, glm::vec3(1, 0, 0));
transformComponent->Orientation = orientation * wheelComponent->OriginalOrientation;
m_PhysicsWorld->unmarkForWrite();
}
@@ -210,8 +213,8 @@ void Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID p
{
auto transformComponentParent = m_World->GetComponent<Components::Transform>(parent);
transformComponent->Position = ConvertPosition(m_RigidBodies[entity]->getPosition());
transformComponent->Orientation = ConvertRotation(m_RigidBodies[entity]->getRotation());
transformComponent->Position = HKVECTOR4_TO_GLMVEC3(m_RigidBodies[entity]->getPosition());
transformComponent->Orientation = HKQUATERNION_TO_GLMQUAT(m_RigidBodies[entity]->getRotation());
// TODO: No support for Scale, MIGHT be possible
if (transformComponentParent)
@@ -274,13 +277,9 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity )
hkpListShape* listShape = new hkpListShape(shapeArray.begin(), shapeArray.getSize(), hkpShapeContainer::REFERENCE_POLICY_INCREMENT);
// Save the listShape for further use
m_ListShapes[entity] = listShape;
shape = listShape;
//////////////////////////////////
//******************************//
// Add a hkpBvShape //
//******************************//
//////////////////////////////////
//shape = listShape;
hkpBoxShape* box = new hkpBoxShape(listShape->m_aabbHalfExtents, 0.0f);
shape = new hkpBvShape(listShape, box);
// Clean up for less memory usage
m_Shapes.erase(entity);
@@ -293,8 +292,8 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity )
rigidBodyInfo.m_shape = shape;
rigidBodyInfo.m_motionType = hkpMotion::MOTION_DYNAMIC;
auto absoluteTransform = m_World->GetSystem<Systems::TransformSystem>()->AbsoluteTransform(entity);
hkVector4 position = ConvertPosition(absoluteTransform.Position);
hkQuaternion rotation = ConvertRotation(absoluteTransform.Orientation);
hkVector4 position = GLMVEC3_TO_HKVECTOR4(absoluteTransform.Position);
hkQuaternion rotation = GLMQUAT_TO_HKQUATERNION(absoluteTransform.Orientation);
rigidBodyInfo.m_position.set(position(0), position(1), position(2), position(3));
rigidBodyInfo.m_rotation.set(rotation(0), rotation(1), rotation(2), rotation(3));
@@ -324,9 +323,11 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity )
m_PhysicsWorld->markForWrite();
vehicleSetup.buildVehicle(m_World, m_PhysicsWorld, *m_Vehicles[entity], entity, m_Wheels);
// Add the vehicle's entities and phantoms to the world
rigidBody->addContactListener( m_collisionResolution );
m_Vehicles[entity]->addToWorld(m_PhysicsWorld);
m_RigidBodies[entity] = rigidBody;
m_collisionResolution->m_RigidBodies[rigidBody] = entity;
// The vehicle is an action
m_PhysicsWorld->addAction(m_Vehicles[entity]);
@@ -340,8 +341,10 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity )
else
{
m_PhysicsWorld->markForWrite();
rigidBody->addContactListener( m_collisionResolution );
m_PhysicsWorld->addEntity(rigidBody);
m_RigidBodies[entity] = rigidBody;
m_collisionResolution->m_RigidBodies[rigidBody] = entity;
m_PhysicsWorld->unmarkForWrite();
shape->removeReference();
@@ -359,9 +362,9 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity )
auto childTransformComponent = m_World->GetComponent<Components::Transform>(shapeData.Entity);
hkVector4 position = ConvertPosition(childTransformComponent->Position);
hkQuaternion rotation = ConvertRotation(childTransformComponent->Orientation);
hkVector4 scale = ConvertScale(childTransformComponent->Scale);
hkVector4 position = GLMVEC3_TO_HKVECTOR4(childTransformComponent->Position);
hkQuaternion rotation = GLMQUAT_TO_HKQUATERNION(childTransformComponent->Orientation);
hkVector4 scale = GLMVEC3_TO_HKVECTOR4(childTransformComponent->Scale);
hkQsTransform transform(position, rotation, scale);
staticCompoundShape->addInstance(shapeData.Shape, transform);
@@ -379,8 +382,8 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity )
rigidBodyInfo.m_shape = shape;
rigidBodyInfo.m_motionType = hkpMotion::MOTION_FIXED;
auto absoluteTransform = m_World->GetSystem<Systems::TransformSystem>()->AbsoluteTransform(entity);
hkVector4 position = ConvertPosition(absoluteTransform.Position);
hkQuaternion rotation = ConvertRotation(absoluteTransform.Orientation);
hkVector4 position = GLMVEC3_TO_HKVECTOR4(absoluteTransform.Position);
hkQuaternion rotation = GLMQUAT_TO_HKQUATERNION(absoluteTransform.Orientation);
rigidBodyInfo.m_position.set(position(0), position(1), position(2), position(3));
rigidBodyInfo.m_rotation.set(rotation(0), rotation(1), rotation(2), rotation(3));
@@ -394,6 +397,7 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity )
m_PhysicsWorld->markForWrite();
m_PhysicsWorld->addEntity(rigidBody);
m_RigidBodies[entity] = rigidBody;
m_collisionResolution->m_RigidBodies[rigidBody] = entity;
m_PhysicsWorld->unmarkForWrite();
shape->removeReference();
@@ -411,7 +415,7 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity )
{
hkpSphereShape* sphereShape = new hkpSphereShape(sphereComponent->Radius);
hkQsTransform transform( ConvertPosition(transformComponent->Position), ConvertRotation(transformComponent->Orientation), ConvertScale(transformComponent->Scale));
hkQsTransform transform( GLMVEC3_TO_HKVECTOR4(transformComponent->Position), GLMQUAT_TO_HKQUATERNION(transformComponent->Orientation), GLMVEC3_TO_HKVECTOR4(transformComponent->Scale));
hkpConvexTransformShape* transformedSphereShape = new hkpConvexTransformShape( sphereShape, transform );
m_Shapes[entityParent].push_back(ShapeArrayData(entity, transformedSphereShape));
@@ -424,7 +428,7 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity )
hkReal thickness = 0.05;
hkpBoxShape* boxShape = new hkpBoxShape(hkVector4(boxComponent->Width- thickness, boxComponent->Height -thickness, boxComponent->Depth - thickness), thickness);
hkQsTransform transform( ConvertPosition(transformComponent->Position), ConvertRotation(transformComponent->Orientation), ConvertScale(transformComponent->Scale));
hkQsTransform transform( GLMVEC3_TO_HKVECTOR4(transformComponent->Position), GLMQUAT_TO_HKQUATERNION(transformComponent->Orientation), GLMVEC3_TO_HKVECTOR4(transformComponent->Scale));
hkpConvexTransformShape* transformedBoxShape = new hkpConvexTransformShape( boxShape, transform );
m_Shapes[entityParent].push_back(ShapeArrayData(entity, transformedBoxShape));
boxShape->removeReference();
@@ -532,50 +536,19 @@ void HK_CALL Systems::PhysicsSystem::HavokErrorReport(const char* msg, void*)
LOG_INFO("%s", msg);
}
glm::vec3 Systems::PhysicsSystem::ConvertPosition(const hkVector4 &hkPosition)
{
return glm::vec3(hkPosition(0), hkPosition(1), hkPosition(2));
}
const hkVector4& Systems::PhysicsSystem::ConvertPosition(glm::vec3 glmPosition)
{
return hkVector4( glmPosition.x, glmPosition.y, glmPosition.z);
}
glm::quat Systems::PhysicsSystem::ConvertRotation(const hkQuaternion &hkRotation)
{
return glm::quat(hkRotation(3), hkRotation(0), hkRotation(1), hkRotation(2));
}
const hkQuaternion& Systems::PhysicsSystem::ConvertRotation(glm::quat glmRotation)
{
hkQuaternion quat = hkQuaternion(glmRotation.x, glmRotation.y, glmRotation.z, glmRotation.w);
quat.normalize();
return quat;
}
glm::vec3 Systems::PhysicsSystem::ConvertScale(const hkVector4 &hkScale)
{
return glm::vec3(hkScale(0), hkScale(1), hkScale(2));
}
const hkVector4& Systems::PhysicsSystem::ConvertScale(glm::vec3 glmScale)
{
return hkVector4(glmScale.x, glmScale.y, glmScale.z);
}
bool Systems::PhysicsSystem::OnTankSteer(const Events::TankSteer &event)
{
auto vehicleComponent = m_World->GetComponent<Components::Vehicle>(event.Entity);
if (vehicleComponent && m_Vehicles.find(event.Entity) != m_Vehicles.end() && m_RigidBodies.find(event.Entity) != m_RigidBodies.end())
{
m_PhysicsWorld->markForWrite();
hkpVehicleDriverInputAnalogStatus* deviceStatus = (hkpVehicleDriverInputAnalogStatus*)m_Vehicles[event.Entity]->m_deviceStatus;
deviceStatus->m_positionX = event.PositionX;
deviceStatus->m_positionY = event.PositionY;
if(event.PositionY > 0)
{
deviceStatus->m_reverseButtonPressed = true;
}
deviceStatus->m_handbrakeButtonPressed = event.Handbrake;
m_PhysicsWorld->unmarkForWrite();
}
return true;
@@ -584,7 +557,23 @@ bool Systems::PhysicsSystem::OnTankSteer(const Events::TankSteer &event)
bool Systems::PhysicsSystem::OnSetVelocity( const Events::SetVelocity &event )
{
m_PhysicsWorld->markForWrite();
m_RigidBodies[event.Entity]->setLinearVelocity(ConvertPosition(event.Velocity));
m_RigidBodies[event.Entity]->setLinearVelocity(GLMVEC3_TO_HKVECTOR4(event.Velocity));
m_PhysicsWorld->unmarkForWrite();
return true;
}
bool Systems::PhysicsSystem::OnApplyForce(const Events::ApplyForce &event)
{
m_PhysicsWorld->markForWrite();
m_RigidBodies[event.Entity]->applyForce(event.DeltaTime, GLMVEC3_TO_HKVECTOR4(event.Force));
m_PhysicsWorld->unmarkForWrite();
return true;
}
bool Systems::PhysicsSystem::OnApplyPointImpulse( const Events::ApplyPointImpulse &event )
{
m_PhysicsWorld->markForWrite();
m_RigidBodies[event.Entity]->applyPointImpulse(GLMVEC3_TO_HKVECTOR4(event.Impulse), GLMVEC3_TO_HKVECTOR4(event.Position));
m_PhysicsWorld->unmarkForWrite();
return true;
}
+40 -16
View File
@@ -1,6 +1,16 @@
#ifndef PhysicsSystem_h__
#define PhysicsSystem_h__
#define HKVECTOR4_TO_GLMVEC3(hkvec) \
glm::vec3(hkvec(0), hkvec(1), hkvec(2))
#define GLMVEC3_TO_HKVECTOR4(glmvec) \
hkVector4(glmvec.x, glmvec.y, glmvec.z)
#define HKQUATERNION_TO_GLMQUAT(gkquat) \
glm::quat(gkquat(3), gkquat(0), gkquat(1), gkquat(2))
#define GLMQUAT_TO_HKQUATERNION(glmquat) \
hkQuaternion(glmquat.x, glmquat.y, glmquat.z, glmquat.w)
#include "System.h"
#include "Systems/TransformSystem.h"
#include "Components/Transform.h"
@@ -15,6 +25,8 @@
#include "Components/TowerSteering.h"
#include "Events/TankSteer.h"
#include "Events/SetVelocity.h"
#include "Events/ApplyForce.h"
#include "Events/ApplyPointImpulse.h"
#include "OBJ.h"
// Math and base include
@@ -60,9 +72,27 @@
#include "Physics/VehicleSetup.h"
#include <unordered_map>
#include <Physics2012/Dynamics/Collide/ContactListener/hkpContactListener.h>
class MyCollisionResolution: public hkReferencedObject, public hkpContactListener
{
public:
std::unordered_map<hkpRigidBody*, EntityID> m_RigidBodies;
virtual void contactPointCallback( const hkpContactPointEvent& event )
{
EntityID entity1 = m_RigidBodies[event.getBody(0)];
EntityID entity2 = m_RigidBodies[event.getBody(1)];
//LOG_INFO("Entities colliding: %i, %i ", entity1, entity2);
}
};
namespace Systems
{
class PhysicsSystem : public System
{
public:
@@ -77,17 +107,20 @@ public:
void OnComponentCreated(std::string type, std::shared_ptr<Component> component) override;
void OnComponentRemoved(std::string type, Component* component) override;
void OnEntityCommit(EntityID entity) override;
private:
double m_Accumulator;
hkpWorld* m_PhysicsWorld;
// Events
EventRelay<Events::TankSteer> m_ETankSteer;
EventRelay<PhysicsSystem, Events::TankSteer> m_ETankSteer;
bool OnTankSteer(const Events::TankSteer &event);
EventRelay<Events::SetVelocity> m_ESetVelocity;
EventRelay<PhysicsSystem, Events::SetVelocity> m_ESetVelocity;
bool OnSetVelocity(const Events::SetVelocity &event);
EventRelay<PhysicsSystem, Events::ApplyForce> m_EApplyForce;
bool OnApplyForce(const Events::ApplyForce &event);
EventRelay<PhysicsSystem, Events::ApplyPointImpulse> m_EApplyPointImpulse;
bool OnApplyPointImpulse(const Events::ApplyPointImpulse &event);
void SetUpPhysicsState(EntityID entity, EntityID parent);
void TearDownPhysicsState(EntityID entity, EntityID parent);
@@ -98,16 +131,6 @@ private:
static void HK_CALL HavokErrorReport(const char* msg, void*);
void SetupPhysics(hkpWorld* physicsWorld);
// Converterfunctions
glm::vec3 ConvertPosition(const hkVector4 &hkPosition);
const hkVector4& ConvertPosition(glm::vec3 glmPosition);
glm::quat ConvertRotation(const hkQuaternion &hkRotation);
const hkQuaternion& ConvertRotation(glm::quat glmRotation);
glm::vec3 ConvertScale(const hkVector4 &hkScale);
const hkVector4&ConvertScale(glm::vec3 glmScale);
std::unordered_map<EntityID, hkpRigidBody*> m_RigidBodies;
hkJobThreadPool* m_ThreadPool;
@@ -145,8 +168,9 @@ private:
hkpMoppBvTreeShape* MoppShape;
};
std::unordered_map<EntityID, ExtendedShapeData > m_ExtendedMeshShapes;
MyCollisionResolution* m_collisionResolution;
};
}
#endif // PhysicsSystem_h__
+25
View File
@@ -0,0 +1,25 @@
#include "PrecompiledHeader.h"
#include "RaySystem.h"
#include "World.h"
void Systems::RaySystem::Initialize()
{
// Subscribe to events
EVENT_SUBSCRIBE_MEMBER(m_ECastRay, &Systems::RaySystem::OnCastRay);
}
void Systems::RaySystem::Update(double dt)
{
}
void Systems::RaySystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
{
}
bool Systems::RaySystem::OnCastRay(const Events::CastRay &event)
{
Ray r;
return true;
}
+35
View File
@@ -0,0 +1,35 @@
#ifndef DebugSystem_h__
#define DebugSystem_h__
#include "System.h"
#include "Events/CastRay.h"
namespace Systems
{
class RaySystem : public System
{
public:
RaySystem(World* world, std::shared_ptr<::EventBroker> eventBroker)
: System(world, eventBroker) { }
void Initialize() override;
void Update(double dt) override;
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
private:
struct Ray
{
glm::vec3 Direction;
};
EventRelay<Events::CastRay> m_ECastRay;
bool OnCastRay(const Events::CastRay &event);
std::list<Ray> m_UnresolvedRays;
//void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
};
}
#endif // DebugSystem_h__
+57 -35
View File
@@ -2,26 +2,59 @@
#include "RenderSystem.h"
#include "World.h"
void Systems::RenderSystem::OnComponentCreated(std::string type, std::shared_ptr<Component> component)
void Systems::RenderSystem::RegisterResourceTypes(ResourceManager* rm)
{
if(type == "Model")
rm->RegisterType("Model", [rm](std::string resourceName) { return new Model(rm, *rm->Load<OBJ>("OBJ", resourceName)); });
rm->RegisterType("OBJ", [](std::string resourceName) { return new OBJ(resourceName); });
rm->RegisterType("Texture", [](std::string resourceName) { return new Texture(resourceName); });
}
void Systems::RenderSystem::RegisterComponents(ComponentFactory* cf)
{
cf->Register<Components::Camera>([]() { return new Components::Camera(); });
cf->Register<Components::Model>([]() { return new Components::Model(); });
cf->Register<Components::Sprite>([]() { return new Components::Sprite(); });
cf->Register<Components::PointLight>([]() { return new Components::PointLight(); });
cf->Register<Components::DirectionalLight>([]() { return new Components::DirectionalLight(); });
cf->Register<Components::Viewport>([]() { return new Components::Viewport(); });
}
void Systems::RenderSystem::OnEntityCommit(EntityID entity)
{
auto transform = m_World->GetComponent<Components::Transform>(entity);
auto camera = m_World->GetComponent<Components::Camera>(entity);
if (transform && camera)
{
auto modelComponent = std::static_pointer_cast<Components::Model>(component);
m_Renderer->RegisterCamera(entity, camera->FOV, camera->NearClip, camera->FarClip);
m_Renderer->UpdateCamera(entity, m_TransformSystem->AbsolutePosition(entity), m_TransformSystem->AbsoluteOrientation(entity), camera->FOV, camera->NearClip, camera->FarClip);
}
auto viewport = m_World->GetComponent<Components::Viewport>(entity);
if (viewport)
{
m_Renderer->RegisterViewport(entity, viewport->Left, viewport->Top, viewport->Right, viewport->Bottom);
if (viewport->Camera != 0)
{
m_Renderer->UpdateViewport(entity, viewport->Camera);
}
}
}
void Systems::RenderSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
{
auto transformComponent = m_World->GetComponent<Components::Transform>(entity);
if (transformComponent == nullptr)
auto templateComponent = m_World->GetComponent<Components::Template>(entity);
if (templateComponent)
return;
auto transformComponent = m_World->GetComponent<Components::Transform>(entity);
// Draw models
auto modelComponent = m_World->GetComponent<Components::Model>(entity);
if (modelComponent != nullptr)
if (transformComponent && modelComponent)
{
auto model = m_World->GetResourceManager()->Load<Model>("Model", modelComponent->ModelFile);
if (model != nullptr)
if (model)
{
/*glm::vec3 position = m_TransformSystem->AbsolutePosition(entity);
glm::quat orientation = m_TransformSystem->AbsoluteOrientation(entity);
@@ -32,7 +65,7 @@ void Systems::RenderSystem::UpdateEntity(double dt, EntityID entity, EntityID pa
}
auto pointLightComponent = m_World->GetComponent<Components::PointLight>(entity);
if (pointLightComponent != nullptr)
if (transformComponent && pointLightComponent)
{
glm::vec3 position = m_TransformSystem->AbsolutePosition(entity);
m_Renderer->AddPointLightToDraw(
@@ -47,18 +80,27 @@ void Systems::RenderSystem::UpdateEntity(double dt, EntityID entity, EntityID pa
}
auto cameraComponent = m_World->GetComponent<Components::Camera>(entity);
if (cameraComponent != nullptr)
if (transformComponent && cameraComponent)
{
m_Renderer->GetCamera()->Position(m_TransformSystem->AbsolutePosition(entity));
m_Renderer->GetCamera()->Orientation(m_TransformSystem->AbsoluteOrientation(entity));
m_Renderer->UpdateCamera(entity
, m_TransformSystem->AbsolutePosition(entity)
, m_TransformSystem->AbsoluteOrientation(entity)
, cameraComponent->FOV
, cameraComponent->NearClip
, cameraComponent->FarClip);
}
m_Renderer->GetCamera()->FOV(cameraComponent->FOV);
m_Renderer->GetCamera()->NearClip(cameraComponent->NearClip);
m_Renderer->GetCamera()->FarClip(cameraComponent->FarClip);
auto viewportComponent = m_World->GetComponent<Components::Viewport>(entity);
if (viewportComponent)
{
if (viewportComponent->Camera != 0)
{
m_Renderer->UpdateViewport(entity, viewportComponent->Camera);
}
}
auto spriteComponent = m_World->GetComponent<Components::Sprite>(entity);
if(spriteComponent != nullptr)
if (transformComponent && spriteComponent)
{
//TEMP
Texture* texture = m_World->GetResourceManager()->Load<Texture>("Texture", spriteComponent->SpriteFile);
@@ -76,23 +118,3 @@ void Systems::RenderSystem::Initialize()
m_Renderer->SetSphereModel(m_World->GetResourceManager()->Load<Model>("Model", "Models/Placeholders/PhysicsTest/Sphere.obj"));
}
void Systems::RenderSystem::RegisterComponents(ComponentFactory* cf)
{
cf->Register<Components::Camera>([]() { return new Components::Camera(); });
cf->Register<Components::Model>([]() { return new Components::Model(); });
cf->Register<Components::Sprite>([]() { return new Components::Sprite(); });
cf->Register<Components::PointLight>([]() { return new Components::PointLight(); });
cf->Register<Components::DirectionalLight>([]() { return new Components::DirectionalLight(); });
}
void Systems::RenderSystem::RegisterResourceTypes(ResourceManager* rm)
{
rm->RegisterType("Model", [rm](std::string resourceName) { return new Model(rm, *rm->Load<OBJ>("OBJ", resourceName)); });
rm->RegisterType("OBJ", [](std::string resourceName) { return new OBJ(resourceName); });
rm->RegisterType("Texture", [](std::string resourceName) { return new Texture(resourceName); });
}
+2 -1
View File
@@ -13,6 +13,7 @@
#include "Components/Sprite.h"
#include "Components/PointLight.h"
#include "Components/DirectionalLight.h"
#include "Components/Viewport.h"
#include "Components/Template.h"
#include "Components/Transform.h"
@@ -34,7 +35,7 @@ public:
std::unordered_map<std::string, std::shared_ptr<Model>> m_CachedModels;
void OnComponentCreated(std::string type, std:: shared_ptr<Component> component) override;
void OnEntityCommit(EntityID entity) override;
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
+1 -1
View File
@@ -32,7 +32,7 @@ public:
private:
// Events
EventRelay<Events::PlaySound> m_EPlaySound;
EventRelay<SoundSystem, Events::PlaySound> m_EPlaySound;
FMOD_RESULT LoadSound(FMOD_SOUND*&, std::string, float, float, bool, float, float);
void PlaySound(FMOD_CHANNEL*, FMOD_SOUND*, float volume, bool loop);
+61 -48
View File
@@ -11,60 +11,85 @@ void Systems::TankSteeringSystem::RegisterComponents( ComponentFactory* cf )
void Systems::TankSteeringSystem::Initialize()
{
m_TankInputController = std::unique_ptr<TankSteeringInputController>(new TankSteeringInputController(EventBroker));
m_TowerInputController = std::unique_ptr<TowerSteeringInputController>(new TowerSteeringInputController(EventBroker));
for (int i = 0; i < 4; i++)
{
m_TankInputControllers[i] = std::shared_ptr<TankSteeringInputController>(new TankSteeringInputController(EventBroker, i + 1));
}
}
void Systems::TankSteeringSystem::Update(double dt)
{
m_TankInputController->Update(dt);
m_TowerInputController->Update(dt);
for (int i = 0; i < 4; i++)
{
m_TankInputControllers[i]->Update(dt);
}
}
void Systems::TankSteeringSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
{
auto tankSteeringComponent = m_World->GetComponent<Components::TankSteering>(entity);
if(tankSteeringComponent)
{
Events::TankSteer e;
e.Entity = entity;
e.PositionX = m_TankInputController->PositionX;
e.PositionY = m_TankInputController->PositionY;
e.Handbrake = m_TankInputController->Handbrake;
EventBroker->Publish(e);
}
if(!tankSteeringComponent)
return;
auto playerComponent = m_World->GetComponent<Components::Player>(tankSteeringComponent->Player);
if (!playerComponent)
return;
if (playerComponent->ID == 0)
return;
auto inputController = m_TankInputControllers[playerComponent->ID - 1];
Events::TankSteer eSteering;
eSteering.Entity = entity;
eSteering.PositionX = inputController->PositionX;
eSteering.PositionY = inputController->PositionY;
eSteering.Handbrake = inputController->Handbrake;
EventBroker->Publish(eSteering);
auto towerSteeringComponent = m_World->GetComponent<Components::TowerSteering>(tankSteeringComponent->Turret);
auto barrelSteeringComponent = m_World->GetComponent<Components::BarrelSteering>(tankSteeringComponent->Barrel);
auto towerSteeringComponent = m_World->GetComponent<Components::TowerSteering>(entity);
if(towerSteeringComponent)
{
auto transformComponent = m_World->GetComponent<Components::Transform>(entity);
glm::quat orientation = glm::angleAxis(towerSteeringComponent->TurnSpeed * m_TowerInputController->TowerDirection * (float)dt, towerSteeringComponent->Axis);
auto transformComponent = m_World->GetComponent<Components::Transform>(tankSteeringComponent->Turret);
glm::quat orientation = glm::angleAxis(towerSteeringComponent->TurnSpeed * inputController->TowerDirection * (float)dt, towerSteeringComponent->Axis);
transformComponent->Orientation *= orientation;
}
auto barrelSteeringComponent = m_World->GetComponent<Components::BarrelSteering>(entity);
if(barrelSteeringComponent)
{
auto transformComponent = m_World->GetComponent<Components::Transform>(entity);
auto absoluteTransform = m_World->GetSystem<Systems::TransformSystem>()->AbsoluteTransform(entity);
glm::quat orientation = glm::angleAxis(barrelSteeringComponent->TurnSpeed * m_TowerInputController->BarrelDirection * (float)dt, barrelSteeringComponent->Axis);
auto transformComponent = m_World->GetComponent<Components::Transform>(tankSteeringComponent->Barrel);
auto absoluteTransform = m_World->GetSystem<Systems::TransformSystem>()->AbsoluteTransform(tankSteeringComponent->Barrel);
glm::quat orientation = glm::angleAxis(barrelSteeringComponent->TurnSpeed * inputController->BarrelDirection * (float)dt, barrelSteeringComponent->Axis);
transformComponent->Orientation *= orientation;
if(m_TowerInputController->Shoot && m_TimeSinceLastShot[entity] > 1.0)
if(inputController->Shoot && m_TimeSinceLastShot[tankSteeringComponent->Barrel] > 0.5)
{
EntityID clone = m_World->CloneEntity(barrelSteeringComponent->ShotTemplate);
auto templateAbsoluteTransform = m_World->GetSystem<Systems::TransformSystem>()->AbsoluteTransform(barrelSteeringComponent->ShotTemplate);
auto cloneTransform = m_World->GetComponent<Components::Transform>(clone);
cloneTransform->Position = templateAbsoluteTransform.Position;
cloneTransform->Orientation = absoluteTransform.Orientation * cloneTransform->Orientation;
Events::SetVelocity e;
e.Entity = clone;
e.Velocity = absoluteTransform.Orientation * (glm::vec3(0.f, 0.f, -1.f) * barrelSteeringComponent->ShotSpeed);
EventBroker->Publish(e);
m_TimeSinceLastShot[entity] = 0;
Events::SetVelocity eSetVelocity;
eSetVelocity.Entity = clone;
eSetVelocity.Velocity = absoluteTransform.Orientation * (glm::vec3(0.f, 0.f, -1.f) * barrelSteeringComponent->ShotSpeed);
EventBroker->Publish(eSetVelocity);
m_TimeSinceLastShot[tankSteeringComponent->Barrel] = 0;
auto clonePhysicsComponent = m_World->GetComponent<Components::Physics>(clone);
//1,670m/s
//25kg
Events::ApplyPointImpulse ePointImpulse ;
ePointImpulse.Entity = entity;
ePointImpulse.Position = absoluteTransform.Position;
ePointImpulse.Impulse = glm::normalize(absoluteTransform.Orientation * glm::vec3(0, 0, 1)) * clonePhysicsComponent->Mass * 1670.f;
EventBroker->Publish(ePointImpulse);
}
m_TimeSinceLastShot[entity] += dt;
m_TimeSinceLastShot[tankSteeringComponent->Barrel] += dt;
}
}
@@ -72,10 +97,7 @@ void Systems::TankSteeringSystem::TankSteeringInputController::Update( double dt
{
PositionX = m_Horizontal;
PositionY = m_Vertical;
}
void Systems::TankSteeringSystem::TowerSteeringInputController::Update( double dt )
{
TowerDirection = m_TowerDirection;
BarrelDirection = m_BarrelDirection;
Shoot = m_Shoot;
@@ -83,10 +105,16 @@ void Systems::TankSteeringSystem::TowerSteeringInputController::Update( double d
bool Systems::TankSteeringSystem::TankSteeringInputController::OnCommand(const Events::InputCommand &event)
{
if (event.PlayerID != this->PlayerID)
return false;
float val = event.Value;
// Tank
if (event.Command == "horizontal")
{
m_Horizontal = val;
m_Vertical = -0.4f;
}
else if (event.Command == "vertical")
{
@@ -98,12 +126,7 @@ bool Systems::TankSteeringSystem::TankSteeringInputController::OnCommand(const E
Handbrake = val > 0;
}
return true;
}
bool Systems::TankSteeringSystem::TowerSteeringInputController::OnCommand( const Events::InputCommand &event )
{
float val = event.Value;
// Turret
if(event.Command == "tower_rotation")
{
m_TowerDirection = -val;
@@ -117,19 +140,9 @@ bool Systems::TankSteeringSystem::TowerSteeringInputController::OnCommand( const
{
m_Shoot = val > 0;
}
return true;
}
bool Systems::TankSteeringSystem::TowerSteeringInputController::OnMouseMove( const Events::MouseMove &event )
{
return false;
}
bool Systems::TankSteeringSystem::TankSteeringInputController::OnMouseMove( const Events::MouseMove &event )
{
return false;
}
+24 -30
View File
@@ -3,11 +3,15 @@
#include "System.h"
#include "Events/TankSteer.h"
#include "Events/SetVelocity.h"
#include "Events/ApplyForce.h"
#include "Events/ApplyPointImpulse.h"
#include "Components/Transform.h"
#include "Components/TankSteering.h"
#include "Components/TowerSteering.h"
#include "Components/BarrelSteering.h"
#include "Components/Physics.h"
#include "Components/Vehicle.h"
#include "Components/Player.h"
#include "Systems/TransformSystem.h"
#include "InputController.h"
@@ -28,45 +32,25 @@ namespace Systems
private:
class TankSteeringInputController;
std::unique_ptr<TankSteeringInputController> m_TankInputController;
class TowerSteeringInputController;
std::unique_ptr<TowerSteeringInputController> m_TowerInputController;
std::array<std::shared_ptr<TankSteeringInputController>, 4> m_TankInputControllers;
std::map<EntityID, double> m_TimeSinceLastShot;
};
class TankSteeringSystem::TankSteeringInputController : InputController
class TankSteeringSystem::TankSteeringInputController : InputController<TankSteeringSystem>
{
public:
TankSteeringInputController(std::shared_ptr<::EventBroker> eventBroker)
TankSteeringInputController(std::shared_ptr<::EventBroker> eventBroker, int playerID)
: InputController(eventBroker)
{
PlayerID = playerID;
m_Horizontal = 0.f;
m_Vertical = 0.f;
PositionX = 0;
PositionY = 0;
Handbrake = false;
}
float PositionY;
float PositionX;
bool Handbrake;
void Update(double dt);
protected:
virtual bool OnCommand(const Events::InputCommand &event);
virtual bool OnMouseMove(const Events::MouseMove &event);
private:
float m_Horizontal;
float m_Vertical;
};
class TankSteeringSystem::TowerSteeringInputController : InputController
{
public:
TowerSteeringInputController(std::shared_ptr<::EventBroker> eventBroker)
: InputController(eventBroker)
{
m_TowerDirection = 0.f;
m_BarrelDirection = 0.f;
TowerDirection = 0.f;
@@ -75,18 +59,28 @@ namespace Systems
m_Shoot = false;
}
int PlayerID;
float PositionY;
float PositionX;
bool Handbrake;
float TowerDirection;
float BarrelDirection;
bool Shoot;
void Update(double dt);
protected:
virtual bool OnCommand(const Events::InputCommand &event);
virtual bool OnMouseMove(const Events::MouseMove &event);
//virtual bool OnMouseMove(const Events::MouseMove &event);
private:
float m_TowerDirection;
float m_BarrelDirection;
bool m_Shoot;
};
float m_Horizontal;
float m_Vertical;
float m_TowerDirection;
float m_BarrelDirection;
bool m_Shoot;
};
}