Skip to content

Commit

Permalink
[ERA-0] physx vehicles
Browse files Browse the repository at this point in the history
  • Loading branch information
EldarMuradov committed Aug 18, 2024
1 parent cfbb97a commit b342237
Show file tree
Hide file tree
Showing 33 changed files with 4,843 additions and 32 deletions.
30 changes: 15 additions & 15 deletions modules/core/src/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,20 +332,20 @@ namespace era_engine
#if PX_BLAST_ENABLE

{
if (auto mesh = loadMeshFromFileAsync(getAssetPath("/resources/assets/obj/untitled.obj")))
{
model_asset ass = load3DModelFromFile(getAssetPath("/resources/assets/obj/untitled.obj"));

auto& px_blast_entt1 = scene.createEntity("BlastPXTest")
.addComponent<transform_component>(vec3(0.0f, 5.0f, 0.0f), quat::identity, vec3(1.0f))
.addComponent<mesh_component>(mesh);

physics::fracture fracture;
auto ref = make_ref<submesh_asset>(ass.meshes[0].submeshes[0]);
unsigned int seed = 7249U;
fracture.fractureGameObject(ref, px_blast_entt1, physics::anchor::anchor_bottom, seed, 10, defaultmat, defaultmat, 400.0f, 1.0f);
scene.deleteEntity(px_blast_entt1.handle);
}
//if (auto mesh = loadMeshFromFileAsync(getAssetPath("/resources/assets/obj/untitled.obj")))
//{
// model_asset ass = load3DModelFromFile(getAssetPath("/resources/assets/obj/untitled.obj"));

// auto& px_blast_entt1 = scene.createEntity("BlastPXTest")
// .addComponent<transform_component>(vec3(0.0f, 5.0f, 0.0f), quat::identity, vec3(1.0f))
// .addComponent<mesh_component>(mesh);

// physics::fracture fracture;
// auto ref = make_ref<submesh_asset>(ass.meshes[0].submeshes[0]);
// unsigned int seed = 7249U;
// fracture.fractureGameObject(ref, px_blast_entt1, physics::anchor::anchor_bottom, seed, 3, defaultmat, defaultmat, 10.0f, 1.0f);
// scene.deleteEntity(px_blast_entt1.handle);
//}
}

#endif
Expand Down Expand Up @@ -853,7 +853,7 @@ namespace era_engine
renderer->setCamera(camera);

//if (running)
// physics::physics_holder::physicsRef->endSimulation();
// physics::physics_holder::physicsRef->endSimulation(dt);
#ifndef ERA_RUNTIME
editor.visualizePhysics(&ldrRenderPass);
#endif
Expand Down
3 changes: 2 additions & 1 deletion modules/core/src/px/blast/px_blast_destructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,11 @@ namespace era_engine::physics

rb.setAngularDamping(0.1f);
rb.setLinearDamping(0.2f);
rb.setThreshold(0.02f, 0.02f);

auto dyn = rb.getRigidDynamic();
dyn->setMaxDepenetrationVelocity(3.0f);
dyn->setSolverIterationCounts(16);
dyn->setSolverIterationCounts(32, 32);
rb.updateMassAndInertia(renderEntity.getComponent<nvmesh_chunk_component>().mass);

if (rb.isKinematicBody())
Expand Down
1 change: 1 addition & 0 deletions modules/core/src/px/blast/px_blast_fracture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ namespace era_engine::physics
// Graph manager freezes/unfreezes blocks depending on whether they are connected to the graph or not
graphManager.setup(chunks);

physics::physics_holder::physicsRef->update(0.016f);
// Connect blocks that are touching with fixed joints
for (size_t i = 0; i < chunks.size(); i++)
{
Expand Down
21 changes: 13 additions & 8 deletions modules/core/src/px/core/px_physics_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,10 +324,10 @@ namespace era_engine
PxVehicleSetBasisVectors(PxVec3(0, 1, 0), PxVec3(0, 0, 1));
PxVehicleSetUpdateMode(PxVehicleUpdateMode::eVELOCITY_CHANGE);

//bool res = initVehicle(this);
bool res = initVehicle(this);

//if(!res)
//LOG_ERROR("Physics> Failed to initialize PxVehicles.");
if(!res)
LOG_ERROR("Physics> Failed to initialize PxVehicles.");
#endif
}

Expand All @@ -345,7 +345,7 @@ namespace era_engine

#if PX_VEHICLE
PxCloseVehicleSDK();
//cleanupVehicle();
cleanupVehicle();
#endif

PxCloseExtensions();
Expand Down Expand Up @@ -378,7 +378,7 @@ namespace era_engine
void physics::px_physics_engine::update(float dt)
{
startSimulation(dt);
endSimulation();
endSimulation(dt);
}

void physics::px_physics_engine::startSimulation(float dt)
Expand All @@ -393,11 +393,14 @@ namespace era_engine

static void* scratchMemBlock = allocator.allocate(scratchMemBlockSize, align, true);

#if PX_VEHICLE
vehicleStep(dt);
#endif

#if PX_GPU_BROAD_PHASE

{
physics_lock_write lock{};

scene->simulate(stepSize, NULL, scratchMemBlock, scratchMemBlockSize);
scene->fetchResults(true);
}
Expand All @@ -410,14 +413,16 @@ namespace era_engine
return;

stepper.renderDone();

#endif
}

void physics::px_physics_engine::endSimulation()
void physics::px_physics_engine::endSimulation(float dt)
{
#if !PX_GPU_BROAD_PHASE
stepper.wait(scene);
#endif
#if PX_VEHICLE
vehiclePostStep(dt);
#endif
{
CPU_PROFILE_BLOCK("PhysX process simulation event callbacks steps");
Expand Down
7 changes: 5 additions & 2 deletions modules/core/src/px/core/px_physics_engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@
#define PX_NB_MAX_RAYCAST_HITS 64
#define PX_NB_MAX_RAYCAST_DISTANCE 128

#define PX_VEHICLE 0
#define PX_VEHICLE 1

#if DEBUG
#define PX_BLAST_ENABLE 0
#define PX_VEHICLE_DEBUG 1
#elif _DEBUG
#define PX_BLAST_ENABLE 0
#define PX_VEHICLE_DEBUG 1
#else
#define PX_BLAST_ENABLE 1
#define PX_VEHICLE_DEBUG 0
#endif

#ifndef PX_RELEASE
Expand Down Expand Up @@ -567,7 +570,7 @@ filterData.data.word2 = hitTriggers ? 1 : 0
void update(float dt);

void startSimulation(float dt);
void endSimulation();
void endSimulation(float dt);

void resetActorsVelocityAndInertia();

Expand Down
13 changes: 7 additions & 6 deletions modules/core/src/px/features/px_vehicles.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#pragma once
#include "px/core/px_physics_engine.h"
#include "core/project.h"
#include "core/string.h"

#if PX_VEHICLE

Expand All @@ -13,8 +15,6 @@ namespace era_engine::physics
using namespace snippetvehicle2;
#endif

static inline const char* vehicleDataPath = "E:\\Era Engine\\ext\\PhysX\\physx\\vehicledata";

static inline EngineDriveVehicle vehicle;

static inline PxVehiclePhysXSimulationContext vehicleSimulationContext;
Expand Down Expand Up @@ -63,22 +63,23 @@ namespace era_engine::physics

inline bool initVehicle(px_physics_engine* physics)
{
const char* path = wstringToString((eproject::enginePath + L"/resources/vehicledata")).c_str();
BaseVehicleParams baseParams;
if (!readBaseParamsFromJsonFile(vehicleDataPath, "Base.json", baseParams))
if (!readBaseParamsFromJsonFile(path, "Base.json", baseParams))
return false;

EngineDrivetrainParams engineDrivetrainParams;
if (!readEngineDrivetrainParamsFromJsonFile(vehicleDataPath, "EngineDrive.json",
if (!readEngineDrivetrainParamsFromJsonFile(path, "EngineDrive.json",
engineDrivetrainParams))
return false;

initMaterialFrictionTable(physics);

readBaseParamsFromJsonFile(vehicleDataPath, "Base.json", vehicle.mBaseParams);
readBaseParamsFromJsonFile(path, "Base.json", vehicle.mBaseParams);
setPhysXIntegrationParams(vehicle.mBaseParams.axleDescription,
materialFrictions, nbMaterialFrictions, defaultMaterialFriction,
vehicle.mPhysXParams);
readEngineDrivetrainParamsFromJsonFile(vehicleDataPath, "EngineDrive.json",
readEngineDrivetrainParamsFromJsonFile(path, "EngineDrive.json",
vehicle.mEngineDriveParams);

if (!vehicle.initialize(*physics->getPhysics(), PxCookingParams(PxTolerancesScale()), *material, EngineDriveVehicle::eDIFFTYPE_FOURWHEELDRIVE))
Expand Down
70 changes: 70 additions & 0 deletions modules/core/src/px/snippets_utils/SnippetVehicleHelpers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of NVIDIA CORPORATION nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Copyright (c) 2008-2023 NVIDIA Corporation. All rights reserved.
// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.


#include <ctype.h>

#include "SnippetVehicleHelpers.h"

using namespace physx;

namespace snippetvehicle2
{

PxFilterFlags VehicleFilterShader(
PxFilterObjectAttributes attributes0, PxFilterData filterData0,
PxFilterObjectAttributes attributes1, PxFilterData filterData1,
PxPairFlags& pairFlags, const void* constantBlock, PxU32 constantBlockSize)
{
PX_UNUSED(attributes0);
PX_UNUSED(filterData0);
PX_UNUSED(attributes1);
PX_UNUSED(filterData1);
PX_UNUSED(pairFlags);
PX_UNUSED(constantBlock);
PX_UNUSED(constantBlockSize);
return PxFilterFlag::eSUPPRESS;
}


bool parseVehicleDataPath(int argc, const char *const* argv, const char* snippetName,
const char*& vehicleDataPath)
{
if (argc != 2 || 0 != strncmp(argv[1], "--vehicleDataPath", strlen("--vehicleDataPath")))
{
printf("%s usage:\n"
"%s "
"--vehicleDataPath=<path to the [PHYSX_ROOT]/snippets/media/vehicledata folder containing the vehiclejson files to be loaded> \n",
snippetName, snippetName);
return false;
}
vehicleDataPath = argv[1] + strlen("--vehicleDataPath=");
return true;
}

}//namespace snippetvehicle2
47 changes: 47 additions & 0 deletions modules/core/src/px/snippets_utils/SnippetVehicleHelpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of NVIDIA CORPORATION nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Copyright (c) 2008-2023 NVIDIA Corporation. All rights reserved.
// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.

#ifdef setBit
#undef setBit
#endif // setBit
#include "PxPhysicsAPI.h"


namespace snippetvehicle2
{

physx::PxFilterFlags VehicleFilterShader(
physx::PxFilterObjectAttributes attributes0, physx::PxFilterData filterData0,
physx::PxFilterObjectAttributes attributes1, physx::PxFilterData filterData1,
physx::PxPairFlags& pairFlags, const void* constantBlock, physx::PxU32 constantBlockSize);


bool parseVehicleDataPath(int argc, const char *const* argv, const char* snippetName,
const char*& vehicleDataPath);

}//namespace snippetvehicle2
Loading

0 comments on commit b342237

Please sign in to comment.