Vehicle kind of working.

This commit is contained in:
ViktorLjung
2014-04-11 23:54:00 +02:00
parent f2259f712d
commit d7e65ff118
8 changed files with 153 additions and 7 deletions
+18
View File
@@ -0,0 +1,18 @@
#ifndef Components_Vehicle_h__
#define Components_Vehicle_h__
#include "Component.h"
namespace Components
{
struct Vehicle : Component
{
};
}
#endif // Components_Vehicle_h__
+31
View File
@@ -0,0 +1,31 @@
#ifndef Components_Wheel_h__
#define Components_Wheel_h__
#include "Component.h"
namespace Components
{
struct Wheel : Component
{
EntityID CarID;
glm::vec3 ConnectionPoint;
glm::vec3 Direction;
glm::vec3 Axle;
float SuspensionRestLength;
float Radius;
bool IsFrontWheel;
float SuspensionStiffness;
float SuspensionCompression;
float SuspensionDamping;
float MaxSuspensionTravelCm;
float FrictionSlip;
float MaxSuspensionForce;
};
}
#endif // Components_Wheel_h__
+13 -5
View File
@@ -17,22 +17,25 @@ void GameWorld::Initialize()
{ auto terrain = CreateEntity();
auto transform = AddComponent<Components::Transform>(terrain, "Transform");
transform->Scale = glm::vec3(1000.0f);
transform->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, glm::pi<float>() / 20.f));
transform->Position = glm::vec3(0, -5, 0);
transform->Scale = glm::vec3(1000.0f, 1, 1000.0f);
transform->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, 0.0f));
auto model = AddComponent<Components::Model>(terrain, "Model");
model->ModelFile = "Models/Placeholders/PhysicsTest/Plane.obj";
auto physics = AddComponent<Components::Physics>(terrain, "Physics");
physics->Friction = 0.5f;
auto Box = AddComponent<Components::BoxShape>(terrain, "BoxShape");
Box->Width = 0.5f;
Box->Height = 0;
Box->Height = 0.5;
Box->Depth = 0.5f;
physics->Friction = 0.5f;
}
{
for (int i = 0; i < 1; i++)
{ auto entity = CreateEntity();
auto transform = AddComponent<Components::Transform>(entity, "Transform");
@@ -41,7 +44,6 @@ void GameWorld::Initialize()
transform->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, 0.0f));
auto model = AddComponent<Components::Model>(entity, "Model");
model->ModelFile = "Models/Placeholders/PhysicsTest/ArrowCube.obj";
auto physics = AddComponent<Components::Physics>(entity, "Physics");
physics->Mass = 10;
@@ -52,6 +54,9 @@ void GameWorld::Initialize()
{ auto entity1 = CreateEntity();
auto transform = AddComponent<Components::Transform>(entity1, "Transform");
transform->Scale = glm::vec3(1.0f);
transform->Position = glm::vec3(-5.f, 10+i, 0);
@@ -123,6 +128,9 @@ void GameWorld::RegisterComponents()
m_ComponentFactory.Register("HingeConstraint", []() { return new Components::HingeConstraint(); });
m_ComponentFactory.Register("BallSocketConstraint", []() { return new Components::BallSocketConstraint(); });
m_ComponentFactory.Register("SliderConstraint", []() { return new Components::SliderConstraint(); });
m_ComponentFactory.Register("Vehicle", []() { return new Components::Vehicle(); });
m_ComponentFactory.Register("Wheel", []() { return new Components::Wheel(); });
}
void GameWorld::RegisterSystems()
+3
View File
@@ -39,6 +39,9 @@
#include "Components/HingeConstraint.h"
#include "Components/SliderConstraint.h"
#include "Components/Vehicle.h"
#include "Components/Wheel.h"
class GameWorld : public World
{
public:
+62 -2
View File
@@ -11,7 +11,6 @@ Systems::PhysicsSystem::PhysicsSystem(World* world) : System(world)
m_Solver = new btSequentialImpulseConstraintSolver();
m_DynamicsWorld = new btDiscreteDynamicsWorld(m_Dispatcher, m_Broadphase, m_Solver, m_CollisionConfiguration);
m_DynamicsWorld->setGravity(btVector3(0, -9.82f, 0));
}
@@ -57,8 +56,10 @@ void Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID p
auto boxShapeComponent = m_World->GetComponent<Components::BoxShape>(entity, "BoxShape");
auto meshShapeComponent = m_World->GetComponent<Components::MeshShape>(entity, "MeshShape");
auto staticMeshShapeComponent = m_World->GetComponent<Components::StaticMeshShape>(entity, "StaticMeshShape");
auto vehicleComponent = m_World->GetComponent<Components::Vehicle>(entity, "Vehicle");
auto wheelComponent = m_World->GetComponent<Components::Wheel>(entity, "Wheel");
if (physicsComponent || sphereShapeComponent || boxShapeComponent || meshShapeComponent || staticMeshShapeComponent)
if (physicsComponent || sphereShapeComponent || boxShapeComponent || meshShapeComponent || staticMeshShapeComponent)
{ if (m_PhysicsData.find(entity) == m_PhysicsData.end())
{ SetUpPhysicsState(entity, parent);
}
@@ -99,6 +100,7 @@ void Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID p
m_Constraints[std::make_pair(entityA, entityB)] = new btPoint2PointConstraint(*m_PhysicsData[entityA].RigidBody, *m_PhysicsData[entityB].RigidBody, pivotA, pivotB);
m_DynamicsWorld->addConstraint(m_Constraints[std::make_pair(entityA, entityB)]);
}
}
}
@@ -141,6 +143,64 @@ void Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID p
}
}
if (vehicleComponent )
{
if (m_Vehicles.find(entity) == m_Vehicles.end()) {
if (m_PhysicsData.find(entity) == m_PhysicsData.end()){
LOG_WARNING("Chassi missing rigidbody on vehicle i%", entity);
return;
}
m_Vehicles[entity].VehicleRaycaster = new btDefaultVehicleRaycaster(m_DynamicsWorld);
m_Vehicles[entity].RaycastVehicle = new btRaycastVehicle(m_Vehicles[entity].Tuning, m_PhysicsData[entity].RigidBody, m_Vehicles[entity].VehicleRaycaster);
//m_Vehicles[entity].RaycastVehicle->setCoordinateSystem(0, 1, 2);
m_DynamicsWorld->addVehicle(m_Vehicles[entity].RaycastVehicle);
}
// for (int i = 0; i < m_Vehicles[entity].RaycastVehicle->getNumWheels(); i++)
// {
// m_Vehicles[entity].RaycastVehicle->applyEngineForce(10, i);
//
// }
}
if (wheelComponent)
{
EntityID carID = wheelComponent->CarID;
if (m_Vehicles.find(carID) != m_Vehicles.end()){
auto wheels = &m_Vehicles[carID].Wheels;
if (wheels->find(entity) == wheels->end())
{
wheels->insert(entity);
btVector3 connectionPoint = btVector3(wheelComponent->ConnectionPoint.x, wheelComponent->ConnectionPoint.y, wheelComponent->ConnectionPoint.z);
btVector3 direction = btVector3(wheelComponent->Direction.x, wheelComponent->Direction.y, wheelComponent->Direction.z);
btVector3 axle = btVector3(wheelComponent->Axle.x, wheelComponent->Axle.y, wheelComponent->Axle.z);
btWheelInfo& wheel = m_Vehicles[carID].RaycastVehicle->addWheel(connectionPoint, direction, axle, wheelComponent->SuspensionRestLength, wheelComponent->Radius, m_Vehicles[entity].Tuning, wheelComponent->IsFrontWheel);
wheel.m_suspensionStiffness = wheelComponent->SuspensionStiffness;
wheel.m_wheelsDampingRelaxation = wheelComponent->SuspensionDamping;
wheel.m_wheelsDampingCompression = wheelComponent->SuspensionCompression;
wheel.m_frictionSlip = wheelComponent->FrictionSlip;
wheel.m_rollInfluence = 0.1f;
}
}
}
}
void Systems::PhysicsSystem::OnComponentCreated(std::string type, std::shared_ptr<Component> component)
+18
View File
@@ -12,9 +12,12 @@
#include "Components/BallSocketConstraint.h"
#include "Components/HingeConstraint.h"
#include "Components/SliderConstraint.h"
#include "Components/Vehicle.h"
#include "Components/Wheel.h"
#include "btBulletDynamicsCommon.h"
#include "LinearMath/btMatrix3x3.h"
#include <unordered_set>
namespace Systems
{
@@ -35,6 +38,10 @@ private:
btSequentialImpulseConstraintSolver* m_Solver;
btDiscreteDynamicsWorld* m_DynamicsWorld;
//m_DynamicsWorld = new btDiscreteDynamicsWorld(m_Dispatcher, m_Broadphase, m_Solver, m_CollisionConfiguration);
struct PhysicsData
{ btRigidBody* RigidBody;
btMotionState* MotionState;
@@ -43,6 +50,17 @@ private:
std::map<EntityID, PhysicsData> m_PhysicsData;
std::map<std::pair<EntityID, EntityID>, btTypedConstraint*> m_Constraints;
struct Vehicle
{
btVehicleRaycaster* VehicleRaycaster;
btRaycastVehicle* RaycastVehicle;
btRaycastVehicle::btVehicleTuning Tuning;
std::unordered_set<EntityID> Wheels;
};
std::map<EntityID, Vehicle> m_Vehicles;
void SetUpPhysicsState(EntityID entity, EntityID parent);
void TearDownPhysicsState(EntityID entity, EntityID parent);
};
+2
View File
@@ -137,6 +137,8 @@
<ClInclude Include="..\..\src\Components\StaticMeshShape.h" />
<ClInclude Include="..\..\src\Components\Template.h" />
<ClInclude Include="..\..\src\Components\Transform.h" />
<ClInclude Include="..\..\src\Components\Vehicle.h" />
<ClInclude Include="..\..\src\Components\Wheel.h" />
<ClInclude Include="..\..\src\CubemapTexture.h" />
<ClInclude Include="..\..\src\Engine.h" />
<ClInclude Include="..\..\src\Entity.h" />
@@ -158,8 +158,14 @@
<Filter>Components</Filter>
</ClInclude>
<ClInclude Include="..\..\src\Components\MeshShape.h">
<Filter>Components</Filter>
</ClInclude>
<ClInclude Include="..\..\src\Components\Vehicle.h">
<Filter>Components</Filter>
</ClInclude>
<ClInclude Include="..\..\src\Components\Wheel.h">
<Filter>Components</Filter>
</ClInclude>
<ClInclude Include="..\..\src\Components\StaticMeshShape.h">
<Filter>Components</Filter>
</ClInclude>