-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphysics_constraint.cpp
1865 lines (1597 loc) · 61.3 KB
/
physics_constraint.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 Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//
//=============================================================================//
#include "cbase.h"
#include "vcollide_parse.h"
#include "ivp_listener_object.hxx"
#include "vphysics/constraints.h"
#include "isaverestore.h"
// HACKHACK: Mathlib defines this too!
#undef clamp
#undef max
#undef min
// There's some constructor problems in the hk stuff...
// The classes inherit from other classes with private constructor
#pragma warning (disable : 4510 )
#pragma warning (disable : 4610 )
// new havana constraint class
#include "hk_physics/physics.h"
#include "hk_physics/constraint/constraint.h"
#include "hk_physics/constraint/breakable_constraint/breakable_constraint_bp.h"
#include "hk_physics/constraint/breakable_constraint/breakable_constraint.h"
#include "hk_physics/constraint/limited_ball_socket/limited_ball_socket_bp.h"
#include "hk_physics/constraint/limited_ball_socket/limited_ball_socket_constraint.h"
#include "hk_physics/constraint/fixed/fixed_bp.h"
#include "hk_physics/constraint/fixed/fixed_constraint.h"
#include "hk_physics/constraint/stiff_spring/stiff_spring_bp.h"
#include "hk_physics/constraint/stiff_spring/stiff_spring_constraint.h"
#include "hk_physics/constraint/ball_socket/ball_socket_bp.h"
#include "hk_physics/constraint/ball_socket/ball_socket_constraint.h"
#include "hk_physics/constraint/prismatic/prismatic_bp.h"
#include "hk_physics/constraint/prismatic/prismatic_constraint.h"
#include "hk_physics/constraint/ragdoll/ragdoll_constraint.h"
#include "hk_physics/constraint/ragdoll/ragdoll_constraint_bp.h"
#include "hk_physics/constraint/ragdoll/ragdoll_constraint_bp_builder.h"
#include "hk_physics/constraint/hinge/hinge_constraint.h"
#include "hk_physics/constraint/hinge/hinge_bp.h"
#include "hk_physics/constraint/hinge/hinge_bp_builder.h"
#include "hk_physics/constraint/pulley/pulley_constraint.h"
#include "hk_physics/constraint/pulley/pulley_bp.h"
#include "hk_physics/constraint/local_constraint_system/local_constraint_system.h"
#include "hk_physics/constraint/local_constraint_system/local_constraint_system_bp.h"
#include "ivp_cache_object.hxx"
#include "ivp_template_constraint.hxx"
extern void qh_srand( int seed);
#include "qhull_user.hxx"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
const float UNBREAKABLE_BREAK_LIMIT = 1e12f;
hk_Vector3 TransformHLWorldToHavanaLocal( const Vector &hlWorld, IVP_Real_Object *pObject )
{
IVP_U_Float_Point tmp;
IVP_U_Point pointOut;
ConvertPositionToIVP( hlWorld, tmp );
TransformIVPToLocal( tmp, pointOut, pObject, true );
return hk_Vector3( pointOut.k[0], pointOut.k[1], pointOut.k[2] );
}
Vector TransformHavanaLocalToHLWorld( const hk_Vector3 &input, IVP_Real_Object *pObject, bool translate )
{
IVP_U_Float_Point ivpLocal( input.x, input.y, input.z );
IVP_U_Float_Point ivpWorld;
TransformLocalToIVP( ivpLocal, ivpWorld, pObject, translate );
Vector hlOut;
if ( translate )
{
ConvertPositionToHL( ivpWorld, hlOut );
}
else
{
ConvertDirectionToHL( ivpWorld, hlOut );
}
return hlOut;
}
inline hk_Vector3 vec( const IVP_U_Point &point )
{
hk_Vector3 tmp(point.k[0], point.k[1], point.k[2] );
return tmp;
}
// UNDONE: if vector were aligned we could simply cast these.
inline hk_Vector3 vec( const Vector &point )
{
hk_Vector3 tmp(point.x, point.y, point.z );
return tmp;
}
void ConvertHLLocalMatrixToHavanaLocal( const matrix3x4_t& hlMatrix, hk_Transform &out )
{
IVP_U_Matrix ivpMatrix;
ConvertMatrixToIVP( hlMatrix, ivpMatrix );
ivpMatrix.get_4x4_column_major( (hk_real *)&out );
}
void set_4x4_column_major( IVP_U_Matrix &ivpMatrix, const float *in4x4 )
{
ivpMatrix.set_elem( 0, 0, in4x4[0] );
ivpMatrix.set_elem( 1, 0, in4x4[1] );
ivpMatrix.set_elem( 2, 0, in4x4[2] );
ivpMatrix.set_elem( 0, 1, in4x4[4] );
ivpMatrix.set_elem( 1, 1, in4x4[5] );
ivpMatrix.set_elem( 2, 1, in4x4[6] );
ivpMatrix.set_elem( 0, 2, in4x4[8] );
ivpMatrix.set_elem( 1, 2, in4x4[9] );
ivpMatrix.set_elem( 2, 2, in4x4[10] );
ivpMatrix.vv.k[0] = in4x4[12];
ivpMatrix.vv.k[1] = in4x4[13];
ivpMatrix.vv.k[2] = in4x4[14];
}
inline void ConvertPositionToIVP( const Vector &point, hk_Vector3 &out )
{
IVP_U_Float_Point ivp;
ConvertPositionToIVP( point, ivp );
out = vec( ivp );
}
inline void ConvertPositionToHL( const hk_Vector3 &point, Vector& out )
{
float tmpY = IVP2HL(point.z);
out.z = -IVP2HL(point.y);
out.y = tmpY;
out.x = IVP2HL(point.x);
}
inline void ConvertDirectionToHL( const hk_Vector3 &point, Vector& out )
{
float tmpY = point.z;
out.z = -point.y;
out.y = tmpY;
out.x = point.x;
}
void ConvertHavanaLocalMatrixToHL( const hk_Transform &in, matrix3x4_t& hlMatrix, IVP_Real_Object *pObject )
{
IVP_U_Matrix ivpMatrix;
set_4x4_column_major( ivpMatrix, (const hk_real *)&in );
ConvertMatrixToHL( ivpMatrix, hlMatrix );
}
static bool IsBreakableConstraint( const constraint_breakableparams_t &constraint )
{
return ( (constraint.forceLimit != 0 && constraint.forceLimit < UNBREAKABLE_BREAK_LIMIT) ||
(constraint.torqueLimit != 0 && constraint.torqueLimit < UNBREAKABLE_BREAK_LIMIT) ||
(constraint.bodyMassScale[0] != 1.0f && constraint.bodyMassScale[0] != 0.0f) ||
(constraint.bodyMassScale[1] != 1.0f && constraint.bodyMassScale[1] != 0.0f) ) ? true : false;
}
struct vphysics_save_cphysicsconstraintgroup_t : public constraint_groupparams_t
{
bool isActive;
DECLARE_SIMPLE_DATADESC();
};
BEGIN_SIMPLE_DATADESC( vphysics_save_cphysicsconstraintgroup_t )
DEFINE_FIELD( isActive, FIELD_BOOLEAN ),
DEFINE_FIELD( additionalIterations, FIELD_INTEGER ),
DEFINE_FIELD( minErrorTicks, FIELD_INTEGER ),
DEFINE_FIELD( errorTolerance, FIELD_FLOAT ),
END_DATADESC()
// a little list that holds active groups so they can activate after
// the constraints are restored and added to the groups
static CUtlVector<CPhysicsConstraintGroup *> g_ConstraintGroupActivateList;
class CPhysicsConstraintGroup: public IPhysicsConstraintGroup
{
public:
hk_Local_Constraint_System *GetLCS() { return m_pLCS; }
void WriteToTemplate( vphysics_save_cphysicsconstraintgroup_t &groupParams )
{
hk_Local_Constraint_System_BP bp;
m_pLCS->write_to_blueprint( &bp );
groupParams.additionalIterations = bp.m_n_iterations;
groupParams.isActive = bp.m_active;
groupParams.minErrorTicks = bp.m_minErrorTicks;
groupParams.errorTolerance = ConvertDistanceToHL(bp.m_errorTolerance);
}
public:
CPhysicsConstraintGroup( IVP_Environment *pEnvironment, const constraint_groupparams_t &group );
~CPhysicsConstraintGroup();
virtual void Activate();
virtual bool IsInErrorState();
virtual void ClearErrorState();
void GetErrorParams( constraint_groupparams_t *pParams );
void SetErrorParams( const constraint_groupparams_t ¶ms );
void SolvePenetration( IPhysicsObject *pObj0, IPhysicsObject *pObj1 );
private:
hk_Local_Constraint_System *m_pLCS;
};
void CPhysicsConstraintGroup::Activate()
{
if (m_pLCS)
{
m_pLCS->activate();
}
}
bool CPhysicsConstraintGroup::IsInErrorState()
{
if (m_pLCS)
{
return m_pLCS->has_error();
}
return false;
}
void CPhysicsConstraintGroup::ClearErrorState()
{
if (m_pLCS)
{
m_pLCS->clear_error();
}
}
void CPhysicsConstraintGroup::GetErrorParams( constraint_groupparams_t *pParams )
{
if ( !m_pLCS )
return;
vphysics_save_cphysicsconstraintgroup_t tmp;
WriteToTemplate( tmp );
*pParams = tmp;
}
void CPhysicsConstraintGroup::SetErrorParams( const constraint_groupparams_t ¶ms )
{
if ( !m_pLCS )
return;
m_pLCS->set_error_ticks( params.minErrorTicks );
m_pLCS->set_error_tolerance( ConvertDistanceToIVP(params.errorTolerance) );
}
void CPhysicsConstraintGroup::SolvePenetration( IPhysicsObject *pObj0, IPhysicsObject *pObj1 )
{
if ( m_pLCS && pObj0 && pObj1 )
{
CPhysicsObject *pPhys0 = static_cast<CPhysicsObject *>(pObj0);
CPhysicsObject *pPhys1 = static_cast<CPhysicsObject *>(pObj1);
m_pLCS->solve_penetration(pPhys0->GetObject(), pPhys1->GetObject());
}
}
CPhysicsConstraintGroup::~CPhysicsConstraintGroup()
{
delete m_pLCS;
}
CPhysicsConstraintGroup::CPhysicsConstraintGroup( IVP_Environment *pEnvironment, const constraint_groupparams_t &group )
{
hk_Local_Constraint_System_BP cs_bp;
cs_bp.m_n_iterations = group.additionalIterations;
cs_bp.m_minErrorTicks = group.minErrorTicks;
cs_bp.m_errorTolerance = ConvertDistanceToIVP(group.errorTolerance);
m_pLCS = new hk_Local_Constraint_System( static_cast<hk_Environment *>(pEnvironment), &cs_bp );
m_pLCS->set_client_data( (void *)this );
}
enum vphysics_save_constrainttypes_t
{
CONSTRAINT_UNKNOWN = 0,
CONSTRAINT_RAGDOLL,
CONSTRAINT_HINGE,
CONSTRAINT_FIXED,
CONSTRAINT_BALLSOCKET,
CONSTRAINT_SLIDING,
CONSTRAINT_PULLEY,
CONSTRAINT_LENGTH,
};
struct vphysics_save_cphysicsconstraint_t
{
int constraintType;
CPhysicsConstraintGroup *pGroup;
CPhysicsObject *pObjReference;
CPhysicsObject *pObjAttached;
DECLARE_SIMPLE_DATADESC();
};
BEGIN_SIMPLE_DATADESC( vphysics_save_cphysicsconstraint_t )
DEFINE_FIELD( constraintType, FIELD_INTEGER ),
DEFINE_VPHYSPTR( pGroup ),
DEFINE_VPHYSPTR( pObjReference ),
DEFINE_VPHYSPTR( pObjAttached ),
END_DATADESC()
struct vphysics_save_constraintbreakable_t : public constraint_breakableparams_t
{
DECLARE_SIMPLE_DATADESC();
};
BEGIN_SIMPLE_DATADESC( vphysics_save_constraintbreakable_t )
DEFINE_FIELD( strength, FIELD_FLOAT ),
DEFINE_FIELD( forceLimit, FIELD_FLOAT ),
DEFINE_FIELD( torqueLimit, FIELD_FLOAT ),
DEFINE_AUTO_ARRAY( bodyMassScale, FIELD_FLOAT ),
DEFINE_FIELD( isActive, FIELD_BOOLEAN ),
END_DATADESC()
struct vphysics_save_constraintaxislimit_t : public constraint_axislimit_t
{
DECLARE_SIMPLE_DATADESC();
};
BEGIN_SIMPLE_DATADESC( vphysics_save_constraintaxislimit_t )
DEFINE_FIELD( minRotation, FIELD_FLOAT ),
DEFINE_FIELD( maxRotation, FIELD_FLOAT ),
DEFINE_FIELD( angularVelocity, FIELD_FLOAT ),
DEFINE_FIELD( torque, FIELD_FLOAT ),
END_DATADESC()
struct vphysics_save_constraintfixed_t : public constraint_fixedparams_t
{
DECLARE_SIMPLE_DATADESC();
};
BEGIN_SIMPLE_DATADESC( vphysics_save_constraintfixed_t )
DEFINE_AUTO_ARRAY2D( attachedRefXform, FIELD_FLOAT ),
DEFINE_EMBEDDED_OVERRIDE( constraint, vphysics_save_constraintbreakable_t ),
END_DATADESC()
struct vphysics_save_constrainthinge_t : public constraint_hingeparams_t
{
DECLARE_SIMPLE_DATADESC();
};
BEGIN_SIMPLE_DATADESC( vphysics_save_constrainthinge_t )
DEFINE_FIELD( worldPosition, FIELD_POSITION_VECTOR ),
DEFINE_FIELD( worldAxisDirection, FIELD_VECTOR ),
DEFINE_EMBEDDED_OVERRIDE( constraint, vphysics_save_constraintbreakable_t ),
DEFINE_EMBEDDED_OVERRIDE( hingeAxis, vphysics_save_constraintaxislimit_t ),
END_DATADESC()
struct vphysics_save_constraintsliding_t : public constraint_slidingparams_t
{
DECLARE_SIMPLE_DATADESC();
};
BEGIN_SIMPLE_DATADESC( vphysics_save_constraintsliding_t )
DEFINE_AUTO_ARRAY2D( attachedRefXform, FIELD_FLOAT ),
DEFINE_FIELD( slideAxisRef, FIELD_VECTOR ),
DEFINE_FIELD( limitMin, FIELD_FLOAT ),
DEFINE_FIELD( limitMax, FIELD_FLOAT ),
DEFINE_FIELD( friction, FIELD_FLOAT ),
DEFINE_FIELD( velocity, FIELD_FLOAT ),
DEFINE_EMBEDDED_OVERRIDE( constraint, vphysics_save_constraintbreakable_t ),
END_DATADESC()
struct vphysics_save_constraintpulley_t : public constraint_pulleyparams_t
{
DECLARE_SIMPLE_DATADESC();
};
BEGIN_SIMPLE_DATADESC( vphysics_save_constraintpulley_t )
DEFINE_AUTO_ARRAY( pulleyPosition, FIELD_POSITION_VECTOR ),
DEFINE_AUTO_ARRAY( objectPosition, FIELD_VECTOR ),
DEFINE_FIELD( totalLength, FIELD_FLOAT ),
DEFINE_FIELD( gearRatio, FIELD_FLOAT ),
DEFINE_FIELD( isRigid, FIELD_BOOLEAN ),
DEFINE_EMBEDDED_OVERRIDE( constraint, vphysics_save_constraintbreakable_t ),
END_DATADESC()
struct vphysics_save_constraintlength_t : public constraint_lengthparams_t
{
DECLARE_SIMPLE_DATADESC();
};
BEGIN_SIMPLE_DATADESC( vphysics_save_constraintlength_t )
DEFINE_AUTO_ARRAY( objectPosition, FIELD_VECTOR ),
DEFINE_FIELD( totalLength, FIELD_FLOAT ),
DEFINE_FIELD( minLength, FIELD_FLOAT ),
DEFINE_EMBEDDED_OVERRIDE( constraint, vphysics_save_constraintbreakable_t ),
END_DATADESC()
struct vphysics_save_constraintballsocket_t : public constraint_ballsocketparams_t
{
DECLARE_SIMPLE_DATADESC();
};
BEGIN_SIMPLE_DATADESC( vphysics_save_constraintballsocket_t )
DEFINE_AUTO_ARRAY( constraintPosition, FIELD_VECTOR ),
DEFINE_EMBEDDED_OVERRIDE( constraint, vphysics_save_constraintbreakable_t ),
END_DATADESC()
struct vphysics_save_constraintragdoll_t : public constraint_ragdollparams_t
{
DECLARE_SIMPLE_DATADESC();
};
BEGIN_SIMPLE_DATADESC( vphysics_save_constraintragdoll_t )
DEFINE_EMBEDDED_OVERRIDE( constraint, vphysics_save_constraintbreakable_t ),
DEFINE_AUTO_ARRAY2D( constraintToReference, FIELD_FLOAT ),
DEFINE_AUTO_ARRAY2D( constraintToAttached, FIELD_FLOAT ),
DEFINE_EMBEDDED_OVERRIDE( axes[0], vphysics_save_constraintaxislimit_t ),
DEFINE_EMBEDDED_OVERRIDE( axes[1], vphysics_save_constraintaxislimit_t ),
DEFINE_EMBEDDED_OVERRIDE( axes[2], vphysics_save_constraintaxislimit_t ),
DEFINE_FIELD( onlyAngularLimits, FIELD_BOOLEAN ),
DEFINE_FIELD( isActive, FIELD_BOOLEAN ),
DEFINE_FIELD( useClockwiseRotations, FIELD_BOOLEAN ),
END_DATADESC()
struct vphysics_save_constraint_t
{
vphysics_save_constraintfixed_t fixed;
vphysics_save_constrainthinge_t hinge;
vphysics_save_constraintsliding_t sliding;
vphysics_save_constraintpulley_t pulley;
vphysics_save_constraintlength_t length;
vphysics_save_constraintballsocket_t ballsocket;
vphysics_save_constraintragdoll_t ragdoll;
};
// UNDONE: May need to change interface to specify limits before construction
// UNDONE: Refactor constraints to contain a separate object for the various constraint types?
class CPhysicsConstraint: public IPhysicsConstraint, public IVP_Listener_Object
{
public:
CPhysicsConstraint( CPhysicsObject *pReferenceObject, CPhysicsObject *pAttachedObject );
~CPhysicsConstraint( void );
// init as ragdoll constraint
void InitRagdoll( IVP_Environment *pEnvironment, CPhysicsConstraintGroup *constraint_group, const constraint_ragdollparams_t &ragdoll );
// init as hinge
void InitHinge( IVP_Environment *pEnvironment, CPhysicsConstraintGroup *constraint_group, const constraint_limitedhingeparams_t &hinge );
// init as fixed (BUGBUG: This is broken)
void InitFixed( IVP_Environment *pEnvironment, CPhysicsConstraintGroup *constraint_group, const constraint_fixedparams_t &fixed );
// init as ballsocket
void InitBallsocket( IVP_Environment *pEnvironment, CPhysicsConstraintGroup *constraint_group, const constraint_ballsocketparams_t &ballsocket );
// init as sliding constraint
void InitSliding( IVP_Environment *pEnvironment, CPhysicsConstraintGroup *constraint_group, const constraint_slidingparams_t &sliding );
// init as pulley
void InitPulley( IVP_Environment *pEnvironment, CPhysicsConstraintGroup *constraint_group, const constraint_pulleyparams_t &pulley );
// init as stiff spring / length constraint
void InitLength( IVP_Environment *pEnvironment, CPhysicsConstraintGroup *constraint_group, const constraint_lengthparams_t &length );
void WriteToTemplate( vphysics_save_cphysicsconstraint_t &header, vphysics_save_constraint_t &constraintTemplate ) const;
void WriteRagdoll( constraint_ragdollparams_t &ragdoll ) const;
void WriteHinge( constraint_hingeparams_t &hinge ) const;
void WriteFixed( constraint_fixedparams_t &fixed ) const;
void WriteSliding( constraint_slidingparams_t &sliding ) const;
void WriteBallsocket( constraint_ballsocketparams_t &ballsocket ) const;
void WritePulley( constraint_pulleyparams_t &pulley ) const;
void WriteLength( constraint_lengthparams_t &length ) const;
CPhysicsConstraintGroup *GetConstraintGroup() const;
hk_Constraint *CreateBreakableConstraint( hk_Constraint *pRealConstraint, hk_Local_Constraint_System *pLcs, const constraint_breakableparams_t &constraint )
{
m_isBreakable = true;
hk_Breakable_Constraint_BP bp;
bp.m_real_constraint = pRealConstraint;
float forceLimit = ConvertDistanceToIVP( constraint.forceLimit );
bp.m_linear_strength = forceLimit > 0 ? forceLimit : UNBREAKABLE_BREAK_LIMIT;
bp.m_angular_strength = constraint.torqueLimit > 0 ? DEG2RAD(constraint.torqueLimit) : UNBREAKABLE_BREAK_LIMIT;
//lwss hack - comment these new args
//bp.m_bodyMassScale[0] = constraint.bodyMassScale[0] > 0 ? constraint.bodyMassScale[0] : 1.0f;
//bp.m_bodyMassScale[1] = constraint.bodyMassScale[1] > 0 ? constraint.bodyMassScale[1] : 1.0f;;
//lwss end
return new hk_Breakable_Constraint( pLcs, &bp );
}
void ReadBreakableConstraint( constraint_breakableparams_t ¶ms ) const;
hk_Constraint *GetRealConstraint() const
{
if ( m_isBreakable )
{
hk_Breakable_Constraint_BP bp;
((hk_Breakable_Constraint *)m_HkConstraint)->write_to_blueprint( &bp );
return bp.m_real_constraint;
}
return m_HkConstraint;
}
void Activate( void );
void Deactivate( void );
void SetupRagdollAxis( int axis, const constraint_axislimit_t &axisData, hk_Limited_Ball_Socket_BP *ballsocketBP );
// UNDONE: Implement includeStatic for havana
void SetGameData( void *gameData ) { m_pGameData = gameData; }
void *GetGameData( void ) const { return m_pGameData; }
IPhysicsObject *GetReferenceObject( void ) const;
IPhysicsObject *GetAttachedObject( void ) const;
void SetLinearMotor( float speed, float maxForce );
void SetAngularMotor( float rotSpeed, float maxAngularImpulse );
void UpdateRagdollTransforms( const matrix3x4_t &constraintToReference, const matrix3x4_t &constraintToAttached );
bool GetConstraintTransform( matrix3x4_t *pConstraintToReference, matrix3x4_t *pConstraintToAttached ) const;
bool GetConstraintParams( constraint_breakableparams_t *pParams ) const;
void OutputDebugInfo()
{
//lwss hack - no debug output
//hk_Local_Constraint_System *pLCS = m_HkLCS;
//if ( m_HkConstraint )
//{
// pLCS = m_HkConstraint->get_constraint_system();
//}
//if ( pLCS )
//{
// int count = 0;
// hk_Array<hk_Constraint *> list;
// pLCS->get_constraints_in_system( list );
// Msg("System of %d constraints\n", list.length());
// for ( hk_Array<hk_Constraint*>::iterator i = list.start(); list.is_valid(i); i = list.next(i) )
// {
// hk_Constraint *pConstraint = list.get_element(i);
// Msg("\tConstraint %d) %s\n", count, pConstraint->get_constraint_type() );
// count++;
// }
//}
Msg("LWSS- debug output unimplemented\n");
//lwss end
}
void DetachListener();
// Object listener
virtual void event_object_deleted( IVP_Event_Object *);
virtual void event_object_created( IVP_Event_Object *) {}
virtual void event_object_revived( IVP_Event_Object *) {}
virtual void event_object_frozen ( IVP_Event_Object *) {}
private:
CPhysicsObject *m_pObjReference;
CPhysicsObject *m_pObjAttached;
// havana constraints
hk_Constraint *m_HkConstraint;
hk_Local_Constraint_System *m_HkLCS;
void *m_pGameData;
// these are used to crack the abstract pointers on save/load
short m_constraintType;
short m_isBreakable;
};
CPhysicsConstraint::CPhysicsConstraint( CPhysicsObject *pReferenceObject, CPhysicsObject *pAttachedObject )
{
m_pGameData = NULL;
m_HkConstraint = NULL;
m_HkLCS = NULL;
m_constraintType = CONSTRAINT_UNKNOWN;
m_isBreakable = false;
if ( pReferenceObject && pAttachedObject )
{
m_pObjReference = (CPhysicsObject *)pReferenceObject;
m_pObjAttached = (CPhysicsObject *)pAttachedObject;
if ( !(m_pObjReference->CallbackFlags() & CALLBACK_NEVER_DELETED) )
{
m_pObjReference->GetObject()->add_listener_object( this );
}
if ( !(m_pObjAttached->CallbackFlags() & CALLBACK_NEVER_DELETED) )
{
m_pObjAttached->GetObject()->add_listener_object( this );
}
}
else
{
m_pObjReference = NULL;
m_pObjAttached = NULL;
}
}
// Check to see if this is a single degree of freedom joint, if so, convert to a hinge
static bool ConvertRagdollToHinge( constraint_limitedhingeparams_t *pHingeOut, const constraint_ragdollparams_t &ragdoll, IPhysicsObject *pReferenceObject, IPhysicsObject *pAttachedObject )
{
int nDOF = 0;
int dofIndex = 0;
for ( int i = 0; i < 3; i++ )
{
if ( ragdoll.axes[i].minRotation != ragdoll.axes[i].maxRotation )
{
dofIndex = i;
nDOF++;
}
}
// found multiple degrees of freedom
if ( nDOF != 1 )
return false;
// convert params to hinge
pHingeOut->Defaults();
pHingeOut->constraint = ragdoll.constraint;
// get the hinge axis in world space
matrix3x4_t refToWorld, constraintToWorld;
pReferenceObject->GetPositionMatrix( &refToWorld );
ConcatTransforms( refToWorld, ragdoll.constraintToReference, constraintToWorld );
// many ragdoll constraints don't set this and the ragdoll solver ignores it
// force it to the default
pHingeOut->constraint.strength = 1.0f;
MatrixGetColumn( constraintToWorld, 3, pHingeOut->worldPosition );
MatrixGetColumn( constraintToWorld, dofIndex, pHingeOut->worldAxisDirection );
pHingeOut->referencePerpAxisDirection.Init();
pHingeOut->referencePerpAxisDirection[(dofIndex+1)%3] = 1;
Vector perpCS;
VectorIRotate( pHingeOut->referencePerpAxisDirection, ragdoll.constraintToReference, perpCS );
VectorRotate( perpCS, ragdoll.constraintToAttached, pHingeOut->attachedPerpAxisDirection );
pHingeOut->hingeAxis = ragdoll.axes[dofIndex];
// Funky math to insure that the friction is preserved after the math that the hinge code uses.
pHingeOut->hingeAxis.torque = RAD2DEG( pHingeOut->hingeAxis.torque * pReferenceObject->GetMass() );
// need to flip the limits, just flip the axis instead
if ( !ragdoll.useClockwiseRotations )
{
float tmp = pHingeOut->hingeAxis.minRotation;
pHingeOut->hingeAxis.minRotation = -pHingeOut->hingeAxis.maxRotation;
pHingeOut->hingeAxis.maxRotation = -tmp;
}
return true;
}
// ragdoll constraint
void CPhysicsConstraint::InitRagdoll( IVP_Environment *pEnvironment, CPhysicsConstraintGroup *constraint_group, const constraint_ragdollparams_t &ragdoll )
{
// UNDONE: If this is a hinge parameterized using the ragdoll params, use a hinge instead!
constraint_limitedhingeparams_t hinge;
if ( ConvertRagdollToHinge( &hinge, ragdoll, m_pObjReference, m_pObjAttached ) )
{
InitHinge( pEnvironment, constraint_group, hinge );
return;
}
m_constraintType = CONSTRAINT_RAGDOLL;
hk_Rigid_Body *ref = (hk_Rigid_Body*)m_pObjReference->GetObject();
hk_Rigid_Body *att = (hk_Rigid_Body*)m_pObjAttached->GetObject();
hk_Limited_Ball_Socket_BP ballsocketBP;
ConvertHLLocalMatrixToHavanaLocal( ragdoll.constraintToReference, ballsocketBP.m_transform_os_ks[0] );
ConvertHLLocalMatrixToHavanaLocal( ragdoll.constraintToAttached, ballsocketBP.m_transform_os_ks[1] );
bool breakable = IsBreakableConstraint( ragdoll.constraint );
int i;
// BUGBUG: Handle incorrect clockwise rotations here
for ( i = 0; i < 3; i++ )
{
SetupRagdollAxis( i, ragdoll.axes[i], &ballsocketBP );
}
ballsocketBP.m_constrainTranslation = ragdoll.onlyAngularLimits ? false : true;
// swap the input limits if they are clockwise (angles are counter-clockwise)
if ( ragdoll.useClockwiseRotations )
{
for ( i = 0; i < 3; i++ )
{
float tmp = ballsocketBP.m_angular_limits[i].m_min;
ballsocketBP.m_angular_limits[i].m_min = -ballsocketBP.m_angular_limits[i].m_max;
ballsocketBP.m_angular_limits[i].m_max = -tmp;
}
}
hk_Ragdoll_Constraint_BP_Builder r_builder;
r_builder.initialize_from_limited_ball_socket_bp( &ballsocketBP, ref, att );
hk_Ragdoll_Constraint_BP *bp = (hk_Ragdoll_Constraint_BP *)r_builder.get_blueprint(); // get non const bp
int revAxisMapHK[3];
//lwss hack
// ok this looks like it's converting the axis' from the physics engine to source.
// I'll guess it for now lol
// TODO: fix axis if it's screwey
//revAxisMapHK[bp->m_axisMap[0]] = 0;
//revAxisMapHK[bp->m_axisMap[1]] = 1;
//revAxisMapHK[bp->m_axisMap[2]] = 2;
revAxisMapHK[0] = 0;
revAxisMapHK[1] = 1;
revAxisMapHK[2] = 2;
//lwss end
for ( i = 0; i < 3; i++ )
{
// remap HL axis to IVP axis
int ivpAxis = ConvertCoordinateAxisToIVP( i );
// initialize_from_limited_ball_socket_bp remapped the axes too! So remap again.
int hkAxis = revAxisMapHK[ivpAxis];
const constraint_axislimit_t &axisData = ragdoll.axes[i];
bp->m_limits[hkAxis].set_motor( DEG2RAD(axisData.angularVelocity), axisData.torque * m_pObjReference->GetMass() );
bp->m_tau = 1.0f;
}
hk_Local_Constraint_System *lcs = constraint_group ? constraint_group->GetLCS() : NULL;
hk_Environment *hkEnvironment = static_cast<hk_Environment *>(pEnvironment);
if ( !lcs )
{
hk_Local_Constraint_System_BP bp;
lcs = new hk_Local_Constraint_System( hkEnvironment, &bp );
m_HkLCS = lcs;
}
if ( breakable )
{
hk_Ragdoll_Constraint *pConstraint = new hk_Ragdoll_Constraint( hkEnvironment, bp, ref, att);
m_HkConstraint = CreateBreakableConstraint( pConstraint, lcs, ragdoll.constraint );
}
else
{
m_HkConstraint = new hk_Ragdoll_Constraint( lcs, bp, ref, att);
}
if ( m_HkLCS && ragdoll.isActive )
{
m_HkLCS->activate();
}
m_HkConstraint->set_client_data( (void *)this );
}
// hinge constraint
void CPhysicsConstraint::InitHinge( IVP_Environment *pEnvironment, CPhysicsConstraintGroup *constraint_group, const constraint_limitedhingeparams_t &hinge )
{
m_constraintType = CONSTRAINT_HINGE;
hk_Environment *hkEnvironment = static_cast<hk_Environment *>(pEnvironment);
bool breakable = IsBreakableConstraint( hinge.constraint );
hk_Hinge_BP_Builder builder;
IVP_U_Point axisIVP_ws, axisPerpIVP_os, axisStartIVP_ws, axisStartIVP_os;
ConvertDirectionToIVP( hinge.worldAxisDirection, axisIVP_ws );
builder.set_axis_ws( (hk_Rigid_Body*)m_pObjReference->GetObject(), (hk_Rigid_Body*)m_pObjAttached->GetObject(), vec(axisIVP_ws) );
builder.set_position_os( 0, TransformHLWorldToHavanaLocal( hinge.worldPosition, m_pObjReference->GetObject() ) );
builder.set_position_os( 1, TransformHLWorldToHavanaLocal( hinge.worldPosition, m_pObjAttached->GetObject() ) );
ConvertDirectionToIVP( hinge.referencePerpAxisDirection, axisPerpIVP_os );
builder.set_axis_perp_os( 0, vec(axisPerpIVP_os) );
ConvertDirectionToIVP( hinge.attachedPerpAxisDirection, axisPerpIVP_os );
builder.set_axis_perp_os( 1, vec(axisPerpIVP_os) );
builder.set_tau( hinge.constraint.strength );
// torque is an impulse radians/sec * inertia
if ( hinge.hingeAxis.torque != 0 )
{
builder.set_angular_motor( DEG2RAD(hinge.hingeAxis.angularVelocity), DEG2RAD(hinge.hingeAxis.torque) );
}
if ( hinge.hingeAxis.minRotation != hinge.hingeAxis.maxRotation )
{
builder.set_angular_limits( DEG2RAD(hinge.hingeAxis.minRotation), DEG2RAD(hinge.hingeAxis.maxRotation) );
}
hk_Local_Constraint_System *lcs = constraint_group ? constraint_group->GetLCS() : NULL;
if ( !lcs )
{
hk_Local_Constraint_System_BP bp;
lcs = new hk_Local_Constraint_System( hkEnvironment, &bp );
m_HkLCS = lcs;
}
if ( breakable )
{
hk_Hinge_Constraint *pHinge = new hk_Hinge_Constraint( hkEnvironment, builder.get_blueprint(), (hk_Rigid_Body*)m_pObjReference->GetObject(), (hk_Rigid_Body*)m_pObjAttached->GetObject() );
m_HkConstraint = CreateBreakableConstraint( pHinge, lcs, hinge.constraint );
}
else
{
m_HkConstraint = new hk_Hinge_Constraint( lcs, builder.get_blueprint(), (hk_Rigid_Body*)m_pObjReference->GetObject(), (hk_Rigid_Body*)m_pObjAttached->GetObject() );
}
if ( m_HkLCS && hinge.constraint.isActive )
{
m_HkLCS->activate();
}
m_HkConstraint->set_client_data( (void *)this );
}
// fixed constraint
void CPhysicsConstraint::InitFixed( IVP_Environment *pEnvironment, CPhysicsConstraintGroup *constraint_group, const constraint_fixedparams_t &fixed )
{
m_constraintType = CONSTRAINT_FIXED;
hk_Environment *hkEnvironment = static_cast<hk_Environment *>(pEnvironment);
bool breakable = IsBreakableConstraint( fixed.constraint );
hk_Fixed_BP fixed_bp;
ConvertHLLocalMatrixToHavanaLocal( fixed.attachedRefXform, fixed_bp.m_transform_os_ks );
fixed_bp.m_tau = fixed.constraint.strength;
hk_Local_Constraint_System *lcs = constraint_group ? constraint_group->GetLCS() : NULL;
if ( !lcs )
{
hk_Local_Constraint_System_BP bp;
lcs = new hk_Local_Constraint_System( hkEnvironment, &bp );
m_HkLCS = lcs;
}
if ( breakable )
{
hk_Constraint *pFixed = new hk_Fixed_Constraint( hkEnvironment, &fixed_bp, (hk_Rigid_Body*)m_pObjReference->GetObject(), (hk_Rigid_Body*)m_pObjAttached->GetObject() );
m_HkConstraint = CreateBreakableConstraint( pFixed, lcs, fixed.constraint );
}
else
{
m_HkConstraint = new hk_Fixed_Constraint( lcs, &fixed_bp, (hk_Rigid_Body*)m_pObjReference->GetObject(), (hk_Rigid_Body*)m_pObjAttached->GetObject() );
}
if ( m_HkLCS && fixed.constraint.isActive )
{
m_HkLCS->activate();
}
m_HkConstraint->set_client_data( (void *)this );
}
void CPhysicsConstraint::InitBallsocket( IVP_Environment *pEnvironment, CPhysicsConstraintGroup *constraint_group, const constraint_ballsocketparams_t &ballsocket )
{
m_constraintType = CONSTRAINT_BALLSOCKET;
hk_Environment *hkEnvironment = static_cast<hk_Environment *>(pEnvironment);
bool breakable = IsBreakableConstraint( ballsocket.constraint );
hk_Ball_Socket_BP builder;
for ( int i = 0; i < 2; i++ )
{
hk_Vector3 hkConstraintLocal;
ConvertPositionToIVP( ballsocket.constraintPosition[i], hkConstraintLocal );
builder.set_position_os( i, hkConstraintLocal );
}
builder.m_strength = ballsocket.constraint.strength;
hk_Local_Constraint_System *lcs = constraint_group ? constraint_group->GetLCS() : NULL;
if ( !lcs )
{
hk_Local_Constraint_System_BP bp;
lcs = new hk_Local_Constraint_System( hkEnvironment, &bp );
m_HkLCS = lcs;
}
if ( breakable )
{
hk_Ball_Socket_Constraint *pConstraint = new hk_Ball_Socket_Constraint( hkEnvironment, &builder, (hk_Rigid_Body*)m_pObjReference->GetObject(), (hk_Rigid_Body*)m_pObjAttached->GetObject() );
m_HkConstraint = CreateBreakableConstraint( pConstraint, lcs, ballsocket.constraint );
}
else
{
m_HkConstraint = new hk_Ball_Socket_Constraint( lcs, &builder, (hk_Rigid_Body*)m_pObjReference->GetObject(), (hk_Rigid_Body*)m_pObjAttached->GetObject() );
}
if ( m_HkLCS && ballsocket.constraint.isActive )
{
m_HkLCS->activate();
}
m_HkConstraint->set_client_data( (void *)this );
}
void CPhysicsConstraint::InitSliding( IVP_Environment *pEnvironment, CPhysicsConstraintGroup *constraint_group, const constraint_slidingparams_t &sliding )
{
m_constraintType = CONSTRAINT_SLIDING;
hk_Environment *hkEnvironment = static_cast<hk_Environment *>(pEnvironment);
bool breakable = IsBreakableConstraint( sliding.constraint );
hk_Prismatic_BP prismatic_bp;
hk_Transform t;
ConvertHLLocalMatrixToHavanaLocal( sliding.attachedRefXform, t );
prismatic_bp.m_transform_Ros_Aos.m_translation = t.get_translation();
prismatic_bp.m_transform_Ros_Aos.m_rotation.set( t );
IVP_U_Float_Point refAxisDir;
ConvertDirectionToIVP( sliding.slideAxisRef, refAxisDir );
prismatic_bp.m_axis_Ros = vec(refAxisDir);
prismatic_bp.m_tau = sliding.constraint.strength;
hk_Constraint_Limit_BP bp;
if ( sliding.limitMin != sliding.limitMax )
{
bp.set_limits( ConvertDistanceToIVP(sliding.limitMin), ConvertDistanceToIVP(sliding.limitMax) );
}
if ( sliding.friction )
{
if ( sliding.velocity )
{
bp.set_motor( ConvertDistanceToIVP(sliding.velocity), ConvertDistanceToIVP(sliding.friction) );
}
else
{
bp.set_friction( ConvertDistanceToIVP(sliding.friction) );
}
}
prismatic_bp.m_limit.init_limit( bp, 1.0 );
hk_Local_Constraint_System *lcs = constraint_group ? constraint_group->GetLCS() : NULL;
if ( !lcs )
{
hk_Local_Constraint_System_BP bp;
lcs = new hk_Local_Constraint_System( hkEnvironment, &bp );
m_HkLCS = lcs;
}
if ( breakable )
{
hk_Constraint *pFixed = new hk_Prismatic_Constraint( hkEnvironment, &prismatic_bp, (hk_Rigid_Body*)m_pObjReference->GetObject(), (hk_Rigid_Body*)m_pObjAttached->GetObject() );
m_HkConstraint = CreateBreakableConstraint( pFixed, lcs, sliding.constraint );
}
else
{
m_HkConstraint = new hk_Prismatic_Constraint( lcs, &prismatic_bp, (hk_Rigid_Body*)m_pObjReference->GetObject(), (hk_Rigid_Body*)m_pObjAttached->GetObject() );
}
if ( m_HkLCS && sliding.constraint.isActive )
{
m_HkLCS->activate();
}
m_HkConstraint->set_client_data( (void *)this );
}
void CPhysicsConstraint::InitPulley( IVP_Environment *pEnvironment, CPhysicsConstraintGroup *constraint_group, const constraint_pulleyparams_t &pulley )
{
m_constraintType = CONSTRAINT_PULLEY;
hk_Environment *hkEnvironment = static_cast<hk_Environment *>(pEnvironment);
bool breakable = IsBreakableConstraint( pulley.constraint );
hk_Pulley_BP pulley_bp;
pulley_bp.m_tau = pulley.constraint.strength;
//pulley_bp.m_strength = pulley.constraint.strength;
pulley_bp.m_gearing = pulley.gearRatio;
pulley_bp.m_is_rigid = pulley.isRigid;
// Get the current length of rope
pulley_bp.m_length = ConvertDistanceToIVP( pulley.totalLength );
// set the anchor positions in object space
ConvertPositionToIVP( pulley.objectPosition[0], pulley_bp.m_translation_os_ks[0] );
ConvertPositionToIVP( pulley.objectPosition[1], pulley_bp.m_translation_os_ks[1] );
// set the pully positions in world space
ConvertPositionToIVP( pulley.pulleyPosition[0], pulley_bp.m_worldspace_point[0] );
ConvertPositionToIVP( pulley.pulleyPosition[1], pulley_bp.m_worldspace_point[1] );
hk_Local_Constraint_System *lcs = constraint_group ? constraint_group->GetLCS() : NULL;
if ( !lcs )
{
hk_Local_Constraint_System_BP bp;
lcs = new hk_Local_Constraint_System( hkEnvironment, &bp );
m_HkLCS = lcs;
}
if ( breakable )
{
hk_Constraint *pPulley = new hk_Pulley_Constraint( hkEnvironment, &pulley_bp, (hk_Rigid_Body*)m_pObjReference->GetObject(), (hk_Rigid_Body*)m_pObjAttached->GetObject() );
m_HkConstraint = CreateBreakableConstraint( pPulley, lcs, pulley.constraint );
}
else
{
m_HkConstraint = new hk_Pulley_Constraint( lcs, &pulley_bp, (hk_Rigid_Body*)m_pObjReference->GetObject(), (hk_Rigid_Body*)m_pObjAttached->GetObject() );
}
if ( m_HkLCS && pulley.constraint.isActive )
{
m_HkLCS->activate();
}
m_HkConstraint->set_client_data( (void *)this );
}