This commit is contained in:
det-fys 2024-03-30 16:49:56 +01:00
parent 525160a978
commit dd7d2360cd
6 changed files with 332 additions and 72 deletions

5
.gitignore vendored
View File

@ -360,4 +360,7 @@ MigrationBackup/
.ionide/
# Fody - auto-generated XML schema
FodyWeavers.xsd
FodyWeavers.xsd
#vcpkg dependencies
vcpkg_installed/

81
camera.cpp Normal file
View File

@ -0,0 +1,81 @@
#include "camera.hpp"
#include <algorithm>
using namespace TSR;
void Camera::UpdateVectors() {
auto yaw_rad = glm::radians(yaw);
auto pitch_rad = glm::radians(pitch);
front_vector = glm::normalize(glm::vec3(cos(yaw_rad) * glm::cos(pitch_rad), glm::sin(pitch_rad), glm::sin(yaw_rad) * glm::cos(pitch_rad)));
forward_backward_vector = glm::normalize(glm::vec3(front_vector.x, 0.0f, front_vector.z));
right_vector = glm::normalize(glm::cross(front_vector, world_up));
up_vector = glm::normalize(glm::cross(right_vector, front_vector));
}
Camera::Camera(glm::vec3 i_position, float i_movement_speed, float i_mouse_sensitivity, float i_third_person_distance, float i_yaw, float i_pitch) :
position(i_position),
movement_speed(i_movement_speed),
mouse_sensitivity(i_mouse_sensitivity),
third_person_distance(i_third_person_distance),
yaw(i_yaw),
pitch(i_pitch),
world_up(glm::vec3(0.0f, 1.0f, 0.0f))
{
UpdateVectors();
}
glm::mat4 Camera::GetViewMatrix(bool firstperson) {
float space = 0.3f;
auto dist = firstperson ? 0.0f : third_person_distance;
//if (world) {
// auto to_glm = position - front_vector * (third_person_distance + space);
// auto position_bt = btVector3(position.x, position.y, position.z);
//
// auto to_bt = btVector3(to_glm.x, to_glm.y, to_glm.z);
// btCollisionWorld::ClosestRayResultCallback ray_callback(position_bt, to_bt);
// world->rayTest(position_bt, to_bt, ray_callback);
// if (ray_callback.hasHit()) {
// to_glm.x = ray_callback.m_hitPointWorld.x();
// to_glm.y = ray_callback.m_hitPointWorld.y();
// to_glm.z = ray_callback.m_hitPointWorld.z();
// dist = glm::distance(position, to_glm) - space;
// }
//}
return glm::lookAt(position - front_vector * dist, position + front_vector, up_vector);
}
void Camera::ProcessMovement(Movement direction, float time) {
float diff = movement_speed * time;
switch (direction) {
case Movement::FORWARD:
position += forward_backward_vector * diff;
break;
case Movement::BACKWARD:
position -= forward_backward_vector * diff;
break;
case Movement::RIGHT:
position += right_vector * diff;
break;
case Movement::LEFT:
position -= right_vector * diff;
break;
case Movement::UP:
position += world_up * diff;
break;
case Movement::DOWN:
position -= world_up * diff;
break;
}
}
void Camera::ProcessMouse(float x_offset, float y_offset) {
yaw += x_offset * mouse_sensitivity;
//pitch = std::min(89.0f, std::max(-89.0f, pitch + y_offset * mouse_sensitivity));
pitch = std::min(89.9f, std::max(-89.9f, pitch + y_offset * mouse_sensitivity));
UpdateVectors();
}

33
camera.hpp Normal file
View File

@ -0,0 +1,33 @@
#pragma once
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
namespace TSR {
class Camera {
glm::vec3 world_up;
void UpdateVectors();
public:
glm::vec3 forward_backward_vector;
glm::vec3 front_vector;
glm::vec3 up_vector;
glm::vec3 right_vector;
enum class Movement { FORWARD, BACKWARD, LEFT, RIGHT, UP, DOWN };
glm::vec3 position;
float yaw;
float pitch;
float movement_speed;
float mouse_sensitivity;
float third_person_distance;
Camera(glm::vec3 i_position = glm::vec3(0.0f), float i_movement_speed = 20.0f, float i_mouse_sensitivity = 0.1f, float i_third_person_distance = 0.0f, float i_yaw = 0.0f, float i_pitch = 0.0f);
glm::mat4 GetViewMatrix(bool firstperson = false);
void ProcessMovement(Movement direction, float time);
void ProcessMouse(float x_offset, float y_offset);
};
}

266
karo.cpp
View File

@ -20,6 +20,8 @@
#include <vehicle/PxVehicleSDK.h>
//#include <physx/PxFiltering.h>
#include "camera.hpp"
using namespace physx;
struct Vertex {
@ -358,7 +360,7 @@ enum
static PxF32 gTireFrictionMultipliers[MAX_NUM_SURFACE_TYPES][MAX_NUM_TIRE_TYPES] =
{
//NORMAL, WORN
{1.00f, 0.1f}//TARMAC
{30.0f, 0.1f}//TARMAC
};
PxVehicleDrivableSurfaceToTireFrictionPairs* createFrictionPairs(const PxMaterial* defaultMaterial)
@ -420,29 +422,29 @@ PxRigidStatic* createDrivablePlane(const PxFilterData& simFilterData, PxMaterial
return groundPlane;
}
struct ActorUserData
{
ActorUserData()
: vehicle(NULL),
actor(NULL)
{
}
const PxVehicleWheels* vehicle;
const PxActor* actor;
};
struct ShapeUserData
{
ShapeUserData()
: isWheel(false),
wheelId(0xffffffff)
{
}
bool isWheel;
PxU32 wheelId;
};
//struct ActorUserData
//{
// ActorUserData()
// : vehicle(NULL),
// actor(NULL)
// {
// }
//
// const PxVehicleWheels* vehicle;
// const PxActor* actor;
//};
//
//struct ShapeUserData
//{
// ShapeUserData()
// : isWheel(false),
// wheelId(0xffffffff)
// {
// }
//
// bool isWheel;
// PxU32 wheelId;
//};
struct VehicleDesc
@ -457,9 +459,9 @@ struct VehicleDesc
wheelWidth(0.0f),
wheelRadius(0.0f),
wheelMOI(0.0f),
wheelMaterial(NULL),
actorUserData(NULL),
shapeUserDatas(NULL)
wheelMaterial(NULL)
//actorUserData(NULL),
//shapeUserDatas(NULL)
{
}
@ -478,8 +480,8 @@ struct VehicleDesc
PxU32 numWheels;
PxFilterData wheelSimFilterData; //word0 = collide type, word1 = collide against types, word2 = PxPairFlags
ActorUserData* actorUserData;
ShapeUserData* shapeUserDatas;
//ActorUserData* actorUserData;
//ShapeUserData* shapeUserDatas;
};
@ -519,7 +521,7 @@ VehicleDesc initVehicleDesc()
vehicleDesc.wheelMOI = wheelMOI;
vehicleDesc.numWheels = nbWheels;
vehicleDesc.wheelMaterial = s_material;
vehicleDesc.chassisSimFilterData = PxFilterData(COLLISION_FLAG_WHEEL, COLLISION_FLAG_WHEEL_AGAINST, 0, 0);
vehicleDesc.wheelSimFilterData = PxFilterData(COLLISION_FLAG_WHEEL, COLLISION_FLAG_WHEEL_AGAINST, 0, 0);
return vehicleDesc;
}
@ -665,6 +667,8 @@ void setupWheelsSimulationData
wheels[i].mMOI = wheelMOI;
wheels[i].mRadius = wheelRadius;
wheels[i].mWidth = wheelWidth;
wheels[i].mMaxBrakeTorque = 2000.0f;
}
//Enable the handbrake for the rear wheels only.
@ -780,27 +784,27 @@ void setupWheelsSimulationData
wheelsSimData->addAntiRollBarData(barRear);
}
void configureUserData(PxVehicleWheels* vehicle, ActorUserData* actorUserData, ShapeUserData* shapeUserDatas)
{
if (actorUserData)
{
vehicle->getRigidDynamicActor()->userData = actorUserData;
actorUserData->vehicle = vehicle;
}
if (shapeUserDatas)
{
PxShape* shapes[PX_MAX_NB_WHEELS + 1];
vehicle->getRigidDynamicActor()->getShapes(shapes, PX_MAX_NB_WHEELS + 1);
for (PxU32 i = 0; i < vehicle->mWheelsSimData.getNbWheels(); i++)
{
const PxI32 shapeId = vehicle->mWheelsSimData.getWheelShapeMapping(i);
shapes[shapeId]->userData = &shapeUserDatas[i];
shapeUserDatas[i].isWheel = true;
shapeUserDatas[i].wheelId = i;
}
}
}
//void configureUserData(PxVehicleWheels* vehicle, ActorUserData* actorUserData, ShapeUserData* shapeUserDatas)
//{
// if (actorUserData)
// {
// vehicle->getRigidDynamicActor()->userData = actorUserData;
// actorUserData->vehicle = vehicle;
// }
//
// if (shapeUserDatas)
// {
// PxShape* shapes[PX_MAX_NB_WHEELS + 1];
// vehicle->getRigidDynamicActor()->getShapes(shapes, PX_MAX_NB_WHEELS + 1);
// for (PxU32 i = 0; i < vehicle->mWheelsSimData.getNbWheels(); i++)
// {
// const PxI32 shapeId = vehicle->mWheelsSimData.getWheelShapeMapping(i);
// shapes[shapeId]->userData = &shapeUserDatas[i];
// shapeUserDatas[i].isWheel = true;
// shapeUserDatas[i].wheelId = i;
// }
// }
//}
PxVehicleDrive4W* createVehicle4W(const VehicleDesc& vehicle4WDesc, PxPhysics* physics, PxCooking* cooking)
@ -876,7 +880,8 @@ PxVehicleDrive4W* createVehicle4W(const VehicleDesc& vehicle4WDesc, PxPhysics* p
{
//Diff
PxVehicleDifferential4WData diff;
diff.mType = PxVehicleDifferential4WData::eDIFF_TYPE_LS_4WD;
//diff.mType = PxVehicleDifferential4WData::eDIFF_TYPE_LS_4WD;
diff.mType = PxVehicleDifferential4WData::eDIFF_TYPE_OPEN_FRONTWD;
driveSimData.setDiffData(diff);
//Engine
@ -887,7 +892,7 @@ PxVehicleDrive4W* createVehicle4W(const VehicleDesc& vehicle4WDesc, PxPhysics* p
//Gears
PxVehicleGearsData gears;
gears.mSwitchTime = 0.5f;
gears.mSwitchTime = 0.2f;
driveSimData.setGearsData(gears);
//Clutch
@ -915,7 +920,7 @@ PxVehicleDrive4W* createVehicle4W(const VehicleDesc& vehicle4WDesc, PxPhysics* p
vehDrive4W->setup(physics, veh4WActor, *wheelsSimData, driveSimData, numWheels - 4);
//Configure the userdata
configureUserData(vehDrive4W, vehicle4WDesc.actorUserData, vehicle4WDesc.shapeUserDatas);
//configureUserData(vehDrive4W, vehicle4WDesc.actorUserData, vehicle4WDesc.shapeUserDatas);
//Free the sim data because we don't need that any more.
wheelsSimData->free();
@ -980,7 +985,7 @@ static void InitPhysics() {
bool enable = true;
m_scene->setVisualizationParameter(PxVisualizationParameter::eSCALE, enable ? 1.0f : 0.0f);
//m_scene->setVisualizationParameter(PxVisualizationParameter::eACTOR_AXES, enable ? 2.0f : 0.0f);
m_scene->setVisualizationParameter(PxVisualizationParameter::eACTOR_AXES, enable ? 2.0f : 0.0f);
////m_scene->setVisualizationParameter(PxVisualizationParameter::eCOLLISION_EDGES, enable ? 1.0f : 0.0f);
////m_scene->setVisualizationParameter(PxVisualizationParameter::eCOLLISION_COMPOUNDS, enable ? 1.0f : 0.0f);
//m_scene->setVisualizationParameter(PxVisualizationParameter::eCOLLISION_AABBS, enable ? 1.0f : 0.0f);
@ -1043,7 +1048,7 @@ static void InitPhysics() {
//Create a vehicle that will drive on the plane.
VehicleDesc vehicleDesc = initVehicleDesc();
gVehicle4W = createVehicle4W(vehicleDesc, s_physics, s_cooking);
PxTransform startTransform(PxVec3(0, (vehicleDesc.chassisDims.y * 0.5f + vehicleDesc.wheelRadius + 1.0f), 0), PxQuat(PxIdentity));
PxTransform startTransform(PxVec3(0, (vehicleDesc.chassisDims.y * 0.5f + vehicleDesc.wheelRadius + 1.0f) + 2.0, 0), PxQuat(PxIdentity));
gVehicle4W->getRigidDynamicActor()->setGlobalPose(startTransform);
s_scene->addActor(*gVehicle4W->getRigidDynamicActor());
@ -1055,30 +1060,60 @@ static void InitPhysics() {
}
//PxVehicleKeySmoothingData gKeySmoothingData =
//{
// {
// 6.0f, //rise rate eANALOG_INPUT_ACCEL
// 6.0f, //rise rate eANALOG_INPUT_BRAKE
// 6.0f, //rise rate eANALOG_INPUT_HANDBRAKE
// 2.5f, //rise rate eANALOG_INPUT_STEER_LEFT
// 2.5f, //rise rate eANALOG_INPUT_STEER_RIGHT
// },
// {
// 10.0f, //fall rate eANALOG_INPUT_ACCEL
// 10.0f, //fall rate eANALOG_INPUT_BRAKE
// 10.0f, //fall rate eANALOG_INPUT_HANDBRAKE
// 5.0f, //fall rate eANALOG_INPUT_STEER_LEFT
// 5.0f //fall rate eANALOG_INPUT_STEER_RIGHT
// }
//};
PxVehicleKeySmoothingData gKeySmoothingData =
{
{
6.0f, //rise rate eANALOG_INPUT_ACCEL
6.0f, //rise rate eANALOG_INPUT_BRAKE
4.0f, //rise rate eANALOG_INPUT_ACCEL
10.0f, //rise rate eANALOG_INPUT_BRAKE
6.0f, //rise rate eANALOG_INPUT_HANDBRAKE
2.5f, //rise rate eANALOG_INPUT_STEER_LEFT
2.5f, //rise rate eANALOG_INPUT_STEER_RIGHT
5.0f, //rise rate eANALOG_INPUT_STEER_LEFT
5.0f, //rise rate eANALOG_INPUT_STEER_RIGHT
},
{
10.0f, //fall rate eANALOG_INPUT_ACCEL
10.0f, //fall rate eANALOG_INPUT_BRAKE
10.0f, //fall rate eANALOG_INPUT_HANDBRAKE
5.0f, //fall rate eANALOG_INPUT_STEER_LEFT
5.0f //fall rate eANALOG_INPUT_STEER_RIGHT
7.0f, //fall rate eANALOG_INPUT_STEER_LEFT
7.0f //fall rate eANALOG_INPUT_STEER_RIGHT
}
};
//PxF32 gSteerVsForwardSpeedData[2 * 8] =
//{
// 0.0f, 0.75f,
// 5.0f, 0.75f,
// 30.0f, 0.125f,
// 120.0f, 0.06f,
// PX_MAX_F32, PX_MAX_F32,
// PX_MAX_F32, PX_MAX_F32,
// PX_MAX_F32, PX_MAX_F32,
// PX_MAX_F32, PX_MAX_F32
//};
PxF32 gSteerVsForwardSpeedData[2 * 8] =
{
0.0f, 0.75f,
5.0f, 0.75f,
30.0f, 0.125f,
120.0f, 0.1f,
5.0f, 0.35f,
30.0f, 0.06f,
120.0f, 0.02f,
PX_MAX_F32, PX_MAX_F32,
PX_MAX_F32, PX_MAX_F32,
PX_MAX_F32, PX_MAX_F32,
@ -1090,6 +1125,8 @@ PxVehicleDrive4WRawInputData gVehicleInputData;
bool gIsVehicleInAir = true;
static TSR::Camera s_cam;
static void PhysicsFrame() {
auto timestep = 1.0f / 75.0f;
@ -1123,6 +1160,8 @@ static void PhysicsFrame() {
s_scene->simulate(timestep);
s_scene->fetchResults(true);
}
static glm::vec4 U32ColorToVec4(uint32_t rgba_value) {
@ -1163,6 +1202,7 @@ static void ShutdownPhysics() {
static int s_count = 0;
static bool s_first_person = false;
int main() {
if (!glfwInit()) {
@ -1258,9 +1298,9 @@ int main() {
glfwSetWindowShouldClose(window, true);
break;
case GLFW_KEY_SPACE:
s_box1->addForce(PxVec3(0.0f, 100.0f, 0.0f));
break;
//case GLFW_KEY_SPACE:
// s_box1->addForce(PxVec3(0.0f, 100.0f, 0.0f));
// break;
case GLFW_KEY_E:
//for (int i = 0; i < 1; i++) {
@ -1278,10 +1318,29 @@ int main() {
printf("pocet: %d\n", s_count);
break;
case GLFW_KEY_V:
s_first_person = !s_first_person;
break;
default:
break;
}
});
glfwSetCursorPosCallback(window, [](GLFWwindow* window, double x, double y) {
static double prev_x = x;
static double prev_y = y;
s_cam.ProcessMouse(x - prev_x, prev_y - y);
prev_x = x;
prev_y = y;
});
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
auto KeyDown = [window](int key) {
return glfwGetKey(window, key) == GLFW_PRESS;
};
@ -1297,10 +1356,58 @@ int main() {
glViewport(0, 0, width, height);
auto pos = gVehicle4W->getRigidDynamicActor()->getGlobalPose().p;
glm::vec3 pos_glm(pos.x, pos.y + 1.0f, pos.z);
glm::vec3 pos_glm(pos.x, pos.y + 2.0f, pos.z);
glm::mat4 proj = glm::perspective(glm::radians(60.0f), (float)width / (float)height, 0.1f, 1000.0f);
//glm::mat4 view = glm::lookAt(glm::vec3(1.0f, 10.0f, 20.0f) * 5.0f /*pos_glm*/, glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0, 1.0, 0.0));
glm::mat4 view;
if (!s_first_person) {
s_cam.position = pos_glm;
s_cam.third_person_distance = 15.0f;
view = s_cam.GetViewMatrix();
}
else {
//auto trans = gVehicle4W->getRigidDynamicActor()->getGlobalPose();
auto actor = gVehicle4W->getRigidDynamicActor();
PxShape* shapes[8];
actor->getShapes(shapes, 8);
PxMat44 trans_mat = PxShapeExt::getGlobalPose(*shapes[4], *actor);
auto fd = shapes[4]->getSimulationFilterData();
glm::mat4& trans = *((glm::mat4*)(&trans_mat));
//
//shapes[0]->getLocalPose
//
//glm::vec3 position(trans.p.x, trans.p.y, trans.p.z);
//glm::quat rotation(trans.q.x, trans.q.y, trans.q.z, trans.q.w);
//glm::mat4 rotationMatrix = glm::mat4_cast(rotation);
//// Step 2: Create translation matrix
//glm::mat4 translationMatrix = glm::translate(glm::mat4(1.0f), position);
//// Step 3: Combine rotation and translation to get the final transformation matrix
//glm::mat4 transformationMatrix = translationMatrix * rotationMatrix;
s_cam.position = glm::vec3(0.0f);
//s_cam.up_vector.y = -1.0f;
view = s_cam.GetViewMatrix(true) * glm::inverse(trans);
}
auto& dyndata = gVehicle4W->mDriveDynData;
printf("e: %9.2f g: %u\n", dyndata.getEngineRotationSpeed() / (PxPi * 2.0) * 60.0f, dyndata.getCurrentGear());
glm::mat4 proj = glm::perspective(glm::radians(45.0f), (float)width / (float)height, 0.1f, 1000.0f);
glm::mat4 view = glm::lookAt(glm::vec3(1.0f, 10.0f, 20.0f) * 5.0f /*pos_glm*/, glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0, 1.0, 0.0));
glm::mat4 mvp = proj * view;
@ -1319,10 +1426,27 @@ int main() {
gVehicleInputData.setDigitalBrake(KeyDown(GLFW_KEY_S));
gVehicleInputData.setDigitalHandbrake(KeyDown(GLFW_KEY_SPACE));
gVehicleInputData.setGearUp(KeyDown(GLFW_KEY_R));
gVehicleInputData.setGearDown(KeyDown(GLFW_KEY_F));
auto grid_center = glm::floor(s_cam.position * 0.2f) * 5.0f;
grid_center.y = 0.0f;
for (int i = -50; i <= 50; i++) {
s_draw_lines.push_back({ grid_center + glm::vec3(-250.0f, 0.0f, i * 5.0f), glm::vec4(0.5f, 0.5f, 0.5f, 1.0f)});
s_draw_lines.push_back({ grid_center + glm::vec3(250.0f, 0.0f, i * 5.0f), glm::vec4(0.5f, 0.5f, 0.5f, 1.0f) });
}
for (int i = -50; i <= 50; i++) {
s_draw_lines.push_back({ grid_center + glm::vec3(i * 5.0f, 0.0f, -250.0f), glm::vec4(0.5f, 0.5f, 0.5f, 1.0f) });
s_draw_lines.push_back({ grid_center + glm::vec3(i * 5.0f, 0.0f, 250.0f), glm::vec4(0.5f, 0.5f, 0.5f, 1.0f) });
}
PhysicsFrame();
DrawPhysxDebug();
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);

View File

@ -73,6 +73,12 @@
<PropertyGroup Label="Vcpkg">
<VcpkgEnableManifest>true</VcpkgEnableManifest>
</PropertyGroup>
<PropertyGroup Label="Vcpkg" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<VcpkgUseStatic>false</VcpkgUseStatic>
</PropertyGroup>
<PropertyGroup Label="Vcpkg" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<VcpkgUseStatic>false</VcpkgUseStatic>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
@ -123,6 +129,7 @@
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp20</LanguageStandard>
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
@ -132,12 +139,16 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="camera.cpp" />
<ClCompile Include="karo.cpp" />
</ItemGroup>
<ItemGroup>
<None Include="vcpkg-configuration.json" />
<None Include="vcpkg.json" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="camera.hpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>

View File

@ -18,9 +18,17 @@
<ClCompile Include="karo.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="camera.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="vcpkg.json" />
<None Include="vcpkg-configuration.json" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="camera.hpp">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>