Car physics not working, yet!
This commit is contained in:
@@ -9,9 +9,10 @@ namespace Components
|
||||
struct Physics : Component
|
||||
{
|
||||
Physics()
|
||||
: Mass(0.f) { }
|
||||
: Mass(0.f), Static(false){}
|
||||
|
||||
float Mass;
|
||||
bool Static;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
#ifndef Components_Vehicle_h__
|
||||
#define Components_Vehicle_h__
|
||||
|
||||
#include "Component.h"
|
||||
|
||||
namespace Components
|
||||
{
|
||||
|
||||
struct Vehicle : Component
|
||||
{
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // Components_Vehicle_h__
|
||||
+23
-2
@@ -5,6 +5,7 @@ void GameWorld::Initialize()
|
||||
{
|
||||
World::Initialize();
|
||||
|
||||
|
||||
{
|
||||
auto camera = CreateEntity();
|
||||
auto transform = AddComponent<Components::Transform>(camera, "Transform");
|
||||
@@ -33,13 +34,14 @@ void GameWorld::Initialize()
|
||||
|
||||
auto physics = AddComponent<Components::Physics>(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<Components::Transform>(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<Components::Model>(ball, "Model");
|
||||
@@ -50,6 +52,23 @@ void GameWorld::Initialize()
|
||||
physics->Mass = 1;
|
||||
}
|
||||
|
||||
{
|
||||
auto car = CreateEntity();
|
||||
auto transform = AddComponent<Components::Transform>(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<Components::Model>(car, "Model");
|
||||
model->ModelFile = "Models/Placeholders/PhysicsTest/Box.obj";
|
||||
auto physics = AddComponent<Components::Physics>(car, "Physics");
|
||||
physics->Mass = 1;
|
||||
auto box = AddComponent<Components::Box>(car, "Box");
|
||||
box->Width = 3;
|
||||
box->Height = 1;
|
||||
box->Depth = 5;
|
||||
auto vehicle = AddComponent<Components::Vehicle>(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()
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
#include "Components/Physics.h"
|
||||
#include "Components/Sphere.h"
|
||||
#include "Components/Box.h"
|
||||
#include "Components/Vehicle.h"
|
||||
|
||||
class GameWorld : public World
|
||||
{
|
||||
|
||||
@@ -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<hkpVehicleDefaultAnalogDriverInput*>(vehicle.m_driverInput));
|
||||
setupComponent(*vehicle.m_data, *static_cast<hkpVehicleDefaultSteering*>(vehicle.m_steering));
|
||||
setupComponent(*vehicle.m_data, *static_cast<hkpVehicleDefaultEngine*>(vehicle.m_engine));
|
||||
setupComponent(*vehicle.m_data, *static_cast<hkpVehicleDefaultTransmission*>(vehicle.m_transmission));
|
||||
setupComponent(*vehicle.m_data, *static_cast<hkpVehicleDefaultBrake*>(vehicle.m_brake));
|
||||
setupComponent(*vehicle.m_data, *static_cast<hkpVehicleDefaultSuspension*>(vehicle.m_suspension));
|
||||
setupComponent(*vehicle.m_data, *static_cast<hkpVehicleDefaultAerodynamics*>(vehicle.m_aerodynamics));
|
||||
setupComponent(*vehicle.m_data, *static_cast<hkpVehicleDefaultVelocityDamper*>(vehicle.m_velocityDamper));
|
||||
|
||||
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.
|
||||
//
|
||||
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;
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
#ifndef Physics_Vehicle_h__
|
||||
#define Physics_Vehicle_h__
|
||||
|
||||
//#include "PrecompiledHeader.h"
|
||||
|
||||
#include <Common/Base/hkBase.h>
|
||||
#include <Common/Base/Memory/System/Util/hkMemoryInitUtil.h>
|
||||
#include <Common/Base/System/Error/hkDefaultError.h>
|
||||
#include <Common/Base/Monitor/hkMonitorStream.h>
|
||||
#include <Common/Base/Config/hkConfigVersion.h>
|
||||
#include <Common/Base/Memory/System/hkMemorySystem.h>
|
||||
#include <Common/Base/Memory/Allocator/Malloc/hkMallocAllocator.h>
|
||||
#include <Common/Base/Container/String/hkStringBuf.h>
|
||||
|
||||
// Vehicle page 425 in documentation
|
||||
#include <Physics2012/Vehicle/hkpVehicleInstance.h>
|
||||
|
||||
#include <Physics2012/Vehicle/AeroDynamics/Default/hkpVehicleDefaultAerodynamics.h>
|
||||
#include <Physics2012/Vehicle/DriverInput/Default/hkpVehicleDefaultAnalogDriverInput.h>
|
||||
#include <Physics2012/Vehicle/Brake/Default/hkpVehicleDefaultBrake.h>
|
||||
#include <Physics2012/Vehicle/Engine/Default/hkpVehicleDefaultEngine.h>
|
||||
#include <Physics2012/Vehicle/VelocityDamper/Default/hkpVehicleDefaultVelocityDamper.h>
|
||||
#include <Physics2012/Vehicle/Steering/Default/hkpVehicleDefaultSteering.h>
|
||||
#include <Physics2012/Vehicle/Suspension/Default/hkpVehicleDefaultSuspension.h>
|
||||
#include <Physics2012/Vehicle/Transmission/Default/hkpVehicleDefaultTransmission.h>
|
||||
#include <Physics2012/Vehicle/WheelCollide/RayCast/hkpVehicleRayCastWheelCollide.h>
|
||||
#include <Physics2012/Vehicle/TyreMarks/hkpTyremarksInfo.h>
|
||||
|
||||
#include <Physics2012/Vehicle/WheelCollide/RayCast/hkpVehicleRayCastWheelCollide.h>
|
||||
|
||||
#include <Physics2012/Collide/Filter/Group/hkpGroupFilter.h>
|
||||
|
||||
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__
|
||||
@@ -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<Components::Transform>(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<Components::Vehicle >(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)
|
||||
{
|
||||
|
||||
|
||||
@@ -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 <Common/Visualize/hkVisualDebugger.h>
|
||||
#include <Physics2012/Utilities/VisualDebugger/hkpPhysicsContext.h>
|
||||
|
||||
|
||||
|
||||
#include "Physics/VehicleSetup.h"
|
||||
|
||||
|
||||
#include <unordered_map>
|
||||
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<EntityID, hkpRigidBody*> m_RigidBodies;
|
||||
std::unordered_map<EntityID, hkpVehicleInstance*> m_Vehicles;
|
||||
|
||||
|
||||
hkpVehicleInstance* Systems::PhysicsSystem::createVehicle(VehicleSetup& vehicleSetup, hkpRigidBody* chassis);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<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;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<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)</AdditionalDependencies>
|
||||
<AdditionalOptions> /ignore:4221</AdditionalOptions>
|
||||
</Link>
|
||||
<CustomBuildStep />
|
||||
@@ -89,7 +89,7 @@
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<AdditionalDependencies>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)</AdditionalDependencies>
|
||||
<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)</AdditionalDependencies>
|
||||
</Link>
|
||||
<CustomBuildStep />
|
||||
</ItemDefinitionGroup>
|
||||
@@ -100,6 +100,7 @@
|
||||
<ClCompile Include="..\..\src\main.cpp" />
|
||||
<ClCompile Include="..\..\src\Model.cpp" />
|
||||
<ClCompile Include="..\..\src\OBJ.cpp" />
|
||||
<ClCompile Include="..\..\src\Physics\VehicleSetup.cpp" />
|
||||
<ClCompile Include="..\..\src\PrecompiledHeader.cpp" />
|
||||
<ClCompile Include="..\..\src\Renderer.cpp" />
|
||||
<ClCompile Include="..\..\src\ShaderProgram.cpp" />
|
||||
@@ -133,6 +134,7 @@
|
||||
<ClInclude Include="..\..\src\Components\Stat.h" />
|
||||
<ClInclude Include="..\..\src\Components\Template.h" />
|
||||
<ClInclude Include="..\..\src\Components\Transform.h" />
|
||||
<ClInclude Include="..\..\src\Components\Vehicle.h" />
|
||||
<ClInclude Include="..\..\src\CubemapTexture.h" />
|
||||
<ClInclude Include="..\..\src\Engine.h" />
|
||||
<ClInclude Include="..\..\src\Entity.h" />
|
||||
@@ -140,6 +142,7 @@
|
||||
<ClInclude Include="..\..\src\GameWorld.h" />
|
||||
<ClInclude Include="..\..\src\Model.h" />
|
||||
<ClInclude Include="..\..\src\OBJ.h" />
|
||||
<ClInclude Include="..\..\src\Physics\VehicleSetup.h" />
|
||||
<ClInclude Include="..\..\src\PrecompiledHeader.h" />
|
||||
<ClInclude Include="..\..\src\Renderer.h" />
|
||||
<ClInclude Include="..\..\src\ShaderProgram.h" />
|
||||
|
||||
@@ -34,6 +34,9 @@
|
||||
<ClCompile Include="..\..\src\Systems\SoundSystem.cpp">
|
||||
<Filter>Systems</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\src\Physics\VehicleSetup.cpp">
|
||||
<Filter>Physics</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Filter Include="Util">
|
||||
@@ -48,6 +51,9 @@
|
||||
<Filter Include="Systems">
|
||||
<UniqueIdentifier>{1a6674dd-e1ce-4a28-a6e8-3f28468bb2f0}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="Physics">
|
||||
<UniqueIdentifier>{5f790fcc-08a9-4dd1-96da-9d642acc7ffe}</UniqueIdentifier>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="..\..\src\World.h" />
|
||||
@@ -139,6 +145,12 @@
|
||||
<ClInclude Include="..\..\src\Components\Box.h">
|
||||
<Filter>Components</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\Components\Vehicle.h">
|
||||
<Filter>Components</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\Physics\VehicleSetup.h">
|
||||
<Filter>Physics</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="..\..\src\Shaders\AABB.frag.glsl">
|
||||
|
||||
Reference in New Issue
Block a user