-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathViewer.pas
More file actions
6091 lines (5309 loc) · 231 KB
/
Viewer.pas
File metadata and controls
6091 lines (5309 loc) · 231 KB
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
unit Viewer;
{$MODE Delphi}
interface
uses
Windows, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, GLScene, GLObjects, {GLMisc,} GLCadencer, ODEImport,
GLShadowPlane, GLVectorGeometry, GLGeomObjects, ExtCtrls, ComCtrls,
GLWindowsFont, keyboard, GLTexture, math, {GLSpaceText,} Remote,
GLShadowVolume, GLSkydome, GLGraph, OmniXML, OmniXMLUtils, ODERobots,
ProjConfig, GLHUDObjects, Menus, IniPropStorage, GLVectorFileObjects,
GLFireFX, GlGraphics, OpenGL1x, SimpleParser, GLBitmapFont,
GLMesh, GLWaterPlane, glzbuffer, GLLCLViewer, GLMaterial, GLColor,
GLKeyboard, GLFileOBJ, GLFileSTL, GLFilePLY, GLFileB3D, IntfGraphics,
GLVectorTypes, GLVectorLists;
type
TRemoteImage = packed record
id, Number, size, NumPackets, ActPacket: integer;
data: array[0..511] of byte;
end;
TRGBfloat = record
r, g, b: single;
end;
TSolidDef = record
ID: string;
sizeX, sizeY, SizeZ, radius: double;
posX, posY, posZ: double;
angX, angY, angZ: double; // XYZ seq
end;
TSolidXMLProperties = record
radius, sizeX, sizeY, sizeZ, posX, posY, posZ, angX, angY, angZ, mass: double;
I11, I22, I33, I12, I13, I23: double;
BuoyantMass, Drag, StokesDrag, RollDrag: double;
BuoyanceX, BuoyanceY, BuoyanceZ: double;
colorR, colorG, colorB: double;
ID: string;
descr: string;
TextureName: string;
TextureScale: double;
MeshFile, MeshShadowFile: string;
MeshScale: double;
MeshCastsShadows: boolean;
Surf: TdSurfaceParameters;
dMass: TdMass;
MatterProps: TMatterProperties;
hasCanvas, isLiquid: boolean;
CanvasWidth, CanvasHeigth: integer;
MotherSolidId: string;
SolidIdx: integer;
NewPos, ActPos: TdVector3;
GravityMode: integer;
transparency: double;
end;
//Physic World ODE
type
{ TWorld_ODE }
TWorld_ODE = class
//SampleCount: integer;
Ode_dt, TimeFactor: double;
Ode_ERP, Ode_CFM: double;
Ode_QuickStepIters: integer;
OdeScene: TGLBaseSceneObject;
MaxWorldX, MaxWorldY, MinWorldX, MinWorldY: double;
world: PdxWorld;
space: PdxSpace;
contactgroup: TdJointGroupID;
default_n_mu: double;
AirDensity: double;
WindSpeed: TdVector3;
Walls: TSolidList;
Environment: TSolid;
Robots: TRobotList;
Obstacles: TSolidList;
Things: TSolidList;
Sensors: TSensorList;
OldPick : TGLCustomSceneObject;
PickSolid: TSolid;
PickJoint: TdJointID;
PickPoint, TargetPickPoint: TVector;
PickDist, PickLinDamping, PickAngularDamping: double;
TestBody: TSolid;
ground_box : PdxGeom;
ground_box2 : PdxGeom;
ODEEnable : boolean;
physTime : double;
SecondsCount, DecPeriod: double;
Ode_UseQuickStep: boolean;
XMLFiles, XMLErrors: TStringList;
Parser: TSimpleParser;
MemCameraSolid: TSolid;
RemoteImage: TRemoteImage;
RemoteImageDecimation: integer;
destructor destroy; override;
constructor create;
procedure WorldUpdate;
private
procedure CreateCylinderObstacle(var Obstacle: TSolid; radius, sizeZ, posX,
posY, posZ: double);
//procedure CreateSubGeomBox(var body: PdxBody; var geom: PdxGeom; xsize, ysize, zsize, x, y, z: double);
//procedure CreateGlBox(var GLCube: TGLCube; var geom: PdxGeom);
procedure CreateHingeJoint(var Link: TSolidLink; Solid1, Solid2: TSolid;
anchor_x, anchor_y, anchor_z, axis_x, axis_y, axis_z: double);
procedure SetHingeLimits(var Link: TSolidLink; LimitMin, LimitMax: double);
procedure CreateShellBox(var Solid: TSolid; motherbody: PdxBody; posX, posY, posZ, L, W, H: double);
procedure CreateShellCylinder(var Solid: TSolid; motherbody: PdxBody; posX, posY, posZ, R, H: double);
procedure UpdateOdometry(Axis: TAxis);
procedure CreateWheel(Robot: TRobot; Wheel: TWheel; const Pars: TWheelPars; const wFriction: TFriction; const wMotor: TMotor);
function CreateOneRaySensor(motherbody: PdxBody; Sensor: TSensor; SensorLength: double): TSensorRay;
procedure LoadObstaclesFromXML(XMLFile: string; OffsetDef: TSolidDef; Parser: TSimpleParser);
procedure LoadSensorsFromXML(Robot: TRobot; const root: IXMLNode; Parser: TSimpleParser);
//procedure LoadHumanoidJointsFromXML(Robot: TRobot; XMLFile: string);
procedure LoadLinksFromXML(Robot: TRobot; const root: IXMLNode; Parser: TSimpleParser);
procedure ReadFrictionFromXMLNode(var Friction: TFriction; sufix: string; const prop: IXMLNode; Parser: TSimpleParser);
procedure ReadSpringFromXMLNode(var Spring: TSpring; sufix: string; const prop: IXMLNode; Parser: TSimpleParser);
procedure ReadMotorFromXMLNode(var Motor: TMotor; sufix: string; const prop: IXMLNode; Parser: TSimpleParser);
procedure CreateUniversalJoint(var Link: TSolidLink; Solid1,
Solid2: TSolid; anchor_x, anchor_y, anchor_z, axis_x, axis_y, axis_z,
axis2_x, axis2_y, axis2_z: double);
procedure SetUniversalLimits(var Link: TSolidLink; LimitMin, LimitMax,
Limit2Min, Limit2Max: double);
procedure LoadSolidsFromXML(SolidList: TSolidList; const root: IXMLNode; Parser: TSimpleParser);
// procedure CreateBall(bmass, radius, posX, posY, posZ: double);
procedure LoadWheelsFromXML(Robot: TRobot; const root: IXMLNode; Parser: TSimpleParser);
procedure LoadShellsFromXML(Robot: TRobot; const root: IXMLNode; Parser: TSimpleParser);
procedure LoadSceneFromXML(XMLFile: string);
function LoadRobotFromXML(XMLFile: string; Parser: TSimpleParser): TRobot;
procedure CreateSliderJoint(var Link: TSolidLink; Solid1, Solid2: TSolid; axis_x, axis_y, axis_z: double);
procedure SetSliderLimits(var Link: TSolidLink; LimitMin, LimitMax: double);
procedure UpdatePickJoint;
procedure LoadThingsFromXML(XMLFile: string; Parser: TSimpleParser);
procedure CreateFixedJoint(var Link: TSolidLink; Solid1, Solid2: TSolid);
function GetNodeAttrRealParse(parentNode: IXMLNode; attrName: string; defaultValue: double; const Parser: TSimpleParser): double;
procedure LoadDefinesFromXML(Parser: TSimpleParser; const root: IXMLNode);
procedure LoadTrackFromXML(XMLFile: string; Parser: TSimpleParser);
procedure LoadPolygonFromXML(const root: IXMLNode; Parser: TSimpleParser);
procedure LoadArcFromXML(const root: IXMLNode; Parser: TSimpleParser);
procedure LoadLineFromXML(const root: IXMLNode; Parser: TSimpleParser);
procedure CreateShellSphere(var Solid: TSolid; motherbody: PdxBody; posX, posY, posZ, R: double);
procedure LoadGlobalSensorsFromXML(tag: string; XMLFile: string; Parser: TSimpleParser);
procedure CreateSensorBody(Sensor: TSensor; GLBaseObject: TGLBaseSceneObject; SensorHeight,
SensorRadius, posX, posY, posZ: double);
function CreateGLPolygoLine(aWinColor: longWord; a: double; posX, posY, posZ,
lineLength, lineWidth, angle: double; s_tag: string): TGLPolygon;
function CreateGLArc(aWinColor: longWord; a: double; Xc, Yc, Zc, angX, angY, AngZ, StartAngle,
StopAngle, step, innerRadius, outerRadius: double; s_tag: string): TGLPolygon;
//procedure CreateInvisiblePlane(Plane: TSolid; dirX, dirY, dirZ, offset: double);
function CreateInvisiblePlane(planeKind: TSolidKind; dirX, dirY, dirZ, offset: double): TSolid;
procedure CreateSensorBeamGLObj(Sensor: TSensor; SensorLength, InitialWidth, FinalWidth: double);
function SolidDefProcessXMLNode(var SolidDef: TSolidDef; prop: IXMLNode; Parser: TSimpleParser): boolean;
procedure SolidDefSetDefaults(var SolidDef: TSolidDef);
procedure CreateSphereObstacle(var Obstacle: TSolid; radius, posX,
posY, posZ: double);
// procedure LoadSolidXMLProperties(XMLSolid: IXMLNode; Parser: TSimpleParser);
procedure InitSolidXMLProperties(
var SolidXMLProperties: TSolidXMLProperties);
procedure CreateSensorRanger2dGLObj(Sensor: TSensor);
procedure CreateBallJoint(var Link: TSolidLink; Solid1, Solid2: TSolid;
anchor_x, anchor_y, anchor_z: double);
// procedure AxisGLCreate(axis: Taxis; aRadius, aHeight: double);
// procedure AxisGLSetPosition(axis: Taxis);
public
procedure CreateObstacleBox(var Obstacle: TSolid; sizeX, sizeY, sizeZ, posX, posY, posZ: double);
procedure CreateSolidBox(var Solid: TSolid; bmass, posX, posY, posZ, L, W, H: double);
procedure CreateSolidCylinder(var Solid: TSolid; cmass, posX, posY, posZ: double; c_radius, c_length: double);
procedure CreateSolidSphere(var Solid: TSolid; bmass, posX, posY, posZ: double; c_radius: double);
procedure DeleteSolid(Solid: TSolid);
procedure LoadSolidMesh(newSolid: TSolid; MeshFile, MeshShadowFile: string; delta: TAffineVector; MeshScale: double;
MeshCastsShadows: boolean);
procedure exportGLPolygonsText(St: TStringList; tags: TStrings);
procedure getGLPolygonsTags(TagsList: TStrings);
procedure LoadJointWayPointsFromXML(XMLFile: string; r: integer);
procedure SaveJointWayPointsToXML(XMLFile: string; r: integer);
procedure SetCameraTarget(r: integer);
procedure CreatePickJoint(Solid: TSolid; anchor_x, anchor_y, anchor_z: double);
procedure MovePickJoint(anchor_x, anchor_y, anchor_z: double);
procedure DestroyPickJoint;
//function InsideGLPolygonsTaged(x, y: double; tags: TStrings): boolean;
end;
//Form
type
{ TFViewer }
TFViewer = class(TForm)
GLBitmapFont: TGLBitmapFont;
GLScene: TGLScene;
GLCadencer: TGLCadencer;
GLSceneViewer: TGLSceneViewer;
GLCamera: TGLCamera;
GLLightSource: TGLLightSource;
GLDummyTargetCam: TGLDummyCube;
GLWindowsBitmapFont: TGLWindowsBitmapFont;
IniPropStorage: TIniPropStorage;
ODEScene: TGLDummyCube;
Timer: TTimer;
GLShadowVolume: TGLShadowVolume;
GLPlaneFloor: TGLPlane;
GLCylinder1: TGLCylinder;
GLEarthSkyDome: TGLEarthSkyDome;
GLXYZGrid: TGLXYZGrid;
GLDummyCamPos: TGLDummyCube;
GLDummyCamPosRel: TGLDummyCube;
GLDummyTargetCamRel: TGLDummyCube;
GLDummyCubeAxis: TGLDummyCube;
GLFlatText_X: TGLFlatText;
GLFlatText_Y: TGLFlatText;
GLFlatTextOrigin: TGLFlatText;
GLPolygonArrowX: TGLPolygon;
GLPolygonArrowY: TGLPolygon;
GLCylinder2: TGLCylinder;
GLLinesXY: TGLLines;
GLHUDTextObjName: TGLHUDText;
PopupMenu: TPopupMenu;
MenuConfig: TMenuItem;
MenuChart: TMenuItem;
MenuEditor: TMenuItem;
GLMaterialLibrary3ds: TGLMaterialLibrary;
GLFireFXManager: TGLFireFXManager;
GLDummyCFire: TGLDummyCube;
GLCube1: TGLCube;
MenuScene: TMenuItem;
GLHUDTextGeneric: TGLHUDText;
GLDTrails: TGLDummyCube;
MenuSheets: TMenuItem;
MenuSnapshot: TMenuItem;
MenuChangeScene: TMenuItem;
GLCone1: TGLCone;
N1: TMenuItem;
MenuAbort: TMenuItem;
GLPlaneTex: TGLPlane;
GLPlane1: TGLPlane;
GLLineMeasure: TGLLines;
GLHUDTextMeasure: TGLHUDText;
N2: TMenuItem;
MenuCameras: TMenuItem;
TimerCadencer: TTimer;
GLWaterPlane1: TGLWaterPlane;
GLCameraMem: TGLCamera;
MenuQuit: TMenuItem;
GLDisk1: TGLDisk;
procedure FormCreate(Sender: TObject);
procedure GLSceneViewerMouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure GLSceneViewerMouseMove(Sender: TObject; Shift: TShiftState;
X, Y: Integer);
procedure GLCadencerProgress(Sender: TObject; const deltaTime, newTime: Double);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormMouseWheel(Sender: TObject; Shift: TShiftState;
WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
procedure FormShow(Sender: TObject);
procedure TimerTimer(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
procedure GLSceneViewerMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure MenuChartClick(Sender: TObject);
procedure MenuConfigClick(Sender: TObject);
procedure MenuEditorClick(Sender: TObject);
procedure MenuSceneClick(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure MenuSheetsClick(Sender: TObject);
procedure MenuSnapshotClick(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure MenuChangeSceneClick(Sender: TObject);
procedure MenuAbortClick(Sender: TObject);
procedure MenuCamerasClick(Sender: TObject);
procedure TimerCadencerTimer(Sender: TObject);
procedure MenuQuitClick(Sender: TObject);
private
{ Private declarations }
my,mx: integer;
t_delta, t_last, t_act: int64;
KeyVals: TKeyVals;
procedure UpdateCamPos(CMode: integer);
procedure FillRemote(r: integer);
procedure UpdateGLScene;
function GLSceneViewerPick(X, Y: Integer): TGLCustomSceneObject;
procedure TestTexture;
procedure ShowOrRestoreForm(Fm: TForm);
procedure TestSaveTexture;
function GetMeasureText: string;
procedure HandleMeasureKeys(var Key: Word; Shift: TShiftState);
procedure UpdateGLCameras;
public
HUDStrings: TStringList;
TrailsCount: integer;
CurrentProject: string;
frameCount: longword;
//IniFileLock: integer;
procedure SetTrailCount(NewCount, NodeCount: integer);
procedure AddTrailNode(T: integer; x, y, z: double);
procedure DelTrailNode(T: integer);
end;
function LoadXML(XMLFile: string; ErrorList: TStringList): IXMLDocument;
var
FViewer: TFViewer;
WorldODE: TWorld_ODE;
RemControl: TRemControl;
RemState: TRemState;
implementation
{$R *.lfm}
uses ODEGL, Params, Editor, FastChart, utils, StdCtrls, VerInfo,
SceneEdit, Sheets, cameras, ChooseScene;
// this is called by dSpaceCollide when two objects in space are
// potentially colliding.
procedure nearCallback (data : pointer; o1, o2 : PdxGeom); cdecl;
var
i,n : integer;
b1, b2: PdxBody;
c : TdJointID;
contact : array[0..MAX_CONTACTS-1] of TdContact;
n_mode: cardinal;
n_mu, n_mu2, n_soft_cfm, tmp: double;
n_fdir1, aux_v1, aux_v2 : TdVector3;
n_motion1: double;
begin
//exit;
b1 := dGeomGetBody(o1);
b2 := dGeomGetBody(o2);
// do not collide static objects
//if not assigned(b1) and not assigned(b2) then exit;
if assigned(b1) and assigned(b2) then begin
//exit without doing anything if the two bodies are connected by a joint
//if (dAreConnected(b1, b2)<>0) then exit;
if (dGeomGetClass(o1) <> dRayClass) and (dGeomGetClass(o2) <> dRayClass) then
if (dAreConnectedExcluding(b1, b2, dJointTypeContact)<>0) then exit;
end;
n := dCollide(o1, o2, MAX_CONTACTS, contact[0].geom, sizeof(TdContact));
if (n > 0) then begin
//FParams.EditDEbug2.Text := '';
if dGeomGetClass(o1) = dRayClass then begin
if (o1.data <> nil) then begin
with TSensorRay(o1.data).Measure do begin
{pos := contact[0].geom.pos;
normal := contact[0].geom.normal;
dist := contact[0].geom.depth;
//measure := min(measure, contact[0].geom.depth);
has_measure:= true;
HitSolid := o2.data;}
if (dist < 0) or (contact[0].geom.depth < dist) then begin
pos := contact[0].geom.pos;
normal := contact[0].geom.normal;
dist := contact[0].geom.depth;
has_measure:= true;
HitSolid := o2.data;
end;
end;
end;
//FParams.EditDEbug2.Text := format('%.2f, %.2f %.2f',[contact[0].geom.pos[0], contact[0].geom.pos[1], contact[0].geom.pos[2]]);;
exit;
end;
if dGeomGetClass(o2) = dRayClass then begin
if (o2.data <> nil) then begin
with TSensorRay(o2.data).Measure do begin
{pos := contact[0].geom.pos;
normal := contact[0].geom.normal;
dist := contact[0].geom.depth;
//measure := min(measure, contact[0].geom.depth);
has_measure:= true;
HitSolid := o1.data;}
if (dist < 0) or (contact[0].geom.depth < dist) then begin
pos := contact[0].geom.pos;
normal := contact[0].geom.normal;
dist := contact[0].geom.depth;
has_measure:= true;
HitSolid := o1.data;
end;
end;
end;
exit;
end;
n_mode := dContactBounce or
dContactSoftCFM or
dContactApprox1;
n_mu := WorldODE.default_n_mu;
n_mu2 := n_mu;
//n_soft_cfm := 0.001;
n_soft_cfm := 1e-5;
n_motion1 := 0;
if (o1.data <> nil) and (TSolid(o1.data).ParSurface.mode <> 0) then begin
n_mu := TSolid(o1.data).ParSurface.mu;
n_mu2 := TSolid(o1.data).ParSurface.mu2;
//n_soft_cfm := max(n_soft_cfm, TSolid(o1.data).ParSurface.soft_cfm);
n_soft_cfm := TSolid(o1.data).ParSurface.soft_cfm;
end;
if (o2.data <> nil) and (TSolid(o2.data).ParSurface.mode <> 0) then begin
n_mu := sqrt(n_mu * TSolid(o2.data).ParSurface.mu);
n_mu2 := sqrt(n_mu2 * TSolid(o2.data).ParSurface.mu2);
n_soft_cfm := max(n_soft_cfm, TSolid(o2.data).ParSurface.soft_cfm);
end;
{if((dGeomGetClass(o1) = dSphereClass) and (dGeomGetClass(o2) <> dPlaneClass)) or
((dGeomGetClass(o2) = dSphereClass) and (dGeomGetClass(o1) <> dPlaneClass)) then begin
//n_mode := 0.9;
n_mu2 := 0.0;
n_mu := 0.1;
zeromemory(@n_fdir1[0], sizeof(n_fdir1));
end else}
//FParams.EditDebug.Text := format('%d- %.2f %.2f %.2f',[n, n_fdir1[0], n_fdir1[1], n_fdir1[2]]);
if (o1.data <> nil) and (TSolid(o1.data).kind in [skOmniWheel, skOmniSurface, skMecanumWheel_L, skMecanumWheel_R]) then begin
n_mode := n_mode or cardinal(dContactMu2 or dContactFDir1);
if TSolid(o1.data).kind = skOmniWheel then begin
//dBodyVectorToWorld(b1, 0, 0, 1, n_fdir1);
//tmp := n_mu2;
//n_mu2 := n_mu;
//n_mu := tmp;
dBodyVectorToWorld(b1, 0, 0, 1, aux_v1);
n_fdir1 := Vector3Cross(contact[0].geom.normal, aux_v1);
end else if TSolid(o1.data).kind = skMecanumWheel_L then begin
//dBodyVectorToWorld(b1, 0, 0, 1, n_fdir1);
dBodyVectorToWorld(b1, 0, 0, 0.707106781186547, aux_v1); // 1/sqrt(2)
aux_v2 := Vector3Cross(contact[0].geom.normal, aux_v1);
n_fdir1 := Vector3ADD(aux_v1, aux_v2);
end else if TSolid(o1.data).kind = skMecanumWheel_R then begin
//dBodyVectorToWorld(b1, 0, 0, 1, n_fdir1);
dBodyVectorToWorld(b1, 0, 0, 0.707106781186547, aux_v1); // 1/sqrt(2)
aux_v2 := Vector3Cross(contact[0].geom.normal, aux_v1);
n_fdir1 := Vector3SUB(aux_v1, aux_v2);
end else begin
dBodyVectorToWorld(b1, 0, 1, 0, n_fdir1);
end;
end else if (o2.data <> nil) and (TSolid(o2.data).kind in [skOmniWheel, skOmniSurface, skMecanumWheel_L, skMecanumWheel_R]) then begin
n_mode := n_mode or cardinal(dContactMu2 or dContactFDir1);
//dBodyVectorToWorld(b2, 0, 0, 1, n_fdir1);
if TSolid(o2.data).kind = skOmniWheel then begin
//dBodyVectorToWorld(b2, 0, 0, 1, n_fdir1);
//tmp := n_mu2;
//n_mu2 := n_mu;
//n_mu := tmp; //0.001;
dBodyVectorToWorld(b2, 0, 0, 1, aux_v1);
n_fdir1 := Vector3Cross(contact[0].geom.normal, aux_v1);
end else if TSolid(o2.data).kind = skMecanumWheel_L then begin
//dBodyVectorToWorld(b2, 0, 0, 1, n_fdir1);
dBodyVectorToWorld(b2, 0, 0, 0.707106781186547, aux_v1); // 1/sqrt(2)
aux_v2 := Vector3Cross(contact[0].geom.normal, aux_v1);
n_fdir1 := Vector3ADD(aux_v1, aux_v2);
end else if TSolid(o2.data).kind = skMecanumWheel_R then begin
//dBodyVectorToWorld(b2, 0, 0, 1, n_fdir1);
dBodyVectorToWorld(b2, 0, 0, 0.707106781186547, aux_v1); // 1/sqrt(2)
aux_v2 := Vector3Cross(contact[0].geom.normal, aux_v1);
n_fdir1 := Vector3SUB(aux_v1, aux_v2);
end else begin
dBodyVectorToWorld(b2, 0, 1, 0, n_fdir1);
end;
end;
// Conveyor belt case
if (o1.data <> nil) and (TSolid(o1.data).kind = skMotorBelt) then begin
n_mode := n_mode or cardinal(dContactMu2 or dContactFDir1 or dContactMotion1);
dBodyVectorToWorld(b1, -1, 0, 0, n_fdir1);
n_mu2 := n_mu; //0.9
n_motion1 := TSolid(o1.data).BeltSpeed;
end else if (o2.data <> nil) and (TSolid(o2.data).kind = skMotorBelt) then begin
n_mode := n_mode or cardinal(dContactMu2 or dContactFDir1 or dContactMotion1);
dBodyVectorToWorld(b2, 1, 0, 0, n_fdir1);
n_mu2 := n_mu;
n_motion1 := TSolid(o2.data).BeltSpeed;
end;
for i := 0 to n-1 do begin
with contact[i].surface do begin
mode := n_mode;
mu := n_mu;
mu2 := n_mu2;
contact[i].fdir1 := n_fdir1;
//soft_cfm := 0.001;
soft_cfm := n_soft_cfm;
bounce := 0.6;
bounce_vel := 0.001;
motion1 := n_motion1;
end;
c := dJointCreateContact(WorldODE.world, WorldODE.contactgroup, @contact[i]);
dJointAttach(c, dGeomGetBody(contact[i].geom.g1), dGeomGetBody(contact[i].geom.g2));
end;
end;
end;
function CalcPID(var PID: TMotController; ref, ek: double): double;
var {ek,} dek, mk: double;
begin
result := 0;
if not PID.active then exit;
//ek := ref - yk;
dek := (ek - PID.ek_1) / WorldODE.Ode_dt;
PID.Sek := PID.Sek + ek * WorldODE.Ode_dt;
mk := PID.Kp * ek + PID.Ki * PID.Sek + PID.Kd * dek + PID.Kf * ref;
// Anti Windup
if abs(mk) >= PID.y_sat then begin
PID.Sek := PID.Sek - ek * WorldODE.Ode_dt;
mk := max(-PID.y_sat, min(PID.y_sat, mk));
end;
PID.ek_1 := ek;
result := mk;
end;
function CalcPD(var PID: TMotController; ref, refw, yk, ywk: double): double;
var ek, ewk, mk: double;
begin
result := 0;
if not PID.active then exit;
ek := ref - yk;
PID.Sek := PID.Sek + ek;
//if abs(ek) < degtorad(1) then exit;
ewk := refw - ywk;
mk := PID.Ki * PID.Sek + PID.Kp * ek + PID.Kd * ewk + PID.Kf * ref;
// Anti Windup
{ if abs(mk) >= PID.y_sat then begin
PID.Sek := PID.Sek - ek;
mk := max(-PID.y_sat, min(PID.y_sat, mk));
end;}
result := mk;
end;
procedure JointGLPosition(var joint: TdJointID; var jointGL: TGLCylinder);
var vtmp: TdVector3;
begin
with TGLCylinder(jointGL) do begin
dJointGetHingeAnchor(joint,vtmp);
Position.x := vtmp[0];
Position.y := vtmp[1];
Position.z := vtmp[2];
dJointGetHingeAxis(joint,vtmp);
up.x := vtmp[0];
up.y := vtmp[1];
up.z := vtmp[2];
end;
end;
{
function GetNodeAttrRealExpr(parentNode: IXMLNode; attrName: string; defaultValue: real): real;
var attrValue, s: WideString;
begin
if not GetNodeAttr(parentNode, attrName, attrValue) then begin
Result := defaultValue;
end else begin
s := StringReplace(attrValue, DEFAULT_DECIMALSEPARATOR, DecimalSeparator, [rfReplaceAll]);
try
Result := SimpleCalc(s);
except on E: Exception do
Result := defaultValue;
end;
end;
end; // GetNodeAttrReal }
function TWorld_ODE.GetNodeAttrRealParse(parentNode: IXMLNode; attrName: string; defaultValue: double; const Parser: TSimpleParser): double;
var attrValue, s: string;
err: string;
begin
if not GetNodeAttr(parentNode, attrName, attrValue) then begin
Result := defaultValue;
end else begin
s := StringReplace(attrValue, DEFAULT_DECIMALSEPARATOR, DefaultFormatSettings.DecimalSeparator, [rfReplaceAll]);
try
Result := Parser.Calc(s);
except on E: Exception do begin
Result := defaultValue;
err := '[Expression error] ' + format('%s(%d): ', [XMLFiles[XMLFiles.count - 1], -1]) + #$0d+#$0A
+ E.Message + ': ' +#$0d+#$0A
+ '"'+ s + '"';
if XMLErrors <> nil then begin
XMLErrors.Add(err);
end else begin
showmessage(err);
end;
end;
end;
end;
end; { GetNodeAttrReal }
{
procedure TWorld_ODE.CreateSubGeomBox(var body: PdxBody; var geom: PdxGeom; xsize, ysize, zsize: double; x,y,z: double);
begin
// create geom in current space
geom := dCreateBox(space, xsize, ysize, zsize);
dGeomSetBody(geom, body);
dGeomSetOffsetPosition(geom, x, y, z);
end;
procedure TWorld_ODE.CreateGlBox(var GLCube: TGLCube; var geom: PdxGeom);
begin
// gutter_base GLScene
GLCube := TGLCube(ODEScene.AddNewChild(TGLCube));
geom.data := GLCube;
CopyCubeSizeFromBox(GLCube, geom);
(OdeScene as TGLShadowVolume).Occluders.AddCaster(GLCube);
// PositionSceneObject(GLCube, geom);
end;
}
procedure TWorld_ODE.CreateSolidBox(var Solid: TSolid; bmass, posX, posY, posZ, L, W, H: double);
var m: TdMass;
begin
Solid.kind := skDefault;
Solid.Body := dBodyCreate(world);
dBodySetPosition(Solid.Body, posX, posY, posZ);
//dRFromAxisAndAngle (R,0,1,0,0);
//dBodySetRotation (Solid.Body, R);
dMassSetBox(m, 1, L, W, H);
dMassAdjust(m, bmass);
dBodySetMass(Solid.Body, @m);
Solid.Geom := dCreateBox(space, L, W, H);
dGeomSetBody(Solid.Geom, Solid.Body);
Solid.Geom.data := Solid;
Solid.Volume := L * W * H;
Solid.Ax := W * H;
Solid.Ay := L * H;
Solid.Az := L * W;
dBodySetDamping(Solid.Body, Solid.StokesDrag, Solid.RollDrag); // TODO anisotripic rolldrag
// dBodySetDamping(Solid.Body, Solid.StokesDrag * (L + W + H), Solid.RollDrag * (L + W + H));
// dBodySetDamping(Solid.Body, 1e-1, 1e-1);
Solid.GLObj := TGLSceneObject(ODEScene.AddNewChild(TGLCube));
PositionSceneObject(Solid.GLObj, Solid.Geom);
with (Solid.GLObj as TGLCube) do begin
TagObject := Solid;
//Scale.x := L;
//Scale.y := W;
//Scale.z := H;
CubeDepth := H;
CubeHeight := W;
CubeWidth := L;
//Material.MaterialLibrary := FViewer.GLMaterialLibrary;
//Material.FrontProperties.Diffuse.AsWinColor := clyellow;
end;
(OdeScene as TGLShadowVolume).Occluders.AddCaster(Solid.GLObj);
end;
procedure TWorld_ODE.CreateSolidCylinder(var Solid: TSolid; cmass, posX, posY, posZ: double; c_radius, c_length: double);
var m: TdMass;
// R: TdMatrix3;
begin
Solid.kind := skDefault;
Solid.Body := dBodyCreate(world);
dBodySetPosition(Solid.Body, posX, posY, posZ);
dMassSetCylinder(m, 1, 3, c_radius, c_length);
dMassAdjust(m, cmass);
dBodySetMass(Solid.Body, @m);
Solid.Geom := dCreateCylinder(space, c_radius, c_length);
dGeomSetBody(Solid.Geom, Solid.Body);
Solid.Geom.data := Solid;
Solid.Volume := Pi* sqr(c_radius) * c_length;
//TODO falta acertar ax, ay, az
Solid.Ax := 2 * c_radius * c_length;
Solid.Ay := 2 * c_radius * c_length;
Solid.Az := pi * sqr(c_radius);
dBodySetDamping(Solid.Body, Solid.StokesDrag, Solid.RollDrag); // TODO anisotripic drag
// dBodySetDamping(Solid.Body, Solid.StokesDrag * (c_radius + c_length), Solid.RollDrag * (c_radius + c_length));
// dRFromAxisAndAngle(R,1,0,0,pi/2);
// dGeomSetOffsetRotation(Solid.Geom, R);
// dBodySetRotation(Solid.Body, R);
Solid.GLObj := TGLSceneObject(ODEScene.AddNewChild(TGLCylinder));
with (Solid.GLObj as TGLCylinder) do begin
Slices := 64;
TagObject := Solid;
TopRadius := c_radius;
BottomRadius := c_radius;
Height := c_length;
//Material.FrontProperties.Diffuse.AsWinColor := clyellow;
//Material.MaterialLibrary := FViewer.GLMaterialLibrary;
//Material.LibMaterialName := 'LibMaterialBumps';
//Material.LibMaterialName := 'LibMaterialFeup';
end;
(OdeScene as TGLShadowVolume).Occluders.AddCaster(Solid.GLObj);
//PositionSceneObject(Solid.GLObj, Solid.Geom);
//if Solid.GLObj is TGLCylinder then Solid.GLObj.pitch(90);
end;
procedure TWorld_ODE.CreateSolidSphere(var Solid: TSolid; bmass, posX, posY, posZ, c_radius: double);
var m: TdMass;
begin
Solid.kind := skDefault;
Solid.Body := dBodyCreate(world);
dBodySetPosition(Solid.Body, posX, posY, posZ);
dMassSetSphereTotal(m, bmass, c_radius);
dBodySetMass(Solid.Body, @m);
Solid.Geom := dCreateSphere(space, c_radius);
dGeomSetBody(Solid.Geom, Solid.Body);
Solid.Geom.data := Solid;
Solid.Volume := 4/3 * Pi * sqr(c_radius) * c_radius;
Solid.Ax := pi * sqr(c_radius);
Solid.Ay := pi * sqr(c_radius);
Solid.Az := pi * sqr(c_radius);
Solid.GLObj := TGLSceneObject(ODEScene.AddNewChild(TGLSphere));
dBodySetDamping(Solid.Body, Solid.StokesDrag, Solid.RollDrag);
// dBodySetDamping(Solid.Body, Solid.StokesDrag * pi * sqr(c_radius), Solid.RollDrag * c_radius);
with (Solid.GLObj as TGLSphere) do begin
TagObject := Solid;
Radius := c_radius;
//Material.FrontProperties.Diffuse.AsWinColor := clyellow;
//Material.MaterialLibrary := FViewer.GLMaterialLibrary;
//Material.LibMaterialName := 'LibMaterialBumps';
//Material.LibMaterialName := 'LibMaterialFeup';
slices := 64;
//stacks := 32;
end;
(OdeScene as TGLShadowVolume).Occluders.AddCaster(Solid.GLObj);
//PositionSceneObject(Solid.GLObj, Solid.Geom);
end;
procedure TWorld_ODE.CreateShellBox(var Solid: TSolid; motherbody: PdxBody; posX, posY, posZ, L, W, H: double);
begin
Solid.kind := skDefault;
Solid.Body := motherbody;
Solid.Geom := dCreateBox(space, L, W, H);
dGeomSetBody(Solid.Geom, Solid.Body);
Solid.Geom.data := Solid;
dGeomSetOffsetPosition(Solid.Geom, posX, posY, posZ);
Solid.GLObj := TGLSceneObject(ODEScene.AddNewChild(TGLCube));
with (Solid.GLObj as TGLCube) do begin
TagObject := Solid;
Scale.x := L;
Scale.y := W;
Scale.z := H;
//Material.FrontProperties.Diffuse.AsWinColor := clyellow;
end;
(OdeScene as TGLShadowVolume).Occluders.AddCaster(Solid.GLObj);
end;
procedure TWorld_ODE.CreateShellCylinder(var Solid: TSolid; motherbody: PdxBody; posX, posY, posZ, R, H: double);
begin
Solid.kind := skDefault;
Solid.Body := motherbody;
Solid.Geom := dCreateCylinder(space, R, H);
dGeomSetBody(Solid.Geom, Solid.Body);
Solid.Geom.data := Solid;
dGeomSetOffsetPosition(Solid.Geom, posX, posY, posZ);
Solid.GLObj := TGLSceneObject(ODEScene.AddNewChild(TGLCylinder));
with (Solid.GLObj as TGLCylinder) do begin
TagObject := Solid;
TopRadius := R;
BottomRadius := R;
Height := H;
//Material.FrontProperties.Diffuse.AsWinColor := clyellow;
end;
(OdeScene as TGLShadowVolume).Occluders.AddCaster(Solid.GLObj);
end;
procedure TWorld_ODE.CreateShellSphere(var Solid: TSolid; motherbody: PdxBody; posX, posY, posZ, R: double);
begin
Solid.kind := skDefault;
Solid.Body := motherbody;
Solid.Geom := dCreateSphere(space, R);
dGeomSetBody(Solid.Geom, Solid.Body);
Solid.Geom.data := Solid;
dGeomSetOffsetPosition(Solid.Geom, posX, posY, posZ);
Solid.GLObj := TGLSceneObject(ODEScene.AddNewChild(TGLSphere));
with (Solid.GLObj as TGLSphere) do begin
TagObject := Solid;
Radius := R;
//Material.FrontProperties.Diffuse.AsWinColor := clyellow;
slices := 64;
end;
(OdeScene as TGLShadowVolume).Occluders.AddCaster(Solid.GLObj);
end;
function TWorld_ODE.CreateOneRaySensor(motherbody: PdxBody; Sensor: TSensor; SensorLength: double): TSensorRay;
var newRay: TSensorRay;
begin
newRay := TSensorRay.Create;
newRay.ParentSensor := Sensor;
newRay.Geom := dCreateRay(space, SensorLength);
dGeomSetBody(newRay.Geom, motherbody);
newRay.Geom.data := newRay;
Sensor.Rays.Add(newRay);
result := newRay;
end;
procedure TWorld_ODE.CreateSensorBeamGLObj(Sensor: TSensor; SensorLength, InitialWidth, FinalWidth: double);
begin
Sensor.GLObj := TGLSceneObject(ODEScene.AddNewChild(TGLCylinder));
with (Sensor.GLObj as TGLCylinder) do begin
TopRadius := InitialWidth;
BottomRadius := FinalWidth;
Height := SensorLength;
Alignment := caTop;
Material.FrontProperties.Diffuse.AsWinColor := clred;
Material.FrontProperties.Diffuse.Alpha := 0.5;
Material.BlendingMode := bmTransparency;
end;
end;
procedure TWorld_ODE.CreateSensorRanger2dGLObj(Sensor: TSensor);
begin
Sensor.GLObj := TGLSceneObject(ODEScene.AddNewChild(TGLDisk));
with (Sensor.GLObj as TGLDisk) do begin
OuterRadius := Sensor.MaxDist;
InnerRadius := Sensor.MinDist;
StartAngle := 90 - deg(Sensor.StartAngle);
SweepAngle := -deg(Sensor.StartAngle - Sensor.EndAngle);
Material.FrontProperties.Diffuse.AsWinColor := clred;
Material.FrontProperties.Diffuse.Alpha := 0.5;
Material.BlendingMode := bmTransparency;
Material.BackProperties.Diffuse.AsWinColor := clred;
Material.BackProperties.Diffuse.Alpha := 0.5;
Material.FaceCulling := fcNoCull;
end;
end;
procedure TWorld_ODE.CreateSensorBody(Sensor: TSensor; GLBaseObject: TGLBaseSceneObject; SensorHeight, SensorRadius, posX, posY, posZ: double);
begin
Sensor.GLObj := TGLSceneObject(GLBaseObject.AddNewChild(TGLCylinder));
with (Sensor.GLObj as TGLCylinder) do begin
TopRadius := SensorRadius;
BottomRadius := SensorRadius;
Height := SensorHeight;
Alignment := caTop;
if GLBaseObject is TGLCylinder then begin
Sensor.GLObj.Position.SetPoint(posX, -posZ, posY); // 90 degree rotation makes this ugly thing (TODO?)
end else begin
Sensor.GLObj.Position.SetPoint(posX, posY, posZ);
PitchAngle := 90;
end;
end;
end;
procedure TWorld_ODE.CreateObstacleBox(var Obstacle: TSolid; sizeX, sizeY, sizeZ, posX, posY, posZ: double);
//var R: TdMatrix3;
begin
Obstacle.kind := skDefault;
// Create 1 GLSCube and a box space.
Obstacle.Geom := dCreateBox(space, sizeX, sizeY, sizeZ);
// dRFromAxisAndAngle(R, 0, 0, 1, obs_teta);
// dGeomSetRotation(Obstacle.Geom,R);
dGeomSetPosition(Obstacle.Geom, posX, posY, posZ);
Obstacle.GLObj := TGLCube(ODEScene.AddNewChild(TGLCube));
Obstacle.GLObj.TagObject := Obstacle;
Obstacle.Geom.data := Obstacle;
dGeomSetCategoryBits(Obstacle.Geom, $00000001);
dGeomSetCollideBits(Obstacle.Geom, $FFFFFFFE);
CopyCubeSizeFromBox(TGLCube(Obstacle.GLObj), Obstacle.Geom);
//TGLCube(Obstacle.GLObj).Material.MaterialLibrary := FViewer.GLMaterialLibrary;
PositionSceneObject(Obstacle.GLObj, Obstacle.Geom);
(OdeScene as TGLShadowVolume).Occluders.AddCaster(Obstacle.GLObj);
end;
procedure TWorld_ODE.CreateCylinderObstacle(var Obstacle: TSolid; radius, sizeZ, posX, posY, posZ: double);
begin
Obstacle.kind := skDefault;
Obstacle.Geom := dCreateCylinder(space, radius, sizeZ);
dGeomSetPosition(Obstacle.Geom, posX, posY, posZ);
Obstacle.GLObj := TGLSceneObject(ODEScene.AddNewChild(TGLCylinder));
Obstacle.GLObj.TagObject := Obstacle;
Obstacle.Geom.data := Obstacle;
dGeomSetCategoryBits(Obstacle.Geom, $00000001);
dGeomSetCollideBits(Obstacle.Geom, $FFFFFFFE);
TGLCylinder(Obstacle.GLObj).TopRadius := radius;
TGLCylinder(Obstacle.GLObj).BottomRadius := radius;
TGLCylinder(Obstacle.GLObj).Height := sizeZ;
TGLCylinder(Obstacle.GLObj).slices := 64;
//TGLCylinder(Obstacle.GLObj).Material.MaterialLibrary := FViewer.GLMaterialLibrary;
PositionSceneObject(Obstacle.GLObj, Obstacle.Geom);
(OdeScene as TGLShadowVolume).Occluders.AddCaster(Obstacle.GLObj);
end;
procedure TWorld_ODE.CreateSphereObstacle(var Obstacle: TSolid; radius, posX, posY, posZ: double);
begin
Obstacle.kind := skDefault;
Obstacle.Geom := dCreateSphere(space, radius);
dGeomSetPosition(Obstacle.Geom, posX, posY, posZ);
Obstacle.GLObj := TGLSphere(ODEScene.AddNewChild(TGLSphere));
Obstacle.GLObj.TagObject := Obstacle;
Obstacle.Geom.data := Obstacle;
TGLSphere(Obstacle.GLObj).radius := radius;
//TGLSphere(Obstacle.GLObj).Material.MaterialLibrary := FViewer.GLMaterialLibrary;
TGLSphere(Obstacle.GLObj).slices := 64;
PositionSceneObject(Obstacle.GLObj, Obstacle.Geom);
(OdeScene as TGLShadowVolume).Occluders.AddCaster(Obstacle.GLObj);
end;
{
Solid.kind := skDefault;
Solid.Body := motherbody;
Solid.Geom := dCreateSphere(space, R);
dGeomSetBody(Solid.Geom, Solid.Body);
Solid.Geom.data := Solid;
dGeomSetOffsetPosition(Solid.Geom, posX, posY, posZ);
Solid.GLObj := TGLSceneObject(ODEScene.AddNewChild(TGLSphere));
with (Solid.GLObj as TGLSphere) do begin
TagObject := Solid;
Radius := R;
//Material.FrontProperties.Diffuse.AsWinColor := clyellow;
end;