From 069d079594559b1b5361b0a9270126f04d21d7ea Mon Sep 17 00:00:00 2001 From: ViktorLjung Date: Wed, 16 Apr 2014 01:33:05 +0200 Subject: [PATCH] Car physics not working, yet! --- src/Components/Physics.h | 3 +- src/Components/Vehicle.h | 16 + src/GameWorld.cpp | 25 +- src/GameWorld.h | 1 + src/Physics/VehicleSetup.cpp | 339 ++++++++++++++++++ src/Physics/VehicleSetup.h | 54 +++ src/Systems/PhysicsSystem.cpp | 82 ++++- src/Systems/PhysicsSystem.h | 11 +- vs11/Returngeance/Returngeance.vcxproj | 7 +- .../Returngeance/Returngeance.vcxproj.filters | 12 + 10 files changed, 535 insertions(+), 15 deletions(-) create mode 100644 src/Components/Vehicle.h create mode 100644 src/Physics/VehicleSetup.cpp create mode 100644 src/Physics/VehicleSetup.h diff --git a/src/Components/Physics.h b/src/Components/Physics.h index 956ae7a..df819c4 100644 --- a/src/Components/Physics.h +++ b/src/Components/Physics.h @@ -9,9 +9,10 @@ namespace Components struct Physics : Component { Physics() - : Mass(0.f) { } + : Mass(0.f), Static(false){} float Mass; + bool Static; }; } diff --git a/src/Components/Vehicle.h b/src/Components/Vehicle.h new file mode 100644 index 0000000..6ab1498 --- /dev/null +++ b/src/Components/Vehicle.h @@ -0,0 +1,16 @@ +#ifndef Components_Vehicle_h__ +#define Components_Vehicle_h__ + +#include "Component.h" + +namespace Components +{ + + struct Vehicle : Component +{ + +}; + +} + +#endif // Components_Vehicle_h__ diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index 9272005..e274828 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -5,6 +5,7 @@ void GameWorld::Initialize() { World::Initialize(); + { auto camera = CreateEntity(); auto transform = AddComponent(camera, "Transform"); @@ -33,13 +34,14 @@ void GameWorld::Initialize() auto physics = AddComponent(ground, "Physics"); physics->Mass = 10; + physics->Static = true; } - for(int i = 0; i < 10; i++) + for(int i = 0; i < 1; i++) { auto ball = CreateEntity(); auto transform = AddComponent(ball, "Transform"); - transform->Position = glm::vec3(0, 5 + i*2, 0); + 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(ball, "Model"); @@ -50,6 +52,23 @@ void GameWorld::Initialize() physics->Mass = 1; } + { + auto car = CreateEntity(); + auto transform = AddComponent(car, "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(car, "Model"); + model->ModelFile = "Models/Placeholders/PhysicsTest/Box.obj"; + auto physics = AddComponent(car, "Physics"); + physics->Mass = 1; + auto box = AddComponent(car, "Box"); + box->Width = 3; + box->Height = 1; + box->Depth = 5; + auto vehicle = AddComponent(car, "Vehicle"); + } + /*{ auto entity = CreateEntity(); AddComponent(entity, "Transform"); @@ -82,6 +101,8 @@ void GameWorld::RegisterComponents() m_ComponentFactory.Register("Physics", []() { return new Components::Physics(); }); m_ComponentFactory.Register("Sphere", []() { return new Components::Sphere(); }); m_ComponentFactory.Register("Box", []() { return new Components::Box (); }); + + m_ComponentFactory.Register("Vehicle", []() { return new Components::Vehicle(); }); } void GameWorld::RegisterSystems() diff --git a/src/GameWorld.h b/src/GameWorld.h index b40807e..2945556 100755 --- a/src/GameWorld.h +++ b/src/GameWorld.h @@ -30,6 +30,7 @@ #include "Components/Physics.h" #include "Components/Sphere.h" #include "Components/Box.h" +#include "Components/Vehicle.h" class GameWorld : public World { diff --git a/src/Physics/VehicleSetup.cpp b/src/Physics/VehicleSetup.cpp new file mode 100644 index 0000000..b656c7b --- /dev/null +++ b/src/Physics/VehicleSetup.cpp @@ -0,0 +1,339 @@ +#include "PrecompiledHeader.h" +#include "Physics/VehicleSetup.h" + + + +void VehicleSetup::buildVehicle(const hkpWorld* world, hkpVehicleInstance& vehicle) +{ + // + // All memory allocations are made here. + // + + vehicle.m_data = new hkpVehicleData; + vehicle.m_driverInput = new hkpVehicleDefaultAnalogDriverInput; + vehicle.m_steering = new hkpVehicleDefaultSteering; + vehicle.m_engine = new hkpVehicleDefaultEngine; + vehicle.m_transmission = new hkpVehicleDefaultTransmission; + vehicle.m_brake = new hkpVehicleDefaultBrake; + vehicle.m_suspension = new hkpVehicleDefaultSuspension; + vehicle.m_aerodynamics = new hkpVehicleDefaultAerodynamics; + vehicle.m_velocityDamper = new hkpVehicleDefaultVelocityDamper; + + // For illustrative purposes we use a custom hkpVehicleRayCastWheelCollide + // which implements varying 'ground' friction in a very simple way. + //vehicle.m_wheelCollide = new hkpVehicleRayCastWheelCollide; + + 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(vehicle.m_driverInput)); + setupComponent(*vehicle.m_data, *static_cast(vehicle.m_steering)); + setupComponent(*vehicle.m_data, *static_cast(vehicle.m_engine)); + setupComponent(*vehicle.m_data, *static_cast(vehicle.m_transmission)); + setupComponent(*vehicle.m_data, *static_cast(vehicle.m_brake)); + setupComponent(*vehicle.m_data, *static_cast(vehicle.m_suspension)); + setupComponent(*vehicle.m_data, *static_cast(vehicle.m_aerodynamics)); + setupComponent(*vehicle.m_data, *static_cast(vehicle.m_velocityDamper)); + + setupWheelCollide(world, vehicle, *static_cast(vehicle.m_wheelCollide)); + + setupTyremarks(*vehicle.m_data, *static_cast(vehicle.m_tyreMarks)); + + // + // Check that all components are present. + // + HK_ASSERT(0x0, vehicle.m_data); + HK_ASSERT(0x7708674a, vehicle.m_driverInput); + HK_ASSERT(0x5a324a2d, vehicle.m_steering); + HK_ASSERT(0x7bcb2aff, vehicle.m_engine); + HK_ASSERT(0x29bddb50, vehicle.m_transmission); + HK_ASSERT(0x2b0323a2, vehicle.m_brake); + HK_ASSERT(0x7a7ade23, vehicle.m_suspension); + HK_ASSERT(0x6ec4d0ed, vehicle.m_aerodynamics); + HK_ASSERT(0x67161206, vehicle.m_wheelCollide); + HK_ASSERT(0x295015f1, vehicle.m_tyreMarks); + + // + // Set up any variables that store cached data. + // + + + // Give driver input default values so that the vehicle (if this input is a default for non + // player cars) will drive, even if it is in circles! + + // Accelerate. + vehicle.m_deviceStatus = new hkpVehicleDriverInputAnalogStatus; + hkpVehicleDriverInputAnalogStatus* deviceStatus = (hkpVehicleDriverInputAnalogStatus*)vehicle.m_deviceStatus; + deviceStatus->m_positionY = -0.4f; + + // Turn. + deviceStatus->m_positionX = 0.3f; + + // Defaults + deviceStatus->m_handbrakeButtonPressed = false; + deviceStatus->m_reverseButtonPressed = false; + + + // + // Don't forget to call init! (This function is necessary to set up derived data) + // + vehicle.init(); +} + +void VehicleSetup::setupVehicleData(const hkpWorld* world, hkpVehicleData& data) +{ + data.m_gravity = world->getGravity(); + + // + // The vehicleData contains information about the chassis. + // + + // The coordinates of the chassis system, used for steering the vehicle. + // up forward right + data.m_chassisOrientation.setCols(hkVector4(0, 1, 0), hkVector4(1, 0, 0), hkVector4(0, 0, 1)); + + data.m_frictionEqualizer = 0.5f; + + + // Inertia tensor for each axis is calculated by using : + // (1 / chassis_mass) * (torque(axis)Factor / chassisUnitInertia) + data.m_torqueRollFactor = 0.625f; + data.m_torquePitchFactor = 0.5f; + data.m_torqueYawFactor = 0.35f; + + data.m_chassisUnitInertiaYaw = 1.0f; + data.m_chassisUnitInertiaRoll = 1.0f; + data.m_chassisUnitInertiaPitch = 1.0f; + + // Adds or removes torque around the yaw axis + // based on the current steering angle. This will + // affect steering. + data.m_extraTorqueFactor = -0.5f; + data.m_maxVelocityForPositionalFriction = 0.0f; + + // + // Wheel specifications + // + data.m_numWheels = 4; + + data.m_wheelParams.setSize(data.m_numWheels); + + data.m_wheelParams[0].m_axle = 0; + data.m_wheelParams[1].m_axle = 0; + data.m_wheelParams[2].m_axle = 1; + data.m_wheelParams[3].m_axle = 1; + + data.m_wheelParams[0].m_friction = 1.5f; + data.m_wheelParams[1].m_friction = 1.5f; + data.m_wheelParams[2].m_friction = 1.5f; + data.m_wheelParams[3].m_friction = 1.5f; + + data.m_wheelParams[0].m_slipAngle = 0.0f; + data.m_wheelParams[1].m_slipAngle = 0.0f; + data.m_wheelParams[2].m_slipAngle = 0.0f; + data.m_wheelParams[3].m_slipAngle = 0.0f; + + for (int i = 0; i < data.m_numWheels; i++) + { + // This value is also used to calculate the m_primaryTransmissionRatio. + data.m_wheelParams[i].m_radius = 0.4f; + data.m_wheelParams[i].m_width = 0.2f; + data.m_wheelParams[i].m_mass = 10.0f; + + data.m_wheelParams[i].m_viscosityFriction = 0.25f; + data.m_wheelParams[i].m_maxFriction = 2.0f * data.m_wheelParams[i].m_friction; + data.m_wheelParams[i].m_forceFeedbackMultiplier = 0.1f; + data.m_wheelParams[i].m_maxContactBodyAcceleration = hkReal(data.m_gravity.length3()) * 2; + } +} + + +void VehicleSetup::setupComponent(const hkpVehicleData& data, hkpVehicleDefaultAnalogDriverInput& driverInput) +{ + // We also use an analog "driver input" class to help converting user input to vehicle behavior. + + driverInput.m_slopeChangePointX = 0.8f; + driverInput.m_initialSlope = 0.7f; + driverInput.m_deadZone = 0.0f; + driverInput.m_autoReverse = true; +} + +void VehicleSetup::setupComponent(const hkpVehicleData& data, hkpVehicleDefaultSteering& steering) +{ + steering.m_doesWheelSteer.setSize(data.m_numWheels); + + // degrees + steering.m_maxSteeringAngle = 35 * (HK_REAL_PI / 180); + + // [mph/h] The steering angle decreases linearly + // based on your overall max speed of the vehicle. + steering.m_maxSpeedFullSteeringAngle = 70.0f * (1.605f / 3.6f); + steering.m_doesWheelSteer[0] = true; + steering.m_doesWheelSteer[1] = true; + steering.m_doesWheelSteer[2] = false; + steering.m_doesWheelSteer[3] = false; +} + +void VehicleSetup::setupComponent(const hkpVehicleData& data, hkpVehicleDefaultEngine& engine) +{ + engine.m_maxTorque = 500.0f; + + engine.m_minRPM = 1000.0f; + engine.m_optRPM = 5500.0f; + + // This value is also used to calculate the m_primaryTransmissionRatio. + engine.m_maxRPM = 7500.0f; + + engine.m_torqueFactorAtMinRPM = 0.8f; + engine.m_torqueFactorAtMaxRPM = 0.8f; + + engine.m_resistanceFactorAtMinRPM = 0.05f; + engine.m_resistanceFactorAtOptRPM = 0.1f; + engine.m_resistanceFactorAtMaxRPM = 0.3f; +} + +void VehicleSetup::setupComponent(const hkpVehicleData& data, hkpVehicleDefaultTransmission& transmission) +{ + int numGears = 4; + + transmission.m_gearsRatio.setSize(numGears); + transmission.m_wheelsTorqueRatio.setSize(data.m_numWheels); + + transmission.m_downshiftRPM = 3500.0f; + transmission.m_upshiftRPM = 6500.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; + + const hkReal vehicleTopSpeed = 130.0f; + const hkReal wheelRadius = 0.4f; + const hkReal maxEngineRpm = 7500.0f; + transmission.m_primaryTransmissionRatio = hkpVehicleDefaultTransmission::calculatePrimaryTransmissionRatio(vehicleTopSpeed, + wheelRadius, + maxEngineRpm, + transmission.m_gearsRatio[numGears - 1]); +} + +void VehicleSetup::setupComponent(const hkpVehicleData& data, hkpVehicleDefaultBrake& brake) +{ + brake.m_wheelBrakingProperties.setSize(data.m_numWheels); + + const float bt = 1500.0f; + brake.m_wheelBrakingProperties[0].m_maxBreakingTorque = bt; + brake.m_wheelBrakingProperties[1].m_maxBreakingTorque = bt; + brake.m_wheelBrakingProperties[2].m_maxBreakingTorque = bt; + brake.m_wheelBrakingProperties[3].m_maxBreakingTorque = bt; + + // Handbrake is attached to rear wheels only. + brake.m_wheelBrakingProperties[0].m_isConnectedToHandbrake = false; + brake.m_wheelBrakingProperties[1].m_isConnectedToHandbrake = false; + brake.m_wheelBrakingProperties[2].m_isConnectedToHandbrake = true; + brake.m_wheelBrakingProperties[3].m_isConnectedToHandbrake = true; + brake.m_wheelBrakingProperties[0].m_minPedalInputToBlock = 0.9f; + brake.m_wheelBrakingProperties[1].m_minPedalInputToBlock = 0.9f; + brake.m_wheelBrakingProperties[2].m_minPedalInputToBlock = 0.9f; + brake.m_wheelBrakingProperties[3].m_minPedalInputToBlock = 0.9f; + brake.m_wheelsMinTimeToBlock = 1000.0f; +} + +void VehicleSetup::setupComponent(const hkpVehicleData& data, hkpVehicleDefaultSuspension& suspension) +{ + suspension.m_wheelParams.setSize(data.m_numWheels); + suspension.m_wheelSpringParams.setSize(data.m_numWheels); + + suspension.m_wheelParams[0].m_length = 0.35f; + suspension.m_wheelParams[1].m_length = 0.35f; + suspension.m_wheelParams[2].m_length = 0.35f; + suspension.m_wheelParams[3].m_length = 0.35f; + + const float str = 50.0f; + suspension.m_wheelSpringParams[0].m_strength = str; + suspension.m_wheelSpringParams[1].m_strength = str; + suspension.m_wheelSpringParams[2].m_strength = str; + suspension.m_wheelSpringParams[3].m_strength = str; + + const float wd = 3.0f; + suspension.m_wheelSpringParams[0].m_dampingCompression = wd; + suspension.m_wheelSpringParams[1].m_dampingCompression = wd; + suspension.m_wheelSpringParams[2].m_dampingCompression = wd; + suspension.m_wheelSpringParams[3].m_dampingCompression = wd; + + suspension.m_wheelSpringParams[0].m_dampingRelaxation = wd; + suspension.m_wheelSpringParams[1].m_dampingRelaxation = wd; + suspension.m_wheelSpringParams[2].m_dampingRelaxation = wd; + suspension.m_wheelSpringParams[3].m_dampingRelaxation = wd; + + // + // NB: The hardpoints MUST be positioned INSIDE the chassis. + // + { + const hkReal hardPointFrontX = 1.3f; + const hkReal hardPointBackX = -1.1f; + const hkReal hardPointY = -0.05f; + const hkReal hardPointZ = 1.1f; + + suspension.m_wheelParams[0].m_hardpointChassisSpace.set(hardPointFrontX, hardPointY, -hardPointZ); + suspension.m_wheelParams[1].m_hardpointChassisSpace.set(hardPointFrontX, hardPointY, hardPointZ); + suspension.m_wheelParams[2].m_hardpointChassisSpace.set(hardPointBackX, hardPointY, -hardPointZ); + suspension.m_wheelParams[3].m_hardpointChassisSpace.set(hardPointBackX, hardPointY, hardPointZ); + } + + const hkVector4 downDirection(0.0f, -1.0f, 0.0f); + suspension.m_wheelParams[0].m_directionChassisSpace = downDirection; + suspension.m_wheelParams[1].m_directionChassisSpace = downDirection; + suspension.m_wheelParams[2].m_directionChassisSpace = downDirection; + suspension.m_wheelParams[3].m_directionChassisSpace = downDirection; +} + +void VehicleSetup::setupComponent(const hkpVehicleData& data, hkpVehicleDefaultAerodynamics& aerodynamics) +{ + aerodynamics.m_airDensity = 1.3f; + // In m^2. + aerodynamics.m_frontalArea = 1.0f; + + aerodynamics.m_dragCoefficient = 0.7f; + aerodynamics.m_liftCoefficient = -0.3f; + + // Extra gavity applies in world space (independent of m_chassisCoordinateSystem). + aerodynamics.m_extraGravityws.set(0.0f, -5.0f, 0.0f); +} + +void VehicleSetup::setupComponent(const hkpVehicleData& data, hkpVehicleDefaultVelocityDamper& velocityDamper) +{ + // Caution: setting negative damping values will add energy to system. + // Setting the value to 0 will not affect the angular velocity. + + // Damping the change of the chassis angular velocity when below m_collisionThreshold. + // This will affect turning radius and steering. + velocityDamper.m_normalSpinDamping = 0.0f; + + // Positive numbers dampen the rotation of the chassis and + // reduce the reaction of the chassis in a collision. + velocityDamper.m_collisionSpinDamping = 4.0f; + + // The threshold in m/s at which the algorithm switches from + // using the normalSpinDamping to the collisionSpinDamping. + velocityDamper.m_collisionThreshold = 1.0f; +} + +void VehicleSetup::setupWheelCollide(const hkpWorld* world, const hkpVehicleInstance& vehicle, hkpVehicleRayCastWheelCollide& wheelCollide) +{ + // Set the wheels to have the same collision filter info as the chassis. + wheelCollide.m_wheelCollisionFilterInfo = vehicle.getChassis()->getCollisionFilterInfo(); +} + +void VehicleSetup::setupTyremarks(const hkpVehicleData& data, hkpTyremarksInfo& tyreMarks) +{ + tyreMarks.m_minTyremarkEnergy = 100.0f; + tyreMarks.m_maxTyremarkEnergy = 1000.0f; +} \ No newline at end of file diff --git a/src/Physics/VehicleSetup.h b/src/Physics/VehicleSetup.h new file mode 100644 index 0000000..7b7d9a5 --- /dev/null +++ b/src/Physics/VehicleSetup.h @@ -0,0 +1,54 @@ +#ifndef Physics_Vehicle_h__ +#define Physics_Vehicle_h__ + +//#include "PrecompiledHeader.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +// Vehicle page 425 in documentation +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include + +class VehicleSetup +{ +public: + virtual void buildVehicle(const hkpWorld* world, hkpVehicleInstance& vehicle); + +public: + + virtual void setupVehicleData(const hkpWorld* world, hkpVehicleData& data); + virtual void setupComponent(const hkpVehicleData& data, hkpVehicleDefaultAnalogDriverInput& driverInput); + virtual void setupComponent(const hkpVehicleData& data, hkpVehicleDefaultSteering& steering); + virtual void setupComponent(const hkpVehicleData& data, hkpVehicleDefaultEngine& engine); + virtual void setupComponent(const hkpVehicleData& data, hkpVehicleDefaultTransmission& transmission); + virtual void setupComponent(const hkpVehicleData& data, hkpVehicleDefaultBrake& brake); + virtual void setupComponent(const hkpVehicleData& data, hkpVehicleDefaultSuspension& suspension); + virtual void setupComponent(const hkpVehicleData& data, hkpVehicleDefaultAerodynamics& aerodynamics); + virtual void setupComponent(const hkpVehicleData& data, hkpVehicleDefaultVelocityDamper& velocityDamper); + + virtual void setupWheelCollide(const hkpWorld* world, const hkpVehicleInstance& vehicle, hkpVehicleRayCastWheelCollide& wheelCollide); + virtual void setupTyremarks(const hkpVehicleData& data, hkpTyremarksInfo& tyremarkscontroller); +}; + +#endif // Physics_Vehicle_h__ diff --git a/src/Systems/PhysicsSystem.cpp b/src/Systems/PhysicsSystem.cpp index c70c8fc..b77fc1b 100644 --- a/src/Systems/PhysicsSystem.cpp +++ b/src/Systems/PhysicsSystem.cpp @@ -29,6 +29,7 @@ Systems::PhysicsSystem::PhysicsSystem(World* world) : System(world) { + m_Accumulator = 0; { hkMemorySystem::FrameInfo finfo(500 * 1024); // Allocate 500KB of Physics solver buffer hkMemoryRouter* memoryRouter = hkMemoryInitUtil::initDefault(hkMallocAllocator::m_defaultMallocAllocator, finfo); @@ -63,7 +64,30 @@ Systems::PhysicsSystem::PhysicsSystem(World* world) : System(world) void Systems::PhysicsSystem::Update(double dt) { - m_PhysicsWorld->stepDeltaTime(0.0166f); + for (auto pair : *m_World->GetEntities()) + { + EntityID entity = pair.first; + + if (m_RigidBodies.find(entity) == m_RigidBodies.end()) + continue; + + auto transformComponent = m_World->GetComponent(entity, "Transform"); + if (!transformComponent) + continue; + + + 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); + m_RigidBodies[entity]->setPositionAndRotation(position, rotation); + } + + static const double timestep = 1 / 60.0; + m_Accumulator += dt; + while (m_Accumulator >= timestep) + { + m_PhysicsWorld->stepDeltaTime(timestep); + m_Accumulator -= timestep; + } // Step the visual debugger StepVisualDebugger(); @@ -113,17 +137,32 @@ void Systems::PhysicsSystem::SetUpPhysicsState(EntityID entity, EntityID parent) { shape = new hkpSphereShape(sphereComponent->Radius); rigidBodyInfo.m_shape = shape; - rigidBodyInfo.m_motionType = hkpMotion::MOTION_SPHERE_INERTIA; + + if (physicsComponent->Static) + { + rigidBodyInfo.m_motionType = hkpMotion::MOTION_FIXED; + } + else + { + rigidBodyInfo.m_motionType = hkpMotion::MOTION_SPHERE_INERTIA; + } hkpInertiaTensorComputer::computeSphereVolumeMassProperties(sphereComponent->Radius, physicsComponent->Mass, massProperties); } else if (boxComponent) { - shape = new hkpBoxShape(hkVector4(boxComponent->Width, boxComponent->Height, boxComponent->Depth)); + hkReal thickness = 0.05; + shape = new hkpBoxShape(hkVector4(boxComponent->Width - thickness, boxComponent->Height - thickness, boxComponent->Depth - thickness)); rigidBodyInfo.m_shape = shape; - rigidBodyInfo.m_motionType = hkpMotion::MOTION_FIXED; - hkReal thickness = 0.1; + if (physicsComponent->Static) + { + rigidBodyInfo.m_motionType = hkpMotion::MOTION_FIXED; + } + else + { + rigidBodyInfo.m_motionType = hkpMotion::MOTION_BOX_INERTIA; + } hkpInertiaTensorComputer::computeBoxSurfaceMassProperties(hkVector4(boxComponent->Width - thickness, boxComponent->Height - thickness, boxComponent->Depth - thickness), physicsComponent->Mass, thickness, massProperties); } else @@ -138,14 +177,39 @@ void Systems::PhysicsSystem::SetUpPhysicsState(EntityID entity, EntityID parent) // Create RigidBody hkpRigidBody* rigidBody = new hkpRigidBody(rigidBodyInfo); - shape->removeReference(); + - m_PhysicsWorld->addEntity(rigidBody); - m_RigidBodies[entity] = rigidBody; - rigidBody->removeReference(); + auto vehicleComponent = m_World->GetComponent(entity, "Vehicle"); + if (vehicleComponent && m_Vehicles.find(entity) == m_Vehicles.end()) + { + VehicleSetup vehicleSetup; + // Create the vehicle. + m_Vehicles[entity] = new hkpVehicleInstance(rigidBody); + // Create the basic vehicle. + m_Vehicles[entity] = new hkpVehicleInstance(rigidBody); + vehicleSetup.buildVehicle(m_PhysicsWorld, *m_Vehicles[entity]); + // Add the vehicle's entities and phantoms to the world + m_Vehicles[entity]->addToWorld(m_PhysicsWorld); + // The vehicle is an action + m_PhysicsWorld->addAction(m_Vehicles[entity]); + + m_Vehicles[entity]->m_rpm = 0.0f; // Not sure why this one should be here + + shape->removeReference(); + rigidBody->removeReference(); + } + else + { + m_PhysicsWorld->addEntity(rigidBody); + m_RigidBodies[entity] = rigidBody; + shape->removeReference(); + rigidBody->removeReference(); + } } + + void Systems::PhysicsSystem::TearDownPhysicsState(EntityID entity, EntityID parent) { diff --git a/src/Systems/PhysicsSystem.h b/src/Systems/PhysicsSystem.h index e0856ed..77c4c21 100644 --- a/src/Systems/PhysicsSystem.h +++ b/src/Systems/PhysicsSystem.h @@ -10,6 +10,7 @@ #include "Components/Physics.h" #include "Components/Sphere.h" #include "Components/Box.h" +#include "Components/Vehicle.h" // Math and base include @@ -39,6 +40,11 @@ #include #include + + +#include "Physics/VehicleSetup.h" + + #include namespace Systems { @@ -54,7 +60,7 @@ public: private: - + double m_Accumulator; hkpWorld* m_PhysicsWorld; void SetUpPhysicsState(EntityID entity, EntityID parent); @@ -67,7 +73,10 @@ private: void SetupPhysics(hkpWorld* physicsWorld); std::unordered_map m_RigidBodies; + std::unordered_map m_Vehicles; + + hkpVehicleInstance* Systems::PhysicsSystem::createVehicle(VehicleSetup& vehicleSetup, hkpRigidBody* chassis); }; } diff --git a/vs11/Returngeance/Returngeance.vcxproj b/vs11/Returngeance/Returngeance.vcxproj index 662155f..7145402 100644 --- a/vs11/Returngeance/Returngeance.vcxproj +++ b/vs11/Returngeance/Returngeance.vcxproj @@ -65,7 +65,7 @@ true - OpenAL32.lib;opengl32.lib;glu32.lib;SOIL.lib;glew32d.lib;glfw3.lib;hkBase.lib;hkVisualize.lib;hkInternal.lib;hkSerialize.lib;hkGeometryUtilities.lib;hkcdInternal.lib;hkcdCollide.lib;hkpCollide.lib;hkpConstraint.lib;hkpConstraintSolver.lib;hkpDynamics.lib;hkpInternal.lib;hkpUtilities.lib;hkSceneData.lib;%(AdditionalDependencies) + OpenAL32.lib;opengl32.lib;glu32.lib;SOIL.lib;glew32d.lib;glfw3.lib;hkBase.lib;hkVisualize.lib;hkInternal.lib;hkSerialize.lib;hkGeometryUtilities.lib;hkcdInternal.lib;hkcdCollide.lib;hkpCollide.lib;hkpConstraint.lib;hkpConstraintSolver.lib;hkpDynamics.lib;hkpInternal.lib;hkpUtilities.lib;hkSceneData.lib;hkpVehicle.lib;%(AdditionalDependencies) /ignore:4221 @@ -89,7 +89,7 @@ true true true - OpenAL32.lib;opengl32.lib;glu32.lib;SOIL.lib;glew32.lib;glfw3.lib;hkaAnimation.lib;hkaInternal.lib;hkaPhysics2012Bridge.lib;hkBase.lib;hkcdCollide.lib;hkcdInternal.lib;hkCompat.lib;hkgBridge.lib;hkgCommon.lib;hkgDx11.lib;hkgDx9s.lib;hkGeometryUtilities.lib;hkgOglES.lib;hkgOglES2.lib;hkgOgls.lib;hkgSoundCommon.lib;hkgSoundXAudio2.lib;hkInternal.lib;hkpCollide.lib;hkpConstraint.lib;hkpConstraintSolver.lib;hkpDynamics.lib;hkpInternal.lib;hkpUtilities.lib;hkpVehicle.lib;hkSceneData.lib;hkSerialize.lib;hkVisualize.lib;%(AdditionalDependencies) + OpenAL32.lib;opengl32.lib;glu32.lib;SOIL.lib;glew32.lib;glfw3.lib;hkBase.lib;hkVisualize.lib;hkInternal.lib;hkSerialize.lib;hkGeometryUtilities.lib;hkcdInternal.lib;hkcdCollide.lib;hkpCollide.lib;hkpConstraint.lib;hkpConstraintSolver.lib;hkpDynamics.lib;hkpInternal.lib;hkpUtilities.lib;hkSceneData.lib;hkpVehicle.lib;%(AdditionalDependencies) @@ -100,6 +100,7 @@ + @@ -133,6 +134,7 @@ + @@ -140,6 +142,7 @@ + diff --git a/vs11/Returngeance/Returngeance.vcxproj.filters b/vs11/Returngeance/Returngeance.vcxproj.filters index 346e46f..2b30567 100644 --- a/vs11/Returngeance/Returngeance.vcxproj.filters +++ b/vs11/Returngeance/Returngeance.vcxproj.filters @@ -34,6 +34,9 @@ Systems + + Physics + @@ -48,6 +51,9 @@ {1a6674dd-e1ce-4a28-a6e8-3f28468bb2f0} + + {5f790fcc-08a9-4dd1-96da-9d642acc7ffe} + @@ -139,6 +145,12 @@ Components + + Components + + + Physics +