Tank tower and barrel steering working

This commit is contained in:
ViktorLjung
2014-05-12 22:33:53 +02:00
parent 92e9fd1049
commit 38482431a1
17 changed files with 246 additions and 96 deletions
+20
View File
@@ -0,0 +1,20 @@
#ifndef BarrelSteering_h__
#define BarrelSteering_h__
#include "Component.h"
namespace Components
{
struct BarrelSteering : Component
{
BarrelSteering()
: Velocity(1.f), Axis(glm::vec3(0,1,0)){ }
float Velocity;
glm::vec3 Axis;
};
}
#endif // BarrelSteering_h__
+20
View File
@@ -0,0 +1,20 @@
#ifndef TowerSteering_h__
#define TowerSteering_h__
#include "Component.h"
namespace Components
{
struct TowerSteering : Component
{
TowerSteering()
: Velocity(1.f), Axis(glm::vec3(0,1,0)){ }
float Velocity;
glm::vec3 Axis;
};
}
#endif // TowerSteering_h__
+2 -1
View File
@@ -11,7 +11,7 @@ struct Vehicle : Component
{ {
Vehicle() Vehicle()
: MaxTorque(1000.0f), MinRPM(1000.0f), OptimalRPM(3000.0f), MaxRPM(4000.0f), MaxSteeringAngle(35), TopSpeed(130.0f), : MaxTorque(1000.0f), MinRPM(1000.0f), OptimalRPM(3000.0f), MaxRPM(4000.0f), MaxSteeringAngle(35), TopSpeed(130.0f),
MaxSpeedFullSteeringAngle(40.0f){ } MaxSpeedFullSteeringAngle(40.0f), SpringDamping(1.f){ }
float MaxTorque; float MaxTorque;
float MinRPM; float MinRPM;
@@ -22,6 +22,7 @@ struct Vehicle : Component
//TopSpeed not working fully yet //TopSpeed not working fully yet
float TopSpeed; float TopSpeed;
float MaxSpeedFullSteeringAngle; float MaxSpeedFullSteeringAngle;
float SpringDamping;
}; };
} }
+3
View File
@@ -1,6 +1,8 @@
#ifndef Events_BindKey_h__ #ifndef Events_BindKey_h__
#define Events_BindKey_h__ #define Events_BindKey_h__
#include <boost/any.hpp>
#include "EventBroker.h" #include "EventBroker.h"
namespace Events namespace Events
@@ -10,6 +12,7 @@ struct BindKey : Event
{ {
int KeyCode; int KeyCode;
std::string Command; std::string Command;
float Value;
}; };
} }
+1 -1
View File
@@ -12,7 +12,7 @@ struct InputCommand : Event
{ {
unsigned int PlayerID; unsigned int PlayerID;
std::string Command; std::string Command;
boost::any Value; float Value;
}; };
} }
+47 -26
View File
@@ -8,11 +8,25 @@ void GameWorld::Initialize()
m_ResourceManager.Preload("Model", "Models/Placeholders/PhysicsTest/Plane.obj"); m_ResourceManager.Preload("Model", "Models/Placeholders/PhysicsTest/Plane.obj");
m_ResourceManager.Preload("Model", "Models/Placeholders/PhysicsTest/ArrowCube.obj"); m_ResourceManager.Preload("Model", "Models/Placeholders/PhysicsTest/ArrowCube.obj");
BindKey(GLFW_KEY_W, "+forward"); BindKey(GLFW_KEY_W, "vertical", -1.f);
BindKey(GLFW_KEY_S, "+backward"); BindKey(GLFW_KEY_S, "vertical", 1.f);
BindKey(GLFW_KEY_A, "+left"); BindKey(GLFW_KEY_A, "horizontal", -1.f);
BindKey(GLFW_KEY_D, "+right"); BindKey(GLFW_KEY_D, "horizontal", 1.f);
BindKey(GLFW_KEY_SPACE, "+handbrake");
BindKey(GLFW_KEY_UP, "barrel_rotation", 1.f);
BindKey(GLFW_KEY_DOWN, "barrel_rotation", -1.f);
BindKey(GLFW_KEY_LEFT, "tower_rotation", 1.f);
BindKey(GLFW_KEY_RIGHT, "tower_rotation", -1.f);
BindKey(GLFW_KEY_SPACE, "handbrake", 1.f);
//
// BindKey(GLFW_KEY_UP, "vertical", -1.f);
// BindKey(GLFW_KEY_DOWN, "vertical", 1.f);
// BindKey(GLFW_KEY_LEFT, "horizontal", -1.f);
// BindKey(GLFW_KEY_RIGHT, "horizontal", 1.f);
/*
BindKey(GLFW_KEY_Q, "+tower_right");
BindKey(GLFW_KEY_E, "+tower_left");
BindKey(GLFW_KEY_Q, "+up"); BindKey(GLFW_KEY_Q, "+up");
BindKey(GLFW_KEY_LEFT_CONTROL, "+down"); BindKey(GLFW_KEY_LEFT_CONTROL, "+down");
@@ -25,8 +39,8 @@ void GameWorld::Initialize()
BindKey(GLFW_KEY_UP, "+cam_forward"); BindKey(GLFW_KEY_UP, "+cam_forward");
BindKey(GLFW_KEY_DOWN, "+cam_backward"); BindKey(GLFW_KEY_DOWN, "+cam_backward");
BindKey(GLFW_KEY_LEFT, "+cam_right"); BindKey(GLFW_KEY_LEFT, "+cam_left");
BindKey(GLFW_KEY_RIGHT, "+cam_left"); BindKey(GLFW_KEY_RIGHT, "+cam_right");*/
RegisterComponents(); RegisterComponents();
@@ -242,23 +256,29 @@ void GameWorld::Initialize()
{ {
auto chassis = CreateEntity(tank); auto chassis = CreateEntity(tank);
auto transform = AddComponent<Components::Transform>(chassis, "Transform"); auto transform = AddComponent<Components::Transform>(chassis, "Transform");
transform->Position = glm::vec3(0, 0, 0); // 0.6577f transform->Position = glm::vec3(0, 0, 0);
auto model = AddComponent<Components::Model>(chassis, "Model"); auto model = AddComponent<Components::Model>(chassis, "Model");
model->ModelFile = "Models/Tank/Fix/Chassi.obj"; model->ModelFile = "Models/Tank/Fix/Chassi.obj";
} }
{ {
auto top = CreateEntity(tank); auto tower = CreateEntity(tank);
auto transform = AddComponent<Components::Transform>(top, "Transform"); SetProperty(tower, "Name", "tower");
transform->Position = glm::vec3(0, 1.2, 1.95); // 0.6577f auto transform = AddComponent<Components::Transform>(tower, "Transform");
auto model = AddComponent<Components::Model>(top, "Model"); transform->Position = glm::vec3(0, 1.2, 1.95);
auto model = AddComponent<Components::Model>(tower, "Model");
model->ModelFile = "Models/Tank/Fix/Top.obj"; model->ModelFile = "Models/Tank/Fix/Top.obj";
auto towerSteering = AddComponent<Components::TowerSteering>(tower, "TowerSteering");
towerSteering->Axis = glm::vec3(0.f, 1.f, 0.f);
towerSteering->Velocity = glm::pi<float>()/4.f;
{ {
auto top = CreateEntity(tank); auto barrel = CreateEntity(tower);
auto transform = AddComponent<Components::Transform>(top, "Transform"); auto transform = AddComponent<Components::Transform>(barrel, "Transform");
transform->Position = glm::vec3(0, 1, 0.5); // 0.6577f transform->Position = glm::vec3(0, 0, -2.f);
auto model = AddComponent<Components::Model>(top, "Model"); auto model = AddComponent<Components::Model>(barrel, "Model");
model->ModelFile = "Models/Tank/Fix/Barrel.obj"; model->ModelFile = "Models/Tank/Fix/Barrel.obj";
auto barrelSteering = AddComponent<Components::BarrelSteering>(barrel, "BarrelSteering");
barrelSteering->Axis = glm::vec3(1.f, 0.f, 0.f);
barrelSteering->Velocity = glm::pi<float>()/4.f;
} }
} }
@@ -297,7 +317,7 @@ void GameWorld::Initialize()
Wheel->Radius = 0.6f; Wheel->Radius = 0.6f;
Wheel->Steering = true; Wheel->Steering = true;
Wheel->SuspensionStrength = suspensionStrength; Wheel->SuspensionStrength = suspensionStrength;
Wheel->Friction = 3.f; Wheel->Friction = 4.f;
Wheel->ConnectedToHandbrake = true; Wheel->ConnectedToHandbrake = true;
Wheel->TorqueRatio = 0.125f; Wheel->TorqueRatio = 0.125f;
CommitEntity(wheel); CommitEntity(wheel);
@@ -316,7 +336,7 @@ void GameWorld::Initialize()
Wheel->Radius = 0.6f; Wheel->Radius = 0.6f;
Wheel->Steering = false; Wheel->Steering = false;
Wheel->SuspensionStrength = suspensionStrength; Wheel->SuspensionStrength = suspensionStrength;
Wheel->Friction = 3.f; Wheel->Friction = 4.f;
Wheel->ConnectedToHandbrake = true; Wheel->ConnectedToHandbrake = true;
Wheel->TorqueRatio = 0.125f; Wheel->TorqueRatio = 0.125f;
CommitEntity(wheel); CommitEntity(wheel);
@@ -336,7 +356,7 @@ void GameWorld::Initialize()
Wheel->Radius = 0.6f; Wheel->Radius = 0.6f;
Wheel->Steering = true; Wheel->Steering = true;
Wheel->SuspensionStrength = suspensionStrength; Wheel->SuspensionStrength = suspensionStrength;
Wheel->Friction = 3.f; Wheel->Friction = 4.f;
Wheel->ConnectedToHandbrake = true; Wheel->ConnectedToHandbrake = true;
Wheel->TorqueRatio = 0.125f; Wheel->TorqueRatio = 0.125f;
CommitEntity(wheel); CommitEntity(wheel);
@@ -355,7 +375,7 @@ void GameWorld::Initialize()
Wheel->Radius = 0.6f; Wheel->Radius = 0.6f;
Wheel->Steering = false; Wheel->Steering = false;
Wheel->SuspensionStrength = suspensionStrength; Wheel->SuspensionStrength = suspensionStrength;
Wheel->Friction = 3.f; Wheel->Friction = 4.f;
Wheel->ConnectedToHandbrake = true; Wheel->ConnectedToHandbrake = true;
Wheel->TorqueRatio = 0.125f; Wheel->TorqueRatio = 0.125f;
CommitEntity(wheel); CommitEntity(wheel);
@@ -376,7 +396,7 @@ void GameWorld::Initialize()
Wheel->Radius = 0.6f; Wheel->Radius = 0.6f;
Wheel->Steering = false; Wheel->Steering = false;
Wheel->SuspensionStrength = suspensionStrength; Wheel->SuspensionStrength = suspensionStrength;
Wheel->Friction = 3.f; Wheel->Friction = 4.f;
Wheel->ConnectedToHandbrake = true; Wheel->ConnectedToHandbrake = true;
Wheel->TorqueRatio = 0.125f; Wheel->TorqueRatio = 0.125f;
CommitEntity(wheel); CommitEntity(wheel);
@@ -394,7 +414,7 @@ void GameWorld::Initialize()
Wheel->Radius = 0.6f; Wheel->Radius = 0.6f;
Wheel->Steering = false; Wheel->Steering = false;
Wheel->SuspensionStrength = suspensionStrength; Wheel->SuspensionStrength = suspensionStrength;
Wheel->Friction = 3.f; Wheel->Friction = 4.f;
Wheel->ConnectedToHandbrake = true; Wheel->ConnectedToHandbrake = true;
Wheel->TorqueRatio = 0.125f; Wheel->TorqueRatio = 0.125f;
CommitEntity(wheel); CommitEntity(wheel);
@@ -414,7 +434,7 @@ void GameWorld::Initialize()
Wheel->Radius = 0.6f; Wheel->Radius = 0.6f;
Wheel->Steering = false; Wheel->Steering = false;
Wheel->SuspensionStrength = suspensionStrength; Wheel->SuspensionStrength = suspensionStrength;
Wheel->Friction = 3.f; Wheel->Friction = 4.f;
Wheel->ConnectedToHandbrake = true; Wheel->ConnectedToHandbrake = true;
Wheel->TorqueRatio = 0.125f; Wheel->TorqueRatio = 0.125f;
CommitEntity(wheel); CommitEntity(wheel);
@@ -432,7 +452,7 @@ void GameWorld::Initialize()
Wheel->Radius = 0.6f; Wheel->Radius = 0.6f;
Wheel->Steering = false; Wheel->Steering = false;
Wheel->SuspensionStrength = suspensionStrength; Wheel->SuspensionStrength = suspensionStrength;
Wheel->Friction = 3.f; Wheel->Friction = 4.f;
Wheel->ConnectedToHandbrake = true; Wheel->ConnectedToHandbrake = true;
Wheel->TorqueRatio = 0.125f; Wheel->TorqueRatio = 0.125f;
CommitEntity(wheel); CommitEntity(wheel);
@@ -573,11 +593,12 @@ void GameWorld::AddSystems()
AddSystem("RenderSystem"); AddSystem("RenderSystem");
} }
void GameWorld::BindKey(int keyCode, std::string command) void GameWorld::BindKey(int keyCode, std::string command, float value)
{ {
Events::BindKey e; Events::BindKey e;
e.KeyCode = keyCode; e.KeyCode = keyCode;
e.Command = command; e.Command = command;
e.Value = value;
m_EventBroker->Publish(e); m_EventBroker->Publish(e);
} }
+5 -1
View File
@@ -34,6 +34,10 @@
#include "Components/Vehicle.h" #include "Components/Vehicle.h"
#include "Components/Wheel.h" #include "Components/Wheel.h"
#include "Components/HingeConstraint.h" #include "Components/HingeConstraint.h"
#include "Components/TankSteering.h"
#include "Components/TowerSteering.h"
#include "Components/BarrelSteering.h"
class GameWorld : public World class GameWorld : public World
{ {
@@ -52,7 +56,7 @@ public:
private: private:
std::shared_ptr<Renderer> m_Renderer; std::shared_ptr<Renderer> m_Renderer;
void BindKey(int keyCode, std::string command); void BindKey(int keyCode, std::string command, float value);
void BindMouseButton(int button, std::string command); void BindMouseButton(int button, std::string command);
}; };
+4 -5
View File
@@ -104,7 +104,7 @@ void VehicleSetup::setupVehicleData(const hkpWorld* world, hkpVehicleData& data
data.m_torquePitchFactor = 0.5f; data.m_torquePitchFactor = 0.5f;
data.m_torqueYawFactor = 0.35f; data.m_torqueYawFactor = 0.35f;
data.m_chassisUnitInertiaYaw = 1.0f; data.m_chassisUnitInertiaYaw = 0.8f;
data.m_chassisUnitInertiaRoll = 1.0f; data.m_chassisUnitInertiaRoll = 1.0f;
data.m_chassisUnitInertiaPitch = 1.0f; data.m_chassisUnitInertiaPitch = 1.0f;
@@ -246,9 +246,8 @@ void VehicleSetup::setupComponent(const hkpVehicleData& data, hkpVehicleDefaultS
suspension.m_wheelParams[i].m_length = suspensionLength; suspension.m_wheelParams[i].m_length = suspensionLength;
suspension.m_wheelSpringParams[i].m_strength = m_Wheels[i].WheelComponent->SuspensionStrength; suspension.m_wheelSpringParams[i].m_strength = m_Wheels[i].WheelComponent->SuspensionStrength;
const float wd = 3.0f; suspension.m_wheelSpringParams[i].m_dampingCompression = vehicleComponent.SpringDamping;
suspension.m_wheelSpringParams[i].m_dampingCompression = wd; suspension.m_wheelSpringParams[i].m_dampingRelaxation = vehicleComponent.SpringDamping;
suspension.m_wheelSpringParams[i].m_dampingRelaxation = wd;
suspension.m_wheelParams[i].m_hardpointChassisSpace.set(m_Wheels[i].WheelComponent->Hardpoint.x, m_Wheels[i].WheelComponent->Hardpoint.y, m_Wheels[i].WheelComponent->Hardpoint.z); suspension.m_wheelParams[i].m_hardpointChassisSpace.set(m_Wheels[i].WheelComponent->Hardpoint.x, m_Wheels[i].WheelComponent->Hardpoint.y, m_Wheels[i].WheelComponent->Hardpoint.z);
@@ -285,7 +284,7 @@ void VehicleSetup::setupComponent(const hkpVehicleData& data, hkpVehicleDefaultV
// The threshold in m/s at which the algorithm switches from // The threshold in m/s at which the algorithm switches from
// using the normalSpinDamping to the collisionSpinDamping. // using the normalSpinDamping to the collisionSpinDamping.
velocityDamper.m_collisionThreshold = 100.0f; velocityDamper.m_collisionThreshold = 1.0f;
} }
void VehicleSetup::setupWheelCollide(const hkpWorld* world, const hkpVehicleInstance& vehicle, hkpVehicleRayCastWheelCollide& wheelCollide) void VehicleSetup::setupWheelCollide(const hkpWorld* world, const hkpVehicleInstance& vehicle, hkpVehicleRayCastWheelCollide& wheelCollide)
+15 -12
View File
@@ -44,7 +44,11 @@ bool Systems::InputSystem::OnKeyDown(const Events::KeyDown &event)
auto bindingIt = m_KeyBindings.find(event.KeyCode); auto bindingIt = m_KeyBindings.find(event.KeyCode);
if (bindingIt != m_KeyBindings.end()) if (bindingIt != m_KeyBindings.end())
{ {
PublishCommand(0, bindingIt->second, 1.f, false); std::string command;
float value;
std::tie(command, value) = bindingIt->second;
m_KeyBindingValues[command] += value;
PublishCommand(0, command, std::max(-1.f, std::min(m_KeyBindingValues[command], 1.f)));
} }
return true; return true;
@@ -55,7 +59,11 @@ bool Systems::InputSystem::OnKeyUp(const Events::KeyUp &event)
auto bindingIt = m_KeyBindings.find(event.KeyCode); auto bindingIt = m_KeyBindings.find(event.KeyCode);
if (bindingIt != m_KeyBindings.end()) if (bindingIt != m_KeyBindings.end())
{ {
PublishCommand(0, bindingIt->second, 1.f, true); std::string command;
float value;
std::tie(command, value) = bindingIt->second;
m_KeyBindingValues[command] -= value;
PublishCommand(0, command, std::max(-1.f, std::min(m_KeyBindingValues[command], 1.f)));
} }
return true; return true;
@@ -66,7 +74,7 @@ bool Systems::InputSystem::OnMousePress(const Events::MousePress &event)
auto bindingIt = m_MouseButtonBindings.find(event.Button); auto bindingIt = m_MouseButtonBindings.find(event.Button);
if (bindingIt != m_MouseButtonBindings.end()) if (bindingIt != m_MouseButtonBindings.end())
{ {
PublishCommand(0, bindingIt->second, 1.f, false); PublishCommand(0, bindingIt->second, 1.f);
} }
return true; return true;
@@ -77,7 +85,7 @@ bool Systems::InputSystem::OnMouseRelease(const Events::MouseRelease &event)
auto bindingIt = m_MouseButtonBindings.find(event.Button); auto bindingIt = m_MouseButtonBindings.find(event.Button);
if (bindingIt != m_MouseButtonBindings.end()) if (bindingIt != m_MouseButtonBindings.end())
{ {
PublishCommand(0, bindingIt->second, 1.f, true); PublishCommand(0, bindingIt->second, 1.f);
} }
return true; return true;
@@ -91,7 +99,7 @@ bool Systems::InputSystem::OnBindKey(const Events::BindKey &event)
} }
else else
{ {
m_KeyBindings[event.KeyCode] = event.Command; m_KeyBindings[event.KeyCode] = std::make_tuple(event.Command, event.Value);
LOG_DEBUG("Input: Bound key %c to %s", (char)event.KeyCode, event.Command.c_str()); LOG_DEBUG("Input: Bound key %c to %s", (char)event.KeyCode, event.Command.c_str());
} }
@@ -113,18 +121,13 @@ bool Systems::InputSystem::OnBindMouseButton(const Events::BindMouseButton &even
return true; return true;
} }
void Systems::InputSystem::PublishCommand(int playerID, std::string command, float value, bool release /*= false*/) void Systems::InputSystem::PublishCommand(int playerID, std::string command, float value)
{ {
if (release && command.at(0) == '+')
{
command[0] = '-';
}
Events::InputCommand e; Events::InputCommand e;
e.PlayerID = playerID; e.PlayerID = playerID;
e.Command = command; e.Command = command;
e.Value = value; e.Value = value;
EventBroker->Publish(e); EventBroker->Publish(e);
LOG_DEBUG("Input: Published command %s for player %i", e.Command.c_str(), playerID); LOG_DEBUG("Input: Published command %s=%f for player %i", e.Command.c_str(), e.Value, playerID);
} }
+4 -2
View File
@@ -3,6 +3,7 @@
#include <array> #include <array>
#include <unordered_map> #include <unordered_map>
#include <boost/any.hpp>
#include "System.h" #include "System.h"
#include "Components/Input.h" #include "Components/Input.h"
@@ -30,7 +31,8 @@ public:
private: private:
// Input binding tables // Input binding tables
std::unordered_map<int, std::string> m_KeyBindings; // GLFW_KEY... -> command string std::unordered_map<int, std::tuple<std::string, float>> m_KeyBindings; // GLFW_KEY... -> command string & value
std::unordered_map<std::string, float> m_KeyBindingValues; // command string -> command value
std::unordered_map<int, std::string> m_MouseButtonBindings; // GLFW_MOUSE_BUTTON... -> command string std::unordered_map<int, std::string> m_MouseButtonBindings; // GLFW_MOUSE_BUTTON... -> command string
// Input events // Input events
@@ -48,7 +50,7 @@ private:
EventRelay<Events::BindMouseButton> m_EBindMouseButton; EventRelay<Events::BindMouseButton> m_EBindMouseButton;
bool OnBindMouseButton(const Events::BindMouseButton &event); bool OnBindMouseButton(const Events::BindMouseButton &event);
void PublishCommand(int playerID, std::string command, float value, bool release = false); void PublishCommand(int playerID, std::string command, float value);
}; };
} }
+5 -6
View File
@@ -31,7 +31,7 @@ void Systems::PhysicsSystem::Initialize()
// Events // Events
EVENT_SUBSCRIBE_MEMBER(m_ETankSteer, &Systems::PhysicsSystem::OnTankSteer); EVENT_SUBSCRIBE_MEMBER(m_ETankSteer, &Systems::PhysicsSystem::OnTankSteer);
hkMemorySystem::FrameInfo finfo(6000 * 1024); // Allocate 6MB of Physics solver buffer hkMemorySystem::FrameInfo finfo(6000 * 1024); // Allocate 6MB of Physics solver buffer
hkMemoryRouter* memoryRouter = hkMemoryInitUtil::initDefault(hkMallocAllocator::m_defaultMallocAllocator, finfo); hkMemoryRouter* memoryRouter = hkMemoryInitUtil::initDefault(hkMallocAllocator::m_defaultMallocAllocator, finfo);
hkBaseSystem::init(memoryRouter, HavokErrorReport); hkBaseSystem::init(memoryRouter, HavokErrorReport);
@@ -115,7 +115,6 @@ void Systems::PhysicsSystem::RegisterComponents(ComponentFactory* cf)
cf->Register("MeshShape", []() { return new Components::MeshShape(); }); cf->Register("MeshShape", []() { return new Components::MeshShape(); });
cf->Register("HingeConstraint", []() { return new Components::HingeConstraint(); }); cf->Register("HingeConstraint", []() { return new Components::HingeConstraint(); });
cf->Register("WheelPair", []() { return new Components::WheelPair(); }); cf->Register("WheelPair", []() { return new Components::WheelPair(); });
} }
void Systems::PhysicsSystem::Update(double dt) void Systems::PhysicsSystem::Update(double dt)
@@ -550,7 +549,9 @@ glm::quat Systems::PhysicsSystem::ConvertRotation(const hkQuaternion &hkRotation
const hkQuaternion& Systems::PhysicsSystem::ConvertRotation(glm::quat glmRotation) const hkQuaternion& Systems::PhysicsSystem::ConvertRotation(glm::quat glmRotation)
{ {
return hkQuaternion(glmRotation.x, glmRotation.y, glmRotation.z, glmRotation.w); hkQuaternion quat = hkQuaternion(glmRotation.x, glmRotation.y, glmRotation.z, glmRotation.w);
quat.normalize();
return quat;
} }
glm::vec3 Systems::PhysicsSystem::ConvertScale(const hkVector4 &hkScale) glm::vec3 Systems::PhysicsSystem::ConvertScale(const hkVector4 &hkScale)
@@ -566,8 +567,7 @@ const hkVector4& Systems::PhysicsSystem::ConvertScale(glm::vec3 glmScale)
bool Systems::PhysicsSystem::OnTankSteer(const Events::TankSteer &event) bool Systems::PhysicsSystem::OnTankSteer(const Events::TankSteer &event)
{ {
auto vehicleComponent = m_World->GetComponent<Components::Vehicle>(event.Entity, "Vehicle"); auto vehicleComponent = m_World->GetComponent<Components::Vehicle>(event.Entity, "Vehicle");
auto inputComponent = m_World->GetComponent<Components::Input>(event.Entity, "Input"); if (vehicleComponent && m_Vehicles.find(event.Entity) != m_Vehicles.end() && m_RigidBodies.find(event.Entity) != m_RigidBodies.end())
if (vehicleComponent && inputComponent && m_Vehicles.find(event.Entity) != m_Vehicles.end() && m_RigidBodies.find(event.Entity) != m_RigidBodies.end())
{ {
m_PhysicsWorld->markForWrite(); m_PhysicsWorld->markForWrite();
hkpVehicleDriverInputAnalogStatus* deviceStatus = (hkpVehicleDriverInputAnalogStatus*)m_Vehicles[event.Entity]->m_deviceStatus; hkpVehicleDriverInputAnalogStatus* deviceStatus = (hkpVehicleDriverInputAnalogStatus*)m_Vehicles[event.Entity]->m_deviceStatus;
@@ -579,4 +579,3 @@ bool Systems::PhysicsSystem::OnTankSteer(const Events::TankSteer &event)
return true; return true;
} }
+1
View File
@@ -12,6 +12,7 @@
#include "Components/MeshShape.h" #include "Components/MeshShape.h"
#include "Components/HingeConstraint.h" #include "Components/HingeConstraint.h"
#include "Components/WheelPair.h" #include "Components/WheelPair.h"
#include "Components/TowerSteering.h"
#include "Events/TankSteer.h" #include "Events/TankSteer.h"
#include "OBJ.h" #include "OBJ.h"
+64 -39
View File
@@ -5,19 +5,20 @@
void Systems::TankSteeringSystem::RegisterComponents( ComponentFactory* cf ) void Systems::TankSteeringSystem::RegisterComponents( ComponentFactory* cf )
{ {
cf->Register("TankSteering", []() { return new Components::TankSteering(); }); cf->Register("TankSteering", []() { return new Components::TankSteering(); });
cf->Register("TowerSteering", []() { return new Components::TowerSteering(); });
cf->Register("BarrelSteering", []() { return new Components::BarrelSteering(); });
} }
void Systems::TankSteeringSystem::Initialize() void Systems::TankSteeringSystem::Initialize()
{ {
m_InputController = std::unique_ptr<TankSteeringInputController>(new TankSteeringInputController(EventBroker)); m_TankInputController = std::unique_ptr<TankSteeringInputController>(new TankSteeringInputController(EventBroker));
m_InputController->PositionX = 0; m_TowerInputController = std::unique_ptr<TowerSteeringInputController>(new TowerSteeringInputController(EventBroker));
m_InputController->PositionY = 0;
m_InputController->Handbrake = false;
} }
void Systems::TankSteeringSystem::Update(double dt) void Systems::TankSteeringSystem::Update(double dt)
{ {
m_TankInputController->Update(dt);
m_TowerInputController->Update(dt);
} }
void Systems::TankSteeringSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) void Systems::TankSteeringSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
@@ -27,61 +28,85 @@ void Systems::TankSteeringSystem::UpdateEntity(double dt, EntityID entity, Entit
{ {
Events::TankSteer e; Events::TankSteer e;
e.Entity = entity; e.Entity = entity;
e.PositionX = m_InputController->PositionX; e.PositionX = m_TankInputController->PositionX;
e.PositionY = m_InputController->PositionY; e.PositionY = m_TankInputController->PositionY;
e.Handbrake = m_InputController->Handbrake; e.Handbrake = m_TankInputController->Handbrake;
EventBroker->Publish(e); EventBroker->Publish(e);
} }
auto towerSteeringComponent = m_World->GetComponent<Components::TowerSteering>(entity, "TowerSteering");
if(towerSteeringComponent)
{
auto transformComponent = m_World->GetComponent<Components::Transform>(entity, "Transform");
glm::quat orientation = glm::angleAxis(towerSteeringComponent->Velocity * m_TowerInputController->TowerDirection * (float)dt, towerSteeringComponent->Axis);
transformComponent->Orientation *= orientation;
}
auto barrelSteeringComponent = m_World->GetComponent<Components::BarrelSteering>(entity, "BarrelSteering");
if(barrelSteeringComponent)
{
auto transformComponent = m_World->GetComponent<Components::Transform>(entity, "Transform");
glm::quat orientation = glm::angleAxis(barrelSteeringComponent->Velocity * m_TowerInputController->BarrelDirection * (float)dt, barrelSteeringComponent->Axis);
transformComponent->Orientation *= orientation;
}
}
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;
} }
bool Systems::TankSteeringSystem::TankSteeringInputController::OnCommand(const Events::InputCommand &event) bool Systems::TankSteeringSystem::TankSteeringInputController::OnCommand(const Events::InputCommand &event)
{ {
float val = boost::any_cast<float>(event.Value); float val = event.Value;
if (event.Command == "+right") if (event.Command == "horizontal")
{ {
PositionX += val; m_Horizontal = val;
} }
else if (event.Command == "-right") else if (event.Command == "vertical")
{ {
PositionX -= val; m_Vertical = val;
} }
else if (event.Command == "+left")
else if (event.Command == "handbrake")
{ {
PositionX += -val; Handbrake = val;
}
else if (event.Command == "-left")
{
PositionX -= -val;
}
else if (event.Command == "+forward")
{
PositionY += -val;
}
else if (event.Command == "-forward")
{
PositionY -= -val;
}
else if (event.Command == "+backward")
{
PositionY += val;
}
else if (event.Command == "-backward")
{
PositionY -= val;
} }
else if (event.Command == "+handbrake") return true;
}
bool Systems::TankSteeringSystem::TowerSteeringInputController::OnCommand( const Events::InputCommand &event )
{
float val = event.Value;
if(event.Command == "tower_rotation")
{ {
Handbrake = true; m_TowerDirection = val;
} }
else if (event.Command == "-handbrake") else if(event.Command == "barrel_rotation")
{ {
Handbrake = false; m_BarrelDirection = val;
} }
return true; return true;
} }
bool Systems::TankSteeringSystem::TowerSteeringInputController::OnMouseMove( const Events::MouseMove &event )
{
return false;
}
bool Systems::TankSteeringSystem::TankSteeringInputController::OnMouseMove( const Events::MouseMove &event ) bool Systems::TankSteeringSystem::TankSteeringInputController::OnMouseMove( const Events::MouseMove &event )
{ {
return false; return false;
} }
+42 -3
View File
@@ -4,6 +4,8 @@
#include "Events/TankSteer.h" #include "Events/TankSteer.h"
#include "Components/Transform.h" #include "Components/Transform.h"
#include "Components/TankSteering.h" #include "Components/TankSteering.h"
#include "Components/TowerSteering.h"
#include "Components/BarrelSteering.h"
#include "Components/Vehicle.h" #include "Components/Vehicle.h"
#include "InputController.h" #include "InputController.h"
@@ -24,22 +26,59 @@ namespace Systems
private: private:
class TankSteeringInputController; class TankSteeringInputController;
std::unique_ptr<TankSteeringInputController> m_InputController; std::unique_ptr<TankSteeringInputController> m_TankInputController;
class TowerSteeringInputController;
std::unique_ptr<TowerSteeringInputController> m_TowerInputController;
}; };
class TankSteeringSystem::TankSteeringInputController : InputController class TankSteeringSystem::TankSteeringInputController : InputController
{ {
public: public:
TankSteeringInputController(std::shared_ptr<::EventBroker> eventBroker) TankSteeringInputController(std::shared_ptr<::EventBroker> eventBroker)
: InputController(eventBroker) { } : InputController(eventBroker)
{
m_Horizontal = 0.f;
m_Vertical = 0.f;
PositionX = 0;
PositionY = 0;
Handbrake = false;
}
float PositionY; float PositionY;
float PositionX; float PositionX;
bool Handbrake; bool Handbrake;
void Update(double dt);
protected: protected:
virtual bool OnCommand(const Events::InputCommand &event); virtual bool OnCommand(const Events::InputCommand &event);
virtual bool OnMouseMove(const Events::MouseMove &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;
BarrelDirection = 0.f;
}
float TowerDirection;
float BarrelDirection;
void Update(double dt);
protected:
virtual bool OnCommand(const Events::InputCommand &event);
virtual bool OnMouseMove(const Events::MouseMove &event);
private:
float m_TowerDirection;
float m_BarrelDirection;
}; };
} }
+5
View File
@@ -61,6 +61,11 @@ public:
m_EntityProperties[entity][property] = value; m_EntityProperties[entity][property] = value;
} }
void SetProperty(EntityID entity, std::string property, char* value)
{
m_EntityProperties[entity][property] = std::string(value);
}
template <class T> template <class T>
std::shared_ptr<T> AddComponent(EntityID entity, std::string componentType); std::shared_ptr<T> AddComponent(EntityID entity, std::string componentType);
std::shared_ptr<Component> AddComponent(EntityID entity, std::string componentType); std::shared_ptr<Component> AddComponent(EntityID entity, std::string componentType);
+2
View File
@@ -124,6 +124,7 @@
<ClInclude Include="..\..\src\Camera.h" /> <ClInclude Include="..\..\src\Camera.h" />
<ClInclude Include="..\..\src\Color.h" /> <ClInclude Include="..\..\src\Color.h" />
<ClInclude Include="..\..\src\Component.h" /> <ClInclude Include="..\..\src\Component.h" />
<ClInclude Include="..\..\src\Components\BarrelSteering.h" />
<ClInclude Include="..\..\src\Components\BoxShape.h" /> <ClInclude Include="..\..\src\Components\BoxShape.h" />
<ClInclude Include="..\..\src\Components\Camera.h" /> <ClInclude Include="..\..\src\Components\Camera.h" />
<ClInclude Include="..\..\src\Components\DirectionalLight.h" /> <ClInclude Include="..\..\src\Components\DirectionalLight.h" />
@@ -141,6 +142,7 @@
<ClInclude Include="..\..\src\Components\Sprite.h" /> <ClInclude Include="..\..\src\Components\Sprite.h" />
<ClInclude Include="..\..\src\Components\TankSteering.h" /> <ClInclude Include="..\..\src\Components\TankSteering.h" />
<ClInclude Include="..\..\src\Components\Template.h" /> <ClInclude Include="..\..\src\Components\Template.h" />
<ClInclude Include="..\..\src\Components\TowerSteering.h" />
<ClInclude Include="..\..\src\Components\Transform.h" /> <ClInclude Include="..\..\src\Components\Transform.h" />
<ClInclude Include="..\..\src\Components\Vehicle.h" /> <ClInclude Include="..\..\src\Components\Vehicle.h" />
<ClInclude Include="..\..\src\Components\Wheel.h" /> <ClInclude Include="..\..\src\Components\Wheel.h" />
@@ -322,6 +322,12 @@
<ClInclude Include="..\..\src\Systems\TankSteeringSystem.h"> <ClInclude Include="..\..\src\Systems\TankSteeringSystem.h">
<Filter>Physics\Systems</Filter> <Filter>Physics\Systems</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\src\Components\TowerSteering.h">
<Filter>Physics\Components</Filter>
</ClInclude>
<ClInclude Include="..\..\src\Components\BarrelSteering.h">
<Filter>Physics\Components</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="..\..\src\Shaders\AABB.frag.glsl"> <None Include="..\..\src\Shaders\AABB.frag.glsl">