Vehicle working, ready for entity-component implementation.

This commit is contained in:
ViktorLjung
2014-04-17 00:47:53 +02:00
parent 069d079594
commit aa64b7074c
4 changed files with 42 additions and 44 deletions
+27 -23
View File
@@ -22,14 +22,14 @@ void GameWorld::Initialize()
{ {
auto ground = CreateEntity(); auto ground = CreateEntity();
auto transform = AddComponent<Components::Transform>(ground, "Transform"); auto transform = AddComponent<Components::Transform>(ground, "Transform");
transform->Position = glm::vec3(0, 0, 0); transform->Position = glm::vec3(0, -5, 0);
transform->Scale = glm::vec3(1000.0f, 1.0f, 1000.0f); transform->Scale = glm::vec3(1000.0f, 10.0f, 1000.0f);
transform->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, 0.0f)); transform->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, 0.0f));
auto model = AddComponent<Components::Model>(ground, "Model"); auto model = AddComponent<Components::Model>(ground, "Model");
model->ModelFile = "Models/Placeholders/PhysicsTest/Cube.obj"; model->ModelFile = "Models/Placeholders/PhysicsTest/Cube.obj";
auto box = AddComponent<Components::Box>(ground, "Box"); auto box = AddComponent<Components::Box>(ground, "Box");
box->Width = 500; box->Width = 500;
box->Height = 0.5; box->Height = 5;
box->Depth = 500; box->Depth = 500;
auto physics = AddComponent<Components::Physics>(ground, "Physics"); auto physics = AddComponent<Components::Physics>(ground, "Physics");
@@ -37,38 +37,42 @@ void GameWorld::Initialize()
physics->Static = true; physics->Static = true;
} }
for(int i = 0; i < 1; i++)
{
auto ball = CreateEntity();
auto transform = AddComponent<Components::Transform>(ball, "Transform");
transform->Position = glm::vec3(0, 5, 0);
transform->Scale = glm::vec3(1.0f, 1.0f, 1.0f);
transform->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, 0.0f));
auto model = AddComponent<Components::Model>(ball, "Model");
model->ModelFile = "Models/Placeholders/PhysicsTest/Sphere.obj";
auto sphere = AddComponent<Components::Sphere>(ball, "Sphere");
sphere->Radius = 0.5;
auto physics = AddComponent<Components::Physics>(ball, "Physics");
physics->Mass = 1;
}
{ {
auto car = CreateEntity(); auto car = CreateEntity();
auto transform = AddComponent<Components::Transform>(car, "Transform"); auto transform = AddComponent<Components::Transform>(car, "Transform");
transform->Position = glm::vec3(0, 5, 0); transform->Position = glm::vec3(0, 5, 0);
transform->Scale = glm::vec3(1.0f, 1.0f, 1.0f); transform->Scale = glm::vec3(3, 1, 5);
transform->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, 0.0f)); transform->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, 0.0f));
auto model = AddComponent<Components::Model>(car, "Model"); auto model = AddComponent<Components::Model>(car, "Model");
model->ModelFile = "Models/Placeholders/PhysicsTest/Box.obj"; model->ModelFile = "Models/Placeholders/PhysicsTest/Cube2.obj";
auto physics = AddComponent<Components::Physics>(car, "Physics"); auto physics = AddComponent<Components::Physics>(car, "Physics");
physics->Mass = 1; physics->Mass = 1200;
auto box = AddComponent<Components::Box>(car, "Box"); auto box = AddComponent<Components::Box>(car, "Box");
box->Width = 3; box->Width = 1.5f;
box->Height = 1; box->Height = 0.5f;
box->Depth = 5; box->Depth = 2.5f;
auto vehicle = AddComponent<Components::Vehicle>(car, "Vehicle"); auto vehicle = AddComponent<Components::Vehicle>(car, "Vehicle");
} }
for(int i = 0; i < 50; i++)
{
auto ball = CreateEntity();
auto transform = AddComponent<Components::Transform>(ball, "Transform");
transform->Position = glm::vec3(243, 10 + i*10, 0);
transform->Scale = glm::vec3(10);
transform->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, 0.0f));
auto model = AddComponent<Components::Model>(ball, "Model");
model->ModelFile = "Models/Placeholders/PhysicsTest/Cube2.obj";
auto physics = AddComponent<Components::Physics>(ball, "Physics");
physics->Mass = 100;
auto box = AddComponent<Components::Box>(ball, "Box");
box->Width = 5.f;
box->Height = 5.f;
box->Depth = 5.f;
}
/*{ /*{
auto entity = CreateEntity(); auto entity = CreateEntity();
AddComponent(entity, "Transform"); AddComponent(entity, "Transform");
+6 -15
View File
@@ -21,12 +21,10 @@ void VehicleSetup::buildVehicle(const hkpWorld* world, hkpVehicleInstance& vehic
// For illustrative purposes we use a custom hkpVehicleRayCastWheelCollide // For illustrative purposes we use a custom hkpVehicleRayCastWheelCollide
// which implements varying 'ground' friction in a very simple way. // which implements varying 'ground' friction in a very simple way.
//vehicle.m_wheelCollide = new hkpVehicleRayCastWheelCollide; vehicle.m_wheelCollide = new hkpVehicleRayCastWheelCollide;
setupVehicleData(world, *vehicle.m_data); setupVehicleData(world, *vehicle.m_data);
// initialise the tyremarks controller with 128 tyremark points.
vehicle.m_tyreMarks = new hkpTyremarksInfo(*vehicle.m_data, 128);
setupComponent(*vehicle.m_data, *static_cast<hkpVehicleDefaultAnalogDriverInput*>(vehicle.m_driverInput)); setupComponent(*vehicle.m_data, *static_cast<hkpVehicleDefaultAnalogDriverInput*>(vehicle.m_driverInput));
setupComponent(*vehicle.m_data, *static_cast<hkpVehicleDefaultSteering*>(vehicle.m_steering)); setupComponent(*vehicle.m_data, *static_cast<hkpVehicleDefaultSteering*>(vehicle.m_steering));
@@ -39,7 +37,6 @@ void VehicleSetup::buildVehicle(const hkpWorld* world, hkpVehicleInstance& vehic
setupWheelCollide(world, vehicle, *static_cast<hkpVehicleRayCastWheelCollide*>(vehicle.m_wheelCollide)); setupWheelCollide(world, vehicle, *static_cast<hkpVehicleRayCastWheelCollide*>(vehicle.m_wheelCollide));
setupTyremarks(*vehicle.m_data, *static_cast<hkpTyremarksInfo*>(vehicle.m_tyreMarks));
// //
// Check that all components are present. // Check that all components are present.
@@ -53,7 +50,6 @@ void VehicleSetup::buildVehicle(const hkpWorld* world, hkpVehicleInstance& vehic
HK_ASSERT(0x7a7ade23, vehicle.m_suspension); HK_ASSERT(0x7a7ade23, vehicle.m_suspension);
HK_ASSERT(0x6ec4d0ed, vehicle.m_aerodynamics); HK_ASSERT(0x6ec4d0ed, vehicle.m_aerodynamics);
HK_ASSERT(0x67161206, vehicle.m_wheelCollide); HK_ASSERT(0x67161206, vehicle.m_wheelCollide);
HK_ASSERT(0x295015f1, vehicle.m_tyreMarks);
// //
// Set up any variables that store cached data. // Set up any variables that store cached data.
@@ -92,7 +88,7 @@ void VehicleSetup::setupVehicleData(const hkpWorld* world, hkpVehicleData& data)
// The coordinates of the chassis system, used for steering the vehicle. // The coordinates of the chassis system, used for steering the vehicle.
// up forward right // up forward right
data.m_chassisOrientation.setCols(hkVector4(0, 1, 0), hkVector4(1, 0, 0), hkVector4(0, 0, 1)); data.m_chassisOrientation.setCols(hkVector4(0, 1, 0), hkVector4(0, 0, 1), hkVector4(-1, 0, 0));
data.m_frictionEqualizer = 0.5f; data.m_frictionEqualizer = 0.5f;
@@ -138,8 +134,8 @@ void VehicleSetup::setupVehicleData(const hkpWorld* world, hkpVehicleData& data)
for (int i = 0; i < data.m_numWheels; i++) for (int i = 0; i < data.m_numWheels; i++)
{ {
// This value is also used to calculate the m_primaryTransmissionRatio. // This value is also used to calculate the m_primaryTransmissionRatio.
data.m_wheelParams[i].m_radius = 0.4f; data.m_wheelParams[i].m_radius = 0.6f;
data.m_wheelParams[i].m_width = 0.2f; data.m_wheelParams[i].m_width = 0.3f;
data.m_wheelParams[i].m_mass = 10.0f; data.m_wheelParams[i].m_mass = 10.0f;
data.m_wheelParams[i].m_viscosityFriction = 0.25f; data.m_wheelParams[i].m_viscosityFriction = 0.25f;
@@ -215,8 +211,8 @@ void VehicleSetup::setupComponent(const hkpVehicleData& data, hkpVehicleDefaultT
transmission.m_wheelsTorqueRatio[2] = 0.3f; transmission.m_wheelsTorqueRatio[2] = 0.3f;
transmission.m_wheelsTorqueRatio[3] = 0.3f; transmission.m_wheelsTorqueRatio[3] = 0.3f;
const hkReal vehicleTopSpeed = 130.0f; const hkReal vehicleTopSpeed = 50.0f;
const hkReal wheelRadius = 0.4f; const hkReal wheelRadius = 0.6f;
const hkReal maxEngineRpm = 7500.0f; const hkReal maxEngineRpm = 7500.0f;
transmission.m_primaryTransmissionRatio = hkpVehicleDefaultTransmission::calculatePrimaryTransmissionRatio(vehicleTopSpeed, transmission.m_primaryTransmissionRatio = hkpVehicleDefaultTransmission::calculatePrimaryTransmissionRatio(vehicleTopSpeed,
wheelRadius, wheelRadius,
@@ -332,8 +328,3 @@ void VehicleSetup::setupWheelCollide(const hkpWorld* world, const hkpVehicleInst
wheelCollide.m_wheelCollisionFilterInfo = vehicle.getChassis()->getCollisionFilterInfo(); wheelCollide.m_wheelCollisionFilterInfo = vehicle.getChassis()->getCollisionFilterInfo();
} }
void VehicleSetup::setupTyremarks(const hkpVehicleData& data, hkpTyremarksInfo& tyreMarks)
{
tyreMarks.m_minTyremarkEnergy = 100.0f;
tyreMarks.m_maxTyremarkEnergy = 1000.0f;
}
-2
View File
@@ -24,7 +24,6 @@
#include <Physics2012/Vehicle/Suspension/Default/hkpVehicleDefaultSuspension.h> #include <Physics2012/Vehicle/Suspension/Default/hkpVehicleDefaultSuspension.h>
#include <Physics2012/Vehicle/Transmission/Default/hkpVehicleDefaultTransmission.h> #include <Physics2012/Vehicle/Transmission/Default/hkpVehicleDefaultTransmission.h>
#include <Physics2012/Vehicle/WheelCollide/RayCast/hkpVehicleRayCastWheelCollide.h> #include <Physics2012/Vehicle/WheelCollide/RayCast/hkpVehicleRayCastWheelCollide.h>
#include <Physics2012/Vehicle/TyreMarks/hkpTyremarksInfo.h>
#include <Physics2012/Vehicle/WheelCollide/RayCast/hkpVehicleRayCastWheelCollide.h> #include <Physics2012/Vehicle/WheelCollide/RayCast/hkpVehicleRayCastWheelCollide.h>
@@ -48,7 +47,6 @@ public:
virtual void setupComponent(const hkpVehicleData& data, hkpVehicleDefaultVelocityDamper& velocityDamper); virtual void setupComponent(const hkpVehicleData& data, hkpVehicleDefaultVelocityDamper& velocityDamper);
virtual void setupWheelCollide(const hkpWorld* world, const hkpVehicleInstance& vehicle, hkpVehicleRayCastWheelCollide& wheelCollide); virtual void setupWheelCollide(const hkpWorld* world, const hkpVehicleInstance& vehicle, hkpVehicleRayCastWheelCollide& wheelCollide);
virtual void setupTyremarks(const hkpVehicleData& data, hkpTyremarksInfo& tyremarkscontroller);
}; };
#endif // Physics_Vehicle_h__ #endif // Physics_Vehicle_h__
+9 -4
View File
@@ -75,13 +75,17 @@ void Systems::PhysicsSystem::Update(double dt)
if (!transformComponent) if (!transformComponent)
continue; continue;
hkVector4 position(transformComponent->Position.x, transformComponent->Position.y, transformComponent->Position.z); hkVector4 position(transformComponent->Position.x, transformComponent->Position.y, transformComponent->Position.z);
hkQuaternion rotation(transformComponent->Orientation.x, transformComponent->Orientation.y, transformComponent->Orientation.z, transformComponent->Orientation.w); hkQuaternion rotation(transformComponent->Orientation.x, transformComponent->Orientation.y, transformComponent->Orientation.z, transformComponent->Orientation.w);
m_RigidBodies[entity]->setPositionAndRotation(position, rotation); m_RigidBodies[entity]->setPositionAndRotation(position, rotation);
} }
static const double timestep = 1 / 60.0;
static const double timestep = 1 / 30.0;
m_Accumulator += dt; m_Accumulator += dt;
while (m_Accumulator >= timestep) while (m_Accumulator >= timestep)
{ {
@@ -184,17 +188,18 @@ void Systems::PhysicsSystem::SetUpPhysicsState(EntityID entity, EntityID parent)
{ {
VehicleSetup vehicleSetup; VehicleSetup vehicleSetup;
// Create the vehicle.
m_Vehicles[entity] = new hkpVehicleInstance(rigidBody);
// Create the basic vehicle. // Create the basic vehicle.
m_Vehicles[entity] = new hkpVehicleInstance(rigidBody); m_Vehicles[entity] = new hkpVehicleInstance(rigidBody);
vehicleSetup.buildVehicle(m_PhysicsWorld, *m_Vehicles[entity]); vehicleSetup.buildVehicle(m_PhysicsWorld, *m_Vehicles[entity]);
// Add the vehicle's entities and phantoms to the world // Add the vehicle's entities and phantoms to the world
m_Vehicles[entity]->addToWorld(m_PhysicsWorld); m_Vehicles[entity]->addToWorld(m_PhysicsWorld);
m_RigidBodies[entity] = rigidBody;
// The vehicle is an action // The vehicle is an action
m_PhysicsWorld->addAction(m_Vehicles[entity]); m_PhysicsWorld->addAction(m_Vehicles[entity]);
m_Vehicles[entity]->m_rpm = 0.0f; // Not sure why this one should be here //m_Vehicles[entity]->m_rpm = 0.0f; // Not sure why this one should be here
shape->removeReference(); shape->removeReference();
rigidBody->removeReference(); rigidBody->removeReference();