-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathphysgun_two-way_coupling.txt
More file actions
205 lines (168 loc) · 7.47 KB
/
physgun_two-way_coupling.txt
File metadata and controls
205 lines (168 loc) · 7.47 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
@name F=MA Compliant Physgun
@inputs
@outputs
@persist BeamLength HeldEntity:entity HeldLocalPos:vector InitialGrabYaw InitialGrabAng:angle InitialGrabQuat:quaternion Holding PLAYER_MASS WORLD_MASS Respawning WheelState
@persist Attack1
@persist [Chip,Owner]:entity TickInterval
@model models/hunter/misc/sphere025x025.mdl
##############################
# F=MA Compliant Physgun #
# by: Fasteroid #
##############################
if( first() ){
PLAYER_MASS = 150
WORLD_MASS = 1500
Chip = entity()
Owner = owner()
Chip:setAlpha(0)
TickInterval = tickInterval()
runOnKeys(Owner,1)
# Thanks to @Jacbo (https://discord.com/users/347893446897434624) for this function!
function vector entity:pointVel(LocalPos:vector){
local AngVel = This:angVelVector()
return This:vel() + ( This:toWorldAxis( AngVel:normalized():cross(LocalPos) * AngVel:length() * _PI/180 ) )
}
function entity:perfectAngForce(Ang:angle){
local Torque = This:toLocalAxis(rotationVector(quat(Ang)/quat(This)))
This:applyTorque((Torque*100-This:angVelVector()*10)*This:inertia()*min(3,500/This:mass()))
}
function supressInitializationWarnings(){
BeamLength = 0 Respawning = 0 Holding = 0 HeldEntity = noentity()
}
}
event keyPressed(E:entity, Key:string, Pressed:number, Enum:string){
Enum = Enum # stupid discarded variable warning
if( E == Owner ){
if( Key == "mouse_wheel_up" ){
WheelState = Pressed
}
if( Key == "mouse_wheel_down" ){
WheelState = -Pressed
}
}
}
event tick(){
BeamLength = clamp( BeamLength + WheelState * 5, 40, 4096 )
if( Owner:weapon():type() == "weapon_physgun" & !Respawning ) {
Attack1 = Owner:keyAttack1()
Chip:propDraw(!Attack1)
Attack2 = Owner:keyAttack2()
Reload = Owner:keyReload()
Chip:propNotSolid(Attack1)
rangerFilter( array(Owner,Chip) )
EyeTrace = rangerOffset( 8192, Owner:shootPos() + Owner:vel() * TickInterval * 1.5, Owner:eye() )
### Other misc fixes
##
# ATTEMPT to fix props spawning inside our face
local SpawnedProp = EyeTrace:entity()
if( SpawnedProp:isValidPhysics() & (SpawnedProp:pos()-Owner:shootPos()==vec()) ){ # no chance a prop just got here by accident
local BoxSize = SpawnedProp:boxSize():rotate(SpawnedProp:angles())
rangerPersist(1)
rangerFilter( array(SpawnedProp,Owner,Chip) )
local SpawnTrace = rangerOffset( 4096, Owner:shootPos() + Owner:vel() * TickInterval * 1.5, Owner:eye() )
local SpawnPos = SpawnTrace:position() + SpawnTrace:hitNormal()*BoxSize*0.5
local FloorTrace = rangerOffset( BoxSize:z(), SpawnPos + SpawnTrace:hitNormal(), vec(0,0,-1) )
local PropBottomOffset = SpawnedProp:boxMin():rotate(SpawnedProp:angles())*vec(0,0,1)
rangerPersist(0)
if(FloorTrace:hit()){ SpawnPos = FloorTrace:position() - PropBottomOffset }
SpawnedProp:setPos( SpawnPos ) # move it to where garry's code would have spawned it
}
# fix the 'unfreeze connected' behavior
if( changed( Reload ) & Reload ){
local Ents = EyeTrace:entity():getConnectedEntities()
foreach(N,Ent:entity=Ents){
Ent:propFreeze(0)
}
}
##
###
### Detect entity holding / dropping
##
if( changed( Attack2 ) & Attack2 ){
if(Holding){
HeldEntity:propFreeze(1)
}
Holding = 0
}
if( changed( Attack1 ) | !Holding ){
if( Attack1 ){
HeldEntity = EyeTrace:entity()
if( HeldEntity:isValid() & HeldEntity:owner():trusts(Owner) ){
Holding = 1
HeldEntity:propFreeze(0)
HeldLocalPos = HeldEntity:toLocal( EyeTrace:pos() )
InitialGrabQuat = quat( Chip:angles() )
InitialGrabYaw = Owner:angles()[2]
InitialGrabAng = HeldEntity:angles()
BeamLength = EyeTrace:distance()
}
elseif( HeldEntity == world() ){
Holding = 2
HeldLocalPos = EyeTrace:pos()
BeamLength = EyeTrace:distance()
}
}
else{
Holding = 0
}
}
##
###
if( Holding & !Respawning ){
switch( Holding ){
case 1,
Mul = clamp(HeldEntity:mass(),0,1000)
BeamEndPos = HeldEntity:toWorld( HeldLocalPos )
Force = Owner:shootPos() + Owner:eye() * BeamLength - BeamEndPos # initial force
Force -= HeldEntity:pointVel( HeldLocalPos ) * 0.1
Force -= HeldEntity:vel() * 0.05
Force += Owner:vel()*0.05
Force *= Mul
TargetAng = InitialGrabAng
CursedQuatThing = quat( Chip:angles():rotateAroundAxis( vec(0,0,1), Owner:angles()[2] - InitialGrabYaw ) ) / InitialGrabQuat
TargetAng = TargetAng:rotateAroundAxis( rotationAxis(CursedQuatThing), rotationAngle(CursedQuatThing) )
Owner:plyApplyForce( -Force / PLAYER_MASS - Owner:vel()*0.00004*Mul )
rangerFilter( array(Owner,Chip) )
GroundTest = rangerOffsetHull(1,Owner:pos(),vec(0,0,-1),Owner:aabbMin(),Owner:aabbMax())
GroundTest:entity():applyOffsetForce( -Force, GroundTest:position() )
HeldEntity:perfectAngForce( TargetAng )
HeldEntity:applyOffsetForce( Force, BeamEndPos )
break
case 2,
BeamEndPos = HeldLocalPos
Force = Owner:shootPos() + Owner:eye() * BeamLength - BeamEndPos # initial force
Force += Owner:vel()*0.05
Force *= WORLD_MASS # world is extremely heavy
Owner:plyApplyForce( -Force / PLAYER_MASS )
break
}
}
elseif(Attack1){
BeamEndPos = EyeTrace:pos()
}
else{
BeamEndPos = Owner:shootPos() + Owner:vel() * TickInterval * 2
}
IsRotating = Holding & Owner:keyUse()
Chip:propFreeze( !IsRotating )
if(IsRotating){
InitialGrabYaw = Owner:angles()[2]
}
Chip:setPos( BeamEndPos )
}
else{
Chip:propNotSolid(1)
}
if( changed(Owner:isAlive()) ){
if( !Owner:isAlive() ){
Holding = 0
Respawning = 1
}
else{
timer("doneRespawn",250)
}
}
if( clk("doneRespawn") ){
Respawning = 0
}
}