-
Notifications
You must be signed in to change notification settings - Fork 183
/
Newton.cpp
8763 lines (6751 loc) · 329 KB
/
Newton.cpp
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) <2003-2016> <Julio Jerez, Newton Game Dynamics>
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution.
*/
#include "NewtonStdAfx.h"
#include "Newton.h"
#include "NewtonClass.h"
#ifdef _NEWTON_BUILD_DLL
#if (defined (_MINGW_32_VER) || defined (_MINGW_64_VER))
int main(int argc, char* argv[])
{
return 0;
}
#endif
#ifdef _MSC_VER
BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_THREAD_ATTACH:
case DLL_PROCESS_ATTACH:
// check for memory leaks
#ifdef _DEBUG
// Track all memory leaks at the operating system level.
// make sure no Newton tool or utility leaves leaks behind.
_CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF));
#endif
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
#endif
#endif
/*! @defgroup Misc Misc
Misc
@{
*/
//#define SAVE_COLLISION
#ifdef SAVE_COLLISION
void SerializeFile (void* serializeHandle, const void* buffer, size_t size)
{
fwrite (buffer, size, 1, (FILE*) serializeHandle);
}
void DeSerializeFile (void* serializeHandle, void* buffer, size_t size)
{
fread (buffer, size, 1, (FILE*) serializeHandle);
}
void SaveCollision (const NewtonCollision* const collisionPtr)
{
FILE* file;
// save the collision file
file = fopen ("collisiontest.bin", "wb");
//SerializeFile (file, MAGIC_NUMBER, strlen (MAGIC_NUMBER) + 1);
NewtonCollisionSerialize (collisionPtr, SerializeFile, file);
fclose (file);
}
#endif
/*!
Return the exact amount of memory (in Bytes) use by the engine at any given time.
@return total memory use by the engine.
Applications can use this function to ascertain that the memory use by the
engine is balanced at all times.
See also: ::NewtonCreate
*/
int NewtonGetMemoryUsed()
{
TRACE_FUNCTION(__FUNCTION__);
return dgMemoryAllocator::GetGlobalMemoryUsed();
}
// fixme: needs docu
// @param mallocFnt is a pointer to the memory allocator callback function. If this parameter is NULL the standard *malloc* function is used.
// @param mfreeFnt is a pointer to the memory release callback function. If this parameter is NULL the standard *free* function is used.
//
void NewtonSetMemorySystem (NewtonAllocMemory mallocFnt, NewtonFreeMemory mfreeFnt)
{
dgMemFree _free;
dgMemAlloc _malloc;
TRACE_FUNCTION(__FUNCTION__);
if (mallocFnt && mfreeFnt) {
_malloc = (dgMemAlloc) mallocFnt;
_free = (dgMemFree) mfreeFnt;
} else {
_malloc = (dgMemAlloc) Newton::DefaultAllocMemory;
_free = (dgMemFree) Newton::DefaultFreeMemory;
}
dgMemoryAllocator::SetGlobalAllocators (_malloc, _free);
}
void* NewtonAlloc (int sizeInBytes)
{
return dgMallocStack(sizeInBytes);
}
void NewtonFree (void* const ptr)
{
dgFreeStack(ptr);
}
/*! @} */ // end of group Misc
/*! @defgroup World World
World interface
@{
*/
/*!
Create an instance of the Newton world.
@return Pointer to new Newton world.
This function must be called before any of the other API functions.
See also: ::NewtonDestroy, ::NewtonDestroyAllBodies
*/
NewtonWorld* NewtonCreate()
{
TRACE_FUNCTION(__FUNCTION__);
dgMemoryAllocator* const allocator = new dgMemoryAllocator();
NewtonWorld* const world = (NewtonWorld*) new (allocator) Newton (allocator);
return world;
}
/*!
Destroy an instance of the Newton world.
@param *newtonWorld Pointer to the Newton world.
@return Nothing.
This function will destroy the entire Newton world.
See also: ::NewtonCreate, ::NewtonDestroyAllBodies
*/
void NewtonDestroy(const NewtonWorld* const newtonWorld)
{
TRACE_FUNCTION(__FUNCTION__);
Newton* const world = (Newton *) newtonWorld;
dgMemoryAllocator* const allocator = world->dgWorld::GetAllocator();
delete world;
delete allocator;
}
void NewtonSetPostUpdateCallback(const NewtonWorld* const newtonWorld, NewtonPostUpdateCallback callback)
{
TRACE_FUNCTION(__FUNCTION__);
Newton* const world = (Newton *) newtonWorld;
world->SetPostUpdateCallback(world, (dgPostUpdateCallback) callback);
}
int NewtonGetBroadphaseAlgorithm (const NewtonWorld* const newtonWorld)
{
TRACE_FUNCTION(__FUNCTION__);
Newton* const world = (Newton *) newtonWorld;
return world->GetBroadPhaseType();
}
void NewtonSelectBroadphaseAlgorithm (const NewtonWorld* const newtonWorld, int algorithmType)
{
TRACE_FUNCTION(__FUNCTION__);
Newton* const world = (Newton *) newtonWorld;
world->SetBroadPhaseType(algorithmType);
}
void NewtonResetBroadphase(const NewtonWorld* const newtonWorld)
{
TRACE_FUNCTION(__FUNCTION__);
Newton* const world = (Newton *) newtonWorld;
return world->ResetBroadPhase();
}
dFloat NewtonGetContactMergeTolerance (const NewtonWorld* const newtonWorld)
{
TRACE_FUNCTION(__FUNCTION__);
Newton* const world = (Newton *) newtonWorld;
return world->GetContactMergeTolerance();
}
void NewtonSetContactMergeTolerance (const NewtonWorld* const newtonWorld, dFloat tolerance)
{
TRACE_FUNCTION(__FUNCTION__);
Newton* const world = (Newton *) newtonWorld;
world->SetContactMergeTolerance(tolerance);
}
/*!
Reset all internal engine states.
@param *newtonWorld Pointer to the Newton world.
Call this function whenever you want to create a reproducible simulation from
a pre-defined initial condition.
It does *not* suffice to merely reset the position and velocity of
objects. This is because Newton takes advantage of frame-to-frame coherence for
performance reasons.
This function must be called outside of a Newton Update.
Note: this kind of synchronization incurs a heavy performance penalty if
called during each update.
See also: ::NewtonUpdate
*/
void NewtonInvalidateCache(const NewtonWorld* const newtonWorld)
{
TRACE_FUNCTION(__FUNCTION__);
Newton* const world = (Newton *)newtonWorld;
world->FlushCache();
}
void NewtonSetJointSerializationCallbacks (const NewtonWorld* const newtonWorld, NewtonOnJointSerializationCallback serializeJoint, NewtonOnJointDeserializationCallback deserializeJoint)
{
TRACE_FUNCTION(__FUNCTION__);
Newton* const world = (Newton *) newtonWorld;
world->SetJointSerializationCallbacks (dgWorld::OnJointSerializationCallback(serializeJoint), dgWorld::OnJointDeserializationCallback(deserializeJoint));
}
void NewtonGetJointSerializationCallbacks (const NewtonWorld* const newtonWorld, NewtonOnJointSerializationCallback* const serializeJoint, NewtonOnJointDeserializationCallback* const deserializeJoint)
{
TRACE_FUNCTION(__FUNCTION__);
Newton* const world = (Newton *) newtonWorld;
world->GetJointSerializationCallbacks ((dgWorld::OnJointSerializationCallback*)serializeJoint, (dgWorld::OnJointDeserializationCallback*)deserializeJoint);
}
void NewtonSerializeScene(const NewtonWorld* const newtonWorld, NewtonOnBodySerializationCallback bodyCallback, void* const bodyUserData,
NewtonSerializeCallback serializeCallback, void* const serializeHandle)
{
TRACE_FUNCTION(__FUNCTION__);
Newton* const world = (Newton *) newtonWorld;
world->SerializeScene(bodyUserData, dgWorld::OnBodySerialize(bodyCallback), (dgSerialize) serializeCallback, serializeHandle);
}
void NewtonDeserializeScene(const NewtonWorld* const newtonWorld, NewtonOnBodyDeserializationCallback bodyCallback, void* const bodyUserData,
NewtonDeserializeCallback deserializeCallback, void* const serializeHandle)
{
TRACE_FUNCTION(__FUNCTION__);
Newton* const world = (Newton *) newtonWorld;
world->DeserializeScene(bodyUserData, (dgWorld::OnBodyDeserialize)bodyCallback, (dgDeserialize) deserializeCallback, serializeHandle);
}
void NewtonSerializeToFile (const NewtonWorld* const newtonWorld, const char* const filename, NewtonOnBodySerializationCallback bodyCallback, void* const bodyUserData)
{
TRACE_FUNCTION(__FUNCTION__);
FILE* const file = fopen(filename, "wb");
if (file) {
NewtonSerializeScene(newtonWorld, bodyCallback, bodyUserData, dgWorld::OnSerializeToFile, file);
fclose (file);
}
}
void NewtonDeserializeFromFile (const NewtonWorld* const newtonWorld, const char* const filename, NewtonOnBodyDeserializationCallback bodyCallback, void* const bodyUserData)
{
TRACE_FUNCTION(__FUNCTION__);
FILE* const file = fopen(filename, "rb");
if (file) {
NewtonDeserializeScene(newtonWorld, bodyCallback, bodyUserData, dgWorld::OnDeserializeFromFile, file);
fclose (file);
}
}
NewtonBody* NewtonFindSerializedBody(const NewtonWorld* const newtonWorld, int bodySerializedID)
{
TRACE_FUNCTION(__FUNCTION__);
Newton* const world = (Newton *) newtonWorld;
dgAssert (0);
return (NewtonBody*) world->FindBodyFromSerializedID(bodySerializedID);
}
void* NewtonCurrentPlugin(const NewtonWorld* const newtonWorld)
{
TRACE_FUNCTION(__FUNCTION__);
Newton* const world = (Newton *)newtonWorld;
return world->GetCurrentPlugin();
}
void* NewtonGetFirstPlugin(const NewtonWorld* const newtonWorld)
{
TRACE_FUNCTION(__FUNCTION__);
Newton* const world = (Newton *)newtonWorld;
return world->GetFirstPlugin();
}
void* NewtonGetNextPlugin(const NewtonWorld* const newtonWorld, const void* const plugin)
{
TRACE_FUNCTION(__FUNCTION__);
Newton* const world = (Newton *)newtonWorld;
dgWorldPluginList::dgListNode* const node = (dgWorldPluginList::dgListNode*) plugin;
return world->GetNextPlugin(node);
}
const char* NewtonGetPluginString(const NewtonWorld* const newtonWorld, const void* const plugin)
{
TRACE_FUNCTION(__FUNCTION__);
Newton* const world = (Newton *)newtonWorld;
dgWorldPluginList::dgListNode* const node = (dgWorldPluginList::dgListNode*) plugin;
return world->GetPluginId (node);
}
void NewtonSelectPlugin(const NewtonWorld* const newtonWorld, const void* const plugin)
{
TRACE_FUNCTION(__FUNCTION__);
Newton* const world = (Newton *)newtonWorld;
dgWorldPluginList::dgListNode* const node = (dgWorldPluginList::dgListNode*) plugin;
return world->SelectPlugin(node);
}
void* NewtonGetPreferedPlugin(const NewtonWorld* const newtonWorld)
{
TRACE_FUNCTION(__FUNCTION__);
Newton* const world = (Newton *)newtonWorld;
return world->GetpreferedPlugin();
}
/*!
this function block all other threads from executing the same subsequent code simultaneously.
@param *newtonWorld Pointer to the Newton world.
@param threadIndex thread index from whe thsi function is called, zero if call form outsize a newton update
this function should use to present racing conditions when when a call back ins executed form a mutithreaded loop.
In general most call back are thread safe when they do not write to object outside the scope of the call back.
this means for example that the application can modify values of object pointed by the arguments and or call that function
that are allowed to be call for such callback.
There are cases, however, when the application need to collect data for the client logic, example of such case are collecting
information to display debug information, of collecting data for feedback.
In these situations it is possible the the same critical code could be execute at the same time but several thread causing unpredictable side effect.
so it is necessary to block all of the thread from executing any pieces of critical code.
Not calling function *NewtonWorldCriticalSectionUnlock* will result on the engine going into an infinite loop.
it is important that the critical section wrapped by functions *NewtonWorldCriticalSectionLock* and
*NewtonWorldCriticalSectionUnlock* be keep small if the application is using the multi threaded functionality of the engine
no doing so will lead to serialization of the parallel treads since only one thread can run the a critical section at a time.
@return Nothing.
See also: ::NewtonWorldCriticalSectionUnlock
*/
void NewtonWorldCriticalSectionLock (const NewtonWorld* const newtonWorld, int threadIndex)
{
TRACE_FUNCTION(__FUNCTION__);
Newton* const world = (Newton *)newtonWorld;
world->GlobalLock();
}
int NewtonAtomicSwap (int* const ptr, int value)
{
TRACE_FUNCTION(__FUNCTION__);
return dgInterlockedExchange(ptr, value);
}
int NewtonAtomicAdd (int* const ptr, int value)
{
TRACE_FUNCTION(__FUNCTION__);
return dgAtomicExchangeAndAdd (ptr, value);
}
void NewtonYield ()
{
TRACE_FUNCTION(__FUNCTION__);
dgThreadYield();
}
/*!
this function block all other threads from executing the same subsequent code simultaneously.
@param *newtonWorld Pointer to the Newton world.
this function should use to present racing conditions when when a call back ins executed form a multi threaded loop.
In general most call back are thread safe when they do not write to object outside the scope of the call back.
this means for example that the application can modify values of object pointed by the arguments and or call that function
that are allowed to be call for such callback.
There are cases, however, when the application need to collect data for the client logic, example of such case are collecting
information to display debug information, of collecting data for feedback.
In these situations it is possible the the same critical code could be execute at the same time but several thread causing unpredictable side effect.
so it is necessary to block all of the thread from executing any pieces of critical code.
it is important that the critical section wrapped by functions *NewtonWorldCriticalSectionLock* and
*NewtonWorldCriticalSectionUnlock* be keep small if the application is using the multi threaded functionality of the engine
no doing so will lead to serialization of the parallel treads since only one thread can run the a critical section at a time.
@return Nothing.
See also: ::NewtonWorldCriticalSectionLock
*/
void NewtonWorldCriticalSectionUnlock(const NewtonWorld* const newtonWorld)
{
TRACE_FUNCTION(__FUNCTION__);
Newton* const world = (Newton *)newtonWorld;
world->GlobalUnlock();
}
/*!
Set the maximum number of threads the engine can use.
@param *newtonWorld Pointer to the Newton world.
@param threads Maximum number of allowed threads.
@return Nothing
The maximum number of threaded is set on initialization to the maximum number
of CPU in the system.
fixme: this appears to be wrong. It is set to 1.
See also: ::NewtonGetThreadsCount
*/
void NewtonSetThreadsCount(const NewtonWorld* const newtonWorld, int threads)
{
TRACE_FUNCTION(__FUNCTION__);
Newton* const world = (Newton *)newtonWorld;
world->SetThreadsCount(threads);
}
/*!
Return the number of threads currently used by the engine.
@param *newtonWorld Pointer to the Newton world.
@return Number threads
See also: ::NewtonSetThreadsCount, ::NewtonSetMultiThreadSolverOnSingleIsland
*/
int NewtonGetThreadsCount(const NewtonWorld* const newtonWorld)
{
TRACE_FUNCTION(__FUNCTION__);
Newton* const world = (Newton *)newtonWorld;
return world->GetThreadCount();
}
/*!
Return the maximum number of threads supported on this platform.
@param *newtonWorld Pointer to the Newton world.
@return Number threads.
This function will return 1 on single core version of the library.
// fixme; what is a single core version?
See also: ::NewtonSetThreadsCount, ::NewtonSetMultiThreadSolverOnSingleIsland
*/
int NewtonGetMaxThreadsCount(const NewtonWorld* const newtonWorld)
{
TRACE_FUNCTION(__FUNCTION__);
Newton* const world = (Newton *)newtonWorld;
return world->GetMaxThreadCount();
}
/*!
Enable/disable multi-threaded constraint resolution for large islands
(disabled by default).
@param *newtonWorld Pointer to the Newton world.
@param mode 1: enabled 0: disabled (default)
@return Nothing
Multi threaded mode is not always faster. Among the reasons are
1 - Significant software cost to set up threads, as well as instruction overhead.
2 - Different systems have different cost for running separate threads in a shared memory environment.
3 - Parallel algorithms often have decreased converge rate. This can be as
high as half of the of the sequential version. Consequently, the parallel
solver requires a higher number of interactions to achieve similar convergence.
It is recommended this option is enabled on system with more than two cores,
since the performance gain in a dual core system are marginally better. Your
mileage may vary.
At the very least the application must test the option to verify the performance gains.
This option has no impact on other subsystems of the engine.
See also: ::NewtonGetThreadsCount, ::NewtonSetThreadsCount
*/
void NewtonSetMultiThreadSolverOnSingleIsland(const NewtonWorld* const newtonWorld, int mode)
{
TRACE_FUNCTION(__FUNCTION__);
Newton* const world = (Newton *)newtonWorld;
world->EnableThreadOnSingleIsland (mode);
}
void NewtonDispachThreadJob(const NewtonWorld* const newtonWorld, NewtonJobTask task, void* const usedData)
{
TRACE_FUNCTION(__FUNCTION__);
Newton* const world = (Newton *)newtonWorld;
world->ExecuteUserJob (dgWorkerThreadTaskCallback (task), usedData);
}
void NewtonSyncThreadJobs(const NewtonWorld* const newtonWorld)
{
Newton* const world = (Newton *)newtonWorld;
world->SynchronizationBarrier();
}
int NewtonGetMultiThreadSolverOnSingleIsland(const NewtonWorld* const newtonWorld)
{
TRACE_FUNCTION(__FUNCTION__);
Newton* const world = (Newton *)newtonWorld;
return world->GetThreadOnSingleIsland();
}
/*!
Set the solver precision mode.
@param *newtonWorld is the pointer to the Newton world
@param model model of operation n = number of iteration default value is 4.
@return Nothing
n: the solve will execute a maximum of n iteration per cluster of connected joints and will terminate regardless of the
of the joint residual acceleration.
If it happen that the joints residual acceleration fall below the minimum tolerance 1.0e-5
then the solve will terminate before the number of iteration reach N.
*/
void NewtonSetSolverModel(const NewtonWorld* const newtonWorld, int model)
{
Newton* const world = (Newton *)newtonWorld;
TRACE_FUNCTION(__FUNCTION__);
world->SetSolverMode (model);
}
/*!
Get the solver precision mode.
*/
int NewtonGetSolverModel(const NewtonWorld* const newtonWorld)
{
Newton* const world = (Newton *)newtonWorld;
TRACE_FUNCTION(__FUNCTION__);
return world->GetSolverMode();
}
/*!
Advance the simulation by a user defined amount of time.
@param *newtonWorld is the pointer to the Newton world
@param timestep time step in seconds.
@return Nothing
This function will advance the simulation by the specified amount of time.
The Newton Engine does not perform sub-steps, nor does it need
tuning parameters. As a consequence, the application is responsible for
requesting sane time steps.
See also: ::NewtonInvalidateCache
*/
void NewtonUpdate(const NewtonWorld* const newtonWorld, dFloat timestep)
{
TRACE_FUNCTION(__FUNCTION__);
Newton* const world = (Newton *)newtonWorld;
//NewtonSerializeToFile (newtonWorld, "xxx.bin", NULL, NULL);
world->UpdatePhysics (timestep);
}
void NewtonUpdateAsync (const NewtonWorld* const newtonWorld, dFloat timestep)
{
TRACE_FUNCTION(__FUNCTION__);
Newton* const world = (Newton *)newtonWorld;
world->UpdatePhysicsAsync(timestep);
}
void NewtonWaitForUpdateToFinish (const NewtonWorld* const newtonWorld)
{
TRACE_FUNCTION(__FUNCTION__);
Newton* const world = (Newton *)newtonWorld;
world->Sync ();
}
dFloat NewtonGetLastUpdateTime (const NewtonWorld* const newtonWorld)
{
TRACE_FUNCTION(__FUNCTION__);
Newton* const world = (Newton *)newtonWorld;
return world->GetUpdateTime();
}
void NewtonSetNumberOfSubsteps (const NewtonWorld* const newtonWorld, int subSteps)
{
TRACE_FUNCTION(__FUNCTION__);
Newton* const world = (Newton *) newtonWorld;
world->SetSubsteps (subSteps);
}
int NewtonGetNumberOfSubsteps (const NewtonWorld* const newtonWorld)
{
TRACE_FUNCTION(__FUNCTION__);
Newton* const world = (Newton *) newtonWorld;
return world->GetSubsteps ();
}
/*!
Remove all bodies and joints from the Newton world.
@param *newtonWorld Pointer to the Newton world.
@return Nothing
This function will destroy all bodies and all joints in the Newton world, but
will retain group IDs.
Use this function for when you want to clear the world but preserve all the
group IDs and material pairs.
See also: ::NewtonMaterialDestroyAllGroupID
*/
void NewtonDestroyAllBodies(const NewtonWorld* const newtonWorld)
{
TRACE_FUNCTION(__FUNCTION__);
Newton* const world = (Newton *) newtonWorld;
world->DestroyAllBodies ();
}
/*!
Set a function callback to be call on each island update.
@param *newtonWorld Pointer to the Newton world.
@param islandUpdate callback function.
@return Nothing.
The application can set a function callback to be called just after the array
of all bodies making an island of connected bodies are collected. This
function will be called just before the array is accepted for contact
resolution and integration.
The callback function must return an integer 0 or 1 to either skip or process
the bodies in that particular island.
Applications can leverage this function to implement an game physics LOD. For
example the application can determine the AABB of the island and check it
against the view frustum. If the entire island AABB is invisible, then the
application can suspend its simulation, even if it is not in equilibrium.
Other possible applications are to implement of a visual debugger, or freeze
entire islands for application specific reasons.
The application must not create, modify, or destroy bodies inside the callback
or risk putting the engine into an undefined state (ie it will crash, if you
are lucky).
See also: ::NewtonIslandGetBody
*/
void NewtonSetIslandUpdateEvent(const NewtonWorld* const newtonWorld, NewtonIslandUpdate islandUpdate)
{
TRACE_FUNCTION(__FUNCTION__);
Newton* const world = (Newton *) newtonWorld;
world->SetIslandUpdateCallback((dgWorld::OnClusterUpdate) islandUpdate);
}
/*!
Get the first body in the body in the world body list.
@param *newtonWorld Pointer to the Newton world.
@return nothing
The application can call this function to iterate thought every body in the world.
The application call this function for debugging purpose
See also: ::NewtonWorldGetNextBody, ::NewtonWorldForEachBodyInAABBDo, ::NewtonWorldForEachJointDo
*/
NewtonBody* NewtonWorldGetFirstBody(const NewtonWorld* const newtonWorld)
{
Newton* const world = (Newton *) newtonWorld;
dgBodyMasterList& masterList = *world;
TRACE_FUNCTION(__FUNCTION__);
dgAssert (masterList.GetFirst()->GetInfo().GetBody() == world->GetSentinelBody());
dgBodyMasterList::dgListNode* const node = masterList.GetFirst()->GetNext();
// body = node->GetInfo().GetBody();
// node = node->GetNext();
// callback ((const NewtonBody*) body);
// }
if (node) {
return (NewtonBody*)node->GetInfo().GetBody();
} else {
return NULL;
}
}
/*!
Get the first body in the general body.
@param *newtonWorld Pointer to the Newton world.
@param curBody fixme
@return nothing
The application can call this function to iterate through every body in the world.
The application call this function for debugging purpose
See also: ::NewtonWorldGetFirstBody, ::NewtonWorldForEachBodyInAABBDo, ::NewtonWorldForEachJointDo
*/
NewtonBody* NewtonWorldGetNextBody(const NewtonWorld* const newtonWorld, const NewtonBody* const curBody)
{
dgBody* const body = (dgBody*) curBody;
TRACE_FUNCTION(__FUNCTION__);
dgBodyMasterList::dgListNode* const node = body->GetMasterList()->GetNext();
if (node) {
return (NewtonBody*)node->GetInfo().GetBody();
} else {
return NULL;
}
}
/*!
Trigger callback function for each joint in the world.
@param *newtonWorld Pointer to the Newton world.
@param callback The callback function to invoke for each joint.
@param *userData User data to pass into the callback.
@return nothing
The application should provide the function *NewtonJointIterator callback* to
be called by Newton for every joint in the world.
Note that this function is primarily for debugging. The performance penalty
for calling it is high.
See also: ::NewtonWorldForEachBodyInAABBDo, ::NewtonWorldGetFirstBody
*/
void NewtonWorldForEachJointDo(const NewtonWorld* const newtonWorld, NewtonJointIterator callback, void* const userData)
{
Newton* const world = (Newton *) newtonWorld;
dgBodyMasterList& masterList = *world;
TRACE_FUNCTION(__FUNCTION__);
dgTree<dgConstraint*, dgConstraint*> jointMap(world->dgWorld::GetAllocator());
for (dgBodyMasterList::dgListNode* node = masterList.GetFirst()->GetNext(); node; node = node->GetNext()) {
dgBodyMasterListRow& row = node->GetInfo();
for (dgBodyMasterListRow::dgListNode* jointNode = row.GetFirst(); jointNode; jointNode = jointNode->GetNext()) {
const dgBodyMasterListCell& cell = jointNode->GetInfo();
if (cell.m_joint->GetId() != dgConstraint::m_contactConstraint) {
if (!jointMap.Find(cell.m_joint)) {
jointMap.Insert(cell.m_joint, cell.m_joint);
callback ((const NewtonJoint*) cell.m_joint, userData);
}
}
}
}
}
/*!
Trigger a callback for every body that intersects the specified AABB.
@param *newtonWorld Pointer to the Newton world.
@param *p0 - pointer to an array of at least three floats to hold minimum value for the AABB.
@param *p1 - pointer to an array of at least three floats to hold maximum value for the AABB.
@param callback application defined callback
@param *userData pointer to the user defined user data value.
@return nothing
The application should provide the function *NewtonBodyIterator callback* to
be called by Newton for every body in the world.
For small AABB volumes this function is much more inefficients (fixme: more or
less efficient?) than NewtonWorldGetFirstBody. However, if the AABB contains
the majority of objects in the scene, the overhead of scanning the internal
Broadphase collision plus the AABB test make this function more expensive.
See also: ::NewtonWorldGetFirstBody
*/
void NewtonWorldForEachBodyInAABBDo(const NewtonWorld* const newtonWorld, const dFloat* const p0, const dFloat* const p1, NewtonBodyIterator callback, void* const userData)
{
TRACE_FUNCTION(__FUNCTION__);
Newton* const world = (Newton *) newtonWorld;
dgVector q0 (dgMin (p0[0], p1[0]), dgMin (p0[1], p1[1]), dgMin (p0[2], p1[2]), dgFloat32 (0.0f));
dgVector q1 (dgMax (p0[0], p1[0]), dgMax (p0[1], p1[1]), dgMax (p0[2], p1[2]), dgFloat32 (0.0f));
world->GetBroadPhase()->ForEachBodyInAABB (q0, q1, (OnBodiesInAABB) callback, userData);
}
/*!
Return the current library version number.
@return version number as an integer, eg 314.
The version number is a three-digit integer.
First digit: major version (interface changes among other things)
Second digit: major patch number (new features, and bug fixes)
Third Digit: minor bug fixed patch.
*/
int NewtonWorldGetVersion()
{
TRACE_FUNCTION(__FUNCTION__);
return NEWTON_MAJOR_VERSION * 100 + NEWTON_MINOR_VERSION;
}
/*!
Return the size of a Newton dFloat in bytes.
@return sizeof(dFloat)
*/
int NewtonWorldFloatSize ()
{
TRACE_FUNCTION(__FUNCTION__);
return sizeof (dFloat);
}
/*!
Store a user defined data value with the world.
@param *newtonWorld is the pointer to the newton world.
@param *userData pointer to the user defined user data value.
@return Nothing.
The application can attach custom data to the Newton world. Newton will never
look at this data.
The user data is useful for application developing object oriented classes
based on the Newton API.
See also: ::NewtonBodyGetUserData, ::NewtonWorldSetUserData, ::NewtonWorldGetUserData
*/
void NewtonWorldSetUserData(const NewtonWorld* const newtonWorld, void* const userData)
{
TRACE_FUNCTION(__FUNCTION__);
Newton* const world = (Newton *) newtonWorld;
world->SetUserData (userData);
}
/*!
Retrieve the user data attached to the world.
@param *newtonWorld Pointer to the Newton world.
@return Pointer to user data.
See also: ::NewtonBodySetUserData, ::NewtonWorldSetUserData, ::NewtonWorldGetUserData
*/
void* NewtonWorldGetUserData(const NewtonWorld* const newtonWorld)
{
TRACE_FUNCTION(__FUNCTION__);
Newton* const world = (Newton *) newtonWorld;
return world->GetUserData();
}
/*!
Specify a custom destructor callback for destroying the world.
@param *newtonWorld Pointer to the Newton world.
@param destructor function poiter callback
The application may specify its own world destructor.
See also: ::NewtonWorldSetDestructorCallback, ::NewtonWorldGetUserData
*/
void NewtonWorldSetDestructorCallback(const NewtonWorld* const newtonWorld, NewtonWorldDestructorCallback destructor)
{
TRACE_FUNCTION(__FUNCTION__);
Newton* const world = (Newton *) newtonWorld;
world->m_destructor = destructor;
}
/*!
Return pointer to destructor call back function.
@param *newtonWorld Pointer to the Newton world.
See also: ::NewtonWorldGetUserData, ::NewtonWorldSetDestructorCallback
*/
NewtonWorldDestructorCallback NewtonWorldGetDestructorCallback(const NewtonWorld* const newtonWorld)
{
TRACE_FUNCTION(__FUNCTION__);
Newton* const world = (Newton *) newtonWorld;
return world->m_destructor;
}
void NewtonWorldSetCollisionConstructorDestructorCallback (const NewtonWorld* const newtonWorld, NewtonCollisionCopyConstructionCallback constructor, NewtonCollisionDestructorCallback destructor)
{
TRACE_FUNCTION(__FUNCTION__);
Newton* const world = (Newton *) newtonWorld;
world->SetCollisionInstanceConstructorDestructor((dgWorld::OnCollisionInstanceDuplicate) constructor, (dgWorld::OnCollisionInstanceDestroy)destructor);
}
void* NewtonWorldGetListenerUserData (const NewtonWorld* const newtonWorld, void* const listener)
{
TRACE_FUNCTION(__FUNCTION__);
Newton* const world = (Newton *) newtonWorld;
return world->GetListenerUserData (listener);
}
NewtonWorldListenerBodyDestroyCallback NewtonWorldListenerGetBodyDestroyCallback (const NewtonWorld* const newtonWorld, void* const listener)
{
TRACE_FUNCTION(__FUNCTION__);
Newton* const world = (Newton *) newtonWorld;
return (NewtonWorldListenerBodyDestroyCallback) world->GetListenerBodyDestroyCallback (listener);
}
void NewtonWorldListenerSetBodyDestroyCallback (const NewtonWorld* const newtonWorld, void* const listener, NewtonWorldListenerBodyDestroyCallback bodyDestroyCallback)
{
TRACE_FUNCTION(__FUNCTION__);
Newton* const world = (Newton *) newtonWorld;
world->SetListenerBodyDestroyCallback (listener, (dgWorld::OnListenerBodyDestroyCallback) bodyDestroyCallback);
}
void* NewtonWorldAddListener (const NewtonWorld* const newtonWorld, const char* const nameId, void* const listenerUserData)
{
TRACE_FUNCTION(__FUNCTION__);
Newton* const world = (Newton *) newtonWorld;
return world->AddListener (nameId, listenerUserData);
}