-
Notifications
You must be signed in to change notification settings - Fork 836
Expand file tree
/
Copy pathconstraint.lua
More file actions
1684 lines (1235 loc) · 49.2 KB
/
constraint.lua
File metadata and controls
1684 lines (1235 loc) · 49.2 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
if ( SERVER ) then
-- If you're a server admin and you want your physics to spazz out less you can
-- use the convar. The higher you set it the more accurate physics will be.
-- This is set to 4 by default, since we are a physics mod.
CreateConVar( "gmod_physiterations", "4", { FCVAR_REPLICATED, FCVAR_ARCHIVE }, "Additional solver iterations make constraint systems more stable." )
end
module( "constraint", package.seeall )
-- Clients don't need this module.
if ( CLIENT ) then return end
-- I think 128 constraints is around the max that causes the crash
-- So at this number we'll refuse to add more to the system
local MAX_CONSTRAINTS_PER_SYSTEM = 100
local CurrentSystem = NULL
local SystemLookup = {}
-- HACK: Entity.IsConstraint is false for these
local constraintClasses = {}
constraintClasses[ "phys_spring" ] = true
constraintClasses[ "phys_slideconstraint" ] = true
constraintClasses[ "phys_torque" ] = true
constraintClasses[ "logic_collision_pair" ] = true
hook.Add( "EntityRemoved", "Constraint Library - ConstraintRemoved", function( ent )
-- Remove this constraint from Entity.Constraints table of the constrained entities
if ( ent:IsConstraint() || constraintClasses[ ent:GetClass() ] ) then
for i = 1, 6 do
local entX = ent[ "Ent" .. i ]
if ( IsValid( entX ) and entX.Constraints ) then
table.RemoveByValue( entX.Constraints, ent )
end
end
end
-- Update constraint system entity's constraint count
local constSystem = SystemLookup[ ent ]
if ( !IsValid( constSystem ) ) then return end
constSystem.__ConstraintCount = ( constSystem.__ConstraintCount or 0 ) - 1
if ( constSystem.__ConstraintCount <= 0 ) then
constSystem.__BadConstraintSystem = true
constSystem:Remove()
end
end )
local function ConstraintCreated( constr )
assert( IsValid( CurrentSystem ) )
SystemLookup[ constr ] = CurrentSystem
CurrentSystem.__ConstraintCount = ( CurrentSystem.__ConstraintCount or 0 ) + 1
end
--[[----------------------------------------------------------------------
CreateConstraintSystem
------------------------------------------------------------------------]]
local function CreateConstraintSystem()
local iterations = GetConVarNumber( "gmod_physiterations" )
local csystem = ents.Create( "phys_constraintsystem" )
if ( !IsValid( csystem ) ) then return end
csystem:SetKeyValue( "additionaliterations", iterations )
csystem:Spawn()
csystem:Activate()
csystem.__ConstraintCount = 0
return csystem
end
--[[----------------------------------------------------------------------
FindOrCreateConstraintSystem
Takes 2 entities. If the entities don't have a constraint system
associated with them it creates one and associates it with them.
It then returns the constraint system
------------------------------------------------------------------------]]
local function FindOrCreateConstraintSystem( ent1, ent2 )
local system = NULL
ent2 = ent2 or ent1
-- Does Ent1 have a constraint system?
if ( !ent1:IsWorld() && IsValid( ent1.ConstraintSystem ) && !ent1.ConstraintSystem.__BadConstraintSystem ) then
system = ent1.ConstraintSystem
end
-- Don't add to this system - we have too many constraints on it already.
if ( IsValid( system ) && ( system.__ConstraintCount or 0 ) >= MAX_CONSTRAINTS_PER_SYSTEM ) then system = nil end
-- Does Ent2 have a constraint system?
if ( !IsValid( system ) && !ent2:IsWorld() && IsValid( ent2.ConstraintSystem ) && !ent2.ConstraintSystem.__BadConstraintSystem ) then
system = ent2.ConstraintSystem
end
-- Don't add to this system - we have too many constraints on it already.
if ( IsValid( system ) && ( system.__ConstraintCount or 0 ) >= MAX_CONSTRAINTS_PER_SYSTEM ) then system = nil end
-- No constraint system yet (Or they're both full) - make a new one
if ( !IsValid( system ) ) then
--Msg( "New Constrant System\n" )
system = CreateConstraintSystem()
end
ent1.ConstraintSystem = system
ent2.ConstraintSystem = system
return system
end
--[[----------------------------------------------------------------------
onStartConstraint( Ent1, Ent2 )
Should be called before creating a constraint
------------------------------------------------------------------------]]
local function onStartConstraint( ent1, ent2 )
-- Get constraint system
CurrentSystem = FindOrCreateConstraintSystem( ent1, ent2 )
-- Any constraints called after this call will use this system
SetPhysConstraintSystem( CurrentSystem )
end
--[[----------------------------------------------------------------------
onFinishConstraint()
Should be called before creating a constraint
------------------------------------------------------------------------]]
local function onFinishConstraint()
-- Turn off constraint system override
CurrentSystem = nil
SetPhysConstraintSystem( NULL )
end
local function SetPhysicsCollisions( ent, collisions )
if ( !IsValid( ent ) or !IsValid( ent:GetPhysicsObject() ) ) then return end
ent:GetPhysicsObject():EnableCollisions( collisions )
end
--[[----------------------------------------------------------------------
RemoveConstraints( Ent, Type )
Removes all constraints of type from entity
------------------------------------------------------------------------]]
function RemoveConstraints( ent, const_type )
if ( !ent.Constraints ) then return end
local c = ent.Constraints
local i = 0
for k, v in pairs( c ) do
if ( !IsValid( v ) ) then
c[ k ] = nil
elseif ( v.Type == const_type ) then
-- Make sure physics collisions are on!
-- If we don't the unconstrained objects will fall through the world forever.
SetPhysicsCollisions( v.Ent1, true )
SetPhysicsCollisions( v.Ent2, true )
c[ k ] = nil
v:Remove()
i = i + 1
end
end
if ( table.IsEmpty( c ) ) then
-- Update the network var and clear the constraints table.
ent:IsConstrained()
end
local bool = i != 0
return bool, i
end
--[[----------------------------------------------------------------------
RemoveAll( Ent )
Removes all constraints from entity
------------------------------------------------------------------------]]
function RemoveAll( ent )
if ( !ent.Constraints ) then return end
local c = ent.Constraints
local i = 0
for k, v in pairs( c ) do
if ( IsValid( v ) ) then
-- Make sure physics collisions are on!
-- If we don't the unconstrained objects will fall through the world forever.
SetPhysicsCollisions( v.Ent1, true )
SetPhysicsCollisions( v.Ent2, true )
v:Remove()
i = i + 1
end
end
-- Update the network var and clear the constraints table.
ent:IsConstrained()
return ( i != 0 ), i
end
--[[----------------------------------------------------------------------
Find( Ent1, Ent2, Type, Bone1, Bone2 )
Returns a constraint of given type between the two entities, if one exists
------------------------------------------------------------------------]]
function Find( ent1, ent2, const_type, bone1, bone2 )
if ( !ent1.Constraints ) then return end
for k, v in pairs( ent1.Constraints ) do
if ( IsValid( v ) && v.Type == const_type ) then
if ( v.Ent1 == ent1 && v.Ent2 == ent2 && v.Bone1 == bone1 && v.Bone2 == bone2 ) then
return v
end
if ( v.Ent2 == ent1 && v.Ent1 == ent2 && v.Bone2 == bone1 && v.Bone1 == bone2 ) then
return v
end
end
end
return nil
end
--[[----------------------------------------------------------------------
CanConstrain( Ent, Bone )
Returns false if we shouldn't be constraining this entity
------------------------------------------------------------------------]]
function CanConstrain( ent, bone )
if ( !ent ) then return false end
if ( !isnumber( bone ) ) then return false end
if ( !ent:IsWorld() && !ent:IsValid() ) then return false end
if ( !IsValid( ent:GetPhysicsObjectNum( bone ) ) ) then return false end
return true
end
--[[----------------------------------------------------------------------
CalcElasticConsts( ... )
This attempts to scale the elastic constraints such as the winch
to keep a stable but responsive constraint..
------------------------------------------------------------------------]]
local function CalcElasticConsts( phys1, phys2, ent1, ent2, fixed )
local minMass = 0
if ( ent1:IsWorld() ) then minMass = phys2:GetMass()
elseif ( ent2:IsWorld() ) then minMass = phys1:GetMass()
else
minMass = math.min( phys1:GetMass(), phys2:GetMass() )
end
-- const, damp
local const = minMass * 100
local damp = const * 0.2
if ( !fixed ) then
const = minMass * 50
damp = const * 0.1
end
return const, damp
end
--[[----------------------------------------------------------------------
CreateKeyframeRope( ... )
Creates a rope without any constraint
------------------------------------------------------------------------]]
function CreateKeyframeRope( Pos, width, material, Constraint, Ent1, LPos1, Bone1, Ent2, LPos2, Bone2, kv )
-- No rope if 0 or minus
if ( width <= 0 ) then return end
-- Clamp the rope to a sensible width
width = math.Clamp( width, 0.2, 100 )
local rope = ents.Create( "keyframe_rope" )
if ( !IsValid( rope ) ) then return end
rope:SetPos( Pos )
rope:SetKeyValue( "Width", width )
rope:SetKeyValue( "TextureScale", "1.6" ) -- Preserve old scaling before 28 July 2025
if ( isstring( material ) and material != "" ) then
-- Check if the material looks right. Do not allow missing materials. Do not allow weird shaders.
-- This is not ideal because it is tested on the server, but whatever. Better than checking "RopeMaterials" list.
local mat = Material( material )
local shader = mat:GetShader():lower()
local shaderGood = shader == "cable_dx9" or shader == "cable_dx8" or shader == "cable" or shader == "unlitgeneric" or shader == "splinerope"
if ( mat and !mat:IsError() and shaderGood ) then
rope:SetKeyValue( "RopeMaterial", material )
end
end
-- Attachment point 1
rope:SetEntity( "StartEntity", Ent1 )
rope:SetKeyValue( "StartOffset", tostring( LPos1 ) )
rope:SetKeyValue( "StartBone", Bone1 )
-- Attachment point 2
rope:SetEntity( "EndEntity", Ent2 )
rope:SetKeyValue( "EndOffset", tostring( LPos2 ) )
rope:SetKeyValue( "EndBone", Bone2 )
if ( kv ) then
for k, v in pairs( kv ) do
rope:SetKeyValue( k, tostring( v ) )
end
end
rope:Spawn()
rope:Activate()
-- Delete the rope if the attachments get killed
Ent1:DeleteOnRemove( rope )
Ent2:DeleteOnRemove( rope )
if ( IsValid( Constraint ) ) then Constraint:DeleteOnRemove( rope ) end
return rope
end
--[[----------------------------------------------------------------------
AddConstraintTable( Ent, Constraint, Ent2, Ent3, Ent4 )
Stores info about the constraints on the entity's table
------------------------------------------------------------------------]]
function AddConstraintTable( ent, constraint, ent2, ent3, ent4 )
if ( !IsValid( constraint ) ) then return end
if ( IsValid( ent ) || ( ent && ent:IsWorld() ) ) then
ent.Constraints = ent.Constraints or {}
table.insert( ent.Constraints, constraint )
ent:DeleteOnRemove( constraint )
end
if ( ent2 && ent2 != ent ) then
AddConstraintTable( ent2, constraint, ent3, ent4 )
end
end
--[[----------------------------------------------------------------------
AddConstraintTableNoDelete( Ent, Constraint, Ent2, Ent3, Ent4 )
Stores info about the constraints on the entity's table
------------------------------------------------------------------------]]
function AddConstraintTableNoDelete( ent, constraint, ent2, ent3, ent4 )
if ( !IsValid( constraint ) ) then return end
if ( IsValid( ent ) || ( ent && ent:IsWorld() ) ) then
ent.Constraints = ent.Constraints or {}
table.insert( ent.Constraints, constraint )
end
if ( ent2 && ent2 != ent ) then
AddConstraintTableNoDelete( ent2, constraint, ent3, ent4 )
end
end
--[[----------------------------------------------------------------------
Weld( ... )
Creates a solid weld constraint
------------------------------------------------------------------------]]
function Weld( Ent1, Ent2, Bone1, Bone2, forcelimit, nocollide, deleteonbreak )
if ( Ent1 == Ent2 && Bone1 == Bone2 ) then return false end
if ( !CanConstrain( Ent1, Bone1 ) ) then return false end
if ( !CanConstrain( Ent2, Bone2 ) ) then return false end
if ( Find( Ent1, Ent2, "Weld", Bone1, Bone2 ) ) then
-- A weld already exists between these two physics objects.
-- There's totally no point in re-creating it. It doesn't make
-- the weld any stronger - that's just an urban legend.
return false
end
-- Don't weld World to objects, weld objects to World!
-- Prevents crazy physics on some props
if ( Ent1:IsWorld() ) then
Ent1 = Ent2
Ent2 = game.GetWorld()
end
local Phys1 = Ent1:GetPhysicsObjectNum( Bone1 )
local Phys2 = Ent2:GetPhysicsObjectNum( Bone2 )
onStartConstraint( Ent1, Ent2 )
-- Create the constraint
local Constraint = ents.Create( "phys_constraint" )
ConstraintCreated( Constraint )
if ( forcelimit ) then Constraint:SetKeyValue( "forcelimit", forcelimit ) end
if ( nocollide ) then Constraint:SetKeyValue( "spawnflags", 1 ) end
Constraint:SetPhysConstraintObjects( Phys2, Phys1 )
Constraint:Spawn()
Constraint:Activate()
onFinishConstraint()
-- Optionally delete Ent1 when the weld is broken
-- This is to fix bug #310
if ( deleteonbreak ) then
Ent2:DeleteOnRemove( Ent1 )
end
local ctable = {
Type = "Weld",
Ent1 = Ent1,
Ent2 = Ent2,
Bone1 = Bone1,
Bone2 = Bone2,
forcelimit = forcelimit,
nocollide = nocollide,
deleteonbreak = deleteonbreak
}
Constraint:SetTable( ctable )
AddConstraintTable( Ent1, Constraint, Ent2 )
Phys1:Wake()
Phys2:Wake()
return Constraint
end
duplicator.RegisterConstraint( "Weld", Weld, "Ent1", "Ent2", "Bone1", "Bone2", "forcelimit", "nocollide", "deleteonbreak" )
--[[----------------------------------------------------------------------
Rope( ... )
Creates a rope constraint - with rope!
------------------------------------------------------------------------]]
function Rope( Ent1, Ent2, Bone1, Bone2, LPos1, LPos2, length, addLength, forcelimit, width, material, rigid, color )
if ( !CanConstrain( Ent1, Bone1 ) ) then return false end
if ( !CanConstrain( Ent2, Bone2 ) ) then return false end
local Phys1 = Ent1:GetPhysicsObjectNum( Bone1 )
local Phys2 = Ent2:GetPhysicsObjectNum( Bone2 )
local WPos1 = Phys1:LocalToWorld( LPos1 )
local WPos2 = Phys2:LocalToWorld( LPos2 )
addLength = math.Clamp( addLength or 0, -56756, 56756 )
local Constraint = nil
-- Make Constraint
if ( Phys1 != Phys2 ) then
onStartConstraint( Ent1, Ent2 )
-- Create the constraint
Constraint = ents.Create( "phys_lengthconstraint" )
ConstraintCreated( Constraint )
Constraint:SetPos( WPos1 )
Constraint:SetKeyValue( "attachpoint", tostring( WPos2 ) )
Constraint:SetKeyValue( "minlength", "0.0" )
Constraint:SetKeyValue( "length", length + addLength )
if ( forcelimit ) then Constraint:SetKeyValue( "forcelimit", forcelimit ) end
if ( rigid ) then Constraint:SetKeyValue( "spawnflags", 2 ) end
Constraint:SetPhysConstraintObjects( Phys1, Phys2 )
Constraint:Spawn()
Constraint:Activate()
onFinishConstraint()
end
-- Make Rope
local kv = {
Length = length + addLength,
Collide = 1
}
if ( rigid ) then kv.Type = 2 end
local rope = CreateKeyframeRope( WPos1, width, material, Constraint, Ent1, LPos1, Bone1, Ent2, LPos2, Bone2, kv )
if ( IsValid( rope ) && color ) then rope:SetColor( color ) end
-- What the fuck
if ( !Constraint ) then Constraint, rope = rope, nil end
if ( IsValid( Constraint ) ) then
local ctable = {
Type = "Rope",
Ent1 = Ent1,
Ent2 = Ent2,
Bone1 = Bone1,
Bone2 = Bone2,
LPos1 = LPos1,
LPos2 = LPos2,
length = length,
addlength = addLength,
forcelimit = forcelimit,
width = width,
material = material,
rigid = rigid,
color = color
}
Constraint:SetTable( ctable )
AddConstraintTable( Ent1, Constraint, Ent2 )
end
return Constraint, rope
end
duplicator.RegisterConstraint( "Rope", Rope, "Ent1", "Ent2", "Bone1", "Bone2", "LPos1", "LPos2", "length", "addlength", "forcelimit", "width", "material", "rigid", "color" )
--[[----------------------------------------------------------------------
Elastic( ... )
Creates an elastic constraint
------------------------------------------------------------------------]]
function Elastic( Ent1, Ent2, Bone1, Bone2, LPos1, LPos2, constant, damping, rdamping, material, width, stretchonly, color )
if ( !CanConstrain( Ent1, Bone1 ) ) then return false end
if ( !CanConstrain( Ent2, Bone2 ) ) then return false end
local Phys1 = Ent1:GetPhysicsObjectNum( Bone1 )
local Phys2 = Ent2:GetPhysicsObjectNum( Bone2 )
local WPos1 = Phys1:LocalToWorld( LPos1 )
local WPos2 = Phys2:LocalToWorld( LPos2 )
local Constraint = nil
local rope = nil
-- Make Constraint
if ( Phys1 != Phys2 ) then
onStartConstraint( Ent1, Ent2 )
Constraint = ents.Create( "phys_spring" )
ConstraintCreated( Constraint )
Constraint:SetPos( WPos1 )
Constraint:SetKeyValue( "springaxis", tostring( WPos2 ) )
Constraint:SetKeyValue( "constant", constant )
Constraint:SetKeyValue( "damping", damping )
Constraint:SetKeyValue( "relativedamping", rdamping )
Constraint:SetPhysConstraintObjects( Phys1, Phys2 )
if ( stretchonly == 1 or stretchonly == true ) then
Constraint:SetKeyValue( "spawnflags", 1 )
end
Constraint:Spawn()
Constraint:Activate()
onFinishConstraint()
local ctable = {
Type = "Elastic",
Ent1 = Ent1,
Ent2 = Ent2,
Bone1 = Bone1,
Bone2 = Bone2,
LPos1 = LPos1,
LPos2 = LPos2,
constant = constant,
damping = damping,
rdamping = rdamping,
material = material,
width = width,
length = ( WPos1 - WPos2 ):Length(),
stretchonly = stretchonly,
color = color
}
Constraint:SetTable( ctable )
AddConstraintTable( Ent1, Constraint, Ent2 )
-- Make Rope
local kv = {
Collide = 1,
Type = 0
}
rope = CreateKeyframeRope( WPos1, width, material, Constraint, Ent1, LPos1, Bone1, Ent2, LPos2, Bone2, kv )
if ( IsValid( rope ) && color ) then rope:SetColor( color ) end
end
return Constraint, rope
end
duplicator.RegisterConstraint( "Elastic", Elastic, "Ent1", "Ent2", "Bone1", "Bone2", "LPos1", "LPos2", "constant", "damping", "rdamping", "material", "width", "stretchonly", "color" )
--[[----------------------------------------------------------------------
Keepupright( ... )
Creates a KeepUpright constraint
------------------------------------------------------------------------]]
function Keepupright( Ent, Ang, Bone, angularlimit )
if ( !CanConstrain( Ent, Bone ) ) then return false end
-- This was once here. Is there any specific reason this was the case?
--if ( Ent:GetClass() != "prop_physics" && Ent:GetClass() != "prop_ragdoll" ) then return false end
if ( Ent:IsPlayer() || Ent:IsWorld() ) then return false end
if ( !angularlimit or angularlimit < 0 ) then return end
local Phys = Ent:GetPhysicsObjectNum( Bone )
-- Remove any KU's already on entity
RemoveConstraints( Ent, "Keepupright" )
onStartConstraint( Ent )
local Constraint = ents.Create( "phys_keepupright" )
ConstraintCreated( Constraint )
Constraint:SetAngles( Ang )
Constraint:SetKeyValue( "angularlimit", angularlimit )
Constraint:SetPhysConstraintObjects( Phys, Phys )
Constraint:Spawn()
Constraint:Activate()
onFinishConstraint()
local ctable = {
Type = "Keepupright",
Ent1 = Ent,
Ang = Ang,
Bone = Bone,
angularlimit = angularlimit
}
Constraint:SetTable( ctable )
AddConstraintTable( Ent, Constraint )
--
-- This is a hack to keep the KeepUpright context menu in sync..
--
Ent:SetNWBool( "IsUpright", true )
return Constraint
end
duplicator.RegisterConstraint( "Keepupright", Keepupright, "Ent1", "Ang", "Bone", "angularlimit" )
function CreateStaticAnchorPoint( pos )
-- Creates an invisible frozen, not interactive prop.
local anchor = ents.Create( "gmod_anchor" )
if ( !IsValid( anchor ) ) then return end
anchor:SetPos( pos )
anchor:Spawn()
anchor:Activate()
return anchor, anchor:GetPhysicsObject(), 0, vector_origin
end
--[[----------------------------------------------------------------------
Slider( ... )
Creates a slider constraint
------------------------------------------------------------------------]]
function Slider( Ent1, Ent2, Bone1, Bone2, LPos1, LPos2, width, material, color )
-- TODO: If we get rid of sliders we can get rid of gmod_anchor too!
if ( !CanConstrain( Ent1, Bone1 ) ) then return false end
if ( !CanConstrain( Ent2, Bone2 ) ) then return false end
local Phys1 = Ent1:GetPhysicsObjectNum( Bone1 )
local Phys2 = Ent2:GetPhysicsObjectNum( Bone2 )
local WPos1 = Phys1:LocalToWorld( LPos1 )
local WPos2 = Phys2:LocalToWorld( LPos2 )
local StaticAnchor = nil
-- Make Constraint
if ( Phys1 == Phys2 ) then return end
-- Start World Hack.
-- Attaching a slider to the world makes it really sucks so we make
-- a prop and attach to that.
if ( Ent1:IsWorld() ) then
Ent1, Phys1, Bone1, LPos1 = CreateStaticAnchorPoint( WPos1 )
StaticAnchor = Ent1
end
if ( Ent2:IsWorld() ) then
Ent2, Phys2, Bone2, LPos2 = CreateStaticAnchorPoint( WPos2 )
StaticAnchor = Ent2
end
-- End World Hack.
onStartConstraint( Ent1, Ent2 )
local Constraint = ents.Create( "phys_slideconstraint" )
ConstraintCreated( Constraint )
Constraint:SetPos( WPos1 )
Constraint:SetKeyValue( "slideaxis", tostring( WPos2 ) )
Constraint:SetPhysConstraintObjects( Phys1, Phys2 )
Constraint:Spawn()
Constraint:Activate()
onFinishConstraint()
-- Make Rope
local kv = {
Collide = 0,
Type = 2,
Subdiv = 1,
}
local rope = CreateKeyframeRope( WPos1, width, material, Constraint, Ent1, LPos1, Bone1, Ent2, LPos2, Bone2, kv )
if ( IsValid( rope ) && color ) then rope:SetColor( color ) end
-- If we have a static anchor - delete it when we die.
if ( StaticAnchor ) then
Constraint:DeleteOnRemove( StaticAnchor )
end
local ctable = {
Type = "Slider",
Ent1 = Ent1,
Ent2 = Ent2,
Bone1 = Bone1,
Bone2 = Bone2,
LPos1 = LPos1,
LPos2 = LPos2,
width = width,
material = material,
color = color
}
Constraint:SetTable( ctable )
AddConstraintTable( Ent1, Constraint, Ent2 )
return Constraint, rope
end
duplicator.RegisterConstraint( "Slider", Slider, "Ent1", "Ent2", "Bone1", "Bone2", "LPos1", "LPos2", "width", "material", "color" )
--[[----------------------------------------------------------------------
Axis( ... )
Creates an axis constraint
------------------------------------------------------------------------]]
function Axis( Ent1, Ent2, Bone1, Bone2, LPos1, LPos2, forcelimit, torquelimit, friction, nocollide, LocalAxis, DontAddTable )
if ( !CanConstrain( Ent1, Bone1 ) ) then return false end
if ( !CanConstrain( Ent2, Bone2 ) ) then return false end
local Phys1 = Ent1:GetPhysicsObjectNum( Bone1 )
local Phys2 = Ent2:GetPhysicsObjectNum( Bone2 )
local WPos1 = Phys1:LocalToWorld( LPos1 )
local WPos2 = Phys2:LocalToWorld( LPos2 )
if ( Phys1 == Phys2 ) then return false end
-- If we have a LocalAxis, use that
if ( LocalAxis ) then
WPos2 = Phys1:LocalToWorld( LocalAxis )
end
onStartConstraint( Ent1, Ent2 )
local Constraint = ents.Create( "phys_hinge" )
ConstraintCreated( Constraint )
Constraint:SetPos( WPos1 )
Constraint:SetKeyValue( "hingeaxis", tostring( WPos2 ) )
if ( forcelimit && forcelimit > 0 ) then Constraint:SetKeyValue( "forcelimit", forcelimit ) end
if ( torquelimit && torquelimit > 0 ) then Constraint:SetKeyValue( "torquelimit", torquelimit ) end
if ( friction && friction > 0 ) then Constraint:SetKeyValue( "hingefriction", friction ) end
if ( nocollide && nocollide > 0 ) then Constraint:SetKeyValue( "spawnflags", 1 ) end
Constraint:SetPhysConstraintObjects( Phys1, Phys2 )
Constraint:Spawn()
Constraint:Activate()
onFinishConstraint()
local ctable = {
Type = "Axis",
Ent1 = Ent1,
Ent2 = Ent2,
Bone1 = Bone1,
Bone2 = Bone2,
LPos1 = LPos1,
LPos2 = LPos2,
forcelimit = forcelimit,
torquelimit = torquelimit,
friction = friction,
nocollide = nocollide,
LocalAxis = Phys1:WorldToLocal( WPos2 )
}
Constraint:SetTable( ctable )
if ( !DontAddTable ) then
AddConstraintTable( Ent1, Constraint, Ent2 )
end
return Constraint
end
duplicator.RegisterConstraint( "Axis", Axis, "Ent1", "Ent2", "Bone1", "Bone2", "LPos1", "LPos2", "forcelimit", "torquelimit", "friction", "nocollide", "LocalAxis", "DontAddTable" )
--[[----------------------------------------------------------------------
AdvBallsocket( ... )
Creates an advanced ballsocket (ragdoll) constraint
------------------------------------------------------------------------]]
function AdvBallsocket( Ent1, Ent2, Bone1, Bone2, LPos1, LPos2, forcelimit, torquelimit, xmin, ymin, zmin, xmax, ymax, zmax, xfric, yfric, zfric, onlyrotation, nocollide )
if ( !CanConstrain( Ent1, Bone1 ) ) then return false end
if ( !CanConstrain( Ent2, Bone2 ) ) then return false end
local Phys1 = Ent1:GetPhysicsObjectNum( Bone1 )
local Phys2 = Ent2:GetPhysicsObjectNum( Bone2 )
local WPos1 = Phys1:LocalToWorld( LPos1 )
-- local WPos2 = Phys2:LocalToWorld( LPos2 )
if ( Phys1 == Phys2 ) then return false end
-- Make Constraint
onStartConstraint( Ent1, Ent2 )
local flags = 0
if ( onlyrotation && onlyrotation > 0 ) then flags = flags + 2 end
if ( nocollide && nocollide > 0 ) then flags = flags + 1 end
local Constraint = ents.Create( "phys_ragdollconstraint" )
ConstraintCreated( Constraint )
Constraint:SetPos( WPos1 )
Constraint:SetKeyValue( "xmin", xmin )
Constraint:SetKeyValue( "xmax", xmax )
Constraint:SetKeyValue( "ymin", ymin )
Constraint:SetKeyValue( "ymax", ymax )
Constraint:SetKeyValue( "zmin", zmin )
Constraint:SetKeyValue( "zmax", zmax )
if ( xfric && xfric > 0 ) then Constraint:SetKeyValue( "xfriction", xfric ) end
if ( yfric && yfric > 0 ) then Constraint:SetKeyValue( "yfriction", yfric ) end
if ( zfric && zfric > 0 ) then Constraint:SetKeyValue( "zfriction", zfric ) end
if ( forcelimit && forcelimit > 0 ) then Constraint:SetKeyValue( "forcelimit", forcelimit ) end
if ( torquelimit && torquelimit > 0 ) then Constraint:SetKeyValue( "torquelimit", torquelimit ) end
Constraint:SetKeyValue( "spawnflags", flags )
Constraint:SetPhysConstraintObjects( Phys1, Phys2 )
Constraint:Spawn()
Constraint:Activate()
onFinishConstraint()
local ctable = {
Type = "AdvBallsocket",
Ent1 = Ent1,
Ent2 = Ent2,
Bone1 = Bone1,
Bone2 = Bone2,
LPos1 = LPos1,
LPos2 = LPos2,
forcelimit = forcelimit,
torquelimit = torquelimit,
xmin = xmin,
ymin = ymin,
zmin = zmin,
xmax = xmax,
ymax = ymax,
zmax = zmax,
xfric = xfric,
yfric = yfric,
zfric = zfric,
onlyrotation = onlyrotation,
nocollide = nocollide
}
Constraint:SetTable( ctable )
AddConstraintTable( Ent1, Constraint, Ent2 )
return Constraint
end
duplicator.RegisterConstraint( "AdvBallsocket", AdvBallsocket, "Ent1", "Ent2", "Bone1", "Bone2", "LPos1", "LPos2", "forcelimit", "torquelimit", "xmin", "ymin", "zmin", "xmax", "ymax", "zmax", "xfric", "yfric", "zfric", "onlyrotation", "nocollide" )
--[[----------------------------------------------------------------------
NoCollide( ... )
Creates an nocollide `constraint'
------------------------------------------------------------------------]]
function NoCollide( Ent1, Ent2, Bone1, Bone2, disableOnRemove )
if ( !CanConstrain( Ent1, Bone1 ) ) then return false end
if ( !CanConstrain( Ent2, Bone2 ) ) then return false end
local Phys1 = Ent1:GetPhysicsObjectNum( Bone1 )
local Phys2 = Ent2:GetPhysicsObjectNum( Bone2 )
if ( Phys1 == Phys2 ) then return false end
if ( Find( Ent1, Ent2, "NoCollide", Bone1, Bone2 ) ) then return false end
-- Make Constraint
local constr = ents.Create( "logic_collision_pair" )
if ( !IsValid( constr ) ) then return end
constr:SetKeyValue( "startdisabled", 1 )
if ( disableOnRemove ) then constr:SetKeyValue( "disable_on_remove", 1 ) end
constr:SetPhysConstraintObjects( Phys1, Phys2 )
constr:Spawn()
constr:Activate()
constr:Input( "DisableCollisions" )
local ctable = {
Type = "NoCollide",
Ent1 = Ent1,
Ent2 = Ent2,
Bone1 = Bone1,
Bone2 = Bone2,
disableOnRemove = disableOnRemove
}
constr:SetTable( ctable )
AddConstraintTable( Ent1, constr, Ent2 )
return constr
end
duplicator.RegisterConstraint( "NoCollide", NoCollide, "Ent1", "Ent2", "Bone1", "Bone2", "disableOnRemove" )
--[[----------------------------------------------------------------------
MotorControl( pl, motor, onoff, dir )
Numpad controls for the motor constraints
------------------------------------------------------------------------]]
local function MotorControl( pl, motor, onoff, dir )
if ( !IsValid( motor ) ) then return false end
local activate = false
if ( motor.toggle == 1 || motor.toggle == true ) then
-- Toggle mode, only do something when the key is pressed
-- if the motor is off, turn it on, and vice-versa.
-- This only happens if the same key as the current
-- direction is pressed, otherwise the direction is changed
-- with the motor being left on.
if ( onoff ) then
if ( motor.direction == dir or !motor.is_on ) then
-- Direction is the same, Activate if the motor is off
-- Deactivate if the motor is on.
motor.is_on = !motor.is_on
activate = motor.is_on