From d7e5630f1b3d09386b0221905e4ab5c703696bea Mon Sep 17 00:00:00 2001 From: ViktorLjung Date: Sun, 11 May 2014 01:52:00 +0200 Subject: [PATCH] Tank with 8 wheels --- assets | 2 +- src/Components/Wheel.h | 4 +- src/Components/WheelPair.h | 16 ++ src/GameWorld.cpp | 269 ++++++++++++++++-- src/Physics/VehicleSetup.cpp | 30 +- src/Physics/VehicleSetup.h | 4 +- src/Systems/PhysicsSystem.cpp | 91 ++++-- src/Systems/PhysicsSystem.h | 1 + vs11/Returngeance/Returngeance.vcxproj | 1 + .../Returngeance/Returngeance.vcxproj.filters | 3 + 10 files changed, 357 insertions(+), 64 deletions(-) create mode 100644 src/Components/WheelPair.h diff --git a/assets b/assets index 672e8a2..8b6c48b 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit 672e8a2b11ecaafc95a5b0286b5ec310c62438ad +Subproject commit 8b6c48b26b3bbc10f5e66c1f59fec6ca935f641b diff --git a/src/Components/Wheel.h b/src/Components/Wheel.h index d4917db..533d193 100644 --- a/src/Components/Wheel.h +++ b/src/Components/Wheel.h @@ -14,7 +14,7 @@ struct Wheel : Component Wheel() : AxleID(0), Radius(0), Width(0), Mass(0), Steering(false), DownDirection(glm::vec3(0, -1, 0)), Friction(1.5f), SlipAngle(0.0f), - MaxBreakingTorque(1500.0f), ConnectedToHandbrake(false), SuspensionStrength(50.0f) { } + MaxBreakingTorque(1500.0f), ConnectedToHandbrake(false), SuspensionStrength(50.0f), TorqueRatio(0.25f) { } // The Hardpoint MUST be positioned INSIDE the chassis. glm::vec3 Hardpoint; @@ -30,6 +30,8 @@ struct Wheel : Component float SlipAngle; float MaxBreakingTorque; bool ConnectedToHandbrake; + // The wheels total TorqueRatio must be equal to 1 + float TorqueRatio; private: int ID; diff --git a/src/Components/WheelPair.h b/src/Components/WheelPair.h new file mode 100644 index 0000000..0173e30 --- /dev/null +++ b/src/Components/WheelPair.h @@ -0,0 +1,16 @@ +#ifndef Components_WheelPair_h__ +#define Components_WheelPair_h__ + +#include "Component.h" + +namespace Components +{ + + struct WheelPair : Component + { + // Flag for pair wheels + }; + +} + +#endif // Components_WheelPair_h__ diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index 8c36206..ad840a9 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -21,7 +21,7 @@ void GameWorld::Initialize() transform->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, 0.0f)); auto model = AddComponent(ground, "Model"); //model->ModelFile = "Models/TestScene/testScene.obj"; - model->ModelFile = "Models/Placeholders/Terrain/Terrain.obj"; + model->ModelFile = "Models/Placeholders/Terrain/Terrain2.obj"; auto physics = AddComponent(ground, "Physics"); physics->Mass = 10; @@ -31,7 +31,7 @@ void GameWorld::Initialize() auto groundshape = CreateEntity(ground); auto transformshape = AddComponent(groundshape, "Transform"); auto meshShape = AddComponent(groundshape, "MeshShape"); - meshShape->ResourceName = "Models/Placeholders/Terrain/Terrain.obj"; + meshShape->ResourceName = "Models/Placeholders/Terrain/Terrain2.obj"; //meshShape->ResourceName = "Models/TestScene/testScene.obj"; @@ -52,26 +52,31 @@ void GameWorld::Initialize() CommitEntity(camera); } - { + /*{ auto jeep = CreateEntity(); auto transform = AddComponent(jeep, "Transform"); transform->Position = glm::vec3(0, 5, 0); transform->Orientation = glm::angleAxis(glm::pi()/2, glm::vec3(0, 1, 0)); auto physics = AddComponent(jeep, "Physics"); physics->Mass = 1800; - -// auto box = AddComponent(jeep, "Box"); -// box->Width = 1.487f; -// box->Height = 0.727f; -// box->Depth = 2.594f; - - auto meshShape = AddComponent(jeep, "MeshShape"); - meshShape->ResourceName = "Models/Jeep/Chassi/ChassiCollision.obj"; - + physics->Static = false; auto vehicle = AddComponent(jeep, "Vehicle"); - AddComponent(jeep, "Input"); + { + auto shape = CreateEntity(jeep); + auto transform = AddComponent(shape, "Transform"); + auto meshShape = AddComponent(shape, "MeshShape"); + meshShape->ResourceName = "Models/Jeep/Chassi/ChassiCollision.obj"; + CommitEntity(shape); + + // auto box = AddComponent(jeep, "Box"); + // box->Width = 1.487f; + // box->Height = 0.727f; + // box->Depth = 2.594f; + + } + { auto chassis = CreateEntity(jeep); auto transform = AddComponent(chassis, "Transform"); @@ -174,6 +179,236 @@ void GameWorld::Initialize() } CommitEntity(jeep); + }*/ + + + { + auto tank = CreateEntity(); + auto transform = AddComponent(tank, "Transform"); + transform->Position = glm::vec3(0, 5, 0); + transform->Orientation = glm::angleAxis(glm::pi()/2, glm::vec3(0, 1, 0)); + auto physics = AddComponent(tank, "Physics"); + physics->Mass = 45000; + physics->Static = false; + auto vehicle = AddComponent(tank, "Vehicle"); + vehicle->MaxTorque = 5200.f; + + AddComponent(tank, "Input"); + + { + auto shape = CreateEntity(tank); + auto transform = AddComponent(shape, "Transform"); + auto meshShape = AddComponent(shape, "MeshShape"); + meshShape->ResourceName = "Models/Tank/Fix/ChassiCollision.obj"; + CommitEntity(shape); + + // auto box = AddComponent(jeep, "Box"); + // box->Width = 1.487f; + // box->Height = 0.727f; + // box->Depth = 2.594f; + + } + + { + auto chassis = CreateEntity(tank); + auto transform = AddComponent(chassis, "Transform"); + transform->Position = glm::vec3(0, 0, 0); // 0.6577f + auto model = AddComponent(chassis, "Model"); + model->ModelFile = "Models/Tank/Fix/Chassi.obj"; + } + { + auto top = CreateEntity(tank); + auto transform = AddComponent(top, "Transform"); + transform->Position = glm::vec3(0, 1.2, 1.95); // 0.6577f + auto model = AddComponent(top, "Model"); + model->ModelFile = "Models/Tank/Fix/Top.obj"; + + { + auto top = CreateEntity(tank); + auto transform = AddComponent(top, "Transform"); + transform->Position = glm::vec3(0, 1, 0.5); // 0.6577f + auto model = AddComponent(top, "Model"); + model->ModelFile = "Models/Tank/Fix/Barrel.obj"; + } + } + + { + auto lightentity = CreateEntity(tank); + auto transform = AddComponent(lightentity, "Transform"); + transform->Position = glm::vec3(0, 15, 0); + auto light = AddComponent(lightentity, "PointLight"); + light->Diffuse = glm::vec3(128.f/255.f, 172.f/255.f, 242.f/255.f); + light->Specular = glm::vec3(1.f); + light->constantAttenuation = 0.3f; + light->linearAttenuation = 0.003f; + light->quadraticAttenuation = 0.002f; + } + +// auto wheelpair = CreateEntity(tank); +// SetProperty(wheelpair, "Name", "WheelPair"); +// AddComponent(wheelpair, "WheelPairThingy"); + + //Create wheels + float wheelOffset = 0.4f; + float springLength = 0.3f; + float suspensionStrength = 25.f; + + { + auto wheel = CreateEntity(tank); + auto transform = AddComponent(wheel, "Transform"); + transform->Position = glm::vec3(1.88f, -0.83f - wheelOffset, -2.6f); + transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 1, 0)); + auto model = AddComponent(wheel, "Model"); + model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; + auto Wheel = AddComponent(wheel, "Wheel"); + Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); + Wheel->AxleID = 0; + Wheel->Mass = 2000; + Wheel->Radius = 0.6f; + Wheel->Steering = true; + Wheel->SuspensionStrength = suspensionStrength; + Wheel->Friction = 3.f; + Wheel->ConnectedToHandbrake = true; + Wheel->TorqueRatio = 0.125f; + CommitEntity(wheel); + } + { + auto wheel = CreateEntity(tank); + auto transform = AddComponent(wheel, "Transform"); + transform->Position = glm::vec3(1.88f, -0.83f - wheelOffset, -0.83f); + transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 1, 0)); + auto model = AddComponent(wheel, "Model"); + model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; + auto Wheel = AddComponent(wheel, "Wheel"); + Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); + Wheel->AxleID = 0; + Wheel->Mass = 2000; + Wheel->Radius = 0.6f; + Wheel->Steering = false; + Wheel->SuspensionStrength = suspensionStrength; + Wheel->Friction = 3.f; + Wheel->ConnectedToHandbrake = true; + Wheel->TorqueRatio = 0.125f; + CommitEntity(wheel); + } + + { + auto wheel = CreateEntity(tank); + auto transform = AddComponent(wheel, "Transform"); + transform->Position = glm::vec3(-1.88f, -0.83f - wheelOffset, -2.6f); + transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 1, 0)); + auto model = AddComponent(wheel, "Model"); + model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; + auto Wheel = AddComponent(wheel, "Wheel"); + Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); + Wheel->AxleID = 0; + Wheel->Mass = 2000; + Wheel->Radius = 0.6f; + Wheel->Steering = true; + Wheel->SuspensionStrength = suspensionStrength; + Wheel->Friction = 3.f; + Wheel->ConnectedToHandbrake = true; + Wheel->TorqueRatio = 0.125f; + CommitEntity(wheel); + } + { + auto wheel = CreateEntity(tank); + auto transform = AddComponent(wheel, "Transform"); + transform->Position = glm::vec3(-1.88f, -0.83f - wheelOffset, -0.83f); + transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 1, 0)); + auto model = AddComponent(wheel, "Model"); + model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; + auto Wheel = AddComponent(wheel, "Wheel"); + Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); + Wheel->AxleID = 0; + Wheel->Mass = 2000; + Wheel->Radius = 0.6f; + Wheel->Steering = false; + Wheel->SuspensionStrength = suspensionStrength; + Wheel->Friction = 3.f; + Wheel->ConnectedToHandbrake = true; + Wheel->TorqueRatio = 0.125f; + CommitEntity(wheel); + } + + + //Back + { + auto wheel = CreateEntity(tank); + auto transform = AddComponent(wheel, "Transform"); + transform->Position = glm::vec3(1.88f, -0.83f - wheelOffset, 1.f); + auto model = AddComponent(wheel, "Model"); + model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; + auto Wheel = AddComponent(wheel, "Wheel"); + Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); + Wheel->AxleID = 1; + Wheel->Mass = 2000; + Wheel->Radius = 0.6f; + Wheel->Steering = false; + Wheel->SuspensionStrength = suspensionStrength; + Wheel->Friction = 3.f; + Wheel->ConnectedToHandbrake = true; + Wheel->TorqueRatio = 0.125f; + CommitEntity(wheel); + } + { + auto wheel = CreateEntity(tank); + auto transform = AddComponent(wheel, "Transform"); + transform->Position = glm::vec3(1.88f, -0.83f - wheelOffset, 2.95f); + auto model = AddComponent(wheel, "Model"); + model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; + auto Wheel = AddComponent(wheel, "Wheel"); + Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); + Wheel->AxleID = 1; + Wheel->Mass = 2000; + Wheel->Radius = 0.6f; + Wheel->Steering = false; + Wheel->SuspensionStrength = suspensionStrength; + Wheel->Friction = 3.f; + Wheel->ConnectedToHandbrake = true; + Wheel->TorqueRatio = 0.125f; + CommitEntity(wheel); + } + + { + auto wheel = CreateEntity(tank); + auto transform = AddComponent(wheel, "Transform"); + transform->Position = glm::vec3(-1.88f, -0.83f - wheelOffset, 1.f); + transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 1, 0)); + auto model = AddComponent(wheel, "Model"); + model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; + auto Wheel = AddComponent(wheel, "Wheel"); + Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); + Wheel->AxleID = 1; + Wheel->Mass = 2000; + Wheel->Radius = 0.6f; + Wheel->Steering = false; + Wheel->SuspensionStrength = suspensionStrength; + Wheel->Friction = 3.f; + Wheel->ConnectedToHandbrake = true; + Wheel->TorqueRatio = 0.125f; + CommitEntity(wheel); + } + { + auto wheel = CreateEntity(tank); + auto transform = AddComponent(wheel, "Transform"); + transform->Position = glm::vec3(-1.88f, -0.83f - wheelOffset, 2.95f); + auto model = AddComponent(wheel, "Model"); + model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; + auto Wheel = AddComponent(wheel, "Wheel"); + Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); + Wheel->AxleID = 1; + Wheel->Mass = 2000; + Wheel->Radius = 0.6f; + Wheel->Steering = false; + Wheel->SuspensionStrength = suspensionStrength; + Wheel->Friction = 3.f; + Wheel->ConnectedToHandbrake = true; + Wheel->TorqueRatio = 0.125f; + CommitEntity(wheel); + } + + CommitEntity(tank); } /* @@ -202,18 +437,13 @@ void GameWorld::Initialize() for(int i = 0; i < 1; i++) { - auto wall = CreateEntity(); - auto transform = AddComponent(wall, "Transform"); - transform->Position = glm::vec3(10, 0, -20 + (-10 * i)); - //transform->Orientation = glm::angleAxis(glm::pi()/2.f, glm::vec3(0, 1, 0)); - for (int y = 0; y < 15; y++) { for (int x = -5; x < 5; x++) { auto brick = CreateEntity(); auto transform = AddComponent(brick, "Transform"); - transform->Position = glm::vec3(x + 0.01f, y * 0.3f + 0.01f, 0); + transform->Position = glm::vec3(x + 0.01f, y * 0.3f + 0.01f, -20); transform->Position.x += (y % 2)*0.5f; transform->Scale = glm::vec3(1, 0.3f, 0.4f); transform->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, 0.0f)); @@ -235,7 +465,6 @@ void GameWorld::Initialize() CommitEntity(brick); } } - CommitEntity(wall); } /*for (int x = 0; x < 5; x++) diff --git a/src/Physics/VehicleSetup.cpp b/src/Physics/VehicleSetup.cpp index a309824..1babfcb 100644 --- a/src/Physics/VehicleSetup.cpp +++ b/src/Physics/VehicleSetup.cpp @@ -197,23 +197,21 @@ void VehicleSetup::setupComponent(const hkpVehicleData& data, hkpVehicleDefaultT transmission.m_gearsRatio.setSize(numberOfGears); transmission.m_wheelsTorqueRatio.setSize(data.m_numWheels); - transmission.m_downshiftRPM = 1500.0f; - transmission.m_upshiftRPM = 3500.0f; + transmission.m_downshiftRPM = 3500.0f; //HACK: Should be in VehicleComponent + transmission.m_upshiftRPM = 7000.0f; transmission.m_clutchDelayTime = 0.0f; - transmission.m_reverseGearRatio = 1.0f; - transmission.m_gearsRatio[0] = 2.0f; - transmission.m_gearsRatio[1] = 1.5f; - transmission.m_gearsRatio[2] = 1.0f; - transmission.m_gearsRatio[3] = 0.75f; - transmission.m_wheelsTorqueRatio[0] = 0.2f; - transmission.m_wheelsTorqueRatio[1] = 0.2f; - transmission.m_wheelsTorqueRatio[2] = 0.3f; - transmission.m_wheelsTorqueRatio[3] = 0.3f; - - //transmission.m_wheelsTorqueRatio[4] = 0.1f; - //transmission.m_wheelsTorqueRatio[5] = 0.1f; - // HACK: fix support for more than 4 wheels, m_wheelsTorqueRatio must equal 1 for all wheels + transmission.m_reverseGearRatio = 1.2f; + transmission.m_gearsRatio[0] = 3.0f; + transmission.m_gearsRatio[1] = 2.25f; + transmission.m_gearsRatio[2] = 1.5f; + transmission.m_gearsRatio[3] = 1.0f; + + for(int i = 0; i < m_Wheels.size(); i++) + { + // The wheels total TorqueRatio must be equal to 1 + transmission.m_wheelsTorqueRatio[i] = m_Wheels[i].WheelComponent->TorqueRatio; + } transmission.m_primaryTransmissionRatio = hkpVehicleDefaultTransmission::calculatePrimaryTransmissionRatio( vehicleComponent.TopSpeed, @@ -287,7 +285,7 @@ void VehicleSetup::setupComponent(const hkpVehicleData& data, hkpVehicleDefaultV // The threshold in m/s at which the algorithm switches from // using the normalSpinDamping to the collisionSpinDamping. - velocityDamper.m_collisionThreshold = 1.0f; + velocityDamper.m_collisionThreshold = 100.0f; } void VehicleSetup::setupWheelCollide(const hkpWorld* world, const hkpVehicleInstance& vehicle, hkpVehicleRayCastWheelCollide& wheelCollide) diff --git a/src/Physics/VehicleSetup.h b/src/Physics/VehicleSetup.h index b54adbd..83647bb 100644 --- a/src/Physics/VehicleSetup.h +++ b/src/Physics/VehicleSetup.h @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -43,7 +44,7 @@ public: Components::Wheel* WheelComponent; Components::Transform* TransformComponent; }; - + std::vector m_Wheels; virtual void setupVehicleData(const hkpWorld* world, hkpVehicleData& data); @@ -58,6 +59,7 @@ public: virtual void setupComponent(const hkpVehicleData& data, hkpVehicleDefaultVelocityDamper& velocityDamper, Components::Vehicle vehicleComponent); virtual void setupWheelCollide(const hkpWorld* world, const hkpVehicleInstance& vehicle, hkpVehicleRayCastWheelCollide& wheelCollide); + }; #endif // Physics_Vehicle_h__ diff --git a/src/Systems/PhysicsSystem.cpp b/src/Systems/PhysicsSystem.cpp index fb54b87..9e14054 100644 --- a/src/Systems/PhysicsSystem.cpp +++ b/src/Systems/PhysicsSystem.cpp @@ -111,6 +111,8 @@ void Systems::PhysicsSystem::RegisterComponents(ComponentFactory* cf) cf->Register("Wheel", []() { return new Components::Wheel(); }); cf->Register("MeshShape", []() { return new Components::MeshShape(); }); cf->Register("HingeConstraint", []() { return new Components::HingeConstraint(); }); + cf->Register("WheelPair", []() { return new Components::WheelPair(); }); + } void Systems::PhysicsSystem::Update(double dt) @@ -225,9 +227,14 @@ void Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID p m_PhysicsWorld->markForWrite(); hkpVehicleDriverInputAnalogStatus* deviceStatus = (hkpVehicleDriverInputAnalogStatus*)m_Vehicles[entity]->m_deviceStatus; - if(inputComponent->KeyState[GLFW_KEY_UP] != 0 || inputComponent->KeyState[GLFW_KEY_DOWN] != 0) + if(inputComponent->KeyState[GLFW_KEY_UP] != 0) { - deviceStatus->m_positionY += inputComponent->KeyState[GLFW_KEY_UP] * -1 * 1.f * dt + inputComponent->KeyState[GLFW_KEY_DOWN] * 1 * 1.f * dt; + deviceStatus->m_positionY += inputComponent->KeyState[GLFW_KEY_UP] * -1 * 1.f * dt; + } + else if (inputComponent->KeyState[GLFW_KEY_DOWN] != 0) + { + deviceStatus->m_positionY += inputComponent->KeyState[GLFW_KEY_DOWN] * 1 * 1.f * dt; + deviceStatus->m_reverseButtonPressed = true; } else { @@ -310,7 +317,7 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) auto physicsComponent = m_World->GetComponent(entity, "Physics"); if (physicsComponent) { - + hkpShape* shape; if(entityParent != entity) { LOG_ERROR("Entity: %i , Only the baseparent can have a PhysicsComponent", entity); @@ -330,7 +337,7 @@ 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; ////////////////////////////////// //******************************// @@ -342,11 +349,11 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) m_Shapes.erase(entity); hkMassProperties massProperties; - hkpInertiaTensorComputer::computeShapeVolumeMassProperties(listShape, physicsComponent->Mass, massProperties); + hkpInertiaTensorComputer::computeShapeVolumeMassProperties(shape, physicsComponent->Mass, massProperties); hkpRigidBodyCinfo rigidBodyInfo; { - rigidBodyInfo.m_shape = listShape; + rigidBodyInfo.m_shape = shape; rigidBodyInfo.m_motionType = hkpMotion::MOTION_DYNAMIC; auto absoluteTransform = m_World->GetSystem("TransformSystem")->AbsoluteTransform(entity); hkVector4 position = ConvertPosition(absoluteTransform.Position); @@ -361,13 +368,48 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) // Create RigidBody hkpRigidBody* rigidBody = new hkpRigidBody(rigidBodyInfo); - m_PhysicsWorld->markForWrite(); - m_PhysicsWorld->addEntity(rigidBody); - m_RigidBodies[entity] = rigidBody; - m_PhysicsWorld->unmarkForWrite(); + auto vehicleComponent = m_World->GetComponent(entity, "Vehicle"); + if (vehicleComponent && m_Vehicles.find(entity) == m_Vehicles.end()) + { + for (int i = 0; i < m_Wheels.size(); i++) + { + if(m_World->GetEntityParent(m_Wheels[i]) != entity) + { + m_Wheels.erase(m_Wheels.begin() + i); + i--; + } + } - listShape->removeReference(); - rigidBody->removeReference(); + + VehicleSetup vehicleSetup; + // Create the basic vehicle. + m_Vehicles[entity] = new hkpVehicleInstance(rigidBody); + 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 + m_Vehicles[entity]->addToWorld(m_PhysicsWorld); + + m_RigidBodies[entity] = rigidBody; + + // The vehicle is an action + m_PhysicsWorld->addAction(m_Vehicles[entity]); + m_PhysicsWorld->unmarkForWrite(); + + //m_Vehicles[entity]->m_rpm = 0.0f; // Not sure why this one should be here + m_Wheels.clear(); + shape->removeReference(); + rigidBody->removeReference(); + } + else + { + m_PhysicsWorld->markForWrite(); + m_PhysicsWorld->addEntity(rigidBody); + m_RigidBodies[entity] = rigidBody; + m_PhysicsWorld->unmarkForWrite(); + + shape->removeReference(); + rigidBody->removeReference(); + } } else // Static { @@ -390,14 +432,14 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) // This must be called after adding the instances and before using the shape. staticCompoundShape->bake(); - + shape = staticCompoundShape; m_Shapes.erase(entity); hkMassProperties massProperties; - hkpInertiaTensorComputer::computeShapeVolumeMassProperties(staticCompoundShape, physicsComponent->Mass, massProperties); + hkpInertiaTensorComputer::computeShapeVolumeMassProperties(shape, physicsComponent->Mass, massProperties); hkpRigidBodyCinfo rigidBodyInfo; { - rigidBodyInfo.m_shape = staticCompoundShape; + rigidBodyInfo.m_shape = shape; rigidBodyInfo.m_motionType = hkpMotion::MOTION_FIXED; auto absoluteTransform = m_World->GetSystem("TransformSystem")->AbsoluteTransform(entity); hkVector4 position = ConvertPosition(absoluteTransform.Position); @@ -417,8 +459,10 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) m_RigidBodies[entity] = rigidBody; m_PhysicsWorld->unmarkForWrite(); - staticCompoundShape->removeReference(); + shape->removeReference(); rigidBody->removeReference(); + + } } @@ -441,11 +485,7 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) else if(boxComponent) { hkReal thickness = 0.05; - hkpBoxShape* boxShape = new hkpBoxShape(hkVector4(boxComponent->Width, boxComponent->Height, boxComponent->Depth), thickness); - - hkpShapeShrinker* shapeShrinker = new hkpShapeShrinker(); - boxShape = shapeShrinker->shrinkBoxShape(boxShape, thickness, 0); // HACK: Unsure about the 3rd argument - delete shapeShrinker; + 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)); hkpConvexTransformShape* transformedBoxShape = new hkpConvexTransformShape( boxShape, transform ); @@ -477,7 +517,7 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) } hkpExtendedMeshShape* mesh = new hkpExtendedMeshShape(); - hkReal thickness = 0.00f; // HACK: Convex radius should be 0 for static shapes and 0.05 for dynamic shapes. + hkReal thickness = 0.05f; // HACK: Convex radius should be 0 for static shapes and 0.05 for dynamic shapes. mesh->setRadius(thickness); { hkpExtendedMeshShape::TrianglesSubpart part; @@ -527,8 +567,9 @@ void Systems::PhysicsSystem::SetupVisualDebugger(hkpPhysicsContext* worlds) { // Setup the visual debugger hkArray contexts; + contexts.pushBack(worlds); - + m_VisualDebugger = new hkVisualDebugger(contexts); m_VisualDebugger->serve(); @@ -562,7 +603,7 @@ glm::vec3 Systems::PhysicsSystem::ConvertPosition(const hkVector4 &hkPosition) const hkVector4& Systems::PhysicsSystem::ConvertPosition(glm::vec3 glmPosition) { - return hkVector4( glmPosition.x, glmPosition.y, glmPosition.z ); + return hkVector4( glmPosition.x, glmPosition.y, glmPosition.z); } glm::quat Systems::PhysicsSystem::ConvertRotation(const hkQuaternion &hkRotation) @@ -572,7 +613,7 @@ glm::quat Systems::PhysicsSystem::ConvertRotation(const hkQuaternion &hkRotation const hkQuaternion& Systems::PhysicsSystem::ConvertRotation(glm::quat glmRotation) { - return hkQuaternion(glmRotation.x, glmRotation.y, glmRotation.z, glmRotation.w ); + return hkQuaternion(glmRotation.x, glmRotation.y, glmRotation.z, glmRotation.w); } glm::vec3 Systems::PhysicsSystem::ConvertScale(const hkVector4 &hkScale) diff --git a/src/Systems/PhysicsSystem.h b/src/Systems/PhysicsSystem.h index c74a5ec..9ef1260 100644 --- a/src/Systems/PhysicsSystem.h +++ b/src/Systems/PhysicsSystem.h @@ -11,6 +11,7 @@ #include "Components/Input.h" #include "Components/MeshShape.h" #include "Components/HingeConstraint.h" +#include "Components/WheelPair.h" #include "OBJ.h" // Math and base include diff --git a/vs11/Returngeance/Returngeance.vcxproj b/vs11/Returngeance/Returngeance.vcxproj index 6e66309..bae6084 100755 --- a/vs11/Returngeance/Returngeance.vcxproj +++ b/vs11/Returngeance/Returngeance.vcxproj @@ -140,6 +140,7 @@ + diff --git a/vs11/Returngeance/Returngeance.vcxproj.filters b/vs11/Returngeance/Returngeance.vcxproj.filters index cf6f009..6f29356 100755 --- a/vs11/Returngeance/Returngeance.vcxproj.filters +++ b/vs11/Returngeance/Returngeance.vcxproj.filters @@ -242,6 +242,9 @@ Physics\Components + + Physics\Components +