From ed1df6dc60edb0378f6e84e85c83ed4c9879089d Mon Sep 17 00:00:00 2001 From: ViktorLjung Date: Mon, 28 Apr 2014 19:51:13 +0200 Subject: [PATCH] Proper rotation of child physics objects --- src/Components/Vehicle.h | 4 +- src/GameWorld.cpp | 123 +++++++++++++++++++++++----------- src/Physics/VehicleSetup.cpp | 8 +-- src/Renderer.cpp | 2 +- src/Systems/PhysicsSystem.cpp | 89 ++++++++++++++++++------ src/Systems/PhysicsSystem.h | 1 + src/Texture.cpp | 3 + vs11/Returngeance.sln | 3 + 8 files changed, 166 insertions(+), 67 deletions(-) diff --git a/src/Components/Vehicle.h b/src/Components/Vehicle.h index 9e4a59e..3892e8f 100644 --- a/src/Components/Vehicle.h +++ b/src/Components/Vehicle.h @@ -10,7 +10,8 @@ namespace Components struct Vehicle : Component { Vehicle() - : MaxTorque(500.0f), MinRPM(800.0f), OptimalRPM(4000.0f), MaxRPM(6000.0f), MaxSteeringAngle(35), TopSpeed(50.0f) { } + : MaxTorque(1000.0f), MinRPM(1000.0f), OptimalRPM(3000.0f), MaxRPM(4000.0f), MaxSteeringAngle(35), TopSpeed(130.0f), + MaxSpeedFullSteeringAngle(40.0f){ } float MaxTorque; float MinRPM; @@ -19,6 +20,7 @@ struct Vehicle : Component // Degrees float MaxSteeringAngle; float TopSpeed; + float MaxSpeedFullSteeringAngle; }; } diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index bdf3f29..a120fb8 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -64,37 +64,46 @@ void GameWorld::Initialize() auto vehicle = AddComponent(jeep, "Vehicle"); - auto light = AddComponent(jeep, "PointLight"); - light->Diffuse = glm::vec3(0, 1, 0); - light->constantAttenuation = 0.0005f; - light->linearAttenuation = 0.001f; - light->quadraticAttenuation = 0.001f; - AddComponent(jeep, "Input"); { auto chassis = CreateEntity(jeep); auto transform = AddComponent(chassis, "Transform"); - transform->Position = glm::vec3(0, -0.6577f, 0); + transform->Position = glm::vec3(0, 0, 0); // 0.6577f auto model = AddComponent(chassis, "Model"); model->ModelFile = "Models/JeepV2/Chassi/chassi.OBJ"; } + { + auto lightentity = CreateEntity(jeep); + 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; + } + + float wheelOffset = -0.2f; + float hardPointOffset = 0.0f; + { auto wheel = CreateEntity(jeep); auto transform = AddComponent(wheel, "Transform"); - transform->Position = glm::vec3(1.4f, 0.5546f - 0.6577f - 0.2, -0.9242f); + transform->Position = glm::vec3(1.9f, 0.5546f - 0.2 + wheelOffset, -0.9242f); transform->Scale = glm::vec3(1.0f); auto model = AddComponent(wheel, "Model"); model->ModelFile = "Models/JeepV2/WheelFront/wheelFront.obj"; auto Wheel = AddComponent(wheel, "Wheel"); - Wheel->Hardpoint = transform->Position + glm::vec3(0.f, 1.f, 0.f); + Wheel->Hardpoint = transform->Position + glm::vec3(0.f, 1.f - hardPointOffset, 0.f); Wheel->AxleID = 0; Wheel->Mass = 50; Wheel->Radius = 0.837f; Wheel->Steering = true; - Wheel->SuspensionStrength = 40.f; - Wheel->Friction = 3.0f; + Wheel->SuspensionStrength = 50.f; + Wheel->Friction = 3.5f; Wheel->ConnectedToHandbrake = true; CommitEntity(wheel); } @@ -102,19 +111,19 @@ void GameWorld::Initialize() { auto wheel = CreateEntity(jeep); auto transform = AddComponent(wheel, "Transform"); - transform->Position = glm::vec3(-1.4f, 0.5546f - 0.6577f - 0.2, -0.9242f); + transform->Position = glm::vec3(-1.9f, 0.5546f - 0.2 + wheelOffset, -0.9242f); transform->Scale = glm::vec3(1.0f); transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 0, 1)); auto model = AddComponent(wheel, "Model"); model->ModelFile = "Models/JeepV2/WheelFront/wheelFront.obj"; auto Wheel = AddComponent(wheel, "Wheel"); - Wheel->Hardpoint = transform->Position + glm::vec3(0.f, 1.f, 0.f); + Wheel->Hardpoint = transform->Position + glm::vec3(0.f, 1.f - hardPointOffset, 0.f); Wheel->AxleID = 0; Wheel->Mass = 50; Wheel->Radius = 0.837f; Wheel->Steering = true; - Wheel->SuspensionStrength = 40.f; - Wheel->Friction = 3.0f; + Wheel->SuspensionStrength = 50.f; + Wheel->Friction = 3.5f; Wheel->ConnectedToHandbrake = true; CommitEntity(wheel); } @@ -122,17 +131,17 @@ void GameWorld::Initialize() { auto wheel = CreateEntity(jeep); auto transform = AddComponent(wheel, "Transform"); - transform->Position = glm::vec3(0.2726f, 0.2805f - 0.6577f, 1.9307f); + transform->Position = glm::vec3(0.2726f, 0.2805f + wheelOffset, 1.9307f); auto model = AddComponent(wheel, "Model"); model->ModelFile = "Models/JeepV2/WheelBack/wheelBack.obj"; auto Wheel = AddComponent(wheel, "Wheel"); - Wheel->Hardpoint = transform->Position + glm::vec3(0.f, 1.f, 0.f); + Wheel->Hardpoint = transform->Position + glm::vec3(0.f, 1.f - hardPointOffset, 0.f); Wheel->AxleID = 1; - Wheel->Mass = 10; + Wheel->Mass = 50; Wheel->Radius = 0.737f; Wheel->Steering = false; - Wheel->SuspensionStrength = 20.f; - Wheel->Friction = 3.0f; + Wheel->SuspensionStrength = 40.f; + Wheel->Friction = 3.5f; Wheel->ConnectedToHandbrake = true; CommitEntity(wheel); } @@ -140,44 +149,78 @@ void GameWorld::Initialize() { auto wheel = CreateEntity(jeep); auto transform = AddComponent(wheel, "Transform"); - transform->Position = glm::vec3(-0.2726f, 0.2805f - 0.6577f, 1.9307f); + transform->Position = glm::vec3(-0.2726f, 0.2805f + wheelOffset, 1.9307f); transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 0, 1)); auto model = AddComponent(wheel, "Model"); model->ModelFile = "Models/JeepV2/WheelBack/wheelBack.obj"; auto Wheel = AddComponent(wheel, "Wheel"); - Wheel->Hardpoint = transform->Position + glm::vec3(0.f, 1.f, 0.f); + Wheel->Hardpoint = transform->Position + glm::vec3(0.f, 1.f - hardPointOffset, 0.f); Wheel->AxleID = 1; - Wheel->Mass = 10; + Wheel->Mass = 50; Wheel->Radius = 0.737f; Wheel->Steering = false; - Wheel->SuspensionStrength = 20.f; - Wheel->Friction = 3.0f; + Wheel->SuspensionStrength = 40.f; + Wheel->Friction = 3.5f; Wheel->ConnectedToHandbrake = true; CommitEntity(wheel); } CommitEntity(jeep); } - - for (int x = 0; x < 5; x++) - for (int y = 0; y < 5; y++) + /* + for(int i = 0; i < 10; i++) { - auto cube = CreateEntity(); - auto transform = AddComponent(cube, "Transform"); - transform->Position = glm::vec3(3 * x + 0.1f + 20.f, 3 * y + 0.1f + 1.f, 0); - transform->Scale = glm::vec3(3); + auto entity = CreateEntity(); + auto transform = AddComponent(entity, "Transform"); + transform->Position = glm::vec3(30 + i*0.1f, 0 + i*0.1f, 10 + i*0.1f); + transform->Scale = glm::vec3(0); transform->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, 0.0f)); - auto model = AddComponent(cube, "Model"); - model->ModelFile = "Models/Placeholders/PhysicsTest/Cube2.obj"; + + std::stringstream ss; + ss << "Models/Placeholders/ShatterTest/" << i+1 << ".obj"; - auto physics = AddComponent(cube, "Physics"); + auto model = AddComponent(entity, "Model"); + model->ModelFile = ss.str(); + + auto physics = AddComponent(entity, "Physics"); physics->Mass = 100; - auto box = AddComponent(cube, "BoxShape"); - box->Width = 1.5f; - box->Height = 1.5f; - box->Depth = 1.5f; - CommitEntity(cube); + physics->Static = true; + auto meshShape = AddComponent(entity, "MeshShape"); + meshShape->ResourceName = ss.str(); + + CommitEntity(entity); + }*/ + + { + auto wall = CreateEntity(); + auto transform = AddComponent(wall, "Transform"); + transform->Position = glm::vec3(10, 0, -20); + transform->Orientation = glm::angleAxis(glm::pi()/2.f, glm::vec3(0, 1, 0)); + + for (int y = 0; y < 10; y++) + { + for (int x = -5; x < 5; x++) + { + auto brick = CreateEntity(wall); + auto transform = AddComponent(brick, "Transform"); + transform->Position = glm::vec3(x + 0.01f, y*0.3f + 0.01f, 0); + transform->Position.x += (y % 2)*0.5f; + transform->Scale = glm::vec3(1, 0.3f, 0.6f); + transform->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, 0.0f)); + auto model = AddComponent(brick, "Model"); + model->ModelFile = "Models/Placeholders/PhysicsTest/Cube2.obj"; + + auto physics = AddComponent(brick, "Physics"); + physics->Mass = 1; + auto box = AddComponent(brick, "BoxShape"); + box->Width = 0.5f; + box->Height = 0.15f; + box->Depth = 0.3f; + CommitEntity(brick); + } } + CommitEntity(wall); + } /*for (int x = 0; x < 5; x++) for (int y = 0; y < 5; y++) diff --git a/src/Physics/VehicleSetup.cpp b/src/Physics/VehicleSetup.cpp index 1768b1e..5041b9d 100644 --- a/src/Physics/VehicleSetup.cpp +++ b/src/Physics/VehicleSetup.cpp @@ -165,7 +165,7 @@ void VehicleSetup::setupComponent(const hkpVehicleData& data, hkpVehicleDefaultS // [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); //MPH???! + steering.m_maxSpeedFullSteeringAngle = vehicleComponent.MaxSpeedFullSteeringAngle; // * (1.605f / 3.6f); //MPH???! for (int i = 0; i < m_Wheels.size(); i++) { @@ -198,8 +198,8 @@ void VehicleSetup::setupComponent(const hkpVehicleData& data, hkpVehicleDefaultT transmission.m_gearsRatio.setSize(numberOfGears); transmission.m_wheelsTorqueRatio.setSize(data.m_numWheels); - transmission.m_downshiftRPM = 3500.0f; - transmission.m_upshiftRPM = 6500.0f; + transmission.m_downshiftRPM = 1500.0f; + transmission.m_upshiftRPM = 3500.0f; transmission.m_clutchDelayTime = 0.0f; transmission.m_reverseGearRatio = 1.0f; @@ -267,7 +267,7 @@ void VehicleSetup::setupComponent(const hkpVehicleData& data, hkpVehicleDefaultA 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); + aerodynamics.m_extraGravityws.set(0.0f, -0.982f, 0.0f); // fuck this shit } void VehicleSetup::setupComponent(const hkpVehicleData& data, hkpVehicleDefaultVelocityDamper& velocityDamper, Components::Vehicle vehicleComponent) diff --git a/src/Renderer.cpp b/src/Renderer.cpp index e7dfed6..6cc7dc2 100755 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -191,7 +191,7 @@ void Renderer::DrawSkybox() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); m_ShaderProgramSkybox.Bind(); - glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix() * glm::toMat4(m_Camera->Orientation()); + glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix() * glm::toMat4(glm::inverse(m_Camera->Orientation())); glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgramSkybox.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(cameraMatrix)); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); m_Skybox->Draw(); diff --git a/src/Systems/PhysicsSystem.cpp b/src/Systems/PhysicsSystem.cpp index ab19101..1b8bc81 100644 --- a/src/Systems/PhysicsSystem.cpp +++ b/src/Systems/PhysicsSystem.cpp @@ -129,21 +129,21 @@ void Systems::PhysicsSystem::Update(double dt) continue; - if(m_RigidBodies[entity]->isActive()) + /*if(m_RigidBodies[entity]->isActive()) { m_PhysicsWorld->markForWrite(); 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); m_PhysicsWorld->unmarkForWrite(); - } + }*/ } - static const double timestep = 1 / 30.0; + static const double timestep = 1 / 60.0; m_Accumulator += dt; while (m_Accumulator >= timestep) { @@ -189,36 +189,84 @@ void Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID p m_PhysicsWorld->unmarkForWrite(); } } - else if(m_Vehicles.find(entity) != m_Vehicles.end()) - { - m_PhysicsWorld->markForWrite(); - hkVector4 position = m_RigidBodies[entity]->getPosition(); - transformComponent->Position = glm::vec3(position(0), position(1), position(2)); - hkQuaternion orientation = m_RigidBodies[entity]->getRotation(); - transformComponent->Orientation = glm::quat(orientation(3), orientation(0), orientation(1), orientation(2)); - m_PhysicsWorld->unmarkForWrite(); - } else if(m_RigidBodies.find(entity) != m_RigidBodies.end()) { - m_PhysicsWorld->markForWrite(); + auto transformComponentParent = m_World->GetComponent(parent, "Transform"); + //m_PhysicsWorld->markForWrite(); hkVector4 position = m_RigidBodies[entity]->getPosition(); transformComponent->Position = glm::vec3(position(0), position(1), position(2)); + if (transformComponentParent) + { + transformComponent->Position -= transformComponentParent->Position; + transformComponent->Position = transformComponent->Position * transformComponentParent->Orientation; + } hkQuaternion orientation = m_RigidBodies[entity]->getRotation(); transformComponent->Orientation = glm::quat(orientation(3),orientation(0), orientation(1), orientation(2)); - m_PhysicsWorld->unmarkForWrite(); + if (transformComponentParent) + { + transformComponent->Orientation = transformComponent->Orientation * glm::inverse(transformComponentParent->Orientation); + } + //m_PhysicsWorld->unmarkForWrite(); } // HACK: Vehicle test-controls auto vehicleComponent = m_World->GetComponent(entity, "Vehicle"); auto inputComponent = m_World->GetComponent(entity, "Input"); - if (vehicleComponent && inputComponent) + if (vehicleComponent && inputComponent && m_Vehicles.find(entity) != m_Vehicles.end() && m_RigidBodies.find(entity) != m_RigidBodies.end()) { m_PhysicsWorld->markForWrite(); hkpVehicleDriverInputAnalogStatus* deviceStatus = (hkpVehicleDriverInputAnalogStatus*)m_Vehicles[entity]->m_deviceStatus; - deviceStatus->m_positionY = inputComponent->KeyState[GLFW_KEY_UP] * -1 + inputComponent->KeyState[GLFW_KEY_DOWN] * 1; - deviceStatus->m_positionX = inputComponent->KeyState[GLFW_KEY_LEFT] * -1 + inputComponent->KeyState[GLFW_KEY_RIGHT] * 1; + + + if(inputComponent->KeyState[GLFW_KEY_UP] != 0 || inputComponent->KeyState[GLFW_KEY_DOWN] != 0) + { + deviceStatus->m_positionY += inputComponent->KeyState[GLFW_KEY_UP] * -1 * 0.05f + inputComponent->KeyState[GLFW_KEY_DOWN] * 1 * 0.05f; + } + else + { + deviceStatus->m_positionY = 0; + } + + if(deviceStatus->m_positionY > 1) + deviceStatus->m_positionY = 1; + else if(deviceStatus->m_positionY < -1) + deviceStatus->m_positionY = -1; + + + + if(inputComponent->KeyState[GLFW_KEY_LEFT] != 0 || inputComponent->KeyState[GLFW_KEY_RIGHT] != 0) + { + deviceStatus->m_positionX += inputComponent->KeyState[GLFW_KEY_LEFT] * -1 * 0.01f + inputComponent->KeyState[GLFW_KEY_RIGHT] * 1 * 0.01f; + } + else + { + if(deviceStatus->m_positionX > 0) + { + deviceStatus->m_positionX += -1 * 0.01f; + } + else if(deviceStatus->m_positionX < 0) + { + deviceStatus->m_positionX += 1 * 0.01f; + } + } + + if(deviceStatus->m_positionX > 1) + deviceStatus->m_positionX = 1; + else if(deviceStatus->m_positionX < -1) + deviceStatus->m_positionX = -1; + + deviceStatus->m_handbrakeButtonPressed = inputComponent->KeyState[GLFW_KEY_RIGHT_CONTROL]; + + if(inputComponent->KeyState[GLFW_KEY_R]) + { + transformComponent->Position = glm::vec3(0, 10, 0); + transformComponent->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, 0.0f)); + m_RigidBodies[entity]->setLinearVelocity(hkVector4(0, 0, 0)); + m_RigidBodies[entity]->setAngularVelocity(hkVector4(0, 0, 0)); + } + m_PhysicsWorld->unmarkForWrite(); } } @@ -322,8 +370,6 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) part.m_stridingType = hkpExtendedMeshShape::INDICES_INT16; - - mesh->addTrianglesSubpart(part); } @@ -347,8 +393,9 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) return; } - - rigidBodyInfo.m_position.set(transformComponent->Position.x, transformComponent->Position.y, transformComponent->Position.z); + auto absoluteTransform = m_World->GetSystem("TransformSystem")->AbsoluteTransform(entity); + rigidBodyInfo.m_position.set(absoluteTransform.Position.x, absoluteTransform.Position.y, absoluteTransform.Position.z); + rigidBodyInfo.m_rotation.set(absoluteTransform.Orientation.x, absoluteTransform.Orientation.y, absoluteTransform.Orientation.z, absoluteTransform.Orientation.w); rigidBodyInfo.m_inertiaTensor = massProperties.m_inertiaTensor; //rigidBodyInfo.m_centerOfMass = massProperties.m_centerOfMass; rigidBodyInfo.m_mass = massProperties.m_mass; diff --git a/src/Systems/PhysicsSystem.h b/src/Systems/PhysicsSystem.h index 82a0222..bee1039 100644 --- a/src/Systems/PhysicsSystem.h +++ b/src/Systems/PhysicsSystem.h @@ -2,6 +2,7 @@ #define PhysicsSystem_h__ #include "System.h" +#include "Systems/TransformSystem.h" #include "Components/Transform.h" #include "Components/Physics.h" #include "Components/BoxShape.h" diff --git a/src/Texture.cpp b/src/Texture.cpp index 49256da..373db0d 100755 --- a/src/Texture.cpp +++ b/src/Texture.cpp @@ -15,6 +15,9 @@ void Texture::Load(std::string path) } m_Texture = m_TextureCache[path]; + glBindTexture(GL_TEXTURE_2D, m_Texture); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); } void Texture::Bind() diff --git a/vs11/Returngeance.sln b/vs11/Returngeance.sln index 10ce52b..8daf9b5 100644 --- a/vs11/Returngeance.sln +++ b/vs11/Returngeance.sln @@ -39,4 +39,7 @@ Global GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(Performance) = preSolution + HasPerformanceSessions = true + EndGlobalSection EndGlobal