Fix KAMAZ vehicle physics body and torque

This commit is contained in:
krustophski 2026-06-18 08:00:56 +03:00
parent 577c60dab1
commit 5bb826d5e0
21 changed files with 176 additions and 26 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
Content/MrPlow/Vehicles/Kamaz/PA_Kamaz_Chassis_Simple.uasset (Stored with Git LFS) Normal file

Binary file not shown.

Binary file not shown.

View file

@ -0,0 +1,93 @@
#include "KamazPhysicsAssetCommandlet.h"
#include "AssetRegistry/AssetRegistryModule.h"
#include "Engine/SkeletalMesh.h"
#include "FileHelpers.h"
#include "Misc/FileHelper.h"
#include "Misc/Paths.h"
#include "PhysicsEngine/AggregateGeom.h"
#include "PhysicsEngine/BoxElem.h"
#include "PhysicsEngine/PhysicsAsset.h"
#include "PhysicsEngine/SkeletalBodySetup.h"
#include "UObject/Package.h"
int32 UKamazPhysicsAssetCommandlet::Main(const FString& Params)
{
const TCHAR* MeshPath = TEXT("/Game/MrPlow/Vehicles/Kamaz/SK_Kamaz_Chassis.SK_Kamaz_Chassis");
const TCHAR* PackageName = TEXT("/Game/MrPlow/Vehicles/Kamaz/PA_Kamaz_Chassis_Simple");
const TCHAR* AssetName = TEXT("PA_Kamaz_Chassis_Simple");
USkeletalMesh* Mesh = LoadObject<USkeletalMesh>(nullptr, MeshPath);
if (!Mesh)
{
UE_LOG(LogTemp, Error, TEXT("Missing skeletal mesh: %s"), MeshPath);
return 1;
}
UPackage* Package = CreatePackage(PackageName);
UPhysicsAsset* PhysicsAsset = LoadObject<UPhysicsAsset>(nullptr, TEXT("/Game/MrPlow/Vehicles/Kamaz/PA_Kamaz_Chassis_Simple.PA_Kamaz_Chassis_Simple"));
if (!PhysicsAsset)
{
PhysicsAsset = NewObject<UPhysicsAsset>(Package, AssetName, RF_Public | RF_Standalone | RF_Transactional);
FAssetRegistryModule::AssetCreated(PhysicsAsset);
}
PhysicsAsset->Modify();
PhysicsAsset->PreviewSkeletalMesh = Mesh;
PhysicsAsset->SkeletalBodySetups.Empty();
PhysicsAsset->ConstraintSetup.Empty();
PhysicsAsset->BoundsBodies.Empty();
USkeletalBodySetup* BodySetup = NewObject<USkeletalBodySetup>(PhysicsAsset, TEXT("Body"), RF_Transactional);
BodySetup->BoneName = FName(TEXT("Body"));
BodySetup->PhysicsType = EPhysicsType::PhysType_Default;
BodySetup->CollisionTraceFlag = CTF_UseSimpleAsComplex;
BodySetup->CollisionReponse = EBodyCollisionResponse::BodyCollision_Enabled;
BodySetup->bConsiderForBounds = true;
BodySetup->DefaultInstance.SetCollisionProfileName(FName(TEXT("Vehicle")));
BodySetup->AggGeom.BoxElems.Reset();
const FBoxSphereBounds Bounds = Mesh->GetBounds();
FKBoxElem ChassisBox;
ChassisBox.Center = FVector(Bounds.Origin.X - 20.0, 0.0, 135.0);
ChassisBox.Rotation = FRotator::ZeroRotator;
ChassisBox.X = FMath::Clamp(Bounds.BoxExtent.X * 1.35f, 520.0f, 720.0f);
ChassisBox.Y = FMath::Clamp(Bounds.BoxExtent.Y * 1.45f, 190.0f, 260.0f);
ChassisBox.Z = 130.0f;
BodySetup->AggGeom.BoxElems.Add(ChassisBox);
BodySetup->InvalidatePhysicsData();
BodySetup->CreatePhysicsMeshes();
PhysicsAsset->SkeletalBodySetups.Add(BodySetup);
PhysicsAsset->BoundsBodies.Add(0);
PhysicsAsset->UpdateBodySetupIndexMap();
PhysicsAsset->UpdateBoundsBodiesArray();
PhysicsAsset->MarkPackageDirty();
Mesh->Modify();
Mesh->SetPhysicsAsset(PhysicsAsset);
Mesh->MarkPackageDirty();
const bool bSaved = UEditorLoadingAndSavingUtils::SavePackages({ PhysicsAsset->GetPackage(), Mesh->GetPackage() }, false);
const FString Report = FString::Printf(
TEXT("{\n \"mesh\": \"%s\",\n \"physicsAsset\": \"%s\",\n \"bodyCount\": %d,\n \"constraintCount\": %d,\n \"boxCenter\": {\"x\": %.2f, \"y\": %.2f, \"z\": %.2f},\n \"boxSize\": {\"x\": %.2f, \"y\": %.2f, \"z\": %.2f},\n \"saved\": %s,\n \"ok\": %s\n}\n"),
*Mesh->GetPathName(),
*PhysicsAsset->GetPathName(),
PhysicsAsset->SkeletalBodySetups.Num(),
PhysicsAsset->ConstraintSetup.Num(),
ChassisBox.Center.X,
ChassisBox.Center.Y,
ChassisBox.Center.Z,
ChassisBox.X,
ChassisBox.Y,
ChassisBox.Z,
bSaved ? TEXT("true") : TEXT("false"),
(bSaved && Mesh->GetPhysicsAsset() == PhysicsAsset && PhysicsAsset->SkeletalBodySetups.Num() == 1) ? TEXT("true") : TEXT("false"));
const FString ReportPath = FPaths::Combine(FPaths::ProjectDir(), TEXT("VehicleExports"), TEXT("unreal_create_kamaz_simple_physics_asset_report.json"));
FFileHelper::SaveStringToFile(Report, *ReportPath);
UE_LOG(LogTemp, Display, TEXT("%s"), *Report);
return (bSaved && Mesh->GetPhysicsAsset() == PhysicsAsset && PhysicsAsset->SkeletalBodySetups.Num() == 1) ? 0 : 2;
}

View file

@ -0,0 +1,13 @@
#pragma once
#include "Commandlets/Commandlet.h"
#include "KamazPhysicsAssetCommandlet.generated.h"
UCLASS()
class UKamazPhysicsAssetCommandlet : public UCommandlet
{
GENERATED_BODY()
public:
virtual int32 Main(const FString& Params) override;
};

View file

@ -6,6 +6,7 @@
#include "ChaosWheeledVehicleMovementComponent.h"
#include "Components/SkeletalMeshComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "PhysicsEngine/PhysicsAsset.h"
#include "UObject/ConstructorHelpers.h"
namespace
@ -26,7 +27,11 @@ AmrPlow58KamazCar::AmrPlow58KamazCar()
GetMesh()->SetSkeletalMesh(KamazMesh.Object);
}
GetMesh()->SetPhysicsAsset(nullptr);
static ConstructorHelpers::FObjectFinder<UPhysicsAsset> KamazPhysicsAsset(TEXT("/Game/MrPlow/Vehicles/Kamaz/PA_Kamaz_Chassis_Simple.PA_Kamaz_Chassis_Simple"));
if (KamazPhysicsAsset.Succeeded())
{
GetMesh()->SetPhysicsAsset(KamazPhysicsAsset.Object);
}
GetMesh()->SetCollisionProfileName(FName("Vehicle"));
GetFrontSpringArm()->SetRelativeLocation(FVector(220.0f, -55.0f, 285.0f));
@ -62,6 +67,12 @@ AmrPlow58KamazCar::AmrPlow58KamazCar()
Movement->EngineSetup.EngineBrakeEffect = 0.25f;
Movement->EngineSetup.EngineRevUpMOI = 12.0f;
Movement->EngineSetup.EngineRevDownRate = 280.0f;
Movement->EngineSetup.TorqueCurve.GetRichCurve()->Reset();
Movement->EngineSetup.TorqueCurve.GetRichCurve()->AddKey(650.0f, 0.45f);
Movement->EngineSetup.TorqueCurve.GetRichCurve()->AddKey(1100.0f, 0.82f);
Movement->EngineSetup.TorqueCurve.GetRichCurve()->AddKey(1400.0f, 1.0f);
Movement->EngineSetup.TorqueCurve.GetRichCurve()->AddKey(1900.0f, 0.96f);
Movement->EngineSetup.TorqueCurve.GetRichCurve()->AddKey(2600.0f, 0.62f);
Movement->DifferentialSetup.DifferentialType = EVehicleDifferential::RearWheelDrive;
Movement->DifferentialSetup.FrontRearSplit = 0.0f;

View file

@ -33,6 +33,14 @@ public class mrPlow58 : ModuleRules
PrivateDependencyModuleNames.AddRange(new string[] { });
if (Target.bBuildEditor)
{
PrivateDependencyModuleNames.AddRange(new string[] {
"UnrealEd",
"AssetRegistry"
});
}
// Uncomment if you are using Slate UI
// PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" });

View file

@ -70,6 +70,11 @@ def load_or_create_material(profile_name, profile):
if not material:
raise RuntimeError(f"Failed to create material {asset_path}")
try:
material.set_editor_property("b_used_with_skeletal_mesh", True)
except Exception:
pass
color = unreal.MaterialEditingLibrary.create_material_expression(material, unreal.MaterialExpressionConstant4Vector, -400, 0)
color.set_editor_property("constant", unreal.LinearColor(*profile["color"]))
unreal.MaterialEditingLibrary.connect_material_property(color, "", unreal.MaterialProperty.MP_BASE_COLOR)
@ -103,6 +108,13 @@ def main():
name: load_or_create_material(name, profile)
for name, profile in MATERIAL_PROFILES.items()
}
for material in materials_by_profile.values():
try:
material.set_editor_property("b_used_with_skeletal_mesh", True)
except Exception:
pass
unreal.MaterialEditingLibrary.recompile_material(material)
unreal.EditorAssetLibrary.save_loaded_asset(material)
assignments = []
slots = mesh.get_editor_property("materials")

View file

@ -0,0 +1,10 @@
{
"mesh": "/Game/MrPlow/Vehicles/Kamaz/SK_Kamaz_Chassis.SK_Kamaz_Chassis",
"physicsAsset": "/Game/MrPlow/Vehicles/Kamaz/PA_Kamaz_Chassis_Simple.PA_Kamaz_Chassis_Simple",
"bodyCount": 1,
"constraintCount": 0,
"boxCenter": {"x": -52.97, "y": 0.00, "z": 135.00},
"boxSize": {"x": 520.00, "y": 212.22, "z": 130.00},
"saved": true,
"ok": true
}

View file

@ -3,59 +3,59 @@
"defaultGameMode": "/Game/MrPlow/Blueprints/BP_KamazGameMode.BP_KamazGameMode_C",
"pawnClass": "/Game/MrPlow/Vehicles/Kamaz/BP_Kamaz_Pawn.BP_Kamaz_Pawn_C",
"skeletalMesh": "/Game/MrPlow/Vehicles/Kamaz/SK_Kamaz_Chassis.SK_Kamaz_Chassis",
"physicsAsset": null,
"physicsAsset": "/Game/MrPlow/Vehicles/Kamaz/PA_Kamaz_Chassis_Simple.PA_Kamaz_Chassis_Simple",
"movementMassKg": 8500.0,
"wheelSetupCount": 10,
"wheelSetups": [
{
"bone": "Phys_Wheel_FL",
"wheelClass": "/Script/mrPlow58.mrPlow58KamazWheelFront",
"offset": "<Struct 'Vector' (0x0000021EAA829648) {x: 0.000000, y: 0.000000, z: 0.000000}>"
"offset": "<Struct 'Vector' (0x000001FCAC8A0F48) {x: 0.000000, y: 0.000000, z: 0.000000}>"
},
{
"bone": "Phys_Wheel_FR",
"wheelClass": "/Script/mrPlow58.mrPlow58KamazWheelFront",
"offset": "<Struct 'Vector' (0x0000021EAA829438) {x: 0.000000, y: 0.000000, z: 0.000000}>"
"offset": "<Struct 'Vector' (0x000001FCAC8A0CA8) {x: 0.000000, y: 0.000000, z: 0.000000}>"
},
{
"bone": "Phys_Wheel_ML1",
"wheelClass": "/Script/mrPlow58.mrPlow58KamazWheelRear",
"offset": "<Struct 'Vector' (0x0000021EAA8293D8) {x: 0.000000, y: 0.000000, z: 0.000000}>"
"offset": "<Struct 'Vector' (0x000001FCAC8A07F8) {x: 0.000000, y: 0.000000, z: 0.000000}>"
},
{
"bone": "Phys_Wheel_ML2",
"wheelClass": "/Script/mrPlow58.mrPlow58KamazWheelRear",
"offset": "<Struct 'Vector' (0x0000021EAA829348) {x: 0.000000, y: 0.000000, z: 0.000000}>"
"offset": "<Struct 'Vector' (0x000001FCAC8A0CA8) {x: 0.000000, y: 0.000000, z: 0.000000}>"
},
{
"bone": "Phys_Wheel_MR1",
"wheelClass": "/Script/mrPlow58.mrPlow58KamazWheelRear",
"offset": "<Struct 'Vector' (0x0000021EAA8291C8) {x: 0.000000, y: 0.000000, z: 0.000000}>"
"offset": "<Struct 'Vector' (0x000001FCAC8A0F48) {x: 0.000000, y: 0.000000, z: 0.000000}>"
},
{
"bone": "Phys_Wheel_MR2",
"wheelClass": "/Script/mrPlow58.mrPlow58KamazWheelRear",
"offset": "<Struct 'Vector' (0x0000021EAA829018) {x: 0.000000, y: 0.000000, z: 0.000000}>"
"offset": "<Struct 'Vector' (0x000001FCAC8A09D8) {x: 0.000000, y: 0.000000, z: 0.000000}>"
},
{
"bone": "Phys_Wheel_RL1",
"wheelClass": "/Script/mrPlow58.mrPlow58KamazWheelRear",
"offset": "<Struct 'Vector' (0x0000021EAA8291C8) {x: 0.000000, y: 0.000000, z: 0.000000}>"
"offset": "<Struct 'Vector' (0x000001FCAC8A07F8) {x: 0.000000, y: 0.000000, z: 0.000000}>"
},
{
"bone": "Phys_Wheel_RL2",
"wheelClass": "/Script/mrPlow58.mrPlow58KamazWheelRear",
"offset": "<Struct 'Vector' (0x0000021EAA829348) {x: 0.000000, y: 0.000000, z: 0.000000}>"
"offset": "<Struct 'Vector' (0x000001FCAC8A09D8) {x: 0.000000, y: 0.000000, z: 0.000000}>"
},
{
"bone": "Phys_Wheel_RR1",
"wheelClass": "/Script/mrPlow58.mrPlow58KamazWheelRear",
"offset": "<Struct 'Vector' (0x0000021EAA8293D8) {x: 0.000000, y: 0.000000, z: 0.000000}>"
"offset": "<Struct 'Vector' (0x000001FCAC8A0CA8) {x: 0.000000, y: 0.000000, z: 0.000000}>"
},
{
"bone": "Phys_Wheel_RR2",
"wheelClass": "/Script/mrPlow58.mrPlow58KamazWheelRear",
"offset": "<Struct 'Vector' (0x0000021EAA829468) {x: 0.000000, y: 0.000000, z: 0.000000}>"
"offset": "<Struct 'Vector' (0x000001FCAC8A0F48) {x: 0.000000, y: 0.000000, z: 0.000000}>"
}
],
"boneNames": [