-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
226 lines (181 loc) · 8.69 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
using MagicPhysX;
using MagicPhysX.Toolkit;
using static MagicPhysX.NativeMethods;
using System.Numerics;
using System.Diagnostics;
using System.Runtime.CompilerServices;
namespace MagicPhysX_Test
{
internal class Program
{
static Physics physics = new Physics();
static Thread? thread;
static bool cleaning = false;
static void Main(string[] args)
{
physics.Initialize();
Console.WriteLine("Hello, World!");
thread = new Thread(Update);
thread.Start();
while (true)
{
string? command = Console.ReadLine();
if (!string.IsNullOrEmpty(command))
{
{
cleaning = true;
thread.Interrupt();
thread.Join();
physics.Cleanup();
return;
}
}
}
}
static void Update()
{
while (!cleaning)
physics.Update();
}
public static void Print(string message)
{
Console.WriteLine(message);
}
}
public unsafe class Physics
{
Stopwatch stopwatch = new Stopwatch();
Stopwatch fpsStopwatch = new Stopwatch();
Random random = new Random();
PxFoundation* foundation;
PxPhysics* physics;
PxDefaultCpuDispatcher* dispatcher;
PxScene* scene;
PxMaterial* material;
int fps = 0;
public void Initialize()
{
foundation = physx_create_foundation();
PxPvd* pvd = phys_PxCreatePvd(foundation);
fixed (byte* bytePointer = "127.0.0.1"u8.ToArray())
{
var transport = phys_PxDefaultPvdSocketTransportCreate(bytePointer, 5425, 10);
pvd->ConnectMut(transport, PxPvdInstrumentationFlags.All);
}
uint PX_PHYSICS_VERSION_MAJOR = 5;
uint PX_PHYSICS_VERSION_MINOR = 1;
uint PX_PHYSICS_VERSION_BUGFIX = 3;
uint versionNumber = (PX_PHYSICS_VERSION_MAJOR << 24) + (PX_PHYSICS_VERSION_MINOR << 16) + (PX_PHYSICS_VERSION_BUGFIX << 8);
var tolerancesScale = new PxTolerancesScale { length = 1, speed = 10 };
physics = phys_PxCreatePhysics(versionNumber, foundation, &tolerancesScale, true, pvd, null);
phys_PxInitExtensions(physics, pvd);
PxSceneDesc sceneDesc = PxSceneDesc_new(PxPhysics_getTolerancesScale(physics));
sceneDesc.gravity = new PxVec3 { x = 0f, y = -9.81f, z = 0f };
dispatcher = phys_PxDefaultCpuDispatcherCreate(1, null, PxDefaultCpuDispatcherWaitForWorkMode.WaitForWork, 0);
sceneDesc.cpuDispatcher = (PxCpuDispatcher*)dispatcher;
sceneDesc.filterShader = get_default_simulation_filter_shader();
scene = physics->CreateSceneMut(&sceneDesc);
material = physics->CreateMaterialMut(0.5f, 0.5f, 0.6f);
scene->GetScenePvdClientMut()->SetScenePvdFlagMut(PxPvdSceneFlag.TransmitScenequeries, true);
var pvdClient = scene->GetScenePvdClientMut();
if (pvdClient != null)
{
pvdClient->SetScenePvdFlagMut(PxPvdSceneFlag.TransmitConstraints, true);
pvdClient->SetScenePvdFlagMut(PxPvdSceneFlag.TransmitContacts, true);
pvdClient->SetScenePvdFlagMut(PxPvdSceneFlag.TransmitScenequeries, true);
}
PxPlane plane = PxPlane_new_1(0f, 1f, 0f, 0f);
PxRigidStatic* groundPlane = physics->PhysPxCreatePlane(&plane, material);
scene->AddActorMut((PxActor*)groundPlane, null);
PxSphereGeometry sphereGeo = PxSphereGeometry_new(0.4f);
PxVec3 zero = new PxVec3 { x = 0f, y = 0f, z = 0f };
PxTransform shapeOffset = PxTransform_new_1(&zero);
for (int i = 0; i < 100; i++)
{
//PxVec3 vec3 = new PxVec3 { x = random.NextSingle() * 10f, y = random.NextSingle() * 10f, z = random.NextSingle() * 10f };
PxVec3 vec3 = new PxVec3 { x = 0f, y = 1f, z = -50 + i * 1f };
PxTransform transform = PxTransform_new_1(&vec3);
PxTransform identity = PxTransform_new_2(PxIDENTITY.PxIdentity);
PxRigidStatic* sphere = physics->PhysPxCreateStatic(&transform, (PxGeometry*)&sphereGeo, material, &shapeOffset);
//PxRigidBody_setAngularDamping_mut((PxRigidBody*)sphere, 0.5f);
scene->AddActorMut((PxActor*)sphere, null);
}
for (int i = 0; i < 10; i++)
{
PxVec3 vec3 = new PxVec3 { x = random.NextSingle() * 10f, y = random.NextSingle() * 10f, z = random.NextSingle() * 10f };
//PxVec3 vec3 = new PxVec3 { x = 0f, y = 1f, z = -50 + i * 1f };
PxTransform transform = PxTransform_new_1(&vec3);
PxTransform identity = PxTransform_new_2(PxIDENTITY.PxIdentity);
//PxRigidStatic* sphere = physics->PhysPxCreateStatic(&transform, (PxGeometry*)&sphereGeo, material, &shapeOffset);
//PxRigidBody_setAngularDamping_mut((PxRigidBody*)sphere, 0.5f);
PxRigidDynamic* sphere = physics->PhysPxCreateDynamic(&transform, (PxGeometry*)&sphereGeo, material, 10f, &shapeOffset);
scene->AddActorMut((PxActor*)sphere, null);
}
stopwatch.Start();
fpsStopwatch.Start();
}
public void Cleanup()
{
PxScene_release_mut(scene);
PxDefaultCpuDispatcher_release_mut(dispatcher);
PxPhysics_release_mut(physics);
}
public void Update()
{
fps++;
if (fpsStopwatch.Elapsed.TotalSeconds >= 1d)
{
Program.Print($"FPS : {fps}");
fps = 0;
fpsStopwatch.Restart();
RaycastTest();
}
if (stopwatch.Elapsed.TotalSeconds < 0.02d)
return;
float delta = (float)stopwatch.Elapsed.TotalSeconds;
stopwatch.Restart();
scene->SimulateMut(delta, null, null, 0, true);
uint error = 0;
scene->FetchResultsMut(true, &error);
}
public void RaycastTest()
{
//PxVec3 origin = new PxVec3 { x = random.NextSingle() * 10f, y = random.NextSingle() * 10f, z = random.NextSingle() * 10f };
//PxVec3 direction = new PxVec3 { x = random.NextSingle() * 10f, y = random.NextSingle() * 10f, z = random.NextSingle() * 10f };
//PxVec3 origin = new PxVec3 { x = 0f, y = 1f, z = -55f };
PxVec3 origin = new PxVec3 { x = -1f, y = 1f, z = 0f };
PxVec3 direction = new PxVec3 { x = random.NextSingle(), y = 0f, z = random.NextSingle() };
//PxVec3 direction = new PxVec3 { x = 0f, y = 0f, z = 1f };
direction = direction.GetNormalized();
PxHitFlags outputFlags1 = PxHitFlags.Position;
PxQueryFilterData filterData1 = PxQueryFilterData_new();
//filterData1.flags = PxQueryFlags.NoBlock;
PxRaycastHit[] hitInfo = new PxRaycastHit[128];
bool block = true;
void* blockPtr = █
fixed (PxRaycastHit* hitInfoPtr = &hitInfo[0])
{
int result1 = scene->QueryExtRaycastMultiple((PxVec3*)Unsafe.AsPointer(ref origin), (PxVec3*)Unsafe.AsPointer(ref direction), 128f, outputFlags1, hitInfoPtr, 128u, (bool*)blockPtr, &filterData1, null, null);
//bool result1 = scene->QueryExtRaycastSingle((PxVec3*)Unsafe.AsPointer(ref origin), (PxVec3*)Unsafe.AsPointer(ref direction), 100f, outputFlags1, &hitInfo, &filterData1, null, null);
Program.Print($"Raycast result1: {result1}");
Program.Print($"Block: {block}");
for (int i = 0; i < result1; i++)
{
PxRaycastHit hit = hitInfo[i];
Program.Print($"[{i}] hit.position=x:{hit.position.x}\ty:{hit.position.y}\tz:{hit.position.z}");
if (i > 0)
{
PxRaycastHit prvHit = hitInfo[i - 1];
if (prvHit.position.x == hit.position.x && prvHit.position.y == hit.position.y && prvHit.position.z == hit.position.z)
{
Program.Print("");
Program.Print($"Same Position !!!!!!!!!!!!!!!!!!!");
Program.Print("");
Thread.Sleep(5000);
}
}
}
}
}
}
}