├── LICENSE ├── README.md ├── UnrealDemo ├── Config │ ├── DefaultEditor.ini │ ├── DefaultEditorPerProjectUserSettings.ini │ ├── DefaultEngine.ini │ ├── DefaultGame.ini │ └── DefaultInput.ini ├── Content │ ├── Geometry │ │ └── Meshes │ │ │ ├── 1M_Cube.uasset │ │ │ ├── 1M_Cube_Chamfer.uasset │ │ │ ├── CubeMaterial.uasset │ │ │ └── TemplateFloor.uasset │ ├── Mannequin │ │ ├── Animations │ │ │ ├── ThirdPersonIdle.uasset │ │ │ ├── ThirdPersonJump_End.uasset │ │ │ ├── ThirdPersonJump_Loop.uasset │ │ │ ├── ThirdPersonJump_Start.uasset │ │ │ ├── ThirdPersonRun.uasset │ │ │ ├── ThirdPersonWalk.uasset │ │ │ ├── ThirdPerson_AnimBP.uasset │ │ │ ├── ThirdPerson_IdleRun_2D.uasset │ │ │ └── ThirdPerson_Jump.uasset │ │ └── Character │ │ │ ├── Materials │ │ │ ├── MI_Female_Body.uasset │ │ │ ├── M_Male_Body.uasset │ │ │ ├── M_UE4Man_ChestLogo.uasset │ │ │ └── MaterialLayers │ │ │ │ ├── ML_GlossyBlack_Latex_UE4.uasset │ │ │ │ ├── ML_Plastic_Shiny_Beige.uasset │ │ │ │ ├── ML_Plastic_Shiny_Beige_LOGO.uasset │ │ │ │ ├── ML_SoftMetal_UE4.uasset │ │ │ │ ├── T_ML_Aluminum01.uasset │ │ │ │ ├── T_ML_Aluminum01_N.uasset │ │ │ │ ├── T_ML_Rubber_Blue_01_D.uasset │ │ │ │ └── T_ML_Rubber_Blue_01_N.uasset │ │ │ ├── Mesh │ │ │ ├── SK_Mannequin.uasset │ │ │ ├── SK_Mannequin_Female.uasset │ │ │ ├── SK_Mannequin_Female_PhysicsAsset.uasset │ │ │ ├── SK_Mannequin_PhysicsAsset.uasset │ │ │ └── UE4_Mannequin_Skeleton.uasset │ │ │ └── Textures │ │ │ ├── T_Female_Mask.uasset │ │ │ ├── T_Female_N.uasset │ │ │ ├── T_Male_Mask.uasset │ │ │ ├── T_Male_N.uasset │ │ │ ├── T_UE4Logo_Mask.uasset │ │ │ └── T_UE4Logo_N.uasset │ ├── ThirdPerson │ │ └── Meshes │ │ │ ├── Bump_StaticMesh.uasset │ │ │ ├── LeftArm_StaticMesh.uasset │ │ │ ├── Linear_Stair_StaticMesh.uasset │ │ │ ├── RampMaterial.uasset │ │ │ ├── Ramp_StaticMesh.uasset │ │ │ └── RightArm_StaticMesh.uasset │ └── ThirdPersonCPP │ │ ├── Blueprints │ │ └── ThirdPersonCharacter.uasset │ │ └── Maps │ │ ├── GPhsics.umap │ │ ├── GPhsics_BuiltData.uasset │ │ ├── GRigidTest_Box.umap │ │ ├── GRigidTest_Box_2.umap │ │ ├── GRigidTest_Sphere.umap │ │ ├── GRigidTest_Sphere_2.umap │ │ ├── GRigidTest_Sphere_3.umap │ │ ├── PRigidTest_Box.umap │ │ ├── PRigidTest_Sphere.umap │ │ ├── TestCollisionGPhysics.umap │ │ ├── TestCollisionGPhysics_BuiltData.uasset │ │ ├── ThirdPersonExampleMap.umap │ │ └── ThirdPersonExampleMap_BuiltData.uasset ├── Source │ ├── UnrealDemo.Target.cs │ ├── UnrealDemo │ │ ├── GPhysicsActor.cpp │ │ ├── GPhysicsActor.h │ │ ├── GPhysicsCollistionActor.cpp │ │ ├── GPhysicsCollistionActor.h │ │ ├── GUnrealUtility.cpp │ │ ├── GUnrealUtility.h │ │ ├── RigidPhysicsComponent.cpp │ │ ├── RigidPhysicsComponent.h │ │ ├── UnrealDemo.Build.cs │ │ ├── UnrealDemo.cpp │ │ ├── UnrealDemo.h │ │ ├── UnrealDemoCharacter.cpp │ │ ├── UnrealDemoCharacter.h │ │ ├── UnrealDemoGameMode.cpp │ │ ├── UnrealDemoGameMode.h │ │ └── gphysics │ │ │ ├── include │ │ │ ├── collision │ │ │ │ ├── glacier_collision_algorithm.h │ │ │ │ ├── glacier_collision_box.h │ │ │ │ ├── glacier_collision_gjk.h │ │ │ │ ├── glacier_collision_shape.h │ │ │ │ ├── glacier_collision_sphere.h │ │ │ │ ├── glacier_contact.h │ │ │ │ ├── glacier_convexhull.h │ │ │ │ └── glacier_distance.h │ │ │ ├── dynamic │ │ │ │ ├── glacier_articulate_body.h │ │ │ │ ├── glacier_rigid_body.h │ │ │ │ └── glacier_soft_body.h │ │ │ ├── math │ │ │ │ ├── glacier_aabb.h │ │ │ │ ├── glacier_float.h │ │ │ │ ├── glacier_math.h │ │ │ │ ├── glacier_matrix.h │ │ │ │ ├── glacier_plane.h │ │ │ │ ├── glacier_quaternion.h │ │ │ │ ├── glacier_transform_qt.h │ │ │ │ └── glacier_vector.h │ │ │ ├── utils │ │ │ │ ├── glacier_color.h │ │ │ │ ├── glacier_debug_draw.h │ │ │ │ ├── glacier_physics_utils.h │ │ │ │ └── glacier_time.h │ │ │ └── world │ │ │ │ ├── glacier_bvt.h │ │ │ │ ├── glacier_collision_object.h │ │ │ │ ├── glacier_grid.h │ │ │ │ └── glacier_physics_world.h │ │ │ └── src │ │ │ ├── collision │ │ │ ├── glacier_collision_algorithm.cpp │ │ │ ├── glacier_collision_box.cpp │ │ │ ├── glacier_collision_gjk.cpp │ │ │ ├── glacier_collision_shape.cpp │ │ │ ├── glacier_collision_sphere.cpp │ │ │ ├── glacier_contact.cpp │ │ │ ├── glacier_convexhull.cpp │ │ │ └── glacier_distance.cpp │ │ │ ├── dynamic │ │ │ ├── glacier_articulate_body.cpp │ │ │ ├── glacier_rigid_body.cpp │ │ │ └── glacier_soft_body.cpp │ │ │ ├── gphysics.vcxproj │ │ │ ├── gphysics.vcxproj.filters │ │ │ ├── gphysics.vcxproj.user │ │ │ ├── math │ │ │ ├── glacier_float.cpp │ │ │ ├── glacier_matrix.cpp │ │ │ ├── glacier_quternion.cpp │ │ │ └── glacier_vector.cpp │ │ │ ├── utils │ │ │ ├── glacier_debug_draw.cpp │ │ │ ├── glacier_physics_utils.cpp │ │ │ └── glacier_time.cpp │ │ │ └── world │ │ │ └── glacier_physics_world.cpp │ └── UnrealDemoEditor.Target.cs ├── UnrealDemo.natvis └── UnrealDemo.uproject ├── gphysics ├── include │ ├── collision │ │ ├── glacier_collision_algorithm.h │ │ ├── glacier_collision_box.h │ │ ├── glacier_collision_gjk.h │ │ ├── glacier_collision_shape.h │ │ ├── glacier_collision_sphere.h │ │ ├── glacier_contact.h │ │ ├── glacier_convexhull.h │ │ └── glacier_distance.h │ ├── dynamic │ │ ├── glacier_rigid_body.h │ │ ├── glacier_rigid_dynamic.h │ │ └── glacier_rigid_static.h │ ├── math │ │ ├── glacier_aabb.h │ │ ├── glacier_float.h │ │ ├── glacier_math.h │ │ ├── glacier_matrix.h │ │ ├── glacier_plane.h │ │ ├── glacier_quaternion.h │ │ ├── glacier_transform_qt.h │ │ └── glacier_vector.h │ ├── utils │ │ ├── glacier_color.h │ │ ├── glacier_debug_draw.h │ │ ├── glacier_physics_utils.h │ │ └── glacier_time.h │ └── world │ │ ├── glacier_collision_object.h │ │ └── glacier_physics_world.h └── src │ ├── collision │ ├── glacier_collision_algorithm.cpp │ ├── glacier_collision_box.cpp │ ├── glacier_collision_gjk.cpp │ ├── glacier_collision_shape.cpp │ ├── glacier_collision_sphere.cpp │ ├── glacier_contact.cpp │ ├── glacier_convexhull.cpp │ └── glacier_distance.cpp │ ├── dynamic │ ├── glacier_rigid_body.cpp │ ├── glacier_rigid_dynamic.cpp │ └── glacier_rigid_static.cpp │ ├── gphysics.vcxproj │ ├── gphysics.vcxproj.filters │ ├── math │ ├── glacier_float.cpp │ ├── glacier_matrix.cpp │ ├── glacier_quternion.cpp │ └── glacier_vector.cpp │ ├── utils │ ├── glacier_debug_draw.cpp │ ├── glacier_physics_utils.cpp │ └── glacier_time.cpp │ └── world │ └── glacier_physics_world.cpp ├── samples ├── test.cpp ├── test.vcxproj └── test.vcxproj.filters └── solutions └── deterministic_physics.sln /README.md: -------------------------------------------------------------------------------- 1 | # deterministic_physics 2 | deterministic physics engine for lock-step game development 3 | 4 | ## Technologies 5 | * [deterministic_float](https://github.com/devlinzhou/deterministic_float) 6 | 7 | ## Features 8 | - [ ] Shape 9 | - [x] Plane 10 | - [x] Sphere 11 | - [x] Box 12 | - [x] Convex 13 | - [ ] HeightField & WaterShape 14 | - [ ] TriangleMesh 15 | - [ ] Collision Test Algorithm 16 | - [x] BoxBox 17 | - [x] BoxSphere 18 | - [x] BoxPlane 19 | - [x] SphereSphere 20 | - [ ] BoxTriangle 21 | - [ ] SphereTriangle 22 | - [x] Contact Algorithm 23 | - [ ] SolveConstraint 24 | - [ ] 25 | - [ ] Dynamic 26 | - [x] Rigid Body 27 | - [ ] Soft Body 28 | - [ ] Water Body 29 | - [ ] Articulate body 30 | - [ ] Scene Manager 31 | - [x] Loose Grid 32 | 33 | ## How to Start 34 | * Unreal demo(4.27) : UnrealDemo/UnrealDemo.uproject 35 | * UnrealDemo/Content/ThirdPersonCPP/Maps/GRigidTest_Box.umap : 6x6x6 cubes falling test 36 | * UnrealDemo/Content/ThirdPersonCPP/Maps/GPhsics.umap : physics simulation 37 | * UnrealDemo/Content/ThirdPersonCPP/Maps/TestCollisionGPhysics.umap : Test collision algorithm 38 | 39 | -------------------------------------------------------------------------------- /UnrealDemo/Config/DefaultEditor.ini: -------------------------------------------------------------------------------- 1 | [UnrealEd.SimpleMap] 2 | SimpleMapName=/Game/ThirdPersonCPP/Maps/ThirdPersonExampleMap 3 | 4 | [EditoronlyBP] 5 | bAllowClassAndBlueprintPinMatching=true 6 | bReplaceBlueprintWithClass= true 7 | bDontLoadBlueprintOutsideEditor= true 8 | bBlueprintIsNotBlueprintType= true -------------------------------------------------------------------------------- /UnrealDemo/Config/DefaultEditorPerProjectUserSettings.ini: -------------------------------------------------------------------------------- 1 | [ContentBrowser] 2 | ContentBrowserTab1.SelectedPaths=/Game/ThirdPersonCPP -------------------------------------------------------------------------------- /UnrealDemo/Config/DefaultEngine.ini: -------------------------------------------------------------------------------- 1 | [/Script/EngineSettings.GameMapsSettings] 2 | GameDefaultMap=/Game/ThirdPersonCPP/Maps/GRigidTest_Box.GRigidTest_Box 3 | EditorStartupMap=/Game/ThirdPersonCPP/Maps/GPhsics.GPhsics 4 | GlobalDefaultGameMode=/Script/Engine.GameModeBase 5 | 6 | [/Script/IOSRuntimeSettings.IOSRuntimeSettings] 7 | MinimumiOSVersion=IOS_12 8 | 9 | [/Script/HardwareTargeting.HardwareTargetingSettings] 10 | TargetedHardwareClass=Desktop 11 | AppliedTargetedHardwareClass=Desktop 12 | DefaultGraphicsPerformance=Maximum 13 | AppliedDefaultGraphicsPerformance=Maximum 14 | 15 | [/Script/Engine.Engine] 16 | +ActiveGameNameRedirects=(OldGameName="TP_ThirdPerson",NewGameName="/Script/UnrealDemo") 17 | +ActiveGameNameRedirects=(OldGameName="/Script/TP_ThirdPerson",NewGameName="/Script/UnrealDemo") 18 | +ActiveClassRedirects=(OldClassName="TP_ThirdPersonGameMode",NewClassName="UnrealDemoGameMode") 19 | +ActiveClassRedirects=(OldClassName="TP_ThirdPersonCharacter",NewClassName="UnrealDemoCharacter") 20 | 21 | [SystemSettings] 22 | 23 | 24 | [/Script/AndroidRuntimeSettings.AndroidRuntimeSettings] 25 | bBuildForArm64=True 26 | bBuildForArmV7=False 27 | PackageName=com.glacier.[PROJECT] 28 | StoreVersionOffsetArm64=1 29 | StoreVersionOffsetArmV7=0 30 | bDisableVerifyOBBOnStartUp=True 31 | bEnableGooglePlaySupport=True 32 | bEnableBundle=False 33 | bPackageDataInsideApk=True 34 | 35 | -------------------------------------------------------------------------------- /UnrealDemo/Config/DefaultGame.ini: -------------------------------------------------------------------------------- 1 | [/Script/EngineSettings.GeneralProjectSettings] 2 | ProjectID=82A9558744F9B3723333D0A28FAD6430 3 | ProjectName=Third Person Game Template 4 | 5 | [StartupActions] 6 | bAddPacks=True 7 | InsertPack=(PackSource="StarterContent.upack",PackName="StarterContent") 8 | 9 | [/Script/UnrealEd.ProjectPackagingSettings] 10 | BuildConfiguration=PPBC_DebugGame 11 | ForDistribution=False 12 | 13 | -------------------------------------------------------------------------------- /UnrealDemo/Content/Geometry/Meshes/1M_Cube.uasset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/Geometry/Meshes/1M_Cube.uasset -------------------------------------------------------------------------------- /UnrealDemo/Content/Geometry/Meshes/1M_Cube_Chamfer.uasset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/Geometry/Meshes/1M_Cube_Chamfer.uasset -------------------------------------------------------------------------------- /UnrealDemo/Content/Geometry/Meshes/CubeMaterial.uasset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/Geometry/Meshes/CubeMaterial.uasset -------------------------------------------------------------------------------- /UnrealDemo/Content/Geometry/Meshes/TemplateFloor.uasset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/Geometry/Meshes/TemplateFloor.uasset -------------------------------------------------------------------------------- /UnrealDemo/Content/Mannequin/Animations/ThirdPersonIdle.uasset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/Mannequin/Animations/ThirdPersonIdle.uasset -------------------------------------------------------------------------------- /UnrealDemo/Content/Mannequin/Animations/ThirdPersonJump_End.uasset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/Mannequin/Animations/ThirdPersonJump_End.uasset -------------------------------------------------------------------------------- /UnrealDemo/Content/Mannequin/Animations/ThirdPersonJump_Loop.uasset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/Mannequin/Animations/ThirdPersonJump_Loop.uasset -------------------------------------------------------------------------------- /UnrealDemo/Content/Mannequin/Animations/ThirdPersonJump_Start.uasset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/Mannequin/Animations/ThirdPersonJump_Start.uasset -------------------------------------------------------------------------------- /UnrealDemo/Content/Mannequin/Animations/ThirdPersonRun.uasset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/Mannequin/Animations/ThirdPersonRun.uasset -------------------------------------------------------------------------------- /UnrealDemo/Content/Mannequin/Animations/ThirdPersonWalk.uasset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/Mannequin/Animations/ThirdPersonWalk.uasset -------------------------------------------------------------------------------- /UnrealDemo/Content/Mannequin/Animations/ThirdPerson_AnimBP.uasset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/Mannequin/Animations/ThirdPerson_AnimBP.uasset -------------------------------------------------------------------------------- /UnrealDemo/Content/Mannequin/Animations/ThirdPerson_IdleRun_2D.uasset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/Mannequin/Animations/ThirdPerson_IdleRun_2D.uasset -------------------------------------------------------------------------------- /UnrealDemo/Content/Mannequin/Animations/ThirdPerson_Jump.uasset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/Mannequin/Animations/ThirdPerson_Jump.uasset -------------------------------------------------------------------------------- /UnrealDemo/Content/Mannequin/Character/Materials/MI_Female_Body.uasset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/Mannequin/Character/Materials/MI_Female_Body.uasset -------------------------------------------------------------------------------- /UnrealDemo/Content/Mannequin/Character/Materials/M_Male_Body.uasset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/Mannequin/Character/Materials/M_Male_Body.uasset -------------------------------------------------------------------------------- /UnrealDemo/Content/Mannequin/Character/Materials/M_UE4Man_ChestLogo.uasset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/Mannequin/Character/Materials/M_UE4Man_ChestLogo.uasset -------------------------------------------------------------------------------- /UnrealDemo/Content/Mannequin/Character/Materials/MaterialLayers/ML_GlossyBlack_Latex_UE4.uasset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/Mannequin/Character/Materials/MaterialLayers/ML_GlossyBlack_Latex_UE4.uasset -------------------------------------------------------------------------------- /UnrealDemo/Content/Mannequin/Character/Materials/MaterialLayers/ML_Plastic_Shiny_Beige.uasset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/Mannequin/Character/Materials/MaterialLayers/ML_Plastic_Shiny_Beige.uasset -------------------------------------------------------------------------------- /UnrealDemo/Content/Mannequin/Character/Materials/MaterialLayers/ML_Plastic_Shiny_Beige_LOGO.uasset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/Mannequin/Character/Materials/MaterialLayers/ML_Plastic_Shiny_Beige_LOGO.uasset -------------------------------------------------------------------------------- /UnrealDemo/Content/Mannequin/Character/Materials/MaterialLayers/ML_SoftMetal_UE4.uasset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/Mannequin/Character/Materials/MaterialLayers/ML_SoftMetal_UE4.uasset -------------------------------------------------------------------------------- /UnrealDemo/Content/Mannequin/Character/Materials/MaterialLayers/T_ML_Aluminum01.uasset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/Mannequin/Character/Materials/MaterialLayers/T_ML_Aluminum01.uasset -------------------------------------------------------------------------------- /UnrealDemo/Content/Mannequin/Character/Materials/MaterialLayers/T_ML_Aluminum01_N.uasset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/Mannequin/Character/Materials/MaterialLayers/T_ML_Aluminum01_N.uasset -------------------------------------------------------------------------------- /UnrealDemo/Content/Mannequin/Character/Materials/MaterialLayers/T_ML_Rubber_Blue_01_D.uasset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/Mannequin/Character/Materials/MaterialLayers/T_ML_Rubber_Blue_01_D.uasset -------------------------------------------------------------------------------- /UnrealDemo/Content/Mannequin/Character/Materials/MaterialLayers/T_ML_Rubber_Blue_01_N.uasset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/Mannequin/Character/Materials/MaterialLayers/T_ML_Rubber_Blue_01_N.uasset -------------------------------------------------------------------------------- /UnrealDemo/Content/Mannequin/Character/Mesh/SK_Mannequin.uasset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/Mannequin/Character/Mesh/SK_Mannequin.uasset -------------------------------------------------------------------------------- /UnrealDemo/Content/Mannequin/Character/Mesh/SK_Mannequin_Female.uasset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/Mannequin/Character/Mesh/SK_Mannequin_Female.uasset -------------------------------------------------------------------------------- /UnrealDemo/Content/Mannequin/Character/Mesh/SK_Mannequin_Female_PhysicsAsset.uasset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/Mannequin/Character/Mesh/SK_Mannequin_Female_PhysicsAsset.uasset -------------------------------------------------------------------------------- /UnrealDemo/Content/Mannequin/Character/Mesh/SK_Mannequin_PhysicsAsset.uasset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/Mannequin/Character/Mesh/SK_Mannequin_PhysicsAsset.uasset -------------------------------------------------------------------------------- /UnrealDemo/Content/Mannequin/Character/Mesh/UE4_Mannequin_Skeleton.uasset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/Mannequin/Character/Mesh/UE4_Mannequin_Skeleton.uasset -------------------------------------------------------------------------------- /UnrealDemo/Content/Mannequin/Character/Textures/T_Female_Mask.uasset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/Mannequin/Character/Textures/T_Female_Mask.uasset -------------------------------------------------------------------------------- /UnrealDemo/Content/Mannequin/Character/Textures/T_Female_N.uasset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/Mannequin/Character/Textures/T_Female_N.uasset -------------------------------------------------------------------------------- /UnrealDemo/Content/Mannequin/Character/Textures/T_Male_Mask.uasset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/Mannequin/Character/Textures/T_Male_Mask.uasset -------------------------------------------------------------------------------- /UnrealDemo/Content/Mannequin/Character/Textures/T_Male_N.uasset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/Mannequin/Character/Textures/T_Male_N.uasset -------------------------------------------------------------------------------- /UnrealDemo/Content/Mannequin/Character/Textures/T_UE4Logo_Mask.uasset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/Mannequin/Character/Textures/T_UE4Logo_Mask.uasset -------------------------------------------------------------------------------- /UnrealDemo/Content/Mannequin/Character/Textures/T_UE4Logo_N.uasset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/Mannequin/Character/Textures/T_UE4Logo_N.uasset -------------------------------------------------------------------------------- /UnrealDemo/Content/ThirdPerson/Meshes/Bump_StaticMesh.uasset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/ThirdPerson/Meshes/Bump_StaticMesh.uasset -------------------------------------------------------------------------------- /UnrealDemo/Content/ThirdPerson/Meshes/LeftArm_StaticMesh.uasset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/ThirdPerson/Meshes/LeftArm_StaticMesh.uasset -------------------------------------------------------------------------------- /UnrealDemo/Content/ThirdPerson/Meshes/Linear_Stair_StaticMesh.uasset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/ThirdPerson/Meshes/Linear_Stair_StaticMesh.uasset -------------------------------------------------------------------------------- /UnrealDemo/Content/ThirdPerson/Meshes/RampMaterial.uasset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/ThirdPerson/Meshes/RampMaterial.uasset -------------------------------------------------------------------------------- /UnrealDemo/Content/ThirdPerson/Meshes/Ramp_StaticMesh.uasset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/ThirdPerson/Meshes/Ramp_StaticMesh.uasset -------------------------------------------------------------------------------- /UnrealDemo/Content/ThirdPerson/Meshes/RightArm_StaticMesh.uasset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/ThirdPerson/Meshes/RightArm_StaticMesh.uasset -------------------------------------------------------------------------------- /UnrealDemo/Content/ThirdPersonCPP/Blueprints/ThirdPersonCharacter.uasset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/ThirdPersonCPP/Blueprints/ThirdPersonCharacter.uasset -------------------------------------------------------------------------------- /UnrealDemo/Content/ThirdPersonCPP/Maps/GPhsics.umap: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/ThirdPersonCPP/Maps/GPhsics.umap -------------------------------------------------------------------------------- /UnrealDemo/Content/ThirdPersonCPP/Maps/GPhsics_BuiltData.uasset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/ThirdPersonCPP/Maps/GPhsics_BuiltData.uasset -------------------------------------------------------------------------------- /UnrealDemo/Content/ThirdPersonCPP/Maps/GRigidTest_Box.umap: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/ThirdPersonCPP/Maps/GRigidTest_Box.umap -------------------------------------------------------------------------------- /UnrealDemo/Content/ThirdPersonCPP/Maps/GRigidTest_Box_2.umap: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/ThirdPersonCPP/Maps/GRigidTest_Box_2.umap -------------------------------------------------------------------------------- /UnrealDemo/Content/ThirdPersonCPP/Maps/GRigidTest_Sphere.umap: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/ThirdPersonCPP/Maps/GRigidTest_Sphere.umap -------------------------------------------------------------------------------- /UnrealDemo/Content/ThirdPersonCPP/Maps/GRigidTest_Sphere_2.umap: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/ThirdPersonCPP/Maps/GRigidTest_Sphere_2.umap -------------------------------------------------------------------------------- /UnrealDemo/Content/ThirdPersonCPP/Maps/GRigidTest_Sphere_3.umap: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/ThirdPersonCPP/Maps/GRigidTest_Sphere_3.umap -------------------------------------------------------------------------------- /UnrealDemo/Content/ThirdPersonCPP/Maps/PRigidTest_Box.umap: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/ThirdPersonCPP/Maps/PRigidTest_Box.umap -------------------------------------------------------------------------------- /UnrealDemo/Content/ThirdPersonCPP/Maps/PRigidTest_Sphere.umap: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/ThirdPersonCPP/Maps/PRigidTest_Sphere.umap -------------------------------------------------------------------------------- /UnrealDemo/Content/ThirdPersonCPP/Maps/TestCollisionGPhysics.umap: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/ThirdPersonCPP/Maps/TestCollisionGPhysics.umap -------------------------------------------------------------------------------- /UnrealDemo/Content/ThirdPersonCPP/Maps/TestCollisionGPhysics_BuiltData.uasset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/ThirdPersonCPP/Maps/TestCollisionGPhysics_BuiltData.uasset -------------------------------------------------------------------------------- /UnrealDemo/Content/ThirdPersonCPP/Maps/ThirdPersonExampleMap.umap: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/ThirdPersonCPP/Maps/ThirdPersonExampleMap.umap -------------------------------------------------------------------------------- /UnrealDemo/Content/ThirdPersonCPP/Maps/ThirdPersonExampleMap_BuiltData.uasset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlinzhou/deterministic_physics/2e07d1f46b905dc481c938b5b5cb798350f6fc10/UnrealDemo/Content/ThirdPersonCPP/Maps/ThirdPersonExampleMap_BuiltData.uasset -------------------------------------------------------------------------------- /UnrealDemo/Source/UnrealDemo.Target.cs: -------------------------------------------------------------------------------- 1 | // Copyright Epic Games, Inc. All Rights Reserved. 2 | 3 | using UnrealBuildTool; 4 | using System.Collections.Generic; 5 | 6 | public class UnrealDemoTarget : TargetRules 7 | { 8 | public UnrealDemoTarget(TargetInfo Target) : base(Target) 9 | { 10 | Type = TargetType.Game; 11 | DefaultBuildSettings = BuildSettingsVersion.V2; 12 | ExtraModuleNames.Add("UnrealDemo"); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /UnrealDemo/Source/UnrealDemo/GPhysicsActor.h: -------------------------------------------------------------------------------- 1 | // Fill out your copyright notice in the Description page of Project Settings. 2 | 3 | #pragma once 4 | 5 | #include "CoreMinimal.h" 6 | #include "GameFramework/Actor.h" 7 | #include "glacier_physics_world.h" 8 | #include "glacier_collision_shape.h" 9 | #include "GPhysicsActor.generated.h" 10 | 11 | 12 | class GStaticRigid; 13 | class GRigidBody; 14 | 15 | UENUM(meta = (Bitflags )) 16 | enum class EGDrawMask : uint32 17 | { 18 | UGPDraw_Shape , 19 | UGPDraw_LocalBox , 20 | UGPDraw_WorldBox , 21 | UGPDraw_CeilBox , 22 | UGPDraw_Contact , 23 | UGPDraw_Moumentum, 24 | 25 | }; 26 | ENUM_CLASS_FLAGS(EGDrawMask); 27 | 28 | UCLASS() 29 | class UNREALDEMO_API AGPhysicsActor : public AActor 30 | { 31 | GENERATED_BODY() 32 | 33 | public: 34 | // Sets default values for this actor's properties 35 | AGPhysicsActor(); 36 | 37 | bool ShouldTickIfViewportsOnly() const override 38 | { 39 | return true; 40 | } 41 | 42 | static AGPhysicsActor* FindScenePhysics( class UWorld* ); 43 | 44 | protected: 45 | // Called when the game starts or when spawned 46 | virtual void BeginPlay() override; 47 | 48 | virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override; 49 | 50 | public: 51 | // Called every frame 52 | virtual void Tick(float DeltaTime) override; 53 | 54 | 55 | void GPysics_PreTick(); 56 | void GPysics_Simulate(float DeltaTime); 57 | void GPysics_CollisionBroadPhase(); 58 | void GPysics_CollisionNarrowPhase(); 59 | void GPysics_SolveContactConstraint(); 60 | void GPysics_UpdateSceneGrid(); 61 | void GPysics_PostTick(); 62 | void GPysics_DebugDraw(); 63 | 64 | GRigidBody* CreateSimpleRigidBody( const GTransform_QT& Trans, GVector3 Halfsize, EShape TShape ); 65 | GRigidBody* CreateStaticPlane(const GTransform_QT& Trans ); 66 | 67 | GPhysicsWorld m_PhysicsWorld; 68 | 69 | UPROPERTY(EditAnywhere,meta = (Bitmask, BitmaskEnum = "EGDrawMask")) 70 | int32 DrawMask; 71 | 72 | // UPROPERTY(EditAnywhere, meta = (Bitmask, BitmaskEnum = "EGDrawMask")) 73 | // float DrawMask; 74 | 75 | }; 76 | -------------------------------------------------------------------------------- /UnrealDemo/Source/UnrealDemo/GPhysicsCollistionActor.h: -------------------------------------------------------------------------------- 1 | // Fill out your copyright notice in the Description page of Project Settings. 2 | 3 | #pragma once 4 | 5 | #include "CoreMinimal.h" 6 | #include "GameFramework/Actor.h" 7 | #include "GPhysicsCollistionActor.generated.h" 8 | 9 | 10 | 11 | UCLASS() 12 | class UNREALDEMO_API AGPhysicsCollistionActor : public AActor 13 | { 14 | GENERATED_BODY() 15 | 16 | public: 17 | // Sets default values for this actor's properties 18 | AGPhysicsCollistionActor(); 19 | bool ShouldTickIfViewportsOnly() const override { return true; } 20 | protected: 21 | // Called when the game starts or when spawned 22 | virtual void BeginPlay() override; 23 | 24 | 25 | 26 | public: 27 | // Called every frame 28 | virtual void Tick(float DeltaTime) override; 29 | 30 | UPROPERTY(EditAnywhere, Category = "Sphere") 31 | bool SphereShow = false; 32 | UPROPERTY(EditAnywhere, Category = "Sphere") 33 | FVector SphereCenterB = FVector(400, 200, 90); 34 | UPROPERTY(EditAnywhere, Category = "Sphere") 35 | float SphereRadiusA = 100.f; 36 | UPROPERTY(EditAnywhere, Category = "Sphere") 37 | float SphereRadiusB = 100.f; 38 | 39 | UPROPERTY(EditAnywhere, Category = "Triangle") 40 | bool TriangleShow = false; 41 | UPROPERTY(EditAnywhere, Category = "Triangle") 42 | FVector Triangle_Pos = FVector(111, 0, 0); 43 | UPROPERTY(EditAnywhere, Category = "Triangle") 44 | FVector Triangle_p0 = FVector(10, 0, 0); 45 | UPROPERTY(EditAnywhere, Category = "Triangle") 46 | FVector Triangle_p1 = FVector(0, 10, 0); 47 | UPROPERTY(EditAnywhere, Category = "Triangle") 48 | FVector Triangle_p2 = FVector(10, 10, 0); 49 | 50 | UPROPERTY(EditAnywhere, Category = "Box") 51 | bool BoxShow = false; 52 | UPROPERTY(EditAnywhere, Category = "Box") 53 | FVector BoxHalfSizeA = FVector(100, 200, 90); 54 | UPROPERTY(EditAnywhere, Category = "Box") 55 | FVector BoxCenterB = FVector(0, 0, 200); 56 | UPROPERTY(EditAnywhere, Category = "Box") 57 | FVector BoxHalfSizeB = FVector(100, 200, 90); 58 | UPROPERTY(EditAnywhere, Category = "Box") 59 | FRotator BoxRotB = FRotator(0, 0, 0); 60 | 61 | UPROPERTY(EditAnywhere, Category = "Capsule") 62 | bool CapsuleShow = false; 63 | UPROPERTY(EditAnywhere, Category = "Capsule") 64 | FVector CapsuleCenter = FVector(100, 200, 90); 65 | UPROPERTY(EditAnywhere, Category = "Capsule") 66 | FVector CapsuleDir = FVector(0, 0, 1); 67 | UPROPERTY(EditAnywhere, Category = "Capsule") 68 | float CapsuleRadius = 20.f; 69 | UPROPERTY(EditAnywhere, Category = "Capsule") 70 | float CapsuleHalfHeight = 20.f; 71 | 72 | 73 | UPROPERTY(EditAnywhere, Category = "ConvexHull") 74 | bool CovexHullShow = false; 75 | UPROPERTY(EditAnywhere, Category = "ConvexHull") 76 | FVector CovexHullCenterA = FVector(0, 0, 190); 77 | UPROPERTY(EditAnywhere, Category = "ConvexHull") 78 | FRotator ConvexRotA = FRotator(0,0,0); 79 | UPROPERTY(EditAnywhere, Category = "ConvexHull") 80 | FVector CovexHullCenterB = FVector(100, 0, 190); 81 | UPROPERTY(EditAnywhere, Category = "ConvexHull") 82 | FRotator ConvexRotB = FRotator(0,0,0);; 83 | UPROPERTY(EditAnywhere, Category = "ConvexHull") 84 | float CovexRandomSize = 100.f; 85 | UPROPERTY(EditAnywhere, Category = "ConvexHull") 86 | int CovexRandomCount = 10; 87 | 88 | UPROPERTY(EditAnywhere, Category = "TestSphere_Box") 89 | bool TestSphere_Box = false; 90 | 91 | class GConvexHull* pConvexHullA = nullptr; 92 | class GConvexHull* pConvexHullB = nullptr; 93 | 94 | class GConvexHullBuilder* pBuilder = nullptr; 95 | 96 | 97 | }; 98 | -------------------------------------------------------------------------------- /UnrealDemo/Source/UnrealDemo/GUnrealUtility.cpp: -------------------------------------------------------------------------------- 1 | // Fill out your copyright notice in the Description page of Project Settings. 2 | 3 | #include "GUnrealUtility.h" 4 | #include "Kismet/KismetMathLibrary.h" 5 | #include "Kismet/KismetSystemLibrary.h" 6 | 7 | 8 | void GPhysicsDraw::DrawLine(const GVector3& V0, const GVector3& V1, GColor uColor) 9 | { 10 | FVector TV0 = GUtility::Unit_G_to_U(V0); 11 | FVector TV1 = GUtility::Unit_G_to_U(V1); 12 | 13 | UKismetSystemLibrary::DrawDebugLine(m_World, TV0, TV1, FColor(uColor.RawValue)); 14 | } 15 | 16 | -------------------------------------------------------------------------------- /UnrealDemo/Source/UnrealDemo/GUnrealUtility.h: -------------------------------------------------------------------------------- 1 | // Fill out your copyright notice in the Description page of Project Settings. 2 | 3 | #pragma once 4 | 5 | #include "CoreMinimal.h" 6 | #include "glacier_vector.h" 7 | #include "glacier_quaternion.h" 8 | #include "glacier_transform_qt.h" 9 | #include "glacier_debug_draw.h" 10 | 11 | class GUtility 12 | { 13 | public: 14 | 15 | static constexpr float fUTG = 0.01f; 16 | static constexpr float fGTU = 100.f; 17 | 18 | static inline f32 U_to_G(float value) {return GMath::FromFloat(value );} 19 | static inline GVector3 U_to_G(const FVector& value) {return GVector3( GMath::FromFloat(value.X ),GMath::FromFloat(value.Y ),GMath::FromFloat(value.Z )); } 20 | static inline GQuaternion U_to_G(const FQuat& value) {return GQuaternion(GMath::FromFloat(value.X), GMath::FromFloat(value.Y), GMath::FromFloat(value.Z),GMath::FromFloat(value.W)); } 21 | static inline GTransform_QT U_to_G(const FTransform& value) {return GTransform_QT(U_to_G(value.GetRotation()), Unit_U_to_G(value.GetTranslation())); } 22 | 23 | static inline float G_to_U(f32 value) {return GMath::ToFloat(value); } 24 | static inline FVector G_to_U(const GVector3& value) {return FVector(GMath::ToFloat(value.x) , GMath::ToFloat(value.y), GMath::ToFloat(value.z)); } 25 | static inline FQuat G_to_U(const GQuaternion& value) {return FQuat(GMath::ToFloat(value.x), GMath::ToFloat(value.y), GMath::ToFloat(value.z), GMath::ToFloat(value.w)); } 26 | static inline FTransform G_to_U(const GTransform_QT& value) {return FTransform(G_to_U(value.m_Rot), Unit_G_to_U(value.m_Pos)); } 27 | 28 | static inline f32 Unit_U_to_G(float value) {return U_to_G(value * fUTG);} 29 | static inline GVector3 Unit_U_to_G(const FVector& value) {return U_to_G(value * fUTG);} 30 | static inline float Unit_G_to_U(f32 value) {return G_to_U(value) * fGTU;} 31 | static inline FVector Unit_G_to_U(const GVector3& value) {return G_to_U(value) * fGTU;} 32 | }; 33 | 34 | 35 | class GPhysicsDraw : public IGlacierDraw 36 | { 37 | public: 38 | GPhysicsDraw(UWorld* pWorld) 39 | { 40 | m_World = pWorld; 41 | } 42 | 43 | virtual void DrawLine(const GVector3& V0, const GVector3& V1, GColor uColor); 44 | 45 | UWorld* m_World = nullptr; 46 | }; 47 | 48 | -------------------------------------------------------------------------------- /UnrealDemo/Source/UnrealDemo/RigidPhysicsComponent.cpp: -------------------------------------------------------------------------------- 1 | // Fill out your copyright notice in the Description page of Project Settings. 2 | 3 | 4 | #include "RigidPhysicsComponent.h" 5 | #include "GPhysicsActor.h" 6 | #include "glacier_rigid_body.h" 7 | #include "GUnrealUtility.h" 8 | 9 | 10 | 11 | // Sets default values for this component's properties 12 | URigidPhysicsComponent::URigidPhysicsComponent() 13 | { 14 | // Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features 15 | // off to improve performance if you don't need them. 16 | PrimaryComponentTick.bCanEverTick = true; 17 | 18 | // ... 19 | } 20 | 21 | 22 | // Called when the game starts 23 | void URigidPhysicsComponent::BeginPlay() 24 | { 25 | Super::BeginPlay(); 26 | 27 | AGPhysicsActor* PPhysics = AGPhysicsActor::FindScenePhysics(GetWorld()); 28 | 29 | if (PPhysics != nullptr) 30 | { 31 | switch (RigidShape) 32 | { 33 | case UGShape::UGShape_Sphere: 34 | case UGShape::UGShape_Box: 35 | { 36 | m_pRigid = PPhysics->CreateSimpleRigidBody( 37 | GUtility::U_to_G(GetOwner()->GetTransform()), 38 | GVector3(GUtility::Unit_U_to_G(VHalfSize)), 39 | RigidShape == UGShape::UGShape_Sphere ? EShape::EShape_Sphere : EShape::EShape_Box); 40 | 41 | m_pRigid->m_Gravity = GUtility::U_to_G(Gravity); 42 | m_pRigid->m_LinearVelocityMax = GUtility::U_to_G(MaxLinearVelocity); 43 | // m_pRigid->m_AngularVelocityMax = GUtility::U_to_G(MaxAngularVelocity); 44 | 45 | m_pRigid->m_AngularVelocityMax = GMath::Makef32(10,0,1); 46 | 47 | m_pRigid->m_LinearVelocity = GUtility::Unit_U_to_G(StartLinearVeloctiy); 48 | m_pRigid->m_AngularVelocity = GUtility::U_to_G(StartAngularVeloctiy); 49 | 50 | m_pRigid->m_LinearDamping = GUtility::U_to_G(LinearDamping); 51 | m_pRigid->m_AngularDamping = GUtility::U_to_G(AngularDamping); 52 | 53 | 54 | m_pRigid->m_bDynamic = Dynamic; 55 | 56 | } 57 | break; 58 | case UGShape::UGShape_Plane: 59 | { 60 | m_pRigid = PPhysics->CreateStaticPlane(GUtility::U_to_G(GetOwner()->GetTransform())); 61 | m_pRigid->m_bDynamic = false; 62 | } 63 | break; 64 | 65 | default: 66 | break; 67 | } 68 | } 69 | } 70 | 71 | 72 | // Called every frame 73 | void URigidPhysicsComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) 74 | { 75 | Super::TickComponent(DeltaTime, TickType, ThisTickFunction); 76 | 77 | if (m_pRigid != nullptr) 78 | { 79 | if (Dynamic) 80 | { 81 | GetOwner()->SetActorLocation(GUtility::Unit_G_to_U(m_pRigid->m_Transform.m_Pos)); 82 | GetOwner()->SetActorRotation(GUtility::G_to_U(m_pRigid->m_Transform.m_Rot)); 83 | } 84 | else 85 | { 86 | m_pRigid->m_Transform = GUtility::U_to_G(GetOwner()->GetTransform()); 87 | 88 | m_pRigid->m_Transform.m_Rot.Normalize(); 89 | m_pRigid->NeedUpdate(); 90 | } 91 | 92 | if( ModifySize ) 93 | { 94 | m_pRigid->m_Shape.HalfSize = GUtility::Unit_U_to_G(VHalfSize); 95 | } 96 | } 97 | } 98 | 99 | -------------------------------------------------------------------------------- /UnrealDemo/Source/UnrealDemo/RigidPhysicsComponent.h: -------------------------------------------------------------------------------- 1 | // Fill out your copyright notice in the Description page of Project Settings. 2 | 3 | #pragma once 4 | 5 | #include "CoreMinimal.h" 6 | #include "Components/ActorComponent.h" 7 | #include "RigidPhysicsComponent.generated.h" 8 | 9 | UENUM() 10 | enum class UGShape : uint8 11 | { 12 | UGShape_Sphere, 13 | UGShape_Box, 14 | UGShape_Plane, 15 | 16 | }; 17 | 18 | UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) ) 19 | class UNREALDEMO_API URigidPhysicsComponent : public UActorComponent 20 | { 21 | GENERATED_BODY() 22 | 23 | public: 24 | // Sets default values for this component's properties 25 | URigidPhysicsComponent(); 26 | 27 | protected: 28 | // Called when the game starts 29 | virtual void BeginPlay() override; 30 | 31 | public: 32 | // Called every frame 33 | virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override; 34 | 35 | UPROPERTY(EditAnywhere) 36 | bool Dynamic = false; 37 | UPROPERTY(EditAnywhere) 38 | bool ModifySize = false; 39 | UPROPERTY(EditAnywhere) 40 | UGShape RigidShape = UGShape::UGShape_Sphere; 41 | UPROPERTY(EditAnywhere) 42 | FVector VHalfSize = FVector(50,50,50); 43 | UPROPERTY(EditAnywhere) 44 | FVector Gravity = FVector(0,0,0); 45 | UPROPERTY(EditAnywhere) 46 | float MaxLinearVelocity = 100.f; 47 | UPROPERTY(EditAnywhere) 48 | float MaxAngularVelocity = 100.f; 49 | UPROPERTY(EditAnywhere) 50 | FVector StartLinearVeloctiy = FVector(0,0,0); 51 | UPROPERTY(EditAnywhere) 52 | FVector StartAngularVeloctiy = FVector(0, 0, 0); 53 | UPROPERTY(EditAnywhere) 54 | float LinearDamping = 0.1f; 55 | UPROPERTY(EditAnywhere) 56 | float AngularDamping = 0.1f; 57 | 58 | class GRigidBody* m_pRigid = nullptr; 59 | 60 | }; 61 | -------------------------------------------------------------------------------- /UnrealDemo/Source/UnrealDemo/UnrealDemo.Build.cs: -------------------------------------------------------------------------------- 1 | // Copyright Epic Games, Inc. All Rights Reserved. 2 | 3 | using UnrealBuildTool; 4 | 5 | public class UnrealDemo : ModuleRules 6 | { 7 | public UnrealDemo(ReadOnlyTargetRules Target) : base(Target) 8 | { 9 | PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs; 10 | 11 | PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "HeadMountedDisplay" }); 12 | 13 | 14 | PrivateIncludePaths.AddRange( 15 | new string[] 16 | { 17 | "UnrealDemo/gphysics/include/collision", 18 | "UnrealDemo/gphysics/include/dynamic", 19 | "UnrealDemo/gphysics/include/math", 20 | "UnrealDemo/gphysics/include/utils", 21 | "UnrealDemo/gphysics/include/world", 22 | 23 | 24 | 25 | }); 26 | 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /UnrealDemo/Source/UnrealDemo/UnrealDemo.cpp: -------------------------------------------------------------------------------- 1 | // Copyright Epic Games, Inc. All Rights Reserved. 2 | 3 | #include "UnrealDemo.h" 4 | #include "Modules/ModuleManager.h" 5 | 6 | IMPLEMENT_PRIMARY_GAME_MODULE( FDefaultGameModuleImpl, UnrealDemo, "UnrealDemo" ); 7 | -------------------------------------------------------------------------------- /UnrealDemo/Source/UnrealDemo/UnrealDemo.h: -------------------------------------------------------------------------------- 1 | // Copyright Epic Games, Inc. All Rights Reserved. 2 | 3 | #pragma once 4 | 5 | #include "CoreMinimal.h" 6 | -------------------------------------------------------------------------------- /UnrealDemo/Source/UnrealDemo/UnrealDemoCharacter.h: -------------------------------------------------------------------------------- 1 | // Copyright Epic Games, Inc. All Rights Reserved. 2 | 3 | #pragma once 4 | 5 | #include "CoreMinimal.h" 6 | #include "GameFramework/Character.h" 7 | #include "UnrealDemoCharacter.generated.h" 8 | 9 | UCLASS(config=Game) 10 | class AUnrealDemoCharacter : public ACharacter 11 | { 12 | GENERATED_BODY() 13 | 14 | /** Camera boom positioning the camera behind the character */ 15 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true")) 16 | class USpringArmComponent* CameraBoom; 17 | 18 | /** Follow camera */ 19 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true")) 20 | class UCameraComponent* FollowCamera; 21 | public: 22 | AUnrealDemoCharacter(); 23 | 24 | /** Base turn rate, in deg/sec. Other scaling may affect final turn rate. */ 25 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Camera) 26 | float BaseTurnRate; 27 | 28 | /** Base look up/down rate, in deg/sec. Other scaling may affect final rate. */ 29 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Camera) 30 | float BaseLookUpRate; 31 | 32 | protected: 33 | 34 | /** Resets HMD orientation in VR. */ 35 | void OnResetVR(); 36 | 37 | /** Called for forwards/backward input */ 38 | void MoveForward(float Value); 39 | 40 | /** Called for side to side input */ 41 | void MoveRight(float Value); 42 | 43 | /** 44 | * Called via input to turn at a given rate. 45 | * @param Rate This is a normalized rate, i.e. 1.0 means 100% of desired turn rate 46 | */ 47 | void TurnAtRate(float Rate); 48 | 49 | /** 50 | * Called via input to turn look up/down at a given rate. 51 | * @param Rate This is a normalized rate, i.e. 1.0 means 100% of desired turn rate 52 | */ 53 | void LookUpAtRate(float Rate); 54 | 55 | /** Handler for when a touch input begins. */ 56 | void TouchStarted(ETouchIndex::Type FingerIndex, FVector Location); 57 | 58 | /** Handler for when a touch input stops. */ 59 | void TouchStopped(ETouchIndex::Type FingerIndex, FVector Location); 60 | 61 | protected: 62 | // APawn interface 63 | virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override; 64 | // End of APawn interface 65 | 66 | public: 67 | /** Returns CameraBoom subobject **/ 68 | FORCEINLINE class USpringArmComponent* GetCameraBoom() const { return CameraBoom; } 69 | /** Returns FollowCamera subobject **/ 70 | FORCEINLINE class UCameraComponent* GetFollowCamera() const { return FollowCamera; } 71 | }; 72 | 73 | -------------------------------------------------------------------------------- /UnrealDemo/Source/UnrealDemo/UnrealDemoGameMode.cpp: -------------------------------------------------------------------------------- 1 | // Copyright Epic Games, Inc. All Rights Reserved. 2 | 3 | #include "UnrealDemoGameMode.h" 4 | #include "UnrealDemoCharacter.h" 5 | #include "UObject/ConstructorHelpers.h" 6 | 7 | AUnrealDemoGameMode::AUnrealDemoGameMode() 8 | { 9 | // set default pawn class to our Blueprinted character 10 | static ConstructorHelpers::FClassFinder PlayerPawnBPClass(TEXT("/Game/ThirdPersonCPP/Blueprints/ThirdPersonCharacter")); 11 | if (PlayerPawnBPClass.Class != NULL) 12 | { 13 | DefaultPawnClass = PlayerPawnBPClass.Class; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /UnrealDemo/Source/UnrealDemo/UnrealDemoGameMode.h: -------------------------------------------------------------------------------- 1 | // Copyright Epic Games, Inc. All Rights Reserved. 2 | 3 | #pragma once 4 | 5 | #include "CoreMinimal.h" 6 | #include "GameFramework/GameModeBase.h" 7 | #include "UnrealDemoGameMode.generated.h" 8 | 9 | UCLASS(minimalapi) 10 | class AUnrealDemoGameMode : public AGameModeBase 11 | { 12 | GENERATED_BODY() 13 | 14 | public: 15 | AUnrealDemoGameMode(); 16 | }; 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /UnrealDemo/Source/UnrealDemo/gphysics/include/collision/glacier_collision_algorithm.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | #pragma once 14 | 15 | #include "glacier_transform_qt.h" 16 | #include "glacier_collision_object.h" 17 | #include "glacier_contact.h" 18 | #include 19 | #include 20 | 21 | 22 | class GCollisionAlgorithm 23 | { 24 | public: 25 | 26 | GCollisionAlgorithm( bool bSwap = false) : m_bSwap( bSwap ), CallTimes(0) 27 | { 28 | 29 | } 30 | 31 | virtual ~GCollisionAlgorithm() 32 | { 33 | } 34 | 35 | virtual bool ProcessCollision( const GCObject* ObjA, const GCObject* ObjB, GCollisionContact* pContact = nullptr ); 36 | 37 | bool m_bSwap; 38 | int32_t CallTimes; 39 | }; 40 | 41 | class GCG_Sphere_Sphere : public GCollisionAlgorithm 42 | { 43 | public: 44 | GCG_Sphere_Sphere( bool bSwap = false) : GCollisionAlgorithm(bSwap) 45 | { 46 | } 47 | 48 | virtual bool ProcessCollision(const GCObject* ObjA, const GCObject* ObjB, GCollisionContact* pContact = nullptr ) override; 49 | }; 50 | 51 | class GCG_Sphere_Box : public GCollisionAlgorithm 52 | { 53 | public: 54 | GCG_Sphere_Box(bool bSwap = false) : GCollisionAlgorithm(bSwap) 55 | { 56 | } 57 | 58 | virtual bool ProcessCollision(const GCObject* ObjA, const GCObject* ObjB, GCollisionContact* pContact = nullptr) override; 59 | }; 60 | 61 | class GCG_Sphere_Capusle : public GCollisionAlgorithm 62 | { 63 | public: 64 | GCG_Sphere_Capusle(bool bSwap = false) : GCollisionAlgorithm(bSwap) 65 | { 66 | } 67 | 68 | virtual bool ProcessCollision(const GCObject* ObjA, const GCObject* ObjB, GCollisionContact* pContact = nullptr) override; 69 | }; 70 | 71 | class GCG_Sphere_Cylinder : public GCollisionAlgorithm 72 | { 73 | public: 74 | GCG_Sphere_Cylinder(bool bSwap = false) : GCollisionAlgorithm(bSwap) 75 | { 76 | } 77 | 78 | virtual bool ProcessCollision(const GCObject* ObjA, const GCObject* ObjB, GCollisionContact* pContact = nullptr) override; 79 | }; 80 | 81 | class GCG_Sphere_Plane : public GCollisionAlgorithm 82 | { 83 | public: 84 | GCG_Sphere_Plane(bool bSwap = false) : GCollisionAlgorithm(bSwap) 85 | { 86 | } 87 | 88 | virtual bool ProcessCollision(const GCObject* ObjA, const GCObject* ObjB, GCollisionContact* pContact = nullptr) override; 89 | }; 90 | 91 | class GCG_Box_Box : public GCollisionAlgorithm 92 | { 93 | public: 94 | GCG_Box_Box(bool bSwap = false) : GCollisionAlgorithm(bSwap) 95 | { 96 | } 97 | 98 | virtual bool ProcessCollision(const GCObject* ObjA, const GCObject* ObjB, GCollisionContact* pContact = nullptr) override; 99 | }; 100 | 101 | class GCG_Plane_Box : public GCollisionAlgorithm 102 | { 103 | public: 104 | GCG_Plane_Box(bool bSwap = false) : GCollisionAlgorithm(bSwap) 105 | { 106 | } 107 | 108 | virtual bool ProcessCollision(const GCObject* ObjA, const GCObject* ObjB, GCollisionContact* pContact = nullptr) override; 109 | }; 110 | 111 | 112 | 113 | class GCollisionManerger 114 | { 115 | public: 116 | 117 | GCollisionManerger(); 118 | ~GCollisionManerger(); 119 | 120 | 121 | void Init( ); 122 | 123 | inline GCollisionAlgorithm* GetAlgrithm( int32_t nShapeA, int32_t nShapeB) 124 | { 125 | return m_Algorithm[nShapeA][nShapeB]; 126 | } 127 | 128 | private: 129 | GCollisionAlgorithm* m_Algorithm[EShape_Max][EShape_Max]; 130 | 131 | }; -------------------------------------------------------------------------------- /UnrealDemo/Source/UnrealDemo/gphysics/include/collision/glacier_collision_box.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | #pragma once 14 | 15 | #include "glacier_transform_qt.h" 16 | #include "glacier_collision_shape.h" 17 | 18 | class GCollision_Box 19 | { 20 | public: 21 | 22 | 23 | static bool Box_Box( 24 | const GShapeBox& ShapA, 25 | const GTransform_QT& TransformA, 26 | const GShapeBox& ShapB, 27 | const GTransform_QT& TransformB, 28 | GVector3* pOutPosition, 29 | GVector3* pOutNormal); 30 | 31 | static bool Box_Plane( 32 | const GShapeBox& ShapA, 33 | const GTransform_QT& TransformA, 34 | const GShapePlane& ShapB, 35 | const GTransform_QT& TransformB); 36 | 37 | 38 | static int32_t Box_Box_Contact( 39 | const GShapeBox& ShapA, 40 | const GTransform_QT& TransformA, 41 | const GShapeBox& ShapB, 42 | const GTransform_QT& TransformB, 43 | class GCollisionContact* pContact); 44 | 45 | static int32_t Box_Box_Contact_PX( 46 | const GShapeBox& ShapA, 47 | const GTransform_QT& TransformA, 48 | const GShapeBox& ShapB, 49 | const GTransform_QT& TransformB, 50 | class GCollisionContact* pContact, 51 | bool& swap); 52 | 53 | 54 | static int32_t Plane_Box_Contact( 55 | const GShapePlane& ShapA, 56 | const GTransform_QT& TransformA, 57 | const GShapeBox& ShapB, 58 | const GTransform_QT& TransformB, 59 | class GCollisionContact* pContact); 60 | }; 61 | 62 | 63 | -------------------------------------------------------------------------------- /UnrealDemo/Source/UnrealDemo/gphysics/include/collision/glacier_collision_gjk.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | #pragma once 14 | 15 | #include "glacier_transform_qt.h" 16 | #include "glacier_collision_shape.h" 17 | #include "glacier_debug_draw.h" 18 | 19 | class IGlacierDraw; 20 | 21 | class GSimplex 22 | { 23 | public: 24 | GVector3 m_Points[4]; 25 | }; 26 | 27 | 28 | class GCollision_GJK 29 | { 30 | public: 31 | 32 | static const f32 s_DefaultEpsilon;// = GMath::Makef32(0,1,1000); 33 | 34 | static bool GJKTest( 35 | const GShapeConvexBase& ShapA, 36 | const GTransform_QT& TransformA, 37 | const GShapeConvexBase& ShapB, 38 | const GTransform_QT& TransformB, 39 | IGlacierDraw* pDebugDraw = nullptr ); 40 | 41 | 42 | template 43 | static inline GVector3 s_GetSupportPos( 44 | const GVector3& DirectionA, 45 | const ShapTypeA& ShapA, 46 | const ShapTypeB& ShapB, 47 | const GTransform_QT& LocalBToLocalA, 48 | const GTransform_QT& LocalAToLocalB, 49 | const GTransform_QT& TransformShapA, 50 | const GTransform_QT& TransformShapB, 51 | IGlacierDraw* pDebugDraw, 52 | uint32_t uColor) 53 | { 54 | GVector3 DirectionB = LocalAToLocalB.TransformNormal(-DirectionA); 55 | GVector3 PosA = ShapA.GetSupportLocalPos(DirectionA); 56 | GVector3 PosB = ShapB.GetSupportLocalPos(DirectionB); 57 | 58 | if (pDebugDraw != nullptr) 59 | { 60 | 61 | f32 ff = GMath::FromFloat(0.02f); 62 | f32 f2 = GMath::FromFloat(0.04f); 63 | 64 | pDebugDraw->DrawSphere(GTransform_QT(GQuaternion::Identity(), TransformShapA.TransformPosition(PosA)), ff, uColor, 12); 65 | pDebugDraw->DrawSphere(GTransform_QT(GQuaternion::Identity(), TransformShapB.TransformPosition(PosB)), ff, uColor, 12); 66 | pDebugDraw->DrawLine(TransformShapA.m_Pos, TransformShapA.m_Pos + TransformShapA.TransformNormal(DirectionA) * f2, uColor); 67 | pDebugDraw->DrawLine(TransformShapB.m_Pos, TransformShapB.m_Pos + TransformShapB.TransformNormal(DirectionB) * f2, uColor); 68 | } 69 | 70 | return PosA - LocalBToLocalA.TransformPosition(PosB); 71 | } 72 | 73 | /* template 74 | static inline f32 GJKDistance( 75 | const ShapTypeA& ShapA, const GTransform_QT& TransformA, 76 | const ShapTypeB& ShapB, const GTransform_QT& TransformB, 77 | f32 Epsilon = s_DefaultEpsilon, 78 | IGlacierDraw* pDebugDraw = nullptr ) 79 | { 80 | GTransform_QT WorlToLocal_A = TransformA.GetInverse(); 81 | GTransform_QT LocalBToLocalA = TransformB * WorlToLocal_A; 82 | GTransform_QT LocalAToLocalB = LocalBToLocalA.GetInverse(); 83 | 84 | 85 | GVector3 DirectionA = LocalBToLocalA.TransformPosition(GVector3::Zero()).GetNormalize(); 86 | GVector3 SupportPos0 = s_GetSupportPos(DirectionA, ShapA, ShapB, LocalBToLocalA, LocalAToLocalB, TransformA, TransformB, pDebugDraw, 0x00FF0000); 87 | 88 | f32 fDistance = SupportPos0.Size(); 89 | if (f < GMath::Epsilon()) 90 | { 91 | return fDistance; 92 | } 93 | 94 | while ( fDistance > Epsilon ) 95 | { 96 | f32 fDotTest = GVector3::DotProduct( SupportPos0,SupportPos0 ); 97 | if (fDotTest > GMath::Zero()) 98 | { 99 | return fDotTest; // not accurate 100 | } 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | f32 fDistance = SupportPos0.Size(); 109 | if (f < GMath::Epsilon()) 110 | { 111 | return fDistance; 112 | } 113 | } 114 | 115 | 116 | 117 | return fDistance; 118 | 119 | }*/ 120 | 121 | }; 122 | 123 | 124 | -------------------------------------------------------------------------------- /UnrealDemo/Source/UnrealDemo/gphysics/include/collision/glacier_collision_sphere.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | #pragma once 14 | 15 | #include "glacier_vector.h" 16 | 17 | class GShapeSphere; 18 | class GShapePlane; 19 | class GShapeBox; 20 | class GTransform_QT; 21 | class GCollisionContact; 22 | class GCollision_Sphere 23 | { 24 | public: 25 | 26 | static inline bool Sphere_Sphere(const GVector3& c0, const f32 r0, const GVector3& c1, const f32 r1) 27 | { 28 | GVector3 d = c0 - c1; 29 | f32 len2 = d.SizeSquare(); 30 | return len2 < (r1+r0) * (r1+r0); 31 | } 32 | 33 | static inline bool Sphere_Point(const GVector3& c0, const f32 r0, const GVector3& Point) { return Sphere_Sphere(c0, r0, Point, GMath::Zero()); } 34 | static inline bool Sphere_Sphere( const GVector3& center0, const f32 radius0, const GVector3& center1, const f32 radius1, GVector3& finalCenter, GVector3* pOutNormal, bool bInner = false) 35 | { 36 | f32 r = radius1 + (bInner ? -radius0 : radius0); 37 | f32 r2 = r * r; 38 | 39 | GVector3 d = center0 - center1; 40 | f32 len2 = d.SizeSquare(); 41 | 42 | if (bInner ^ (len2 >= r2)) //|| (bInner && len2 <= r2) 43 | { 44 | return false; 45 | } 46 | else 47 | { 48 | if (len2 < GMath::Epsilon()) 49 | { 50 | finalCenter = center1 + GVector3(r, GMath::Zero(), GMath::Zero()); 51 | if (pOutNormal != nullptr) 52 | { 53 | *pOutNormal = GVector3(GMath::One(), GMath::Zero(), GMath::Zero()); 54 | } 55 | return true; 56 | } 57 | else 58 | { 59 | f32 invlen = GMath::InvSqrt(len2); 60 | 61 | GVector3 TNormal = d * invlen; 62 | 63 | if (pOutNormal != nullptr) 64 | { 65 | *pOutNormal = bInner ? -TNormal : TNormal; 66 | } 67 | 68 | finalCenter = center1 + TNormal * r; 69 | return true; 70 | } 71 | } 72 | } 73 | 74 | static inline bool LineSphere(const GVector3& lPoint1, const GVector3& lPoint2, const GVector3& dCenter, f32 dRadius) 75 | { 76 | const GVector3& TDir = lPoint2 - lPoint1; 77 | 78 | f32 uLength = TDir.SizeSquare(); 79 | 80 | if (uLength == GMath::Zero()) 81 | { 82 | if ((dCenter - lPoint1).SizeSquare()> dRadius * dRadius) 83 | { 84 | return false; 85 | } 86 | else 87 | { 88 | return true; 89 | } 90 | } 91 | else 92 | { 93 | f32 T = GVector3::DotProduct(dCenter - lPoint1, TDir) / uLength; 94 | 95 | GVector3 VNearest = lPoint1 + GMath::Clamp(T, GMath::Zero(), GMath::One()) * TDir; 96 | 97 | if ((dCenter - VNearest).SizeSquare() > dRadius * dRadius) 98 | { 99 | return false; 100 | } 101 | else 102 | { 103 | return true; 104 | } 105 | } 106 | } 107 | 108 | static inline bool Sphere_Box(const GVector3& c, f32 r, const GVector3& p, const GVector3& h) 109 | { 110 | GVector3 v = (p - c).Abs(); 111 | GVector3 u = GVector3::Max(v - h, GVector3::Zero()); 112 | 113 | return GVector3::DotProduct(u, u) <= r * r; 114 | } 115 | 116 | static bool Sphere_Box(const GVector3& center, const f32 radius, const GVector3& boxCenter, const GVector3& boxExtents, GVector3& finalCenter, GVector3* pOutNormal); 117 | static bool Sphere_Capsule( const GVector3& sphereCenter, const f32 sphereRadius, const GVector3& capsuleP0, const GVector3& capsuleP1, const f32 Radius0, const f32 Radius1, GVector3& result, GVector3* pOutNormal); 118 | 119 | 120 | 121 | 122 | static int32_t Sphere_Sphere_Contact( const GShapeSphere& ShapA, const GTransform_QT& TransformA, const GShapeSphere& ShapB, const GTransform_QT& TransformB, GCollisionContact* pContact); 123 | static int32_t Sphere_Plane_Contact( const GShapeSphere& ShapA, const GTransform_QT& TransformA, const GShapePlane& ShapB, const GTransform_QT& TransformB, GCollisionContact* pContact); 124 | static int32_t Sphere_Box_Contact( const GShapeSphere& ShapA, const GTransform_QT& TransformA, const GShapeBox& ShapB, const GTransform_QT& TransformB, GCollisionContact* pContact); 125 | 126 | }; 127 | 128 | 129 | -------------------------------------------------------------------------------- /UnrealDemo/Source/UnrealDemo/gphysics/include/collision/glacier_contact.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | #pragma once 14 | 15 | #include "glacier_transform_qt.h" 16 | #include "glacier_collision_object.h" 17 | #include 18 | #include 19 | 20 | 21 | 22 | class GManifoldPoint 23 | { 24 | public: 25 | 26 | GVector3 m_PosWorld; 27 | GVector3 m_Normal; 28 | f32 m_depth; 29 | uint32_t m_FaceIndex; 30 | 31 | 32 | bool m_bCurrentSeparate; 33 | }; 34 | 35 | 36 | 37 | class GCollisionContact 38 | { 39 | public: 40 | 41 | GCollisionContact() = default; 42 | GCollisionContact(const GCollisionContact&) = default; 43 | 44 | 45 | void AddContactPoint( 46 | const GVector3& PosWorld, 47 | const GVector3& Normal, 48 | f32 depth, 49 | uint32_t faceid = -1); 50 | 51 | void Clear() 52 | { 53 | PairId = 0; 54 | m_nPointCount = 0; 55 | PointOnSurface = -1; 56 | } 57 | 58 | void ClearPoint() 59 | { 60 | m_nPointCount = 0; 61 | } 62 | 63 | int32_t GetPointCount() const 64 | { 65 | return m_nPointCount < MaxPoint ? m_nPointCount : MaxPoint; 66 | } 67 | 68 | 69 | uint64_t PairId; 70 | uint32_t PointOnSurface; 71 | 72 | static constexpr int32_t MaxPoint = 16; 73 | 74 | int32_t m_nPointCount; 75 | GManifoldPoint m_Point[MaxPoint]; 76 | }; 77 | 78 | 79 | class GRigidBody; 80 | class GBroadPhasePair 81 | { 82 | public: 83 | 84 | GBroadPhasePair() = default; 85 | GBroadPhasePair(const GBroadPhasePair&) = default; 86 | GBroadPhasePair(GCObject* p1, GCObject* p2) 87 | { 88 | if (p1->GetId() < p2->GetId()) 89 | { 90 | pObjectA = p1; 91 | pObjectB = p2; 92 | 93 | PairId = ((uint64_t)p1->GetId() << 32) | (uint64_t)p2->GetId(); 94 | } 95 | else 96 | { 97 | pObjectA = p2; 98 | pObjectB = p1; 99 | 100 | PairId = ((uint64_t)p2->GetId() << 32) | (uint64_t)p1->GetId(); 101 | } 102 | 103 | Solved = false; 104 | } 105 | 106 | void CalculateContactStaticDepth() 107 | { 108 | if (pObjectA->m_ContactStaticDepth > (pObjectB->m_ContactStaticDepth + 1)) 109 | { 110 | pObjectA->m_ContactStaticDepth = (pObjectB->m_ContactStaticDepth + 1); 111 | } 112 | 113 | if (pObjectB->m_ContactStaticDepth > (pObjectA->m_ContactStaticDepth + 1)) 114 | { 115 | pObjectB->m_ContactStaticDepth = (pObjectA->m_ContactStaticDepth + 1); 116 | } 117 | 118 | m_ContactStaticDepth = pObjectA->m_ContactStaticDepth < pObjectB->m_ContactStaticDepth ? 119 | pObjectA->m_ContactStaticDepth : pObjectB->m_ContactStaticDepth; 120 | } 121 | 122 | 123 | GVector3 GetWorldRelativeB(const GVector3& VPos) const; 124 | 125 | f32 GetContactPairEnergy_World() const; 126 | f32 GetContactPairEnergy_Relative(const GVector3& CenterVelocity) const; 127 | 128 | f32 GetContactPairMomentum_world() const; 129 | 130 | void SeparatePair(GRigidBody* pRA, GRigidBody* pRB, bool bSwap); 131 | 132 | uint64_t PairId; 133 | uint32_t m_ContactStaticDepth; 134 | GCObject* pObjectA; 135 | GCObject* pObjectB; 136 | 137 | GCollisionContact PairContact; 138 | 139 | bool Solved; 140 | 141 | }; -------------------------------------------------------------------------------- /UnrealDemo/Source/UnrealDemo/gphysics/include/collision/glacier_convexhull.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | #pragma once 14 | 15 | #include "glacier_vector.h" 16 | #include "glacier_plane.h" 17 | #include "glacier_transform_qt.h" 18 | #include "glacier_collision_shape.h" 19 | #include "glacier_debug_draw.h" 20 | 21 | 22 | #include 23 | #include 24 | #include 25 | 26 | 27 | struct GConvexPolygon 28 | { 29 | GPlane m_Plane; 30 | GVector3 m_CenterPos; 31 | uint16_t m_NbVerts; 32 | uint16_t m_IndexBase; 33 | 34 | inline uint16_t GetNext(uint16_t nCurrentindex) const 35 | { 36 | return m_IndexBase + (nCurrentindex < (m_NbVerts - 1) ? nCurrentindex + 1 : 0); 37 | } 38 | }; 39 | 40 | struct GConvexEdge 41 | { 42 | uint16_t PolygenId_A; 43 | uint16_t PolygenId_B; 44 | 45 | uint16_t PolygenA_Point_Index; 46 | uint16_t PolygenB_Point_Index; 47 | }; 48 | 49 | 50 | 51 | class GConvexHull : public GShapeConvexBase 52 | { 53 | public: 54 | GConvexHull( ) 55 | { 56 | ShapType = EShape::EShape_ConvexHull; 57 | } 58 | virtual ~GConvexHull() 59 | { 60 | 61 | } 62 | 63 | void Clear() 64 | { 65 | m_VPoints.clear(); 66 | m_Indexes.clear(); 67 | m_Polygons.clear(); 68 | m_Edges.clear(); 69 | } 70 | 71 | virtual GVector3 GetSupportLocalPos(const GVector3& Dir) const 72 | { 73 | return Dir;// * Radius; 74 | } 75 | 76 | void BuildEdges(); 77 | 78 | void Draw( class IGlacierDraw* pDraw, const GTransform_QT& Trans, GColor TColor ) const; 79 | 80 | protected: 81 | 82 | std::vector m_VPoints; 83 | std::vector m_Indexes; 84 | std::vector m_Polygons; 85 | std::vector m_Edges; 86 | 87 | 88 | friend class GConvexHullBuilder; 89 | }; 90 | 91 | 92 | class GSortPlane 93 | { 94 | public: 95 | 96 | GPlane m_Plane; 97 | 98 | GSortPlane() 99 | { 100 | 101 | } 102 | 103 | GSortPlane(GPlane TPlane) : m_Plane(TPlane) 104 | { 105 | 106 | } 107 | 108 | GSortPlane(const GSortPlane&) = default; 109 | 110 | inline bool operator == (const GSortPlane& b) const 111 | { 112 | return 113 | GVector3::DistanceSquare( m_Plane.m_Normal, b.m_Plane.m_Normal ) < GMath::Inv_1000000() && 114 | GMath::Abs( m_Plane.m_fDis - b.m_Plane.m_fDis ) < GMath::Inv_1000(); 115 | } 116 | 117 | inline bool operator != (const GSortPlane& b) const 118 | { 119 | return !( *this == b ); 120 | } 121 | 122 | inline bool operator < (const GSortPlane& b) const 123 | { 124 | if( *this == b ) 125 | return false; 126 | else 127 | { 128 | const GVector3& Va = m_Plane.m_Normal; 129 | const GVector3& Vb = b.m_Plane.m_Normal; 130 | 131 | if (Va.z != Vb.z) 132 | { 133 | return Va.z < Vb.z; 134 | } 135 | else if (Va.y != Vb.y) 136 | { 137 | return Va.y < Vb.y; 138 | } 139 | else if (Va.x != Vb.x) 140 | { 141 | return Va.x < Vb.x; 142 | } 143 | else 144 | { 145 | return m_Plane.m_fDis < b.m_Plane.m_fDis; 146 | } 147 | } 148 | } 149 | }; 150 | 151 | 152 | struct GBuildPolygon 153 | { 154 | GPlane m_Plane; 155 | GVector3 m_VCenter; 156 | std::set m_Points; 157 | std::vector m_ListPoints; 158 | }; 159 | 160 | class GConvexHullBuilder 161 | { 162 | public: 163 | 164 | 165 | static void AddBoxPoints(std::vector& Points, const GVector3& VMin, const GVector3& VMax ); 166 | 167 | void BuildConvex(const std::vector& Points, GConvexHull& CResult ); 168 | 169 | void BuildMinkowskiSum( 170 | const GConvexHull& CA, 171 | GTransform_QT TA, 172 | const GConvexHull& CB, 173 | GTransform_QT TB, 174 | GConvexHull& CResult, 175 | bool bAdd = true); 176 | 177 | 178 | void Draw(class IGlacierDraw* pDraw, const GTransform_QT& Trans, GColor TColor) const; 179 | 180 | protected: 181 | 182 | 183 | void AddInputPoints(const std::vector& InputPoints); 184 | 185 | void AddFace(const GSortPlane& TPlane, uint16_t i, uint16_t j, uint16_t k); 186 | 187 | 188 | 189 | void SimpleliseFace( GBuildPolygon& TPolygon ); 190 | 191 | 192 | public: 193 | std::vector m_VPoints; 194 | std::map m_Polygons; 195 | 196 | 197 | std::vector m_PolygonPoints; 198 | 199 | }; -------------------------------------------------------------------------------- /UnrealDemo/Source/UnrealDemo/gphysics/include/collision/glacier_distance.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | #pragma once 14 | 15 | #include "glacier_vector.h" 16 | 17 | class GDistance 18 | { 19 | public: 20 | 21 | static inline GVector3 closestPtPointSphere(const GVector3& p, const GVector3& a, f32 radius) 22 | { 23 | return (p - a).GetNormalize() * radius + a; 24 | } 25 | 26 | static inline GVector3 closestPtPointPlane(const GVector3& p, const GVector3& PlaneNormal, const GVector3& planePos) 27 | { 28 | return p - PlaneNormal * GVector3::DotProduct(p - planePos, PlaneNormal); 29 | } 30 | 31 | static inline GVector3 closestPtPointLine(const GVector3& p, const GVector3& Pos, const GVector3& Vdir) 32 | { 33 | return Pos + Vdir * GVector3::DotProduct(Vdir, p - Pos); 34 | } 35 | 36 | static inline void ClosestPtPointLine(const GVector3& point, const GVector3& sP0, const GVector3& sP1, f32& t, GVector3& d) 37 | { 38 | GVector3 seg = sP1 - sP0; 39 | 40 | t = GVector3::DotProduct(point - sP0, seg); 41 | 42 | f32 denom = GVector3::DotProduct(seg, seg); 43 | 44 | t = t / denom; 45 | d = sP0 + t * seg; 46 | } 47 | 48 | static inline GVector3 ClosestPtPointSegment(const GVector3& point, const GVector3& sP0, const GVector3& sP1 ) 49 | { 50 | GVector3 seg = sP1 - sP0; 51 | 52 | f32 t = GVector3::DotProduct(point - sP0, seg); 53 | if (t <= GMath::Zero()) 54 | { 55 | return sP0; 56 | } 57 | else 58 | { 59 | f32 denom = GVector3::DotProduct(seg, seg); 60 | if (t >= denom) 61 | { 62 | return sP1; 63 | } 64 | else 65 | { 66 | t = t / denom; 67 | return sP0 + t * seg; 68 | } 69 | } 70 | } 71 | 72 | static inline void ClosestPtPointSegment(const GVector3& point, const GVector3& sP0, const GVector3& sP1, f32& t, GVector3& d) 73 | { 74 | GVector3 seg = sP1 - sP0; 75 | 76 | t = GVector3::DotProduct(point - sP0, seg); 77 | if (t <= GMath::Zero()) 78 | { 79 | t = GMath::Zero(); 80 | d = sP0; 81 | } 82 | else 83 | { 84 | f32 denom = GVector3::DotProduct(seg, seg); 85 | if (t >= denom) 86 | { 87 | t = GMath::One(); 88 | d = sP1; 89 | } 90 | else 91 | { 92 | t = t / denom; 93 | d = sP0 + t * seg; 94 | } 95 | } 96 | } 97 | 98 | static inline GVector3 closestPtPointAABB(const GVector3& p, const GVector3& VHalfSize) 99 | { 100 | return GVector3( 101 | GMath::Clamp(p.x, -VHalfSize.x, VHalfSize.x), 102 | GMath::Clamp(p.y, -VHalfSize.y, VHalfSize.y), 103 | GMath::Clamp(p.z, -VHalfSize.z, VHalfSize.z)); 104 | } 105 | 106 | static GVector3 ClosestPointTriangle(const GVector3& point, const GVector3& a, const GVector3& b, const GVector3& c); 107 | 108 | static inline int pointOutsideOfPlane(const GVector3& p, const GVector3& a, const GVector3& b, const GVector3& c, const GVector3& d) 109 | { 110 | GVector3 normal = GVector3::CrossProduct(b - a, c - a); 111 | 112 | f32 signp = GVector3::DotProduct(p - a, normal); // [AP AB AC] 113 | f32 signd = GVector3::DotProduct(d - a, normal); // [AD AB AC] 114 | 115 | if (signd * signd < GMath::Epsilon()) 116 | { 117 | return -1; 118 | } 119 | // Points on opposite sides if expression signs are opposite 120 | return signp * signd < GMath::Zero() ? 1 : 0; 121 | } 122 | 123 | 124 | static inline GVector3 ClosestPtPointTetrahedron(const GVector3& p, const GVector3& a, const GVector3& b, const GVector3& c, const GVector3& d) 125 | { 126 | int pointOutsideABC = pointOutsideOfPlane(p, a, b, c, d); 127 | int pointOutsideACD = pointOutsideOfPlane(p, a, c, d, b); 128 | int pointOutsideADB = pointOutsideOfPlane(p, a, d, b, c); 129 | int pointOutsideBDC = pointOutsideOfPlane(p, b, d, c, a); 130 | 131 | if (pointOutsideABC < 0 || pointOutsideACD < 0 || pointOutsideADB < 0 || pointOutsideBDC < 0) 132 | { 133 | return p; 134 | } 135 | 136 | if (pointOutsideABC == 0 && pointOutsideACD == 0 && pointOutsideADB == 0 && pointOutsideBDC == 0) 137 | { 138 | return p; 139 | } 140 | 141 | // If point outside face abc then compute closest point on abc 142 | if (pointOutsideABC != 0) 143 | { 144 | return ClosestPointTriangle(p, a, b, c); 145 | } 146 | 147 | 148 | // Repeat test for face acd 149 | if (pointOutsideACD != 0) 150 | { 151 | return ClosestPointTriangle(p, a, c, d); 152 | 153 | } 154 | // Repeat test for face adb 155 | 156 | 157 | if (pointOutsideADB != 0) 158 | { 159 | return ClosestPointTriangle(p, a, d, b); 160 | } 161 | // Repeat test for face bdc 162 | 163 | 164 | if (pointOutsideBDC != 0) 165 | { 166 | return ClosestPointTriangle(p, b, d, c); 167 | } 168 | 169 | return p; 170 | } 171 | 172 | }; 173 | 174 | 175 | 176 | -------------------------------------------------------------------------------- /UnrealDemo/Source/UnrealDemo/gphysics/include/dynamic/glacier_articulate_body.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | #pragma once 14 | 15 | #include "glacier_collision_object.h" 16 | 17 | class GArticulateBody : public GCObject 18 | { 19 | public: 20 | 21 | GArticulateBody(uint32_t id, EShape TShape) : GCObject(id, TShape) 22 | { 23 | m_CollisionType = CO_Rigid_Body; 24 | m_Gravity = GVector3(GMath::Zero(), GMath::Zero(), -GMath::Makef32(9,8,10) ); 25 | m_VelocityMax = GMath::Makef32(100,0,1); 26 | m_bDynamic = false; 27 | } 28 | 29 | void Tick_PreTransform(const f32 DetalTime); 30 | 31 | 32 | public: 33 | 34 | bool m_bDynamic; 35 | GTransform_QT m_Transform_Pre; 36 | 37 | 38 | f32 m_Mass; 39 | f32 m_InvMass; 40 | GVector3 m_Velocity; 41 | f32 m_VelocityMax; 42 | 43 | GVector3 m_MoumentInertia; 44 | GVector3 m_InvMoumentInertia; 45 | GVector3 m_AngleVelocity; 46 | 47 | GVector3 m_Gravity; 48 | }; -------------------------------------------------------------------------------- /UnrealDemo/Source/UnrealDemo/gphysics/include/dynamic/glacier_soft_body.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | #pragma once 14 | 15 | #include "glacier_collision_object.h" 16 | 17 | class GSoftBody : public GCObject 18 | { 19 | public: 20 | 21 | GSoftBody(uint32_t id, EShape TShape) : GCObject(id, TShape) 22 | { 23 | m_CollisionType = CO_Rigid_Body; 24 | m_Gravity = GVector3(GMath::Zero(), GMath::Zero(), -GMath::Makef32(9,8,10) ); 25 | m_VelocityMax = GMath::Makef32(100,0,1); 26 | m_bDynamic = false; 27 | } 28 | 29 | void Tick_PreTransform( const f32 DetalTime ) 30 | { 31 | GVector3 VNew = m_Velocity + m_Gravity * DetalTime; 32 | 33 | if( VNew.SizeSquare() > (m_VelocityMax*m_VelocityMax)) 34 | { 35 | VNew = VNew.GetNormalize() * m_VelocityMax; 36 | } 37 | 38 | m_Transform.m_Pos += (m_Velocity + VNew) * GMath::Half() * DetalTime; 39 | 40 | m_Velocity = VNew; 41 | 42 | m_bNeedUpdate = true; 43 | } 44 | 45 | public: 46 | 47 | bool m_bDynamic; 48 | GTransform_QT m_Transform_Pre; 49 | 50 | 51 | f32 m_Mass; 52 | f32 m_InvMass; 53 | GVector3 m_Velocity; 54 | f32 m_VelocityMax; 55 | 56 | GVector3 m_MoumentInertia; 57 | GVector3 m_InvMoumentInertia; 58 | GVector3 m_AngleVelocity; 59 | 60 | GVector3 m_Gravity; 61 | }; -------------------------------------------------------------------------------- /UnrealDemo/Source/UnrealDemo/gphysics/include/math/glacier_plane.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | #pragma once 14 | 15 | #include "glacier_vector.h" 16 | 17 | class GPlane 18 | { 19 | public: 20 | 21 | enum GPlaneSide 22 | { 23 | NO_SIDE = 0, 24 | POSITIVE, 25 | NEGATIVE, 26 | BOTH 27 | }; 28 | 29 | public: 30 | 31 | GPlane( ) = default; 32 | GPlane( const GPlane& P ) = default; 33 | 34 | inline GPlane(const GVector3& V3Normal, f32 fDis ) 35 | { 36 | m_Normal = V3Normal; 37 | m_fDis = fDis; 38 | } 39 | 40 | inline GPlane( const GVector3& V3Normal, const GVector3& V3Point ) 41 | { 42 | Redefine( V3Normal, V3Point ); 43 | } 44 | 45 | inline GPlane( const GVector3& V0, const GVector3& V1, const GVector3& V2 ) 46 | { 47 | Redefine( V0, V1, V2 ); 48 | } 49 | 50 | public: 51 | 52 | bool operator == ( const GPlane& P ) const 53 | { 54 | return ( P.m_fDis == m_fDis && P.m_Normal == m_Normal ); 55 | } 56 | 57 | bool operator != ( const GPlane& P ) const 58 | { 59 | return ( P.m_fDis != m_fDis || P.m_Normal != m_Normal ); 60 | } 61 | 62 | GPlane operator + (const GPlane& P) const 63 | { 64 | return GPlane( m_Normal + P.m_Normal, m_fDis + P.m_fDis); 65 | } 66 | 67 | GPlane operator - (const GPlane& P) const 68 | { 69 | return GPlane(m_Normal - P.m_Normal, m_fDis - P.m_fDis); 70 | } 71 | 72 | void Normalize() 73 | { 74 | f32 fLength = GVector3::DotProduct(m_Normal, m_Normal) + m_fDis * m_fDis; 75 | if (fLength < GMath::Epsilon()) 76 | { 77 | GPlane( GVector3::UnitX(), GMath::Zero()); 78 | } 79 | else 80 | { 81 | f32 fInvLength = GMath::InvSqrt(fLength); 82 | m_Normal *= fInvLength; 83 | m_fDis *= fInvLength; 84 | } 85 | } 86 | 87 | public: 88 | 89 | inline GPlane Flip( void ) const 90 | { 91 | return GPlane( -m_Normal, -m_fDis ); 92 | } 93 | 94 | inline f32 GetDistance(const GVector3& V3Point) const 95 | { 96 | return GVector3::DotProduct(m_Normal, V3Point) + m_fDis; 97 | } 98 | 99 | inline GPlaneSide GetSide( const GVector3& V3Point, f32 Radius = GMath::Epsilon() ) const 100 | { 101 | f32 fDistance = GetDistance( V3Point ); 102 | 103 | if( fDistance < -Radius ) 104 | { 105 | return GPlane::NEGATIVE; 106 | } 107 | else if( fDistance > Radius ) 108 | { 109 | return GPlane::POSITIVE; 110 | } 111 | else 112 | { 113 | return GPlane::NO_SIDE; 114 | } 115 | } 116 | 117 | inline GPlaneSide GetSide( const GVector3& VCenter, const GVector3& VHalfSize ) const 118 | { 119 | f32 fDist = GetDistance( VCenter ); 120 | 121 | f32 fMaxAbsDist = m_Normal.AbsDotProduct( VHalfSize ); 122 | 123 | if( fDist < -fMaxAbsDist ) 124 | { 125 | return GPlane::NEGATIVE; 126 | } 127 | 128 | if( fDist > fMaxAbsDist ) 129 | { 130 | return GPlane::POSITIVE; 131 | } 132 | 133 | return GPlane::BOTH; 134 | } 135 | 136 | inline void Redefine( const GVector3& V0, const GVector3& V1, const GVector3& V2 ) 137 | { 138 | GVector3 VEdge1 = V1 - V0; 139 | GVector3 VEdge2 = V2 - V0; 140 | m_Normal = GVector3::CrossProduct( VEdge1, VEdge2 ).GetNormalize(); 141 | m_fDis =-GVector3::DotProduct(m_Normal, V0 ); 142 | } 143 | 144 | inline void Redefine( const GVector3& VNormal, const GVector3& V3Point ) 145 | { 146 | m_Normal = VNormal; 147 | m_fDis = -GVector3::DotProduct(m_Normal, V3Point ); 148 | } 149 | 150 | inline void Normalise( void ) 151 | { 152 | f32 fLength = m_Normal.SizeSquare( ); 153 | 154 | if( fLength > GMath::Epsilon() ) 155 | { 156 | f32 fInvLength = GMath::InvSqrt( fLength); 157 | m_Normal = m_Normal * fInvLength; 158 | m_fDis = m_fDis * fInvLength; 159 | } 160 | else 161 | { 162 | m_Normal = GVector3::Zero(); 163 | m_fDis = GMath::Zero(); 164 | } 165 | } 166 | 167 | inline GPlane GetNormalise( ) const 168 | { 169 | GPlane TP = *this; 170 | TP.Normalise( ); 171 | return TP; 172 | } 173 | 174 | inline GVector3 ProjectVector(const GVector3& V) const 175 | { 176 | return V - m_Normal * GVector3::DotProduct(m_Normal, V); 177 | } 178 | 179 | inline GVector3 ReflectPoint( const GVector3& V ) const 180 | { 181 | return V - m_Normal * ( GMath::Two() * GetDistance( V ) ); 182 | } 183 | 184 | inline GVector3 ReflectVector( const GVector3& V ) const 185 | { 186 | return V - m_Normal * ( GMath::Two() * GVector3::DotProduct(m_Normal, V) ); 187 | } 188 | 189 | public: 190 | 191 | GVector3 m_Normal; 192 | f32 m_fDis; 193 | }; 194 | 195 | -------------------------------------------------------------------------------- /UnrealDemo/Source/UnrealDemo/gphysics/include/math/glacier_transform_qt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | #pragma once 14 | 15 | #include "glacier_vector.h" 16 | #include "glacier_quaternion.h" 17 | #include "glacier_aabb.h" 18 | 19 | class GTransform_QT 20 | { 21 | public: 22 | 23 | GTransform_QT() = default; 24 | 25 | GTransform_QT(const GTransform_QT&) = default; 26 | 27 | inline GTransform_QT( const GQuaternion& Quat, const GVector3& Trans ) : 28 | m_Rot(Quat ), m_Pos(Trans) 29 | { 30 | 31 | } 32 | 33 | inline GTransform_QT( const GQuaternion& Quat ) : 34 | m_Rot(Quat ), m_Pos( GVector3::Identity() ) 35 | { 36 | 37 | } 38 | 39 | inline GTransform_QT( const GVector3& Trans ) : 40 | m_Rot( GQuaternion::Identity() ), m_Pos(Trans) 41 | { 42 | 43 | } 44 | 45 | inline GTransform_QT operator*( const GTransform_QT& Q ) const 46 | { 47 | return GTransform_QT( m_Rot * Q.m_Rot, Q.m_Rot.RotateVector(m_Pos) + Q.m_Pos ); 48 | } 49 | 50 | inline GTransform_QT operator*( const GQuaternion& Q ) const 51 | { 52 | return GTransform_QT( m_Rot * Q, Q.RotateVector(m_Pos) ); 53 | } 54 | 55 | friend inline GTransform_QT operator*( const GQuaternion& Q, const GTransform_QT& QT ) 56 | { 57 | return GTransform_QT( Q * QT.m_Rot, QT.m_Pos ); 58 | } 59 | 60 | GVector3 TransformNormal( const GVector3& V ) const 61 | { 62 | return m_Rot.RotateVector( V ); 63 | } 64 | 65 | GVector3 TransformPosition(const GVector3& V) const 66 | { 67 | return m_Rot.RotateVector(V) + m_Pos; 68 | } 69 | 70 | GAABB TransformAABB( const GAABB& InBox ) const 71 | { 72 | GAABB TBox( TransformPosition( InBox.GetCorner(0)) ); 73 | for (uint32_t uLoop = 1; uLoop < 8; ++uLoop) 74 | { 75 | TBox.Merge( TransformPosition( InBox.GetCorner(uLoop))); 76 | } 77 | return TBox; 78 | } 79 | 80 | public: 81 | 82 | inline void Inverse( ) 83 | { 84 | m_Rot.Inverse( ); 85 | m_Pos = m_Rot.RotateVector( -m_Pos); 86 | } 87 | 88 | inline GTransform_QT GetInverse( ) const 89 | { 90 | GQuaternion TQ = m_Rot.GetInverse( ); 91 | return GTransform_QT( TQ, TQ.RotateVector( -m_Pos) ); 92 | } 93 | 94 | inline GTransform_QT GetInverse_fast() const 95 | { 96 | GQuaternion TQ = m_Rot.GetUnitInverse(); 97 | return GTransform_QT(TQ, TQ.RotateVector(-m_Pos)); 98 | } 99 | 100 | bool IsIdentity( ) const 101 | { 102 | return m_Rot == GQuaternion::Identity( ) && m_Pos == GVector3::Zero( ); 103 | } 104 | 105 | static inline GTransform_QT Identity( ) 106 | { 107 | return GTransform_QT( GQuaternion::Identity(), GVector3::Zero( ) ); 108 | } 109 | 110 | public: 111 | 112 | GQuaternion m_Rot; 113 | GVector3 m_Pos; 114 | }; 115 | 116 | -------------------------------------------------------------------------------- /UnrealDemo/Source/UnrealDemo/gphysics/include/utils/glacier_color.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | #pragma once 14 | 15 | #include 16 | 17 | // ARGB color 18 | class GColor 19 | { 20 | public: 21 | 22 | GColor( const GColor& ) = default; 23 | 24 | GColor( uint32_t value ): RawValue(value) 25 | { 26 | } 27 | 28 | static GColor White() { return GColor(0xFFFFFFFF); } 29 | static GColor Black() { return GColor(0xFF000000); } 30 | static GColor Red() { return GColor(0xFFFF0000); } 31 | static GColor Green() { return GColor(0xFF00FF00); } 32 | static GColor Blue() { return GColor(0xFF0000FF); } 33 | static GColor Yellow() { return GColor(0xFFFFFF00); } 34 | static GColor Gray() { return GColor(0xFF808080); } 35 | 36 | 37 | uint32_t RawValue; 38 | }; -------------------------------------------------------------------------------- /UnrealDemo/Source/UnrealDemo/gphysics/include/utils/glacier_debug_draw.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | #pragma once 14 | 15 | #include "glacier_color.h" 16 | #include "glacier_math.h" 17 | class GVector3; 18 | class GTransform_QT; 19 | class GPlane; 20 | class IGlacierDraw 21 | { 22 | public: 23 | 24 | virtual void DrawLine(const GVector3& V0, const GVector3& V1, GColor uColor) = 0; 25 | virtual void DrawBox(const GTransform_QT& TTrans, const GVector3& LocalCenter, const GVector3& HalfSize, GColor TColor); 26 | virtual void DrawSphere(const GTransform_QT& TTrans, f32 Radius, GColor TColor, int32_t nSeg); 27 | virtual void DrawCapsule(const GTransform_QT& TTrans, f32 Radius, f32 HalfHeight, GColor TColor, int nSeg = 18); 28 | virtual void DrawCylinder(const GTransform_QT& TTrans, f32 Radius, f32 HalfHeight, GColor TColor, int nSeg = 18); 29 | virtual void DrawPlane(const GTransform_QT& TTrans, const GPlane& TPlane, f32 Size, GColor TColor); 30 | virtual void DrawPlane(const GTransform_QT& TTrans, f32 Size, GColor TColor); 31 | 32 | 33 | virtual void DrawArrow(const GVector3& V0, const GVector3& VDirection, f32 Size, GColor TColor); 34 | virtual void DrawArrow(const GVector3& V0, const GVector3& VDirection, GColor TColor); 35 | 36 | 37 | 38 | 39 | }; 40 | -------------------------------------------------------------------------------- /UnrealDemo/Source/UnrealDemo/gphysics/include/utils/glacier_physics_utils.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | #pragma once 14 | 15 | #include "glacier_vector.h" 16 | #include "glacier_transform_qt.h" 17 | #include "glacier_color.h" 18 | #include "glacier_matrix.h" 19 | #include "glacier_math.h" 20 | #include "glacier_collision_shape.h" 21 | 22 | class GCollisionShape; 23 | class IGlacierDraw; 24 | class GCollisionContact; 25 | class GPhysicsWorld; 26 | class GPhyscsUtils 27 | { 28 | public: 29 | 30 | static void DrawShape( const GTransform_QT& Trans, const GCollisionShape& pShape, IGlacierDraw* pDebugDraw, GColor TColor ); 31 | 32 | static void DrawSphere(const GTransform_QT& Trans, const GShapeSphere& pShape, IGlacierDraw* pDebugDraw, GColor TColor); 33 | 34 | static void DrawBox(const GTransform_QT& Trans, const GShapeBox& pShape, IGlacierDraw* pDebugDraw, GColor TColor); 35 | 36 | static void DrawCapsule(const GTransform_QT& Trans, const GShapeCapsule& pShape, IGlacierDraw* pDebugDraw, GColor TColor); 37 | 38 | static void DrawCoordinateSystem( IGlacierDraw* pDebugDraw, const GTransform_QT& Trans, f32 fSize ); 39 | 40 | static void DrawContact( const GCollisionContact& TContact, IGlacierDraw* pDebugDraw, GColor TColor, const class GPhysicsWorld* = nullptr ); 41 | 42 | static GMatrix3 CalculateInertiaTensor( const GCollisionShape& pShape ); 43 | 44 | static f32 CalculateVolume(const GCollisionShape& pShape); 45 | 46 | static inline GVector3 Project_Vertical(const GVector3& Normal, const GVector3& V) 47 | { 48 | return V - GVector3::DotProduct(Normal, V) * Normal; 49 | } 50 | 51 | static inline GVector3 Project_Parallel(const GVector3& Normal, const GVector3& V) 52 | { 53 | return GVector3::DotProduct(Normal, V) * Normal; 54 | } 55 | 56 | 57 | }; -------------------------------------------------------------------------------- /UnrealDemo/Source/UnrealDemo/gphysics/include/utils/glacier_time.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | #pragma once 14 | 15 | #include "glacier_vector.h" 16 | #include 17 | #include 18 | 19 | 20 | #if defined(__GNUC__) && defined(__x86_64__) && !defined(__aarch64__) 21 | #include 22 | #endif 23 | 24 | #if defined(_MSC_VER) || (defined(__GNUC__)) 25 | #define UseProfiler_RDTSCP 1 26 | #endif 27 | 28 | 29 | #define GPRORILER_FUN GTimeProfiler GProfiler(__FUNCTION__); 30 | 31 | typedef std::chrono::high_resolution_clock Myclock; 32 | typedef std::chrono::nanoseconds Myres; 33 | 34 | class GTimer 35 | { 36 | public: 37 | GTimer() 38 | #if UseProfiler_RDTSCP 39 | : start_(0), end_(0) 40 | #else 41 | : t1(Myres::zero()), t2(Myres::zero()) 42 | #endif 43 | { 44 | Start(); 45 | } 46 | 47 | ~GTimer() 48 | {} 49 | 50 | static inline uint64_t get_CPUCycles() 51 | { 52 | #ifdef _MSC_VER 53 | return __rdtsc(); 54 | #elif __GNUC__ 55 | 56 | #if defined(__x86_64__) 57 | unsigned int lo, hi; 58 | __asm__ __volatile__("rdtsc" : "=a" (lo), "=d" (hi)); 59 | return ((uint64_t)hi << 32) | lo; 60 | #elif defined(__aarch64__) 61 | 62 | uint64_t virtual_timer_value; 63 | asm volatile("mrs %0, cntvct_el0" : "=r"(virtual_timer_value)); 64 | return virtual_timer_value; 65 | 66 | #else 67 | return 0; 68 | #endif 69 | 70 | #else 71 | return 0; 72 | #endif 73 | } 74 | 75 | void Start() 76 | { 77 | #if UseProfiler_RDTSCP 78 | start_ = get_CPUCycles(); 79 | #else 80 | t1 = Myclock::now(); 81 | #endif 82 | } 83 | 84 | void End() 85 | { 86 | #if UseProfiler_RDTSCP 87 | end_ = get_CPUCycles(); 88 | #else 89 | t2 = Myclock::now(); 90 | #endif 91 | } 92 | 93 | 94 | float GetDeltaTimeMS_NoEnd() 95 | { 96 | #if UseProfiler_RDTSCP 97 | return float(double(end_ - start_) * InvCPUGHZ); 98 | #else 99 | return float(std::chrono::duration_cast(t2 - t1).count() * 1e-6); 100 | #endif 101 | } 102 | 103 | float GetDeltaTimeMS() 104 | { 105 | End(); 106 | return GetDeltaTimeMS_NoEnd(); 107 | } 108 | 109 | static double GetCpuFrequency_Compute() 110 | { 111 | #if UseProfiler_RDTSCP 112 | return 1 / InvCPUGHZ; 113 | #else 114 | return 0; 115 | #endif 116 | } 117 | 118 | static int GetCpuFrequency_CpuInfo() 119 | { 120 | int cpuInfo[4] = { 0, 0, 0, 0 }; 121 | #ifdef _MSC_VER 122 | __cpuid(cpuInfo, 0); 123 | if (cpuInfo[0] >= 0x16) { 124 | __cpuid(cpuInfo, 0x16); 125 | return cpuInfo[0]; 126 | } 127 | #elif __GNUC__ 128 | 129 | #if defined(__x86_64__) && !defined(__aarch64__) 130 | 131 | __cpuid(0, cpuInfo[0], cpuInfo[1], cpuInfo[2], cpuInfo[3]); 132 | 133 | if (cpuInfo[0] >= 0x16) { 134 | __cpuid(0x16, cpuInfo[0], cpuInfo[1], cpuInfo[2], cpuInfo[3]); 135 | return cpuInfo[0]; 136 | } 137 | #elif defined(__ARM_ARCH) 138 | 139 | uint64_t freq; 140 | asm volatile("mrs %0, cntfrq_el0" : "=r" (freq)); 141 | return (int)(freq / 1000000); 142 | 143 | #endif 144 | 145 | #else 146 | 147 | #endif 148 | 149 | return 0; 150 | } 151 | 152 | private: 153 | 154 | #if UseProfiler_RDTSCP 155 | 156 | static double InvCPUGHZ; 157 | volatile uint64_t start_; 158 | volatile uint64_t end_; 159 | #else 160 | Myclock::time_point t1; 161 | Myclock::time_point t2; 162 | #endif 163 | 164 | }; 165 | 166 | class GTimeProfiler 167 | { 168 | public: 169 | GTimeProfiler(const char* Str); 170 | ~GTimeProfiler(); 171 | 172 | void EndCuptrue(); 173 | static void DebugOut(); 174 | static void ClearTime(); 175 | 176 | std::string m_Str; 177 | bool m_bCapture; 178 | GTimer m_Time; 179 | bool m_bOutToScreen; 180 | }; -------------------------------------------------------------------------------- /UnrealDemo/Source/UnrealDemo/gphysics/include/world/glacier_bvt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | #pragma once 14 | 15 | #include "glacier_vector.h" 16 | #include "glacier_aabb.h" 17 | #include "glacier_transform_qt.h" 18 | #include "glacier_collision_shape.h" 19 | #include 20 | #include 21 | 22 | 23 | struct GBVNode 24 | { 25 | GAABB Box; 26 | GBVNode* Parent; 27 | GBVNode* Childs[2]; 28 | }; 29 | 30 | 31 | class GBVT 32 | { 33 | public: 34 | 35 | //void Update 36 | 37 | 38 | 39 | 40 | GBVNode* TreeRoot; 41 | }; -------------------------------------------------------------------------------- /UnrealDemo/Source/UnrealDemo/gphysics/include/world/glacier_collision_object.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | #pragma once 14 | 15 | #include "glacier_vector.h" 16 | #include "glacier_aabb.h" 17 | #include "glacier_transform_qt.h" 18 | #include "glacier_collision_shape.h" 19 | #include 20 | #include 21 | 22 | enum ECollisionObjectType 23 | { 24 | CO_Base = 0, 25 | CO_Rigid_Body, 26 | CO_Soft_Body, 27 | 28 | }; 29 | 30 | class GGridCell; 31 | class GCObject // Glacier Physics Collision Object 32 | { 33 | public: 34 | 35 | GCObject( uint32_t uId, EShape TShape ): 36 | m_Shape(TShape), 37 | m_Id(uId), 38 | m_LoaclAABB(GVector3::Zero()), 39 | m_WorldAABB(GVector3::Zero()) 40 | { 41 | m_CollisionType = CO_Base; 42 | m_Transform = GTransform_QT::Identity(); 43 | m_Transform_Last = GTransform_QT::Identity(); 44 | m_UserId = -1; 45 | m_pGridCell = nullptr; 46 | m_bNeedUpdate = true; 47 | m_ContactStaticDepth = 0; 48 | } 49 | 50 | virtual ~GCObject() 51 | { 52 | 53 | } 54 | 55 | virtual ECollisionObjectType GetCOType() const 56 | { 57 | return m_CollisionType; 58 | } 59 | 60 | inline uint32_t GetId()const {return m_Id;} 61 | 62 | virtual bool IsDynamic() const 63 | { 64 | return false; 65 | } 66 | 67 | 68 | void NeedUpdate() 69 | { 70 | m_bNeedUpdate = true; 71 | } 72 | 73 | void UpdateAABB() 74 | { 75 | m_WorldAABB = m_Transform.TransformAABB( m_LoaclAABB ); 76 | } 77 | 78 | const GAABB& GetWorldAABB() const 79 | { 80 | return m_WorldAABB; 81 | } 82 | 83 | GAABB GetWorldAABB( const f32& Extern ) const 84 | { 85 | GAABB TBox(m_WorldAABB.m_VMin - GVector3(Extern), m_WorldAABB.m_VMax + GVector3(Extern)); 86 | 87 | return TBox; 88 | } 89 | 90 | void UpdateLocalBox() 91 | { 92 | m_LoaclAABB = m_Shape.GetLocalBox(); 93 | } 94 | 95 | const GAABB& GetLocalAABB() const 96 | { 97 | return m_LoaclAABB; 98 | } 99 | 100 | void ClearContactPair( ) 101 | { 102 | m_ContactObjs.clear(); 103 | m_ContactArray.clear(); 104 | } 105 | 106 | void AddContactPair( uint64_t uPairId, int32_t id ) 107 | { 108 | uint32_t IdA = (uint32_t)(uPairId >> 32 ); 109 | uint32_t IdB = (uint32_t)(uPairId & 0xFFFFFFFF); 110 | 111 | if( IdA != m_Id ) 112 | { 113 | m_ContactObjs.insert( IdA ); 114 | } 115 | 116 | if (IdB != m_Id) 117 | { 118 | m_ContactObjs.insert(IdB); 119 | } 120 | 121 | m_ContactArray.push_back(id); 122 | } 123 | 124 | 125 | public: 126 | GCollisionShape m_Shape; 127 | uint32_t m_Id; 128 | ECollisionObjectType m_CollisionType; 129 | 130 | GTransform_QT m_Transform_Last; 131 | GTransform_QT m_Transform; 132 | 133 | 134 | 135 | GAABB m_LoaclAABB; 136 | GAABB m_WorldAABB; 137 | 138 | uint32_t m_UserId; 139 | 140 | GGridCell* m_pGridCell; 141 | bool m_bNeedUpdate; 142 | 143 | 144 | uint32_t m_ContactStaticDepth; 145 | 146 | std::set m_ContactObjs; 147 | std::vector m_ContactArray; 148 | 149 | }; 150 | -------------------------------------------------------------------------------- /UnrealDemo/Source/UnrealDemo/gphysics/include/world/glacier_grid.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | #pragma once 14 | 15 | #include "glacier_vector.h" 16 | #include "glacier_aabb.h" 17 | #include 18 | 19 | struct GGridPosition 20 | { 21 | //static const LGridPosition3D zero; 22 | int32_t x; 23 | int32_t y; 24 | int32_t z; 25 | 26 | GGridPosition(): x(0), y(0), z(0){} 27 | 28 | GGridPosition(int32_t _x, int32_t _y, int32_t _z) : 29 | x(_x), y(_y), z(_z) 30 | { 31 | } 32 | 33 | inline GGridPosition operator + (const GGridPosition& b) const 34 | { 35 | return GGridPosition(x + b.x, y + b.y, z + b.z); 36 | } 37 | 38 | inline GGridPosition& operator += (const GGridPosition& b) 39 | { 40 | x += b.x; 41 | y += b.y; 42 | z += b.z; 43 | return *this; 44 | } 45 | 46 | inline bool operator == (const GGridPosition& b) const 47 | { 48 | return x == b.x && y == b.y && z == b.z; 49 | } 50 | 51 | inline bool operator != (const GGridPosition& b) const 52 | { 53 | return x != b.x || y != b.y || z != b.z; 54 | } 55 | 56 | inline bool operator < (const GGridPosition& b) const 57 | { 58 | if (z != b.z) 59 | { 60 | return z < b.z; 61 | } 62 | else 63 | { 64 | if (y != b.y) 65 | { 66 | return y < b.y; 67 | } 68 | else 69 | { 70 | return x < b.x; 71 | } 72 | } 73 | } 74 | }; 75 | 76 | class GCObject; 77 | 78 | class GOCtree 79 | { 80 | public: 81 | GOCtree(const GVector3& VMin, const GVector3& VMax, int32_t nDepth) 82 | { 83 | m_Box.SetBox(VMin, VMax); 84 | m_Depth = nDepth; 85 | 86 | pChild[0] = nullptr; 87 | pChild[1] = nullptr; 88 | pChild[2] = nullptr; 89 | pChild[3] = nullptr; 90 | pChild[4] = nullptr; 91 | pChild[5] = nullptr; 92 | pChild[6] = nullptr; 93 | pChild[7] = nullptr; 94 | } 95 | 96 | void AddObject(GCObject* TObject, int32_t nDepth) 97 | { 98 | //nDepth 99 | 100 | } 101 | 102 | 103 | public: 104 | GAABB m_Box; 105 | int32_t m_Depth; 106 | 107 | GOCtree* pChild[8]; 108 | }; 109 | 110 | class GGridCell 111 | { 112 | public: 113 | GGridPosition m_pos; 114 | std::vector m_Objects; 115 | GAABB m_AABB; 116 | GAABB m_CeilAABB; 117 | 118 | public: 119 | GGridCell(const GGridPosition& TPos, f32 CeilWide, f32 CeilHeight) : m_pos(TPos) 120 | { 121 | GVector3 VMin = GVector3( 122 | f32(TPos.x) * CeilWide, 123 | f32(TPos.y) * CeilWide, 124 | f32(TPos.z) * CeilHeight); 125 | 126 | GVector3 VMax = GVector3( 127 | f32(TPos.x + 1) * CeilWide, 128 | f32(TPos.y + 1) * CeilWide, 129 | f32(TPos.z + 1) * CeilHeight); 130 | 131 | m_CeilAABB = GAABB(VMin, VMax); 132 | } 133 | 134 | ~GGridCell() {} 135 | 136 | public: 137 | inline bool IsAdjacency(const GGridCell& other) 138 | { 139 | return 140 | (abs(m_pos.x - other.m_pos.x) <= 1) && 141 | (abs(m_pos.y - other.m_pos.y) <= 1) && 142 | (abs(m_pos.z - other.m_pos.z) <= 1); 143 | } 144 | 145 | inline GVector3 GetCenter() const 146 | { 147 | return m_AABB.GetCenter(); 148 | } 149 | 150 | inline GVector3 GetHalfSize() const 151 | { 152 | return m_AABB.GetHalfSize(); 153 | } 154 | 155 | void UpdateAABB(); 156 | 157 | bool AddCollisionObject(GCObject* pObject); 158 | 159 | bool RemoveObject(GCObject* pObject); 160 | 161 | void DebugDraw(class IGlacierDraw* pDraw, uint32_t mask) const; 162 | 163 | private: 164 | 165 | }; -------------------------------------------------------------------------------- /UnrealDemo/Source/UnrealDemo/gphysics/include/world/glacier_physics_world.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | #pragma once 14 | 15 | #include "glacier_transform_qt.h" 16 | #include "glacier_collision_object.h" 17 | #include "glacier_contact.h" 18 | #include "glacier_collision_algorithm.h" 19 | #include "glacier_grid.h" 20 | #include 21 | #include 22 | 23 | 24 | 25 | enum GPDraw 26 | { 27 | GPDraw_Shape = 1 << 0, 28 | GPDraw_LocalBox = 1 << 1, 29 | GPDraw_WorldBox = 1 << 2, 30 | GPDraw_CeilBox = 1 << 3, 31 | GPDraw_Contact = 1 << 4, 32 | GPDraw_Momentum = 1 << 5, 33 | }; 34 | 35 | class GPhysicsWorld 36 | { 37 | public: 38 | 39 | void Init( int32_t nCellWide = 20, int32_t nCellHeight = 20 ) 40 | { 41 | m_nCellWide = f32(nCellWide); 42 | m_nCellHeight = f32(nCellHeight); 43 | m_CObjectId = 0; 44 | m_Friction = GMath::Makef32(0,1,10); 45 | m_FrictionClampVelocity = GMath::Makef32(0,1,100); 46 | m_CollisionEnergyLost = GMath::Makef32(0,10,100); 47 | m_fCollisionExtern = GMath::Makef32(0,3,10); 48 | m_CollisionManager.Init(); 49 | } 50 | 51 | void UnInit(){} 52 | 53 | inline GGridPosition GetGridPos( const GVector3& VPos ) 54 | { 55 | return GGridPosition ( 56 | GMath::FloorToInt(VPos.x / m_nCellWide), 57 | GMath::FloorToInt(VPos.y / m_nCellWide), 58 | GMath::FloorToInt(VPos.z / m_nCellHeight)); 59 | } 60 | 61 | bool AddCollisionObject( GCObject* pObject ); 62 | bool DeleteCollisionObject( GCObject* pObject ); 63 | 64 | const GCObject* FindCollisionObject( uint32_t id ) const; 65 | 66 | 67 | bool AddStaticLargeObj(GCObject* pObject ); 68 | bool DeleteStaticLargeObj( GCObject* pObject ); 69 | 70 | bool UpdateCollisionObject( GCObject* pObject ); 71 | 72 | void PreTick( ); 73 | 74 | void Tick( f32 DetltaTime ); 75 | 76 | void PostTick(); 77 | 78 | static void DebugDrawObject( IGlacierDraw* pDraw, const GCObject* pObj, uint32_t mask ); 79 | 80 | void DebugDraw(IGlacierDraw* pDraw, uint32_t mask ) const; 81 | 82 | public: 83 | 84 | static inline uint64_t GetPairId( uint32_t IdA, uint32_t IdB ) 85 | { 86 | if (IdA < IdB) 87 | { 88 | return ((uint64_t)IdA << 32) | (uint64_t)IdB; 89 | } 90 | else 91 | { 92 | return ((uint64_t)IdB << 32) | (uint64_t)IdA; 93 | } 94 | } 95 | 96 | public: 97 | 98 | f32 GetTotalEnergy(); 99 | GVector3 GetTotalLinearMomentum(); 100 | GVector3 GetTotalAngularMomentum(); 101 | 102 | 103 | public: 104 | 105 | void Simulate( f32 DetltaTime ); 106 | 107 | void CollisionBroadPhase( ); 108 | 109 | void CollisionNarrowPhase( ); 110 | 111 | void SolveContactConstraint( GBroadPhasePair& pPair ); 112 | 113 | void SolveContactConstraint( ); 114 | 115 | void UpdateSceneGrid( ); 116 | 117 | void ClearContactPair(); 118 | 119 | void AddContactPair(GCObject* p1, GCObject* p2); 120 | 121 | 122 | uint32_t GetNewCObjectId() 123 | { 124 | return m_CObjectId++; 125 | } 126 | 127 | public: 128 | 129 | f32 m_Friction; 130 | f32 m_FrictionClampVelocity; 131 | f32 m_CollisionEnergyLost; 132 | 133 | bool solvesort = false; 134 | 135 | private: 136 | 137 | uint32_t m_CObjectId; 138 | f32 m_nCellWide; 139 | f32 m_nCellHeight; 140 | f32 m_fCollisionExtern; 141 | 142 | //GGrid m_Grids; 143 | std::map m_Grids; 144 | std::map m_ObjectMap; 145 | std::vector m_Objects; 146 | std::vector m_StaticLargeObj; 147 | 148 | std::vector m_BroadPhasePairs; 149 | std::map m_BroadPhaseMap; 150 | 151 | GCollisionManerger m_CollisionManager; 152 | }; -------------------------------------------------------------------------------- /UnrealDemo/Source/UnrealDemo/gphysics/src/collision/glacier_collision_shape.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | #include "glacier_collision_shape.h" 15 | 16 | 17 | GAABB GCollisionShape::GetLocalBox() const 18 | { 19 | switch (ShapType) 20 | { 21 | case EShape_ConvexBase: 22 | { 23 | 24 | } 25 | break; 26 | case EShape_Sphere: 27 | { 28 | return GAABB( GVector3(-GetRaiuds()), GVector3(GetRaiuds()) ); 29 | } 30 | break; 31 | case EShape_Box: 32 | { 33 | return GAABB( -GetHalfExtern(), GetHalfExtern() ); 34 | } 35 | break; 36 | case EShape_Capsule: 37 | break; 38 | case EShape_Cylinder: 39 | break; 40 | case EShape_ConvexHull: 41 | break; 42 | case EShape_ConcaveBase: 43 | break; 44 | case EShape_Plane: 45 | { 46 | f32 BoxHalfSize = GMath::Makef32(50,1,10 ); 47 | GVector3 HalfExter = GVector3( BoxHalfSize, BoxHalfSize, BoxHalfSize ); 48 | GVector3 VCenter = GVector3(-BoxHalfSize, GMath::Zero(), GMath::Zero() ); 49 | return GAABB( VCenter -HalfExter, VCenter + HalfExter); 50 | } 51 | break; 52 | case EShape_HightField: 53 | break; 54 | case EShape_TriangleMesh: 55 | break; 56 | case EShape_Max: 57 | break; 58 | default: 59 | break; 60 | } 61 | 62 | return GAABB(GVector3::Zero()); 63 | } 64 | -------------------------------------------------------------------------------- /UnrealDemo/Source/UnrealDemo/gphysics/src/collision/glacier_distance.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | #include "glacier_distance.h" 15 | 16 | GVector3 GDistance::ClosestPointTriangle(const GVector3& p, const GVector3& a, const GVector3& b, const GVector3& c) 17 | { 18 | GVector3 ab = b - a; 19 | GVector3 ac = c - a; 20 | GVector3 ap = p - a; 21 | f32 d1 = GVector3::DotProduct(ab, ap); 22 | f32 d2 = GVector3::DotProduct(ac, ap); 23 | if (d1 <= GMath::Zero() && d2 <= GMath::Zero()) 24 | return a; // barycentric coordinates (1,0,0) 25 | 26 | // Check if P in vertex region outside B 27 | GVector3 bp = p - b; 28 | f32 d3 = GVector3::DotProduct(ab, bp); 29 | f32 d4 = GVector3::DotProduct(ac, bp); 30 | if (d3 >= GMath::Zero() && d4 <= d3) 31 | return b;// barycentric ccordinates (0,1,0) 32 | 33 | //Check if P in edge region of AB, if so return projecrion of P onto AB 34 | f32 vc = d1 * d4 - d3 * d2; 35 | if (vc <= GMath::Zero() && d1 >= GMath::Zero() && d3 <= GMath::Zero()) { 36 | f32 v = d1 / (d1 - d3); 37 | return a + v * ab; // barycentric coordinates (1-v,v,0) 38 | } 39 | 40 | // Check if P in vertex region outside C 41 | GVector3 cp = p - c; 42 | f32 d5 = GVector3::DotProduct(ab, cp); 43 | f32 d6 = GVector3::DotProduct(ac, cp); 44 | if (d6 >= GMath::Zero() && d5 <= d6) 45 | return c; // barycentric coordinates (0,0,1) 46 | 47 | // Check if P in edge region of AC, if so return projection of P onto AC 48 | f32 vb = d5 * d2 - d1 * d6; 49 | if (vb <= GMath::Zero() && d2 >= GMath::Zero() && d6 <= GMath::Zero()) { 50 | f32 w = d2 / (d2 - d6); 51 | return a + w * ac; // barycentric coordinates (1-w,0,w) 52 | } 53 | // Check if P in edge region of BC, if so return projection of P onto BC 54 | f32 va = d3 * d6 - d5 * d4; 55 | if (va <= GMath::Zero() && (d4 - d3) >= GMath::Zero() && (d5 - d6) >= GMath::Zero()) { 56 | f32 w = (d4 - d3) / ((d4 - d3) + (d5 - d6)); 57 | return b + w * (c - b); // barycentric coordinates (0,1-w,w) 58 | } 59 | // P inside face region. Compute Q through its barycentric coordinates (u,v,w) 60 | f32 denom = GMath::One() / (va + vb + vc); 61 | f32 v = vb * denom; 62 | f32 w = vc * denom; 63 | return a + ab * v + ac * w; // = u*a + v*b + w*c, u = va * denom = 1.0f-v-w 64 | } 65 | 66 | 67 | 68 | 69 | -------------------------------------------------------------------------------- /UnrealDemo/Source/UnrealDemo/gphysics/src/dynamic/glacier_articulate_body.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | 15 | #include "glacier_articulate_body.h" 16 | #include "glacier_physics_utils.h" 17 | 18 | void GArticulateBody::Tick_PreTransform(const f32 DetalTime) 19 | { 20 | 21 | 22 | m_bNeedUpdate = true; 23 | } 24 | 25 | -------------------------------------------------------------------------------- /UnrealDemo/Source/UnrealDemo/gphysics/src/dynamic/glacier_rigid_body.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | 15 | #include "glacier_rigid_body.h" 16 | #include "glacier_physics_utils.h" 17 | 18 | void GRigidBody::Tick_PreTransform(const f32 DetalTime) 19 | { 20 | GVector3 VNewLinearVelocity = m_LinearVelocity + m_Gravity * DetalTime; 21 | 22 | GVector3::ClampVector(VNewLinearVelocity, m_LinearVelocityMax); 23 | GVector3::ClampVector(m_AngularVelocity, m_AngularVelocityMax); 24 | 25 | m_Transform.m_Pos += (m_LinearVelocity + VNewLinearVelocity) * GMath::Half() * DetalTime; 26 | 27 | m_LinearVelocity = VNewLinearVelocity; 28 | 29 | f32 fAngle = m_AngularVelocity.Size(); 30 | 31 | f32 fDeltaAngle = fAngle * DetalTime; 32 | 33 | GQuaternion DeltaRot = GQuaternion( m_AngularVelocity.GetNormalize(), fDeltaAngle ); 34 | 35 | m_Transform.m_Rot = m_Transform.m_Rot * DeltaRot; 36 | 37 | m_Transform.m_Rot.Normalize(); 38 | 39 | GMatrix3 MRot = GMatrix3(m_Transform.m_Rot); 40 | 41 | m_InvInertiaTensorWorld = MRot.Transpose() * m_InvInertiaTensor * MRot; 42 | 43 | m_bNeedUpdate = true; 44 | } 45 | 46 | void GRigidBody::CalculateInertiaTensor() 47 | { 48 | m_Mass = m_density * GPhyscsUtils::CalculateVolume( m_Shape); 49 | m_InertiaTensor = m_Mass * GPhyscsUtils::CalculateInertiaTensor( m_Shape ); 50 | m_InvInertiaTensor = m_InertiaTensor.GetInverse(); 51 | 52 | m_InvMass = GMath::One() / m_Mass; 53 | } -------------------------------------------------------------------------------- /UnrealDemo/Source/UnrealDemo/gphysics/src/dynamic/glacier_soft_body.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | 15 | #include "glacier_soft_body.h" 16 | 17 | -------------------------------------------------------------------------------- /UnrealDemo/Source/UnrealDemo/gphysics/src/gphysics.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 6 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 7 | 8 | 9 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 10 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 11 | 12 | 13 | {ff9a83ed-50a3-4d93-b405-8c3475a65c28} 14 | 15 | 16 | {24fcd79e-6581-4500-bc2e-e03344394ee3} 17 | 18 | 19 | {efa63c85-62d1-42fe-9ddd-7acd5f08cb75} 20 | 21 | 22 | {0ab87c42-1df9-4ea4-9c0b-a6a74cd14b86} 23 | 24 | 25 | {7a92ed23-b1e4-4ed1-9db3-3831b04f7e40} 26 | 27 | 28 | {89a2cace-669d-4bee-b604-bd1ab35d5165} 29 | 30 | 31 | 32 | 33 | include\math 34 | 35 | 36 | include\math 37 | 38 | 39 | include\math 40 | 41 | 42 | include\math 43 | 44 | 45 | include\collision 46 | 47 | 48 | include\collision 49 | 50 | 51 | include\utils 52 | 53 | 54 | include\collision 55 | 56 | 57 | include\collision 58 | 59 | 60 | include\math 61 | 62 | 63 | 64 | 65 | src\math 66 | 67 | 68 | src\math 69 | 70 | 71 | src\math 72 | 73 | 74 | src\collision 75 | 76 | 77 | src\collision 78 | 79 | 80 | src\utils 81 | 82 | 83 | src\collision 84 | 85 | 86 | -------------------------------------------------------------------------------- /UnrealDemo/Source/UnrealDemo/gphysics/src/gphysics.vcxproj.user: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /UnrealDemo/Source/UnrealDemo/gphysics/src/math/glacier_quternion.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | #include "glacier_quaternion.h" 14 | 15 | void GQuaternion::Test() 16 | { 17 | 18 | } -------------------------------------------------------------------------------- /UnrealDemo/Source/UnrealDemo/gphysics/src/math/glacier_vector.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | #include "glacier_vector.h" 14 | 15 | void GVector3::Test() 16 | { 17 | 18 | } -------------------------------------------------------------------------------- /UnrealDemo/Source/UnrealDemo/gphysics/src/utils/glacier_time.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | #include "glacier_time.h" 15 | #include 16 | #include 17 | 18 | #if UseProfiler_RDTSCP 19 | 20 | static double CountCpuGhz() { 21 | 22 | Myclock::time_point tStart = Myclock::now();; 23 | uint64_t uStart = GTimer::get_CPUCycles(); 24 | 25 | std::this_thread::sleep_for(std::chrono::milliseconds(10)); 26 | 27 | uint64_t uEnd = GTimer::get_CPUCycles(); 28 | Myclock::time_point tEnd = Myclock::now(); 29 | 30 | double time = double(std::chrono::duration_cast(tEnd - tStart).count() * 1e-9); 31 | 32 | double CpuGhz = double(uEnd - uStart) / (time * 1000000000); 33 | return CpuGhz; 34 | 35 | } 36 | 37 | double GTimer::InvCPUGHZ = 0.000001f / CountCpuGhz(); 38 | #endif 39 | 40 | static std::map ms_TotalTime; 41 | 42 | GTimeProfiler::GTimeProfiler(const char* Str ) 43 | { 44 | m_Str = Str; 45 | m_Time.Start(); 46 | } 47 | GTimeProfiler::~GTimeProfiler() 48 | { 49 | 50 | } 51 | 52 | void GTimeProfiler::EndCuptrue() 53 | { 54 | char name[128]; 55 | float fTime = (float)m_Time.GetDeltaTimeMS() * 1000.f; 56 | sprintf(name, "Time:% 8.3fms %s\n", fTime, m_Str.c_str()); 57 | 58 | std::map::iterator iterm = ms_TotalTime.find(m_Str); 59 | if (iterm != ms_TotalTime.end()) 60 | { 61 | iterm->second += fTime; 62 | } 63 | else 64 | { 65 | ms_TotalTime[m_Str] = fTime; 66 | } 67 | 68 | 69 | if (m_bOutToScreen) 70 | { 71 | //g_GlobalSystem.FontToScreen( name ); 72 | } 73 | else 74 | { 75 | // OutputDebugStringA( name ); 76 | } 77 | 78 | m_bCapture = true; 79 | } 80 | 81 | void GTimeProfiler::ClearTime() 82 | { 83 | std::map::iterator iterm = ms_TotalTime.begin(); 84 | for (; iterm != ms_TotalTime.end(); ++iterm) 85 | { 86 | iterm->second = 0; 87 | } 88 | } 89 | 90 | void GTimeProfiler::DebugOut() 91 | { 92 | char name[128]; 93 | std::map::iterator iterm = ms_TotalTime.begin(); 94 | for (; iterm != ms_TotalTime.end(); ++iterm) 95 | { 96 | sprintf(name, "Time:% 8.3fms %s\n", iterm->second, iterm->first.c_str()); 97 | // OutputDebugStringA(name); 98 | } 99 | 100 | //OutputDebugStringA("=========================================\n"); 101 | } 102 | -------------------------------------------------------------------------------- /UnrealDemo/Source/UnrealDemoEditor.Target.cs: -------------------------------------------------------------------------------- 1 | // Copyright Epic Games, Inc. All Rights Reserved. 2 | 3 | using UnrealBuildTool; 4 | using System.Collections.Generic; 5 | 6 | public class UnrealDemoEditorTarget : TargetRules 7 | { 8 | public UnrealDemoEditorTarget(TargetInfo Target) : base(Target) 9 | { 10 | Type = TargetType.Editor; 11 | DefaultBuildSettings = BuildSettingsVersion.V2; 12 | ExtraModuleNames.Add("UnrealDemo"); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /UnrealDemo/UnrealDemo.natvis: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | {{Frac={((rawint32 >> 8)/4194304.f ) * ( 1 << uint64_t( (rawint32 & 255 )-127+22))}, 2^= {int32_t((rawint32 & 255 )-127)}}} 7 | {{Frac={((rawint32 >> 8)/4194304.f ) / ( 1 << uint64_t(-((rawint32 & 255 )-127+22)))}, 2^= {int32_t((rawint32 & 255 )-127)}}} 8 | rawint32 9 | 10 | 11 | 12 | 13 | {{ Frac={rawInt32 /256.f } }} 14 | rawInt32 15 | 16 | 17 | 18 | {{ Frac={rawInt32 /65536.f } }} 19 | rawInt32 20 | 21 | 22 | 23 | {{ Frac={rawInt32 /134217728.f } }} 24 | rawInt32 25 | 26 | 27 | {{ Frac={rawInt32 /268435456.f } }} 28 | rawInt32 29 | 30 | 31 | 32 | {{ Frac={rawInt32 /536870912.f } }} 33 | rawInt32 34 | 35 | 36 | {{ Frac={rawInt32 /1073741824.f } }} 37 | rawInt32 38 | 39 | 40 | 41 | {{ Frac={rawInt64 /4294967296.f } }} 42 | rawInt32 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /UnrealDemo/UnrealDemo.uproject: -------------------------------------------------------------------------------- 1 | { 2 | "FileVersion": 3, 3 | "EngineAssociation": "4.27", 4 | "Category": "", 5 | "Description": "", 6 | "Modules": [ 7 | { 8 | "Name": "UnrealDemo", 9 | "Type": "Runtime", 10 | "LoadingPhase": "Default", 11 | "AdditionalDependencies": [ 12 | "Engine" 13 | ] 14 | } 15 | ] 16 | } -------------------------------------------------------------------------------- /gphysics/include/collision/glacier_collision_algorithm.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | #pragma once 14 | 15 | #include "glacier_transform_qt.h" 16 | #include "glacier_collision_object.h" 17 | #include "glacier_contact.h" 18 | #include 19 | #include 20 | 21 | 22 | class GCollisionAlgorithm 23 | { 24 | public: 25 | 26 | GCollisionAlgorithm( bool value = false) : m_bSwap( value ), CallTimes(0) 27 | { 28 | 29 | } 30 | 31 | virtual bool ProcessCollision( const GCollisionObject* ObjA, const GCollisionObject* ObjB, GCollisionContact* pContact = nullptr ); 32 | 33 | bool m_bSwap; 34 | int32_t CallTimes; 35 | }; 36 | 37 | class GCG_Sphere_Sphere : public GCollisionAlgorithm 38 | { 39 | public: 40 | virtual bool ProcessCollision(const GCollisionObject* ObjA, const GCollisionObject* ObjB, GCollisionContact* pContact = nullptr ) override; 41 | }; 42 | 43 | class GCG_Sphere_Box : public GCollisionAlgorithm 44 | { 45 | public: 46 | virtual bool ProcessCollision(const GCollisionObject* ObjA, const GCollisionObject* ObjB, GCollisionContact* pContact = nullptr) override; 47 | }; 48 | 49 | class GCG_Sphere_Capusle : public GCollisionAlgorithm 50 | { 51 | public: 52 | virtual bool ProcessCollision(const GCollisionObject* ObjA, const GCollisionObject* ObjB, GCollisionContact* pContact = nullptr) override; 53 | }; 54 | 55 | class GCG_Sphere_Cylinder : public GCollisionAlgorithm 56 | { 57 | public: 58 | virtual bool ProcessCollision(const GCollisionObject* ObjA, const GCollisionObject* ObjB, GCollisionContact* pContact = nullptr) override; 59 | }; 60 | 61 | class GCG_Sphere_Plane : public GCollisionAlgorithm 62 | { 63 | public: 64 | virtual bool ProcessCollision(const GCollisionObject* ObjA, const GCollisionObject* ObjB, GCollisionContact* pContact = nullptr) override; 65 | }; 66 | 67 | class GCG_Box_Box : public GCollisionAlgorithm 68 | { 69 | public: 70 | virtual bool ProcessCollision(const GCollisionObject* ObjA, const GCollisionObject* ObjB, GCollisionContact* pContact = nullptr) override; 71 | }; 72 | 73 | 74 | 75 | class GCollisionManerger 76 | { 77 | public: 78 | 79 | GCollisionManerger(); 80 | ~GCollisionManerger(); 81 | 82 | 83 | void Init( ); 84 | 85 | inline GCollisionAlgorithm* GetAlgrithm( int32_t nShapeA, int32_t nShapeB) 86 | { 87 | return m_Algorithm[nShapeA][nShapeB]; 88 | } 89 | 90 | private: 91 | GCollisionAlgorithm* m_Algorithm[EShape_Max][EShape_Max]; 92 | 93 | }; -------------------------------------------------------------------------------- /gphysics/include/collision/glacier_collision_box.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | #pragma once 14 | 15 | #include "glacier_transform_qt.h" 16 | #include "glacier_collision_shape.h" 17 | 18 | class GCollision_Box 19 | { 20 | public: 21 | 22 | 23 | static bool Box_Box( 24 | const GShapeBox& ShapA, 25 | const GTransform_QT& TransformA, 26 | const GShapeBox& ShapB, 27 | const GTransform_QT& TransformB, 28 | GVector3* pOutPosition, 29 | GVector3* pOutNormal); 30 | 31 | 32 | static int32_t Box_Box_Contact( 33 | const GShapeBox& ShapA, 34 | const GTransform_QT& TransformA, 35 | const GShapeBox& ShapB, 36 | const GTransform_QT& TransformB, 37 | class GCollisionContact* pContact); 38 | }; 39 | 40 | 41 | -------------------------------------------------------------------------------- /gphysics/include/collision/glacier_collision_gjk.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | #pragma once 14 | 15 | #include "glacier_transform_qt.h" 16 | #include "glacier_collision_shape.h" 17 | #include "glacier_debug_draw.h" 18 | 19 | class IGlacierDraw; 20 | 21 | 22 | class GSimplex 23 | { 24 | public: 25 | 26 | 27 | GVector3 m_Points[4]; 28 | }; 29 | 30 | 31 | class GCollision_GJK 32 | { 33 | public: 34 | 35 | static const f32 s_DefaultEpsilon;// = GMath::Makef32(0,1,1000); 36 | 37 | static bool GJKTest( 38 | const GShapeConvexBase& ShapA, 39 | const GTransform_QT& TransformA, 40 | const GShapeConvexBase& ShapB, 41 | const GTransform_QT& TransformB, 42 | IGlacierDraw* pDebugDraw = nullptr ); 43 | 44 | 45 | template 46 | static inline GVector3 s_GetSupportPos( 47 | const GVector3& DirectionA, 48 | const ShapTypeA& ShapA, 49 | const ShapTypeB& ShapB, 50 | const GTransform_QT& LocalBToLocalA, 51 | const GTransform_QT& LocalAToLocalB, 52 | const GTransform_QT& TransformShapA, 53 | const GTransform_QT& TransformShapB, 54 | IGlacierDraw* pDebugDraw, 55 | uint32_t uColor) 56 | { 57 | GVector3 DirectionB = LocalAToLocalB.TransformNormal(-DirectionA); 58 | GVector3 PosA = ShapA.GetSupportLocalPos(DirectionA); 59 | GVector3 PosB = ShapB.GetSupportLocalPos(DirectionB); 60 | 61 | if (pDebugDraw != nullptr) 62 | { 63 | 64 | f32 ff = GMath::FromFloat(0.02f); 65 | f32 f2 = GMath::FromFloat(0.04f); 66 | 67 | pDebugDraw->DrawSphere(GTransform_QT(GQuaternion::Identity(), TransformShapA.TransformPosition(PosA)), ff, uColor, 12); 68 | pDebugDraw->DrawSphere(GTransform_QT(GQuaternion::Identity(), TransformShapB.TransformPosition(PosB)), ff, uColor, 12); 69 | pDebugDraw->DrawLine(TransformShapA.m_Translation, TransformShapA.m_Translation + TransformShapA.TransformNormal(DirectionA) * f2, uColor); 70 | pDebugDraw->DrawLine(TransformShapB.m_Translation, TransformShapB.m_Translation + TransformShapB.TransformNormal(DirectionB) * f2, uColor); 71 | } 72 | 73 | return PosA - LocalBToLocalA.TransformPosition(PosB); 74 | } 75 | 76 | /* template 77 | static inline f32 GJKDistance( 78 | const ShapTypeA& ShapA, const GTransform_QT& TransformA, 79 | const ShapTypeB& ShapB, const GTransform_QT& TransformB, 80 | f32 Epsilon = s_DefaultEpsilon, 81 | IGlacierDraw* pDebugDraw = nullptr ) 82 | { 83 | GTransform_QT WorlToLocal_A = TransformA.GetInverse(); 84 | GTransform_QT LocalBToLocalA = TransformB * WorlToLocal_A; 85 | GTransform_QT LocalAToLocalB = LocalBToLocalA.GetInverse(); 86 | 87 | 88 | GVector3 DirectionA = LocalBToLocalA.TransformPosition(GVector3::Zero()).GetNormalize(); 89 | GVector3 SupportPos0 = s_GetSupportPos(DirectionA, ShapA, ShapB, LocalBToLocalA, LocalAToLocalB, TransformA, TransformB, pDebugDraw, 0x00FF0000); 90 | 91 | f32 fDistance = SupportPos0.Size(); 92 | if (f < GMath::Epsilon()) 93 | { 94 | return fDistance; 95 | } 96 | 97 | while ( fDistance > Epsilon ) 98 | { 99 | f32 fDotTest = GVector3::DotProduct( SupportPos0 ); 100 | if (fDotTest > GMath::Zero()) 101 | { 102 | return fDotTest; // not accurate 103 | } 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | f32 fDistance = SupportPos0.Size(); 112 | if (f < GMath::Epsilon()) 113 | { 114 | return fDistance; 115 | } 116 | } 117 | 118 | 119 | 120 | return fDistance; 121 | 122 | }*/ 123 | 124 | }; 125 | 126 | 127 | -------------------------------------------------------------------------------- /gphysics/include/collision/glacier_collision_shape.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | #pragma once 14 | 15 | #include "glacier_vector.h" 16 | #include "glacier_aabb.h" 17 | 18 | enum EShape 19 | { 20 | EShape_ConvexBase = 0, 21 | EShape_Sphere = 1, 22 | EShape_Box = 2, 23 | EShape_Capsule = 3, 24 | EShape_Cylinder = 4, 25 | EShape_ConvexHull = 5, 26 | EShape_ConcaveBase = 6, 27 | EShape_Plane = 7, 28 | EShape_HightField = 8, 29 | EShape_TriangleMesh = 9, 30 | EShape_Max = 10 31 | }; 32 | 33 | class GShapeBase; 34 | class GCollisionShape 35 | { 36 | public: 37 | GCollisionShape( EShape TShape ) : 38 | ShapType(TShape), HalfSize(GVector3::Identity()), pComplexShape(nullptr) 39 | { 40 | 41 | } 42 | 43 | public: 44 | EShape ShapType; // simple shape 45 | GVector3 HalfSize; 46 | GShapeBase* pComplexShape; 47 | 48 | f32 GetRaiuds() const { return HalfSize.x;} 49 | void SetRadius( const f32 value ){ HalfSize.x = value;} 50 | 51 | f32 GetHalfHeight() const { return HalfSize.y; } 52 | void SetHalfHeight(const f32 value) { HalfSize.y = value; } 53 | 54 | GVector3 GetHalfExtern() const { return HalfSize; } 55 | void SetHalfExtern(const GVector3& value) { HalfSize = value; } 56 | 57 | GVector3 GetPlaneNormal() const { return HalfSize; } 58 | void SetPlaneNormal(const GVector3& value) { HalfSize = value; } 59 | 60 | GAABB GetLocalBox() const; 61 | 62 | }; 63 | 64 | class GShapeBase 65 | { 66 | public: 67 | EShape ShapType; 68 | 69 | GShapeBase() : ShapType(EShape::EShape_Max) 70 | { 71 | 72 | } 73 | }; 74 | 75 | class GShapeConvexBase : public GShapeBase 76 | { 77 | public: 78 | 79 | GShapeConvexBase() 80 | { 81 | ShapType = EShape::EShape_ConvexBase; 82 | } 83 | 84 | // Dir is normalize 85 | virtual GVector3 GetSupportLocalPos( const GVector3& Dir ) const 86 | { 87 | return Dir; 88 | } 89 | 90 | }; 91 | 92 | class GShapeSphere : public GShapeConvexBase 93 | { 94 | public: 95 | GShapeSphere( f32 tRaduis ) : Radius(tRaduis) 96 | { 97 | ShapType = EShape::EShape_Sphere; 98 | } 99 | 100 | virtual GVector3 GetSupportLocalPos(const GVector3& Dir) const 101 | { 102 | return Dir * Radius; 103 | } 104 | 105 | f32 Radius; 106 | }; 107 | class GShapeBox : public GShapeConvexBase 108 | { 109 | public: 110 | GShapeBox(GVector3 tvalue) : HalfExtern(tvalue) 111 | { 112 | ShapType = EShape::EShape_Box; 113 | } 114 | 115 | virtual GVector3 GetSupportLocalPos(const GVector3& Dir) const 116 | { 117 | return GVector3( 118 | Dir.x > 0 ? HalfExtern.x : -HalfExtern.x, 119 | Dir.y > 0 ? HalfExtern.y : -HalfExtern.y, 120 | Dir.z > 0 ? HalfExtern.z : -HalfExtern.z); 121 | } 122 | 123 | GVector3 HalfExtern; 124 | }; 125 | 126 | 127 | class GShapeCapsule : public GShapeConvexBase 128 | { 129 | public: 130 | 131 | GShapeCapsule( f32 THalfHeight, f32 tRaduis) : HalfHeight(THalfHeight), Raius(tRaduis) 132 | { 133 | ShapType = EShape::EShape_Capsule; 134 | } 135 | 136 | virtual GVector3 GetSupportLocalPos(const GVector3& Dir) const 137 | { 138 | GVector3 supVec = GVector3::Zero(); 139 | 140 | f32 maxDot(f32(-10000)); 141 | 142 | f32 newDot; 143 | { 144 | GVector3 pos = GVector3( GMath::Zero(), GMath::Zero(), HalfHeight ); 145 | 146 | GVector3 vtx = pos + Dir * Raius ; 147 | newDot = GVector3::DotProduct( Dir, vtx ); 148 | if (newDot > maxDot) 149 | { 150 | maxDot = newDot; 151 | supVec = vtx; 152 | } 153 | } 154 | { 155 | GVector3 pos = GVector3( GMath::Zero(), GMath::Zero(),-HalfHeight ); 156 | GVector3 vtx = pos + Dir * Raius; 157 | newDot = GVector3::DotProduct( Dir, vtx ); 158 | if (newDot > maxDot) 159 | { 160 | maxDot = newDot; 161 | supVec = vtx; 162 | } 163 | } 164 | 165 | return supVec; 166 | } 167 | 168 | f32 HalfHeight; 169 | f32 Raius; 170 | }; 171 | 172 | class GShapeCylinder : public GShapeConvexBase 173 | { 174 | public: 175 | 176 | GShapeCylinder(f32 THalfHeight, f32 tRaduis) : HalfHeight(THalfHeight), Raius(tRaduis) 177 | { 178 | ShapType = EShape::EShape_Capsule; 179 | } 180 | 181 | GVector3 GetSupportLocalPos(const GVector3& Dir) const 182 | { 183 | return Dir * Raius; 184 | } 185 | 186 | f32 HalfHeight; 187 | f32 Raius; 188 | }; 189 | 190 | 191 | class GShapeConcaveBase : public GShapeBase 192 | { 193 | public: 194 | 195 | GShapeConcaveBase() 196 | { 197 | ShapType = EShape::EShape_ConcaveBase; 198 | } 199 | 200 | 201 | }; 202 | 203 | class GShapeHightField : public GShapeConcaveBase 204 | { 205 | public: 206 | 207 | GShapeHightField( ) 208 | { 209 | ShapType = EShape::EShape_HightField; 210 | } 211 | 212 | 213 | }; 214 | 215 | class GShapeTriangleMesh : public GShapeConcaveBase 216 | { 217 | public: 218 | 219 | GShapeTriangleMesh() 220 | { 221 | ShapType = EShape::EShape_TriangleMesh; 222 | } 223 | 224 | 225 | }; -------------------------------------------------------------------------------- /gphysics/include/collision/glacier_collision_sphere.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | #pragma once 14 | 15 | #include "glacier_vector.h" 16 | 17 | class GCollision_Sphere 18 | { 19 | public: 20 | 21 | static inline bool Sphere_Sphere(const GVector3& c0, const f32 r0, const GVector3& c1, const f32 r1) 22 | { 23 | GVector3 d = c0 - c1; 24 | f32 len2 = d.SizeSquare(); 25 | return len2 < (r1+r0) * (r1+r0); 26 | } 27 | 28 | static inline bool Sphere_Point(const GVector3& c0, const f32 r0, const GVector3& Point) { return Sphere_Sphere(c0, r0, Point, GMath::Zero()); } 29 | static inline bool Sphere_Sphere( const GVector3& center0, const f32 radius0, const GVector3& center1, const f32 radius1, GVector3& finalCenter, GVector3* pOutNormal, bool bInner = false) 30 | { 31 | f32 r = radius1 + (bInner ? -radius0 : radius0); 32 | f32 r2 = r * r; 33 | 34 | GVector3 d = center0 - center1; 35 | f32 len2 = d.SizeSquare(); 36 | 37 | if (bInner ^ (len2 >= r2)) //|| (bInner && len2 <= r2) 38 | { 39 | return false; 40 | } 41 | else 42 | { 43 | if (len2 < GMath::Epsilon()) 44 | { 45 | finalCenter = center1 + GVector3(r, GMath::Zero(), GMath::Zero()); 46 | if (pOutNormal != nullptr) 47 | { 48 | *pOutNormal = GVector3(GMath::One(), GMath::Zero(), GMath::Zero()); 49 | } 50 | return true; 51 | } 52 | else 53 | { 54 | f32 invlen = GMath::InvSqrt(len2); 55 | 56 | GVector3 TNormal = d * invlen; 57 | 58 | if (pOutNormal != nullptr) 59 | { 60 | *pOutNormal = bInner ? -TNormal : TNormal; 61 | } 62 | 63 | finalCenter = center1 + TNormal * r; 64 | return true; 65 | } 66 | } 67 | } 68 | 69 | static inline bool LineSphere(const GVector3& lPoint1, const GVector3& lPoint2, const GVector3& dCenter, f32 dRadius) 70 | { 71 | const GVector3& TDir = lPoint2 - lPoint1; 72 | 73 | f32 uLength = TDir.SizeSquare(); 74 | 75 | if (uLength == GMath::Zero()) 76 | { 77 | if ((dCenter - lPoint1).SizeSquare()> dRadius * dRadius) 78 | { 79 | return false; 80 | } 81 | else 82 | { 83 | return true; 84 | } 85 | } 86 | else 87 | { 88 | f32 T = GVector3::DotProduct(dCenter - lPoint1, TDir) / uLength; 89 | 90 | GVector3 VNearest = lPoint1 + GMath::Clamp(T, GMath::Zero(), GMath::One()) * TDir; 91 | 92 | if ((dCenter - VNearest).SizeSquare() > dRadius * dRadius) 93 | { 94 | return false; 95 | } 96 | else 97 | { 98 | return true; 99 | } 100 | } 101 | } 102 | 103 | static inline bool Sphere_Box(const GVector3& c, f32 r, const GVector3& p, const GVector3& h) 104 | { 105 | GVector3 v = (p - c).Abs(); 106 | GVector3 u = GVector3::Max(v - h, GVector3::Zero()); 107 | 108 | return GVector3::DotProduct(u, u) <= r * r; 109 | } 110 | 111 | static bool Sphere_Box(const GVector3& center, const f32 radius, const GVector3& boxCenter, const GVector3& boxExtents, GVector3& finalCenter, GVector3* pOutNormal); 112 | static bool Sphere_Capsule( const GVector3& sphereCenter, const f32 sphereRadius, const GVector3& capsuleP0, const GVector3& capsuleP1, const f32 Radius0, const f32 Radius1, GVector3& result, GVector3* pOutNormal); 113 | 114 | 115 | 116 | }; 117 | 118 | 119 | -------------------------------------------------------------------------------- /gphysics/include/collision/glacier_contact.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | #pragma once 14 | 15 | #include "glacier_transform_qt.h" 16 | #include "glacier_collision_object.h" 17 | #include 18 | #include 19 | 20 | 21 | 22 | class GManifoldPoint 23 | { 24 | public: 25 | 26 | GVector3 m_PosOnSurfaceB_World; 27 | GVector3 m_NormalOnB; 28 | f32 m_depth; 29 | }; 30 | 31 | 32 | 33 | class GCollisionContact 34 | { 35 | public: 36 | 37 | GCollisionContact() = default; 38 | GCollisionContact(const GCollisionContact&) = default; 39 | 40 | 41 | void AddContactPoint( const GVector3& PosOnSurfaceB_World, const GVector3& NormalOnB, f32 depth ); 42 | 43 | void Clear() 44 | { 45 | PairId = 0; 46 | m_nPointCount = 0; 47 | } 48 | 49 | void ClearPoint() 50 | { 51 | m_nPointCount = 0; 52 | } 53 | 54 | uint64_t PairId; 55 | 56 | 57 | static constexpr int32_t MaxPoint = 4; 58 | 59 | int32_t m_nPointCount; 60 | GManifoldPoint m_Point[MaxPoint]; 61 | 62 | 63 | GVector3 VTest; 64 | }; 65 | 66 | 67 | class GContactManerger 68 | { 69 | public: 70 | 71 | GContactManerger() 72 | { 73 | 74 | } 75 | 76 | void Add( const GCollisionContact& TContact ) 77 | { 78 | std::map::iterator iter = m_Finder.find( TContact.PairId); 79 | if( iter != m_Finder.end() ) 80 | { 81 | m_Contacts[iter->second] = TContact;// todo update 82 | } 83 | else 84 | { 85 | uint32_t FreedId = -1; 86 | for( int i = 0; i < m_Contacts.size(); ++i) // todo opimization 87 | { 88 | if( m_Finder.find( m_Contacts[i].PairId ) == m_Finder.end() ) 89 | { 90 | FreedId = i; 91 | break; 92 | } 93 | } 94 | 95 | if( FreedId != -1 ) 96 | { 97 | m_Finder[TContact.PairId] = FreedId; 98 | m_Contacts[FreedId] = TContact; 99 | } 100 | else 101 | { 102 | m_Contacts.push_back(TContact); 103 | m_Finder[TContact.PairId] = m_Contacts.size() - 1; 104 | } 105 | } 106 | } 107 | 108 | void Delete( uint64_t PairId ) 109 | { 110 | std::map::iterator iter = m_Finder.find(PairId); 111 | if (iter != m_Finder.end()) 112 | { 113 | m_Contacts[iter->second].Clear(); 114 | m_Finder.erase(iter); 115 | } 116 | } 117 | 118 | std::vector m_Contacts; 119 | std::map m_Finder; // todo optimization 120 | 121 | }; -------------------------------------------------------------------------------- /gphysics/include/collision/glacier_convexhull.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | #pragma once 14 | 15 | #include "glacier_vector.h" 16 | #include "glacier_plane.h" 17 | #include "glacier_transform_qt.h" 18 | #include "glacier_collision_shape.h" 19 | #include "glacier_debug_draw.h" 20 | 21 | 22 | #include 23 | #include 24 | #include 25 | 26 | 27 | struct GConvexPolygon 28 | { 29 | GPlane m_Plane; 30 | GVector3 m_CenterPos; 31 | uint16_t m_NbVerts; 32 | uint16_t m_IndexBase; 33 | 34 | inline uint16_t GetNext(uint16_t nCurrentindex) const 35 | { 36 | return m_IndexBase + (nCurrentindex < (m_NbVerts - 1) ? nCurrentindex + 1 : 0); 37 | } 38 | }; 39 | 40 | struct GConvexEdge 41 | { 42 | uint16_t PolygenId_A; 43 | uint16_t PolygenId_B; 44 | 45 | uint16_t PolygenA_Point_Index; 46 | uint16_t PolygenB_Point_Index; 47 | }; 48 | 49 | 50 | 51 | class GConvexHull : public GShapeConvexBase 52 | { 53 | public: 54 | GConvexHull( ) 55 | { 56 | ShapType = EShape::EShape_ConvexHull; 57 | } 58 | virtual ~GConvexHull() 59 | { 60 | 61 | } 62 | 63 | void Clear() 64 | { 65 | m_VPoints.clear(); 66 | m_Indexes.clear(); 67 | m_Polygons.clear(); 68 | m_Edges.clear(); 69 | } 70 | 71 | virtual GVector3 GetSupportLocalPos(const GVector3& Dir) const 72 | { 73 | return Dir;// * Radius; 74 | } 75 | 76 | void BuildEdges(); 77 | 78 | void Draw( class IGlacierDraw* pDraw, const GTransform_QT& Trans, GColor TColor ) const; 79 | 80 | protected: 81 | 82 | std::vector m_VPoints; 83 | std::vector m_Indexes; 84 | std::vector m_Polygons; 85 | std::vector m_Edges; 86 | 87 | 88 | friend class GConvexHullBuilder; 89 | }; 90 | 91 | 92 | class GSortPlane 93 | { 94 | public: 95 | 96 | GPlane m_Plane; 97 | 98 | GSortPlane() 99 | { 100 | 101 | } 102 | 103 | GSortPlane(GPlane TPlane) : m_Plane(TPlane) 104 | { 105 | 106 | } 107 | 108 | GSortPlane(const GSortPlane&) = default; 109 | 110 | inline bool operator == (const GSortPlane& b) const 111 | { 112 | return 113 | GVector3::DistanceSquare( m_Plane.m_Normal, b.m_Plane.m_Normal ) < GMath::Inv_1000000() && 114 | GMath::Abs( m_Plane.m_fDis - b.m_Plane.m_fDis ) < GMath::Inv_1000(); 115 | } 116 | 117 | inline bool operator != (const GSortPlane& b) const 118 | { 119 | return !( *this == b ); 120 | } 121 | 122 | inline bool operator < (const GSortPlane& b) const 123 | { 124 | if( *this == b ) 125 | return false; 126 | else 127 | { 128 | const GVector3& Va = m_Plane.m_Normal; 129 | const GVector3& Vb = b.m_Plane.m_Normal; 130 | 131 | if (Va.z != Vb.z) 132 | { 133 | return Va.z < Vb.z; 134 | } 135 | else if (Va.y != Vb.y) 136 | { 137 | return Va.y < Vb.y; 138 | } 139 | else if (Va.x != Vb.x) 140 | { 141 | return Va.x < Vb.x; 142 | } 143 | else 144 | { 145 | return m_Plane.m_fDis < b.m_Plane.m_fDis; 146 | } 147 | } 148 | } 149 | }; 150 | 151 | 152 | struct GBuildPolygon 153 | { 154 | GPlane m_Plane; 155 | GVector3 m_VCenter; 156 | std::set m_Points; 157 | std::vector m_ListPoints; 158 | }; 159 | 160 | class GConvexHullBuilder 161 | { 162 | public: 163 | 164 | 165 | static void AddBoxPoints(std::vector& Points, const GVector3& VMin, const GVector3& VMax ); 166 | 167 | void BuildConvex(const std::vector& Points, GConvexHull& CResult ); 168 | 169 | void BuildMinkowskiSum( 170 | const GConvexHull& CA, 171 | GTransform_QT TA, 172 | const GConvexHull& CB, 173 | GTransform_QT TB, 174 | GConvexHull& CResult, 175 | bool bAdd = true); 176 | 177 | 178 | void Draw(class IGlacierDraw* pDraw, const GTransform_QT& Trans, GColor TColor) const; 179 | 180 | protected: 181 | 182 | 183 | void AddInputPoints(const std::vector& InputPoints); 184 | 185 | void AddFace(const GSortPlane& TPlane, uint16_t i, uint16_t j, uint16_t k); 186 | 187 | 188 | 189 | void SimpleliseFace( GBuildPolygon& TPolygon ); 190 | 191 | 192 | public: 193 | std::vector m_VPoints; 194 | std::map m_Polygons; 195 | 196 | 197 | std::vector m_PolygonPoints; 198 | 199 | }; -------------------------------------------------------------------------------- /gphysics/include/collision/glacier_distance.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | #pragma once 14 | 15 | #include "glacier_vector.h" 16 | 17 | class GDistance 18 | { 19 | public: 20 | 21 | static inline GVector3 closestPtPointSphere(const GVector3& p, const GVector3& a, f32 radius) 22 | { 23 | return (p - a).GetNormalize() * radius + a; 24 | } 25 | 26 | static inline GVector3 closestPtPointPlane(const GVector3& p, const GVector3& PlaneNormal, const GVector3& planePos) 27 | { 28 | return p - PlaneNormal * GVector3::DotProduct(p - planePos, PlaneNormal); 29 | } 30 | 31 | static inline GVector3 closestPtPointLine(const GVector3& p, const GVector3& Pos, const GVector3& Vdir) 32 | { 33 | return Pos + Vdir * GVector3::DotProduct(Vdir, p - Pos); 34 | } 35 | 36 | static inline void ClosestPtPointLine(const GVector3& point, const GVector3& sP0, const GVector3& sP1, f32& t, GVector3& d) 37 | { 38 | GVector3 seg = sP1 - sP0; 39 | 40 | t = GVector3::DotProduct(point - sP0, seg); 41 | 42 | f32 denom = GVector3::DotProduct(seg, seg); 43 | 44 | t = t / denom; 45 | d = sP0 + t * seg; 46 | } 47 | 48 | static inline GVector3 ClosestPtPointSegment(const GVector3& point, const GVector3& sP0, const GVector3& sP1 ) 49 | { 50 | GVector3 seg = sP1 - sP0; 51 | 52 | f32 t = GVector3::DotProduct(point - sP0, seg); 53 | if (t <= GMath::Zero()) 54 | { 55 | return sP0; 56 | } 57 | else 58 | { 59 | f32 denom = GVector3::DotProduct(seg, seg); 60 | if (t >= denom) 61 | { 62 | return sP1; 63 | } 64 | else 65 | { 66 | t = t / denom; 67 | return sP0 + t * seg; 68 | } 69 | } 70 | } 71 | 72 | static inline void ClosestPtPointSegment(const GVector3& point, const GVector3& sP0, const GVector3& sP1, f32& t, GVector3& d) 73 | { 74 | GVector3 seg = sP1 - sP0; 75 | 76 | t = GVector3::DotProduct(point - sP0, seg); 77 | if (t <= GMath::Zero()) 78 | { 79 | t = GMath::Zero(); 80 | d = sP0; 81 | } 82 | else 83 | { 84 | f32 denom = GVector3::DotProduct(seg, seg); 85 | if (t >= denom) 86 | { 87 | t = GMath::One(); 88 | d = sP1; 89 | } 90 | else 91 | { 92 | t = t / denom; 93 | d = sP0 + t * seg; 94 | } 95 | } 96 | } 97 | 98 | static inline GVector3 closestPtPointAABB(const GVector3& p, const GVector3& VHalfSize) 99 | { 100 | return GVector3( 101 | GMath::Clamp(p.x, -VHalfSize.x, VHalfSize.x), 102 | GMath::Clamp(p.y, -VHalfSize.y, VHalfSize.y), 103 | GMath::Clamp(p.z, -VHalfSize.z, VHalfSize.z)); 104 | } 105 | 106 | static GVector3 ClosestPointTriangle(const GVector3& point, const GVector3& a, const GVector3& b, const GVector3& c); 107 | 108 | static inline int pointOutsideOfPlane(const GVector3& p, const GVector3& a, const GVector3& b, const GVector3& c, const GVector3& d) 109 | { 110 | GVector3 normal = GVector3::CrossProduct(b - a, c - a); 111 | 112 | f32 signp = GVector3::DotProduct(p - a, normal); // [AP AB AC] 113 | f32 signd = GVector3::DotProduct(d - a, normal); // [AD AB AC] 114 | 115 | if (signd * signd < GMath::Epsilon()) 116 | { 117 | return -1; 118 | } 119 | // Points on opposite sides if expression signs are opposite 120 | return signp * signd < GMath::Zero() ? 1 : 0; 121 | } 122 | 123 | 124 | static inline GVector3 ClosestPtPointTetrahedron(const GVector3& p, const GVector3& a, const GVector3& b, const GVector3& c, const GVector3& d) 125 | { 126 | int pointOutsideABC = pointOutsideOfPlane(p, a, b, c, d); 127 | int pointOutsideACD = pointOutsideOfPlane(p, a, c, d, b); 128 | int pointOutsideADB = pointOutsideOfPlane(p, a, d, b, c); 129 | int pointOutsideBDC = pointOutsideOfPlane(p, b, d, c, a); 130 | 131 | if (pointOutsideABC < 0 || pointOutsideACD < 0 || pointOutsideADB < 0 || pointOutsideBDC < 0) 132 | { 133 | return p; 134 | } 135 | 136 | if (pointOutsideABC == 0 && pointOutsideACD == 0 && pointOutsideADB == 0 && pointOutsideBDC == 0) 137 | { 138 | return p; 139 | } 140 | 141 | // If point outside face abc then compute closest point on abc 142 | if (pointOutsideABC != 0) 143 | { 144 | return ClosestPointTriangle(p, a, b, c); 145 | } 146 | 147 | 148 | // Repeat test for face acd 149 | if (pointOutsideACD != 0) 150 | { 151 | return ClosestPointTriangle(p, a, c, d); 152 | 153 | } 154 | // Repeat test for face adb 155 | 156 | 157 | if (pointOutsideADB != 0) 158 | { 159 | return ClosestPointTriangle(p, a, d, b); 160 | } 161 | // Repeat test for face bdc 162 | 163 | 164 | if (pointOutsideBDC != 0) 165 | { 166 | return ClosestPointTriangle(p, b, d, c); 167 | } 168 | 169 | return p; 170 | } 171 | 172 | }; 173 | 174 | 175 | 176 | -------------------------------------------------------------------------------- /gphysics/include/dynamic/glacier_rigid_body.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | #pragma once 14 | 15 | #include "glacier_collision_object.h" 16 | 17 | class GRigidBody : public GCollisionObject 18 | { 19 | public: 20 | GRigidBody(uint32_t id, EShape TShape, ECollisionObjectType TType) : GCollisionObject(id, TShape, TType) 21 | { 22 | 23 | } 24 | 25 | public: 26 | 27 | 28 | }; -------------------------------------------------------------------------------- /gphysics/include/dynamic/glacier_rigid_dynamic.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | #pragma once 14 | 15 | #include "glacier_rigid_body.h" 16 | 17 | class GDynamicRigid : public GRigidBody 18 | { 19 | public: 20 | 21 | GDynamicRigid(uint32_t id, EShape TShape) : GRigidBody(id, TShape, ECollisionObjectType::Dynamic) 22 | { 23 | m_Gravity = GVector3(GMath::Zero(), GMath::Zero(), -GMath::Makef32(9,8,10) ); 24 | m_VelocityMax = GMath::Makef32(100,0,1); 25 | } 26 | 27 | void Tick_PreTransform( const f32 DetalTime ) 28 | { 29 | 30 | GVector3 VNew = m_Velocity + m_Gravity * DetalTime; 31 | 32 | if( VNew.SizeSquare() > (m_VelocityMax*m_VelocityMax)) 33 | { 34 | VNew = VNew.GetNormalize() * m_VelocityMax; 35 | 36 | } 37 | 38 | 39 | m_Transform.m_Translation += (m_Velocity + VNew) * GMath::Half() * DetalTime; 40 | 41 | m_Velocity = VNew; 42 | 43 | m_bNeedUpdate = true; 44 | } 45 | 46 | public: 47 | 48 | GTransform_QT m_Transform_Pre; 49 | 50 | f32 m_Mass; 51 | f32 m_InvMass; 52 | GVector3 m_Velocity; 53 | f32 m_VelocityMax; 54 | 55 | GVector3 m_MoumentInertia; 56 | GVector3 m_InvMoumentInertia; 57 | GVector3 m_AngleVelocity; 58 | 59 | GVector3 m_Gravity; 60 | }; -------------------------------------------------------------------------------- /gphysics/include/dynamic/glacier_rigid_static.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | #pragma once 14 | 15 | #include "glacier_rigid_body.h" 16 | 17 | class GStaticRigid : public GRigidBody 18 | { 19 | public: 20 | 21 | GStaticRigid( uint32_t id, EShape TShape ) : GRigidBody(id, TShape, ECollisionObjectType::Static) 22 | { 23 | 24 | } 25 | 26 | 27 | public: 28 | 29 | 30 | 31 | }; -------------------------------------------------------------------------------- /gphysics/include/math/glacier_plane.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | #pragma once 14 | 15 | #include "glacier_vector.h" 16 | 17 | class GPlane 18 | { 19 | public: 20 | 21 | enum GPlaneSide 22 | { 23 | NO_SIDE = 0, 24 | POSITIVE, 25 | NEGATIVE, 26 | BOTH 27 | }; 28 | 29 | public: 30 | 31 | GPlane( ) = default; 32 | GPlane( const GPlane& P ) = default; 33 | 34 | inline GPlane(const GVector3& V3Normal, f32 fDis ) 35 | { 36 | m_Normal = V3Normal; 37 | m_fDis = fDis; 38 | } 39 | 40 | inline GPlane( const GVector3& V3Normal, const GVector3& V3Point ) 41 | { 42 | Redefine( V3Normal, V3Point ); 43 | } 44 | 45 | inline GPlane( const GVector3& V0, const GVector3& V1, const GVector3& V2 ) 46 | { 47 | Redefine( V0, V1, V2 ); 48 | } 49 | 50 | public: 51 | 52 | bool operator == ( const GPlane& P ) const 53 | { 54 | return ( P.m_fDis == m_fDis && P.m_Normal == m_Normal ); 55 | } 56 | 57 | bool operator != ( const GPlane& P ) const 58 | { 59 | return ( P.m_fDis != m_fDis || P.m_Normal != m_Normal ); 60 | } 61 | 62 | GPlane operator + (const GPlane& P) const 63 | { 64 | return GPlane( m_Normal + P.m_Normal, m_fDis + P.m_fDis); 65 | } 66 | 67 | GPlane operator - (const GPlane& P) const 68 | { 69 | return GPlane(m_Normal - P.m_Normal, m_fDis - P.m_fDis); 70 | } 71 | 72 | void Normalize() 73 | { 74 | f32 fLength = GVector3::DotProduct(m_Normal, m_Normal) + m_fDis * m_fDis; 75 | if (fLength < GMath::Epsilon()) 76 | { 77 | GPlane( GVector3::UnitX(), GMath::Zero()); 78 | } 79 | else 80 | { 81 | f32 fInvLength = GMath::InvSqrt(fLength); 82 | m_Normal *= fInvLength; 83 | m_fDis *= fInvLength; 84 | } 85 | } 86 | 87 | public: 88 | 89 | inline GPlane Flip( void ) const 90 | { 91 | return GPlane( -m_Normal, -m_fDis ); 92 | } 93 | 94 | inline f32 GetDistance(const GVector3& V3Point) const 95 | { 96 | return GVector3::DotProduct(m_Normal, V3Point) + m_fDis; 97 | } 98 | 99 | inline GPlaneSide GetSide( const GVector3& V3Point, f32 Radius = GMath::Epsilon() ) const 100 | { 101 | f32 fDistance = GetDistance( V3Point ); 102 | 103 | if( fDistance < -Radius ) 104 | { 105 | return GPlane::NEGATIVE; 106 | } 107 | else if( fDistance > Radius ) 108 | { 109 | return GPlane::POSITIVE; 110 | } 111 | else 112 | { 113 | return GPlane::NO_SIDE; 114 | } 115 | } 116 | 117 | inline GPlaneSide GetSide( const GVector3& VCenter, const GVector3& VHalfSize ) const 118 | { 119 | f32 fDist = GetDistance( VCenter ); 120 | 121 | f32 fMaxAbsDist = m_Normal.AbsDotProduct( VHalfSize ); 122 | 123 | if( fDist < -fMaxAbsDist ) 124 | { 125 | return GPlane::NEGATIVE; 126 | } 127 | 128 | if( fDist > +fMaxAbsDist ) 129 | { 130 | return GPlane::POSITIVE; 131 | } 132 | 133 | return GPlane::BOTH; 134 | } 135 | 136 | inline void Redefine( const GVector3& V0, const GVector3& V1, const GVector3& V2 ) 137 | { 138 | GVector3 VEdge1 = V1 - V0; 139 | GVector3 VEdge2 = V2 - V0; 140 | m_Normal = GVector3::CrossProduct( VEdge1, VEdge2 ).GetNormalize(); 141 | m_fDis =-GVector3::DotProduct(m_Normal, V0 ); 142 | } 143 | 144 | inline void Redefine( const GVector3& VNormal, const GVector3& V3Point ) 145 | { 146 | m_Normal = VNormal; 147 | m_fDis = -GVector3::DotProduct(m_Normal, V3Point ); 148 | } 149 | 150 | inline void Normalise( void ) 151 | { 152 | f32 fLength = m_Normal.SizeSquare( ); 153 | 154 | if( fLength > GMath::Epsilon() ) 155 | { 156 | f32 fInvLength = GMath::InvSqrt( fLength); 157 | m_Normal = m_Normal * fInvLength; 158 | m_fDis = m_fDis * fInvLength; 159 | } 160 | else 161 | { 162 | m_Normal = GVector3::Zero(); 163 | m_fDis = GMath::Zero(); 164 | } 165 | } 166 | 167 | inline GPlane GetNormalise( ) const 168 | { 169 | GPlane TP = *this; 170 | TP.Normalise( ); 171 | return TP; 172 | } 173 | 174 | inline GVector3 ProjectVector(const GVector3& V) const 175 | { 176 | return V - m_Normal * GVector3::DotProduct(m_Normal, V); 177 | } 178 | 179 | inline GVector3 ReflectPoint( const GVector3& V ) const 180 | { 181 | return V - m_Normal * ( GMath::Two() * GetDistance( V ) ); 182 | } 183 | 184 | inline GVector3 ReflectVector( const GVector3& V ) const 185 | { 186 | return V - m_Normal * ( GMath::Two() * GVector3::DotProduct(m_Normal, V) ); 187 | } 188 | 189 | public: 190 | 191 | GVector3 m_Normal; 192 | f32 m_fDis; 193 | }; 194 | 195 | -------------------------------------------------------------------------------- /gphysics/include/math/glacier_transform_qt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | #pragma once 14 | 15 | #include "glacier_vector.h" 16 | #include "glacier_quaternion.h" 17 | #include "glacier_aabb.h" 18 | 19 | class GTransform_QT 20 | { 21 | public: 22 | 23 | GTransform_QT() = default; 24 | 25 | GTransform_QT(const GTransform_QT&) = default; 26 | 27 | inline GTransform_QT( const GQuaternion& Quat, const GVector3& Trans ) : 28 | m_Rotate(Quat ), m_Translation(Trans) 29 | { 30 | 31 | } 32 | 33 | inline GTransform_QT( const GQuaternion& Quat ) : 34 | m_Rotate(Quat ), m_Translation( GVector3::Identity() ) 35 | { 36 | 37 | } 38 | 39 | inline GTransform_QT( const GVector3& Trans ) : 40 | m_Rotate( GQuaternion::Identity() ), m_Translation(Trans) 41 | { 42 | 43 | } 44 | 45 | inline GTransform_QT operator*( const GTransform_QT& Q ) const 46 | { 47 | return GTransform_QT( m_Rotate * Q.m_Rotate, Q.m_Rotate.RotateVector(m_Translation) + Q.m_Translation ); 48 | } 49 | 50 | inline GTransform_QT operator*( const GQuaternion& Q ) const 51 | { 52 | return GTransform_QT( m_Rotate * Q, Q.RotateVector(m_Translation) ); 53 | } 54 | 55 | friend inline GTransform_QT operator*( const GQuaternion& Q, const GTransform_QT& QT ) 56 | { 57 | return GTransform_QT( Q * QT.m_Rotate, QT.m_Translation ); 58 | } 59 | 60 | GVector3 TransformNormal( const GVector3& V ) const 61 | { 62 | return m_Rotate.RotateVector( V ); 63 | } 64 | 65 | GVector3 TransformPosition(const GVector3& V) const 66 | { 67 | return m_Rotate.RotateVector(V) + m_Translation; 68 | } 69 | 70 | GAABB TransformAABB( const GAABB& InBox ) const 71 | { 72 | GAABB TBox( TransformPosition( InBox.GetCorner(0)) ); 73 | for (uint32_t uLoop = 1; uLoop < 8; ++uLoop) 74 | { 75 | TBox.Merge( TransformPosition( InBox.GetCorner(uLoop))); 76 | } 77 | return TBox; 78 | } 79 | 80 | public: 81 | 82 | inline void Inverse( ) 83 | { 84 | m_Rotate.Inverse( ); 85 | m_Translation = m_Rotate.RotateVector( -m_Translation); 86 | } 87 | 88 | inline GTransform_QT GetInverse( ) const 89 | { 90 | GQuaternion TQ = m_Rotate.GetInverse( ); 91 | return GTransform_QT( TQ, TQ.RotateVector( -m_Translation) ); 92 | } 93 | 94 | inline GTransform_QT GetInverse_fast() const 95 | { 96 | GQuaternion TQ = m_Rotate.GetUnitInverse(); 97 | return GTransform_QT(TQ, TQ.RotateVector(-m_Translation)); 98 | } 99 | 100 | bool IsIdentity( ) const 101 | { 102 | return m_Rotate == GQuaternion::Identity( ) && m_Translation == GVector3::Zero( ); 103 | } 104 | 105 | static inline GTransform_QT Identity( ) 106 | { 107 | return GTransform_QT( GQuaternion::Identity(), GVector3::Zero( ) ); 108 | } 109 | 110 | public: 111 | 112 | GQuaternion m_Rotate; 113 | GVector3 m_Translation; 114 | }; 115 | 116 | -------------------------------------------------------------------------------- /gphysics/include/utils/glacier_color.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | #pragma once 14 | 15 | #include 16 | 17 | // ARGB color 18 | class GColor 19 | { 20 | public: 21 | 22 | GColor( const GColor& ) = default; 23 | 24 | GColor( uint32_t value ): RawValue(value) 25 | { 26 | } 27 | 28 | static GColor White() { return GColor(0xFFFFFFFF); } 29 | static GColor Black() { return GColor(0xFF000000); } 30 | static GColor Red() { return GColor(0xFFFF0000); } 31 | static GColor Green() { return GColor(0xFF00FF00); } 32 | static GColor Blue() { return GColor(0xFF0000FF); } 33 | static GColor Yellow() { return GColor(0xFFFFFF00); } 34 | 35 | 36 | 37 | uint32_t RawValue; 38 | }; -------------------------------------------------------------------------------- /gphysics/include/utils/glacier_debug_draw.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | #pragma once 14 | 15 | #include "glacier_color.h" 16 | #include "glacier_math.h" 17 | class GVector3; 18 | class GTransform_QT; 19 | class GPlane; 20 | class IGlacierDraw 21 | { 22 | public: 23 | 24 | virtual void DrawLine(const GVector3& V0, const GVector3& V1, GColor uColor) = 0; 25 | virtual void DrawBox(const GTransform_QT& TTrans, const GVector3& LocalCenter, const GVector3& HalfSize, GColor TColor); 26 | virtual void DrawSphere(const GTransform_QT& TTrans, f32 Radius, GColor TColor, int32_t nSeg); 27 | virtual void DrawCapsule(const GTransform_QT& TTrans, f32 Radius, f32 HalfHeight, GColor TColor, int nSeg = 18); 28 | virtual void DrawCylinder(const GTransform_QT& TTrans, f32 Radius, f32 HalfHeight, GColor TColor, int nSeg = 18); 29 | virtual void DrawPlane(const GTransform_QT& TTrans, const GPlane& TPlane, f32 Size, GColor TColor); 30 | virtual void DrawArrow(const GVector3& V0, const GVector3& VDirection, f32 Size, GColor TColor); 31 | 32 | 33 | 34 | 35 | }; 36 | -------------------------------------------------------------------------------- /gphysics/include/utils/glacier_physics_utils.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | #pragma once 14 | 15 | #include "glacier_vector.h" 16 | #include "glacier_transform_qt.h" 17 | 18 | class GCollisionShape; 19 | class IGlacierDraw; 20 | class GPhyscsUtils 21 | { 22 | public: 23 | 24 | static void DrawShape( const GTransform_QT& Trans, const GCollisionShape& pShape, IGlacierDraw* pDebugDraw ); 25 | 26 | }; -------------------------------------------------------------------------------- /gphysics/include/utils/glacier_time.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | #pragma once 14 | 15 | #include "glacier_vector.h" 16 | #include 17 | #include 18 | 19 | 20 | #if defined(__GNUC__) && defined(__x86_64__) && !defined(__aarch64__) 21 | #include 22 | #endif 23 | 24 | #if defined(_MSC_VER) || (defined(__GNUC__)) 25 | #define UseProfiler_RDTSCP 1 26 | #endif 27 | 28 | 29 | #define GProfilerFun GTimeProfiler GProfiler(__FUNCTION__); 30 | 31 | typedef std::chrono::high_resolution_clock Myclock; 32 | typedef std::chrono::nanoseconds Myres; 33 | 34 | class GTimer 35 | { 36 | public: 37 | GTimer() 38 | #if UseProfiler_RDTSCP 39 | : start_(0), end_(0) 40 | #else 41 | : t1(Myres::zero()), t2(Myres::zero()) 42 | #endif 43 | { 44 | Start(); 45 | } 46 | 47 | ~GTimer() 48 | {} 49 | 50 | static inline uint64_t get_CPUCycles() 51 | { 52 | #ifdef _MSC_VER 53 | return __rdtsc(); 54 | #elif __GNUC__ 55 | 56 | #if defined(__x86_64__) 57 | unsigned int lo, hi; 58 | __asm__ __volatile__("rdtsc" : "=a" (lo), "=d" (hi)); 59 | return ((uint64_t)hi << 32) | lo; 60 | #elif defined(__aarch64__) 61 | 62 | uint64_t virtual_timer_value; 63 | asm volatile("mrs %0, cntvct_el0" : "=r"(virtual_timer_value)); 64 | return virtual_timer_value; 65 | 66 | #else 67 | return 0; 68 | #endif 69 | 70 | #else 71 | return 0; 72 | #endif 73 | } 74 | 75 | void Start() 76 | { 77 | #if UseProfiler_RDTSCP 78 | start_ = get_CPUCycles(); 79 | #else 80 | t1 = Myclock::now(); 81 | #endif 82 | } 83 | 84 | void End() 85 | { 86 | #if UseProfiler_RDTSCP 87 | end_ = get_CPUCycles(); 88 | #else 89 | t2 = Myclock::now(); 90 | #endif 91 | } 92 | 93 | 94 | float GetDeltaTimeMS_NoEnd() 95 | { 96 | #if UseProfiler_RDTSCP 97 | return float(double(end_ - start_) * InvCPUGHZ); 98 | #else 99 | return float(std::chrono::duration_cast(t2 - t1).count() * 1e-6); 100 | #endif 101 | } 102 | 103 | float GetDeltaTimeMS() 104 | { 105 | End(); 106 | return GetDeltaTimeMS_NoEnd(); 107 | } 108 | 109 | static double GetCpuFrequency_Compute() 110 | { 111 | #if UseProfiler_RDTSCP 112 | return 1 / InvCPUGHZ; 113 | #else 114 | return 0; 115 | #endif 116 | } 117 | 118 | static int GetCpuFrequency_CpuInfo() 119 | { 120 | int cpuInfo[4] = { 0, 0, 0, 0 }; 121 | #ifdef _MSC_VER 122 | __cpuid(cpuInfo, 0); 123 | if (cpuInfo[0] >= 0x16) { 124 | __cpuid(cpuInfo, 0x16); 125 | return cpuInfo[0]; 126 | } 127 | #elif __GNUC__ 128 | 129 | #if defined(__x86_64__) && !defined(__aarch64__) 130 | 131 | __cpuid(0, cpuInfo[0], cpuInfo[1], cpuInfo[2], cpuInfo[3]); 132 | 133 | if (cpuInfo[0] >= 0x16) { 134 | __cpuid(0x16, cpuInfo[0], cpuInfo[1], cpuInfo[2], cpuInfo[3]); 135 | return cpuInfo[0]; 136 | } 137 | #elif defined(__ARM_ARCH) 138 | 139 | uint64_t freq; 140 | asm volatile("mrs %0, cntfrq_el0" : "=r" (freq)); 141 | return (int)(freq / 1000000); 142 | 143 | #endif 144 | 145 | #else 146 | 147 | #endif 148 | 149 | return 0; 150 | } 151 | 152 | private: 153 | 154 | #if UseProfiler_RDTSCP 155 | 156 | static double InvCPUGHZ; 157 | volatile uint64_t start_; 158 | volatile uint64_t end_; 159 | #else 160 | Myclock::time_point t1; 161 | Myclock::time_point t2; 162 | #endif 163 | 164 | }; 165 | 166 | class GTimeProfiler 167 | { 168 | public: 169 | GTimeProfiler(const char* Str); 170 | ~GTimeProfiler(); 171 | 172 | void EndCuptrue(); 173 | static void DebugOut(); 174 | static void ClearTime(); 175 | 176 | std::string m_Str; 177 | bool m_bCapture; 178 | GTimer m_Time; 179 | bool m_bOutToScreen; 180 | }; -------------------------------------------------------------------------------- /gphysics/include/world/glacier_collision_object.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | #pragma once 14 | 15 | #include "glacier_vector.h" 16 | #include "glacier_aabb.h" 17 | #include "glacier_transform_qt.h" 18 | #include "glacier_collision_shape.h" 19 | 20 | enum ECollisionObjectType 21 | { 22 | Static= 0, 23 | Dynamic, 24 | 25 | }; 26 | 27 | class GCollisionObject 28 | { 29 | public: 30 | 31 | GCollisionObject( uint32_t uId, EShape TShape, ECollisionObjectType CType ) 32 | : m_Id(uId), m_CollisionType(CType), m_Shape(TShape), m_LoaclAABB(GVector3::Zero()), m_WorldAABB(GVector3::Zero()) 33 | { 34 | m_Transform = GTransform_QT::Identity(); 35 | m_Transform_Last = GTransform_QT::Identity(); 36 | m_UserId = -1; 37 | m_pGridCell = nullptr; 38 | m_bNeedUpdate = true; 39 | } 40 | 41 | ECollisionObjectType GetCollisionObjectType() const 42 | { 43 | return m_CollisionType; 44 | } 45 | 46 | inline uint32_t GetId()const {return m_Id;} 47 | 48 | void UpdateAABB() 49 | { 50 | m_WorldAABB = m_Transform.TransformAABB( m_LoaclAABB ); 51 | } 52 | 53 | const GAABB& GetAABB() const { return m_WorldAABB; } 54 | 55 | 56 | void UpdateLocalBox() 57 | { 58 | m_LoaclAABB = m_Shape.GetLocalBox(); 59 | } 60 | const GAABB& GetLocalAABB() const { return m_LoaclAABB; } 61 | 62 | 63 | public: 64 | uint32_t m_Id; 65 | ECollisionObjectType m_CollisionType; 66 | GCollisionShape m_Shape; 67 | GTransform_QT m_Transform; 68 | GTransform_QT m_Transform_Last; 69 | 70 | GAABB m_LoaclAABB; 71 | GAABB m_WorldAABB; 72 | 73 | uint32_t m_UserId; 74 | 75 | class GGridCell* m_pGridCell; 76 | bool m_bNeedUpdate; 77 | 78 | }; -------------------------------------------------------------------------------- /gphysics/src/collision/glacier_collision_algorithm.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | #include "glacier_collision_algorithm.h" 15 | #include "glacier_collision_sphere.h" 16 | #include "glacier_collision_gjk.h" 17 | #include "glacier_collision_box.h" 18 | 19 | 20 | bool GCollisionAlgorithm::ProcessCollision(const GCollisionObject* ObjA, const GCollisionObject* ObjB, GCollisionContact* pContact ) 21 | { 22 | return false; 23 | } 24 | 25 | bool GCG_Sphere_Sphere::ProcessCollision(const GCollisionObject* ObjA, const GCollisionObject* ObjB, GCollisionContact* pContact ) 26 | { 27 | GVector3 d = ObjA->m_Transform.m_Translation - ObjB->m_Transform.m_Translation; 28 | f32 len2 = d.SizeSquare(); 29 | f32 TRadius = ObjA->m_Shape.GetRaiuds() + ObjB->m_Shape.GetRaiuds(); 30 | 31 | if( len2 < (TRadius * TRadius) ) 32 | { 33 | if (pContact != nullptr) 34 | { 35 | f32 InvLen = GMath::InvSqrt(len2); 36 | 37 | GVector3 VNormalOnB = len2 < GMath::Epsilon() ? GVector3::UnitX() : d * InvLen; 38 | GVector3 VPosOnB = ObjB->m_Transform.m_Translation + VNormalOnB * ObjB->m_Shape.GetRaiuds(); 39 | 40 | pContact->ClearPoint(); 41 | 42 | pContact->AddContactPoint( VPosOnB, VNormalOnB, len2 * InvLen - TRadius ); 43 | } 44 | 45 | return true; 46 | } 47 | 48 | return false; 49 | } 50 | 51 | bool GCG_Sphere_Box::ProcessCollision(const GCollisionObject* ObjA, const GCollisionObject* ObjB, GCollisionContact* pContact ) 52 | { 53 | return false; 54 | } 55 | 56 | bool GCG_Sphere_Capusle::ProcessCollision(const GCollisionObject* ObjA, const GCollisionObject* ObjB, GCollisionContact* pContact ) 57 | { 58 | return false; 59 | } 60 | 61 | bool GCG_Sphere_Cylinder::ProcessCollision(const GCollisionObject* ObjA, const GCollisionObject* ObjB, GCollisionContact* pContact ) 62 | { 63 | return false; 64 | } 65 | 66 | bool GCG_Sphere_Plane::ProcessCollision(const GCollisionObject* ObjA, const GCollisionObject* ObjB, GCollisionContact* pContact ) 67 | { 68 | return false; 69 | } 70 | 71 | bool GCG_Box_Box::ProcessCollision(const GCollisionObject* ObjA, const GCollisionObject* ObjB, GCollisionContact* pContact ) 72 | { 73 | 74 | GShapeBox BoxA(ObjA->m_Shape.GetHalfExtern()); 75 | GShapeBox BoxB(ObjB->m_Shape.GetHalfExtern()); 76 | 77 | 78 | if (GCollision_Box::Box_Box_Contact( 79 | BoxA, ObjA->m_Transform, 80 | BoxB ,ObjB->m_Transform, 81 | pContact) != 0) 82 | { 83 | 84 | return true; 85 | } 86 | 87 | return false; 88 | 89 | } 90 | 91 | 92 | GCollisionManerger::GCollisionManerger() 93 | { 94 | for (int32_t i = 0; i < EShape_Max; ++i) 95 | { 96 | for (int32_t j = 0; j < EShape_Max; ++j) 97 | { 98 | m_Algorithm[i][j] = nullptr; 99 | } 100 | } 101 | } 102 | 103 | GCollisionManerger::~GCollisionManerger() 104 | { 105 | for (int32_t i = 0; i < EShape_Max; ++i) 106 | { 107 | for (int32_t j = 0; j < EShape_Max; ++j) 108 | { 109 | if( m_Algorithm[i][j] != nullptr ) 110 | { 111 | delete m_Algorithm[i][j]; 112 | m_Algorithm[i][j] = nullptr; 113 | } 114 | } 115 | } 116 | } 117 | 118 | 119 | void GCollisionManerger::Init( ) 120 | { 121 | 122 | m_Algorithm[EShape_Sphere][EShape_Sphere] = new GCG_Sphere_Sphere(); 123 | m_Algorithm[EShape_Box][EShape_Box] = new GCG_Box_Box(); 124 | 125 | 126 | 127 | 128 | } 129 | -------------------------------------------------------------------------------- /gphysics/src/collision/glacier_collision_shape.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | #include "glacier_collision_shape.h" 15 | 16 | 17 | GAABB GCollisionShape::GetLocalBox() const 18 | { 19 | switch (ShapType) 20 | { 21 | case EShape_ConvexBase: 22 | { 23 | 24 | } 25 | break; 26 | case EShape_Sphere: 27 | { 28 | return GAABB( GVector3(-GetRaiuds()), GVector3(GetRaiuds()) ); 29 | } 30 | break; 31 | case EShape_Box: 32 | { 33 | return GAABB( -GetHalfExtern(), GetHalfExtern() ); 34 | } 35 | break; 36 | case EShape_Capsule: 37 | break; 38 | case EShape_Cylinder: 39 | break; 40 | case EShape_ConvexHull: 41 | break; 42 | case EShape_ConcaveBase: 43 | break; 44 | case EShape_Plane: 45 | break; 46 | case EShape_HightField: 47 | break; 48 | case EShape_TriangleMesh: 49 | break; 50 | case EShape_Max: 51 | break; 52 | default: 53 | break; 54 | } 55 | 56 | return GAABB(GVector3::Zero()); 57 | } 58 | -------------------------------------------------------------------------------- /gphysics/src/collision/glacier_contact.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | #include "glacier_contact.h" 15 | 16 | 17 | 18 | void GCollisionContact::AddContactPoint(const GVector3& PosOnSurfaceB_World, const GVector3& NormalOnB, f32 depth) 19 | { 20 | if(0<= m_nPointCount && m_nPointCount < (MaxPoint-1) ) 21 | { 22 | GManifoldPoint& TPoint = m_Point[m_nPointCount]; 23 | 24 | TPoint.m_PosOnSurfaceB_World = PosOnSurfaceB_World; 25 | TPoint.m_NormalOnB = NormalOnB; 26 | TPoint.m_depth = depth; 27 | } 28 | 29 | } 30 | 31 | -------------------------------------------------------------------------------- /gphysics/src/collision/glacier_distance.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | #include "glacier_distance.h" 15 | 16 | GVector3 GDistance::ClosestPointTriangle(const GVector3& p, const GVector3& a, const GVector3& b, const GVector3& c) 17 | { 18 | GVector3 ab = b - a; 19 | GVector3 ac = c - a; 20 | GVector3 ap = p - a; 21 | f32 d1 = GVector3::DotProduct(ab, ap); 22 | f32 d2 = GVector3::DotProduct(ac, ap); 23 | if (d1 <= GMath::Zero() && d2 <= GMath::Zero()) 24 | return a; // barycentric coordinates (1,0,0) 25 | 26 | // Check if P in vertex region outside B 27 | GVector3 bp = p - b; 28 | f32 d3 = GVector3::DotProduct(ab, bp); 29 | f32 d4 = GVector3::DotProduct(ac, bp); 30 | if (d3 >= GMath::Zero() && d4 <= d3) 31 | return b;// barycentric ccordinates (0,1,0) 32 | 33 | //Check if P in edge region of AB, if so return projecrion of P onto AB 34 | f32 vc = d1 * d4 - d3 * d2; 35 | if (vc <= GMath::Zero() && d1 >= GMath::Zero() && d3 <= GMath::Zero()) { 36 | f32 v = d1 / (d1 - d3); 37 | return a + v * ab; // barycentric coordinates (1-v,v,0) 38 | } 39 | 40 | // Check if P in vertex region outside C 41 | GVector3 cp = p - c; 42 | f32 d5 = GVector3::DotProduct(ab, cp); 43 | f32 d6 = GVector3::DotProduct(ac, cp); 44 | if (d6 >= GMath::Zero() && d5 <= d6) 45 | return c; // barycentric coordinates (0,0,1) 46 | 47 | // Check if P in edge region of AC, if so return projection of P onto AC 48 | f32 vb = d5 * d2 - d1 * d6; 49 | if (vb <= GMath::Zero() && d2 >= GMath::Zero() && d6 <= GMath::Zero()) { 50 | f32 w = d2 / (d2 - d6); 51 | return a + w * ac; // barycentric coordinates (1-w,0,w) 52 | } 53 | // Check if P in edge region of BC, if so return projection of P onto BC 54 | f32 va = d3 * d6 - d5 * d4; 55 | if (va <= GMath::Zero() && (d4 - d3) >= GMath::Zero() && (d5 - d6) >= GMath::Zero()) { 56 | f32 w = (d4 - d3) / ((d4 - d3) + (d5 - d6)); 57 | return b + w * (c - b); // barycentric coordinates (0,1-w,w) 58 | } 59 | // P inside face region. Compute Q through its barycentric coordinates (u,v,w) 60 | f32 denom = GMath::One() / (va + vb + vc); 61 | f32 v = vb * denom; 62 | f32 w = vc * denom; 63 | return a + ab * v + ac * w; // = u*a + v*b + w*c, u = va * denom = 1.0f-v-w 64 | } 65 | 66 | 67 | 68 | 69 | -------------------------------------------------------------------------------- /gphysics/src/dynamic/glacier_rigid_body.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | 15 | #include "glacier_rigid_body.h" 16 | -------------------------------------------------------------------------------- /gphysics/src/dynamic/glacier_rigid_dynamic.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | 15 | #include "glacier_rigid_dynamic.h" 16 | 17 | -------------------------------------------------------------------------------- /gphysics/src/dynamic/glacier_rigid_static.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | 15 | #include "glacier_rigid_static.h" 16 | -------------------------------------------------------------------------------- /gphysics/src/gphysics.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 6 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 7 | 8 | 9 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 10 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 11 | 12 | 13 | {ff9a83ed-50a3-4d93-b405-8c3475a65c28} 14 | 15 | 16 | {24fcd79e-6581-4500-bc2e-e03344394ee3} 17 | 18 | 19 | {efa63c85-62d1-42fe-9ddd-7acd5f08cb75} 20 | 21 | 22 | {0ab87c42-1df9-4ea4-9c0b-a6a74cd14b86} 23 | 24 | 25 | {7a92ed23-b1e4-4ed1-9db3-3831b04f7e40} 26 | 27 | 28 | {89a2cace-669d-4bee-b604-bd1ab35d5165} 29 | 30 | 31 | {78bb1f80-3ea2-4443-b590-7e3f172c1a11} 32 | 33 | 34 | {86905c70-5b59-4fef-ab83-53cadededa3e} 35 | 36 | 37 | {4edeccba-4e6c-42b2-85ee-a19d47664f0c} 38 | 39 | 40 | {8267621f-5005-4fe1-9c24-cc2917f38932} 41 | 42 | 43 | 44 | 45 | include\math 46 | 47 | 48 | include\math 49 | 50 | 51 | include\math 52 | 53 | 54 | include\math 55 | 56 | 57 | include\collision 58 | 59 | 60 | include\collision 61 | 62 | 63 | include\utils 64 | 65 | 66 | include\collision 67 | 68 | 69 | include\collision 70 | 71 | 72 | include\math 73 | 74 | 75 | include\world 76 | 77 | 78 | include\world 79 | 80 | 81 | include\dynamic 82 | 83 | 84 | include\dynamic 85 | 86 | 87 | include\dynamic 88 | 89 | 90 | 91 | 92 | src\math 93 | 94 | 95 | src\math 96 | 97 | 98 | src\math 99 | 100 | 101 | src\collision 102 | 103 | 104 | src\collision 105 | 106 | 107 | src\utils 108 | 109 | 110 | src\collision 111 | 112 | 113 | src\world 114 | 115 | 116 | src\dynamic 117 | 118 | 119 | src\dynamic 120 | 121 | 122 | src\dynamic 123 | 124 | 125 | -------------------------------------------------------------------------------- /gphysics/src/math/glacier_quternion.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | #include "glacier_quaternion.h" 14 | 15 | void GQuaternion::Test() 16 | { 17 | 18 | } -------------------------------------------------------------------------------- /gphysics/src/math/glacier_vector.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | #include "glacier_vector.h" 14 | 15 | void GVector3::Test() 16 | { 17 | 18 | } -------------------------------------------------------------------------------- /gphysics/src/utils/glacier_physics_utils.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | #include "glacier_physics_utils.h" 14 | #include "glacier_plane.h" 15 | #include "glacier_collision_shape.h" 16 | #include "glacier_debug_draw.h" 17 | 18 | void GPhyscsUtils::DrawShape(const GTransform_QT& Trans, const GCollisionShape& pShape, IGlacierDraw* pDebugDraw ) 19 | { 20 | switch (pShape.ShapType ) 21 | { 22 | case EShape::EShape_ConvexBase: 23 | { 24 | 25 | } 26 | break; 27 | case EShape::EShape_Sphere: 28 | { 29 | pDebugDraw->DrawSphere(Trans, pShape.GetRaiuds(), GColor::White(), 18 ); 30 | } 31 | break; 32 | case EShape::EShape_Box: 33 | { 34 | pDebugDraw->DrawBox(Trans, GVector3::Zero(), pShape.GetHalfExtern(), GColor::White()); 35 | } 36 | break; 37 | case EShape::EShape_Capsule: 38 | { 39 | pDebugDraw->DrawCapsule(Trans, pShape.GetRaiuds(), pShape.GetHalfHeight(), GColor::White()); 40 | } 41 | break; 42 | case EShape::EShape_Cylinder: 43 | { 44 | 45 | } 46 | break; 47 | case EShape::EShape_ConvexHull: 48 | { 49 | 50 | } 51 | break; 52 | 53 | case EShape::EShape_ConcaveBase: 54 | { 55 | 56 | } 57 | break; 58 | case EShape::EShape_Plane: 59 | { 60 | pDebugDraw->DrawPlane(Trans, GPlane( pShape.GetPlaneNormal(), GMath::Zero() ), f32(50), GColor::White()); 61 | } 62 | break; 63 | case EShape::EShape_HightField: 64 | { 65 | 66 | } 67 | break; 68 | case EShape::EShape_TriangleMesh: 69 | { 70 | 71 | } 72 | break; 73 | 74 | default: 75 | break; 76 | } 77 | 78 | } 79 | 80 | -------------------------------------------------------------------------------- /gphysics/src/utils/glacier_time.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 zhou xuan, Email: zhouxuan6676@gmail.com 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at * 6 | * http://www.apache.org/licenses/LICENSE-2.0 * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | #include "glacier_time.h" 15 | #include 16 | #include 17 | 18 | #if UseProfiler_RDTSCP 19 | 20 | static double CountCpuGhz() { 21 | 22 | Myclock::time_point tStart = Myclock::now();; 23 | uint64_t uStart = GTimer::get_CPUCycles(); 24 | 25 | std::this_thread::sleep_for(std::chrono::milliseconds(10)); 26 | 27 | uint64_t uEnd = GTimer::get_CPUCycles(); 28 | Myclock::time_point tEnd = Myclock::now(); 29 | 30 | double time = double(std::chrono::duration_cast(tEnd - tStart).count() * 1e-9); 31 | 32 | double CpuGhz = double(uEnd - uStart) / (time * 1000000000); 33 | return CpuGhz; 34 | 35 | } 36 | 37 | double GTimer::InvCPUGHZ = 0.000001f / CountCpuGhz(); 38 | #endif 39 | 40 | static std::map ms_TotalTime; 41 | 42 | GTimeProfiler::GTimeProfiler(const char* Str ) 43 | { 44 | m_Str = Str; 45 | m_Time.Start(); 46 | } 47 | GTimeProfiler::~GTimeProfiler() 48 | { 49 | 50 | } 51 | 52 | void GTimeProfiler::EndCuptrue() 53 | { 54 | char name[128]; 55 | float fTime = (float)m_Time.GetDeltaTimeMS() * 1000.f; 56 | sprintf(name, "Time:% 8.3fms %s\n", fTime, m_Str.c_str()); 57 | 58 | std::map::iterator iterm = ms_TotalTime.find(m_Str); 59 | if (iterm != ms_TotalTime.end()) 60 | { 61 | iterm->second += fTime; 62 | } 63 | else 64 | { 65 | ms_TotalTime[m_Str] = fTime; 66 | } 67 | 68 | 69 | if (m_bOutToScreen) 70 | { 71 | //g_GlobalSystem.FontToScreen( name ); 72 | } 73 | else 74 | { 75 | // OutputDebugStringA( name ); 76 | } 77 | 78 | m_bCapture = true; 79 | } 80 | 81 | void GTimeProfiler::ClearTime() 82 | { 83 | std::map::iterator iterm = ms_TotalTime.begin(); 84 | for (; iterm != ms_TotalTime.end(); ++iterm) 85 | { 86 | iterm->second = 0; 87 | } 88 | } 89 | 90 | void GTimeProfiler::DebugOut() 91 | { 92 | char name[128]; 93 | std::map::iterator iterm = ms_TotalTime.begin(); 94 | for (; iterm != ms_TotalTime.end(); ++iterm) 95 | { 96 | sprintf(name, "Time:% 8.3fms %s\n", iterm->second, iterm->first.c_str()); 97 | // OutputDebugStringA(name); 98 | } 99 | 100 | //OutputDebugStringA("=========================================\n"); 101 | } 102 | -------------------------------------------------------------------------------- /samples/test.cpp: -------------------------------------------------------------------------------- 1 |  2 | 3 | #include 4 | #include "glacier_time.h" 5 | #include "glacier_distance.h" 6 | #include "glacier_collision_gjk.h" 7 | #include "glacier_collision_sphere.h" 8 | 9 | 10 | int main() 11 | { 12 | 13 | GVector3 Point( f32(1),f32(1), f32(1) ); 14 | GVector3 S0(f32(2), f32(1), f32(1)); 15 | GVector3 S1(f32(4), f32(1), f32(1)); 16 | GVector3 S2(f32(3), f32(7), f32(9)); 17 | 18 | int ntotal = 0; 19 | 20 | GTimer T;T.Start(); 21 | 22 | for( int i = 0; i < 20000000; i ++ ) 23 | { 24 | Point.x += GMath::One(); 25 | //GVector3 b = GDistance::ClosestPointTriangle(Point, S0 , S1, S2); 26 | 27 | GVector3 t(GMath::One()); 28 | if( GCollision_Sphere::Sphere_Box( Point, GMath::One(), S0, S1,t, nullptr) ) 29 | { 30 | ntotal += t.x.rawint32; 31 | } 32 | 33 | // ntotal += b.Max().rawint32; 34 | } 35 | 36 | T.End(); 37 | 38 | 39 | 40 | std::cout << "time : " << T.GetDeltaTimeMS() << " ms " < 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /solutions/deterministic_physics.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.1.32319.34 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "gphysics", "..\gphysics\src\gphysics.vcxproj", "{35E517C1-5EA3-45E7-A200-93CD2EEE0D80}" 7 | EndProject 8 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test", "..\samples\test.vcxproj", "{762B2695-75AC-4F6B-BBBC-D867AC9FFE3A}" 9 | ProjectSection(ProjectDependencies) = postProject 10 | {35E517C1-5EA3-45E7-A200-93CD2EEE0D80} = {35E517C1-5EA3-45E7-A200-93CD2EEE0D80} 11 | EndProjectSection 12 | EndProject 13 | Global 14 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 15 | Debug|x64 = Debug|x64 16 | Debug|x86 = Debug|x86 17 | Release|x64 = Release|x64 18 | Release|x86 = Release|x86 19 | EndGlobalSection 20 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 21 | {35E517C1-5EA3-45E7-A200-93CD2EEE0D80}.Debug|x64.ActiveCfg = Debug|x64 22 | {35E517C1-5EA3-45E7-A200-93CD2EEE0D80}.Debug|x64.Build.0 = Debug|x64 23 | {35E517C1-5EA3-45E7-A200-93CD2EEE0D80}.Debug|x86.ActiveCfg = Debug|Win32 24 | {35E517C1-5EA3-45E7-A200-93CD2EEE0D80}.Debug|x86.Build.0 = Debug|Win32 25 | {35E517C1-5EA3-45E7-A200-93CD2EEE0D80}.Release|x64.ActiveCfg = Release|x64 26 | {35E517C1-5EA3-45E7-A200-93CD2EEE0D80}.Release|x64.Build.0 = Release|x64 27 | {35E517C1-5EA3-45E7-A200-93CD2EEE0D80}.Release|x86.ActiveCfg = Release|Win32 28 | {35E517C1-5EA3-45E7-A200-93CD2EEE0D80}.Release|x86.Build.0 = Release|Win32 29 | {762B2695-75AC-4F6B-BBBC-D867AC9FFE3A}.Debug|x64.ActiveCfg = Debug|x64 30 | {762B2695-75AC-4F6B-BBBC-D867AC9FFE3A}.Debug|x64.Build.0 = Debug|x64 31 | {762B2695-75AC-4F6B-BBBC-D867AC9FFE3A}.Debug|x86.ActiveCfg = Debug|Win32 32 | {762B2695-75AC-4F6B-BBBC-D867AC9FFE3A}.Debug|x86.Build.0 = Debug|Win32 33 | {762B2695-75AC-4F6B-BBBC-D867AC9FFE3A}.Release|x64.ActiveCfg = Release|x64 34 | {762B2695-75AC-4F6B-BBBC-D867AC9FFE3A}.Release|x64.Build.0 = Release|x64 35 | {762B2695-75AC-4F6B-BBBC-D867AC9FFE3A}.Release|x86.ActiveCfg = Release|Win32 36 | {762B2695-75AC-4F6B-BBBC-D867AC9FFE3A}.Release|x86.Build.0 = Release|Win32 37 | EndGlobalSection 38 | GlobalSection(SolutionProperties) = preSolution 39 | HideSolutionNode = FALSE 40 | EndGlobalSection 41 | GlobalSection(ExtensibilityGlobals) = postSolution 42 | SolutionGuid = {B97BBC5A-61C6-4E33-A2B6-22E56A010B23} 43 | EndGlobalSection 44 | EndGlobal 45 | --------------------------------------------------------------------------------