-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSimpleCar.txt
276 lines (242 loc) · 9.14 KB
/
SimpleCar.txt
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
--@name SimpleCar
--@author Sparky
--@shared
--@include libs/criticalpd.txt
--@include libs/xinput.txt
local brakepower = 1 -- Brake multiplier
local throttlepower = 500 -- Throttle multiplier
local maxAngSpeed = 3500 -- Max tire angular velocity
local minTurnRadius = 175.019 -- Minimum turn radius
local maxTurnAcceleration = 1000 -- 800 is 1G
local carForward = Vector(0,0,-1) -- Vector local to car body pointing forward
local carRight = Vector(0,-1,0) -- Vector local to car body pointing right
local hudMatrix = Matrix()
require("libs/xinput.txt")
if SERVER then
local criticalpd = require("libs/criticalpd.txt")
local wheel = class("wheel")
local steering = class("steering", wheel)
local car = class("car")
function wheel:initialize(car, ent)
self.pd = criticalpd:new(ent, 0, 500)
self.car = car
self.ent = ent
self.forward = self.pd.phys:worldToLocalVector(car.bodyPhys:localToWorldVector(car.right))
self.radius = ent:obbMaxs()[1]
end
function wheel:simulate()
self.pd:simulateAngForceCustomError(Vector(), self:calcSpeed()-self.pd.phys:localToWorldVector(self.ent:getAngleVelocity()))
end
function wheel:calcSpeed()
local throttle = self.car.throttle
local brake = self.car.brake
local wheelvel = self.ent:getAngleVelocity():dot(self.forward)
local speed = math.clamp((self.car.bodyrotvel - wheelvel)*brake*brakepower + wheelvel - throttle*throttlepower, -maxAngSpeed, maxAngSpeed)
return self.pd.phys:localToWorldVector(self.forward) * speed
end
function steering:initialize(car, ent, forward)
wheel.initialize(self, car, ent, forward)
end
function steering:simulate(turndir, sign)
local phys = self.pd.phys
local forward = phys:localToWorldVector(self.forward)
local axis = forward:cross(turndir):getNormalized()
local angleDeg = math.deg(math.acos(math.clamp(turndir:dot(forward), -1, 1)))
self.pd:simulateAngForceCustomError(axis*angleDeg, self.car.bodyangvel+self:calcSpeed()-phys:localToWorldVector(self.ent:getAngleVelocity()))
end
function car:initialize(forward, right, body, chair, wheelfr, wheelfl, wheelbr, wheelbl)
self.body = body
self.joystickSteer = nil
self.forward = forward
self.right = right
self.bodyPhys = body:getPhysicsObject()
self.chair = chair
self.wheelfr = steering:new(self, wheelfr)
self.wheelfl = steering:new(self, wheelfl)
self.wheelbr = wheel:new(self, wheelbr)
self.wheelbl = wheel:new(self, wheelbl)
self.turninv = 0
self.throttle = 0
self.brake = 1
end
function car:simulate()
self.bodyvel = self.bodyPhys:worldToLocalVector(self.body:getVelocity()):dot(self.forward)
local angvel = self.body:getAngleVelocity()
self.bodyrotvel = angvel:dot(self.right)
self.bodyangvel = self.bodyPhys:localToWorldVector(angvel)
if self.driver then
local w, a, s, d, turbo, brake =
self.driver:keyDown(IN_KEY.FORWARD),
self.driver:keyDown(IN_KEY.MOVELEFT),
self.driver:keyDown(IN_KEY.BACK),
self.driver:keyDown(IN_KEY.MOVERIGHT),
self.driver:keyDown(IN_KEY.SPEED),
self.driver:keyDown(IN_KEY.JUMP)
local targetradiusinv = math.clamp(maxTurnAcceleration/self.bodyvel^2, 0, 1/minTurnRadius)
if self.joystickSteer then
self.turninv = self.joystickSteer*targetradiusinv
self.throttle = self.joystickThrottle
self.brake = 0
else
if a then
self.turninv = self.turninv*0.9-targetradiusinv*0.1
elseif d then
self.turninv = self.turninv*0.9+targetradiusinv*0.1
else
self.turninv = self.turninv*0.5
end
self.throttle = (w and (turbo and 2 or 1)) or (s and (turbo and -2 or -1)) or 0
self.brake = brake and 1 or 0
end
else
self.throttle = 0
self.brake = 1
self.joystickSteer = nil
end
local right = self.bodyPhys:localToWorldVector(self.right)
local turndirR, turndirL
if math.abs(self.turninv)<0.000001 then
turndirR = right
turndirL = right
else
local wheelfrPos = self.wheelfr.ent:getMassCenterW()
local wheelflPos = self.wheelfl.ent:getMassCenterW()
local wheelbrPos = self.wheelbr.ent:getMassCenterW()
local wheelblPos = self.wheelbl.ent:getMassCenterW()
local pivot = (wheelblPos+wheelbrPos)/2
local turn = pivot + right*(1/self.turninv)
local turnSign = math.sign(self.turninv)
turndirR = (turn - wheelfrPos):getNormalized()*turnSign
turndirL = (turn - wheelflPos):getNormalized()*turnSign
end
self.wheelfr:simulate(turndirR)
self.wheelfl:simulate(turndirL)
self.wheelbr:simulate()
self.wheelbl:simulate()
end
function car:setDriver(driver)
self.driver = driver
end
local mycar
do
local body = chip():isWeldedTo()
local chair, chairdot = nil, -math.huge
local wheelfr, wheelfrdot = nil, -math.huge
local wheelfl, wheelfldot = nil, -math.huge
local wheelbr, wheelbrdot = nil, -math.huge
local wheelbl, wheelbldot = nil, -math.huge
for _, v in ipairs(constraint.getTable(body)) do
local ent = v.Ent1 == body and v.Ent2 or v.Ent1
local pos = body:worldToLocal(ent:getMassCenterW())
if ent:isVehicle() then
local posdot = pos:dot(carForward)-pos:dot(carRight)
if posdot > chairdot then
chairdot = posdot
chair = ent
end
elseif v.Type == "AdvBallsocket" or v.Type == "Axis" then
local posdot
posdot = pos:dot(carForward)+pos:dot(carRight)
if posdot > wheelfrdot then
wheelfrdot = posdot
wheelfr = ent
end
posdot = pos:dot(carForward)-pos:dot(carRight)
if posdot > wheelfldot then
wheelfldot = posdot
wheelfl = ent
end
posdot = -pos:dot(carForward)+pos:dot(carRight)
if posdot > wheelbrdot then
wheelbrdot = posdot
wheelbr = ent
end
posdot = -pos:dot(carForward)-pos:dot(carRight)
if posdot > wheelbldot then
wheelbldot = posdot
wheelbl = ent
end
end
end
if not body then error("Chip isn't welded to the car body!") end
if not chair then error("No vehicle welded to car body!") end
if not (wheelfr and wheelfl and wheelbr and wheelbl) then error("The car needs 4 wheels!") end
mycar = car:new(carForward, carRight, body, chair, wheelfr, wheelfl, wheelbr, wheelbl)
end
hook.add("think","simulate",function()
mycar:simulate()
end)
hook.add("playerenteredvehicle","",function(ply,veh)
if mycar.chair == veh then
mycar:setDriver(ply)
xinput.setListeners(ply, "sThumbLX bRightTrigger bLeftTrigger", true)
end
end)
hook.add("playerleavevehicle","",function(ply,veh)
if mycar.chair == veh then
mycar:setDriver(nil)
xinput.setListeners(ply, "sThumbLX bRightTrigger bLeftTrigger", false)
end
end)
hook.add("xinput","",function(ply, data)
if mycar.driver == ply then
mycar.joystickSteer = data.sThumbLX/32768
mycar.joystickThrottle = data.bRightTrigger/255 - data.bLeftTrigger/255
else
xinput.setListeners(ply, "sThumbLX bRightTrigger bLeftTrigger", false)
end
end)
hook.add("clientinitialized","",function(ply)
net.start("car")
net.writeEntity(mycar.body)
net.send(ply)
end)
else -- CLIENT
local carent
net.receive("car",function()
net.readEntity(function(ent) carent = ent end)
end)
local font = render.createFont("Roboto",32)
local font2 = render.createFont("Roboto",128)
local carvel = 0
hook.add("render","",function()
if not (carent and carent:isValid()) then return end
local forward = carent:localToWorld(carForward) - carent:getPos()
carvel = carvel*0.95 + carent:getVelocity():dot(forward)*0.05*0.0568182
local bearing = math.atan2(forward[1], forward[2])
render.setFont(font2)
render.drawText(256, 256, string.format("%03.0f", math.abs(carvel)), 1)
render.setFont(font)
render.drawText(380, 320, "mph", 1)
local S1, S2
if bearing < -math.pi/2 then
bearing = bearing + math.pi
S1, S2 = "W", "S"
elseif bearing<0 then
bearing = bearing + math.pi/2
S1, S2 = "N", "W"
elseif bearing<math.pi/2 then
S1, S2 = "E", "N"
else
S1, S2 = "S", "E"
bearing = bearing - math.pi/2
end
local compassMatrix = Matrix() compassMatrix:setTranslation(Vector(256, 512, 0))
render.pushMatrix(compassMatrix)
do
local x = 100*math.cos(bearing)
local y = -50*math.sin(bearing)
render.drawText(x*1.2, y*1.2-20, S1, 1)
local tx, ty = -y*0.05, x*0.05
render.drawPoly({{x = -tx, y = -ty},{x = x, y = y},{x = tx, y = ty}})
end
do
local x = 100*math.cos(bearing+math.pi/2)
local y = -50*math.sin(bearing+math.pi/2)
render.drawText(x*1.2, y*1.2-20, S2, 1)
local tx, ty = -y*0.05, x*0.05
render.drawPoly({{x = -tx, y = -ty},{x = x, y = y},{x = tx, y = ty}})
end
render.popMatrix()
end)
end