Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on May 23, 2023. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 510
Expand file tree
/
Copy pathProximitySensor.lua
More file actions
Latest commit
318 lines (280 loc) · 12.7 KB
/
Copy pathProximitySensor.lua
File metadata and controls
318 lines (280 loc) · 12.7 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
--[[
This file is part of Courseplay (https://github.com/Courseplay/courseplay)
Copyright (C) 2020 Peter Vaiko
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
]]--
---@classProximitySensor
ProximitySensor=CpObject()
--- No matter what angle, don't make it extend to left/right more than this
ProximitySensor.maxSideExtension=4
functionProximitySensor:init(node, yRotationDeg, range, height, xOffset)
self.node=node
self.xOffset=xOffset
self.yRotation=math.rad(yRotationDeg)
self.lx, self.lz=MathUtil.getDirectionFromYRotation(self.yRotation)
self.range=math.min(range, ProximitySensor.maxSideExtension/math.cos((math.pi/2-math.abs(self.yRotation))))
self.dx, self.dz=self.lx*self.range, self.lz*self.range
self.height=heightor0
self.lastUpdateLoopIndex=0
self.enabled=true
-- vehicles can only be ignored temporarily
self.ignoredVehicle=CpTemporaryObject()
end
functionProximitySensor:enable()
self.enabled=true
end
functionProximitySensor:disable()
self.enabled=false
end
---@paramvehicletable vehicle to ignore
---@paramttlMsnumber milliseconds to ignore this vehicle. After ttlMs ms it won't be ignored.
functionProximitySensor:setIgnoredVehicle(vehicle, ttlMs)
self.ignoredVehicle:set(vehicle, ttlMs)
end
functionProximitySensor:update()
-- already updated in this loop, no need to raycast again
ifg_updateLoopIndex==self.lastUpdateLoopIndexthenreturnend
self.lastUpdateLoopIndex=g_updateLoopIndex
localx, y, z=localToWorld(self.node, self.xOffset, 0, 0)
-- get the terrain height at the end of the raycast line
localtx, _, tz=localToWorld(self.node, self.dx, 0, self.dz)
localy2=getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, tx, 0, tz)
-- make sure the raycast line is parallel with the ground
localny= (y2-y) /self.range
localnx, _, nz=localDirectionToWorld(self.node, self.lx, 0, self.lz)
self.distanceOfClosestObject=math.huge
self.objectId=nil
ifself.enabledthen
raycastClosest(x, y+self.height, z, nx, ny, nz, 'raycastCallback', self.range, self,
0x1000+0x100+0x80+0x40+0x20)
--bitOR(AIVehicleUtil.COLLISION_MASK, 0x1f0))
--cpDebug:drawLine(x, y + self.height, z, 0, 1, 0, x + 5 * nx, y + self.height + 5 * ny, z + 5 * nz)
end
ifcourseplay.debugChannels[courseplay.DBG_TRAFFIC] andself.distanceOfClosestObject<=self.rangethen
localgreen=self.distanceOfClosestObject/self.range
localred=1-green
cpDebug:drawLine(x, y+self.height, z, red, green, 0, self.closestObjectX, self.closestObjectY, self.closestObjectZ)
end
end
functionProximitySensor:raycastCallback(objectId, x, y, z, distance)
localobject=g_currentMission:getNodeObject(objectId)
ifobjectandobject.getRootVehicleandobject:getRootVehicle() ==self.ignoredVehicle:get() then
-- ignore this vehicle
return
end
self.distanceOfClosestObject=distance
self.objectId=objectId
self.closestObjectX, self.closestObjectY, self.closestObjectZ=x, y, z
end
functionProximitySensor:getClosestObjectDistance()
--self:showDebugInfo()
returnself.distanceOfClosestObject
end
functionProximitySensor:getClosestObject()
returng_currentMission:getNodeObject(self.objectId)
end
functionProximitySensor:getClosestRootVehicle()
ifself.objectIdthen
localobject=g_currentMission:getNodeObject(self.objectId)
ifobjectandobject.getRootVehiclethen
returnobject:getRootVehicle()
end
end
end
functionProximitySensor:showDebugInfo()
ifnotcourseplay.debugChannels[courseplay.DBG_TRAFFIC] thenreturnend
localtext=string.format('%.1f ', self.distanceOfClosestObject)
ifself.objectIdthen
localobject=g_currentMission:getNodeObject(self.objectId)
ifobjectthen
ifobject.getRootVehiclethen
text=text..'vehicle ' ..object:getName()
else
text=text..object:getName()
end
else
forkey, classIdinpairs(ClassIds) do
ifgetHasClassId(self.objectId, classId) then
text=text..'' ..key
end
end
end
end
renderText(0.6, 0.4+self.yRotation/10, 0.018, text..string.format(' %d', math.deg(self.yRotation)))
end
---@classProximitySensorPack
ProximitySensorPack=CpObject()
-- maximum angle we rotate the sensor pack into the direction the vehicle is turning
ProximitySensorPack.maxRotation=math.rad(30)
---@paramnamestring a name for this sensor, when multiple sensors are attached to the same node, they need
--- a unique name
---@paramvehicletable vehicle we attach the sensor to, used only to rotate the sensor with the steering angle
---@paramppcPurePursuitController PPC of the vehicle, used only to rotate the sensor towards the goal point
---@paramnodenumber node (front or back) to attach the sensor to
---@paramrangenumber range of the sensor in meters
---@paramheightnumber height relative to the node in meters
---@paramdirectionsDegtable of numbers, list of angles in degrees to emit a ray to find objects, 0 is forward, >0 left, <0 right
---@paramxOffsetstable of numbers, left/right offset of the corresponding sensor in meters, left > 0, right < 0
functionProximitySensorPack:init(name, vehicle, ppc, node, range, height, directionsDeg, xOffsets)
---@typeProximitySensor[]
self.sensors= {}
self.vehicle=vehicle
self.ppc=ppc
self.range=range
self.name=name
self.node=getChild(node, name)
ifself.node<=0then
-- node with this name does not yet exist
-- add a separate node for the proximity sensor (so we can rotate it independently from 'node'
self.node=courseplay.createNode(name, 0, 0, 0, node)
end
-- reset it on the parent node
setTranslation(self.node, 0, 0, 0)
setRotation(self.node, 0, 0, 0)
self.directionsDeg=directionsDeg
self.xOffsets=xOffsets
self.rotateToGoalPoint=false
self.rotation=0
fori, deginipairs(self.directionsDeg) do
self.sensors[deg] =ProximitySensor(self.node, deg, self.range, height, xOffsets[i] or0)
end
end
functionProximitySensorPack:debug(...)
courseplay.debugVehicle(courseplay.DBG_TRAFFIC, self.vehicle, ...)
end
functionProximitySensorPack:adjustForwardPosition()
-- are we looking forward
localforward=1
-- if a sensor about in the middle is pointing back, we are looking back
ifmath.abs(self.directionsDeg[math.floor(#self.directionsDeg/2)]) >90then
forward=-1
end
localx, y, z=getTranslation(self.node)
self:debug('moving proximity sensor %s %.1f so it does not interfere with own vehicle', self.name, forward*0.1)
-- move pack forward/back a bit
setTranslation(self.node, x, y, z+forward*0.1)
end
functionProximitySensorPack:getRange()
returnself.range
end
functionProximitySensorPack:callForAllSensors(func, ...)
for_, deginipairs(self.directionsDeg) do
func(self.sensors[deg], ...)
end
end
functionProximitySensorPack:disableRotateToGoalPoint()
self.rotateToGoalPoint=false
end
functionProximitySensorPack:update()
ifself.rotateToGoalPointthen
-- rotate the entire pack in the direction of the goal point
localdx, dz=self.ppc:getGoalPointDirection()
localyRot=MathUtil.getYRotationFromDirection(dx, dz)
self.rotation=MathUtil.clamp(yRot, -ProximitySensorPack.maxRotation, ProximitySensorPack.maxRotation)
setRotation(self.node, 0, self.rotation, 0)
end
self:callForAllSensors(ProximitySensor.update)
-- show the position of the pack
ifcourseplay.debugChannels[courseplay.DBG_TRAFFIC] then
localx, y, z=getWorldTranslation(self.node)
localx1, y1, z1=localToWorld(self.node, 0, 0, 0.5)
cpDebug:drawLine(x, y, z, 0, 0, 1, x, y+3, z)
cpDebug:drawLine(x, y+1, z, 0, 1, 0, x1, y1+1, z1)
end
end
functionProximitySensorPack:enable()
self:callForAllSensors(ProximitySensor.enable)
end
functionProximitySensorPack:disable()
self:callForAllSensors(ProximitySensor.disable)
end
functionProximitySensorPack:setIgnoredVehicle(vehicle, ttlMs)
self:callForAllSensors(ProximitySensor.setIgnoredVehicle, vehicle, ttlMs)
end
--- @returnnumber, table, number distance of closest object in meters, root vehicle of the closest object,
--- the closest object, average direction of the obstacle in degrees, > 0 right, < 0 left
functionProximitySensorPack:getClosestObjectDistanceAndRootVehicle(deg)
-- make sure we have the latest info, the sensors will make sure they only raycast once per loop
self:update()
ifdegandself.sensors[deg] then
returnself.sensors[deg]:getClosestObjectDistance(), self.sensors[deg]:getClosestRootVehicle(), deg
else
localclosestDistance=math.huge
localclosestRootVehicle, closestObject
-- weighted average over the different direction, weight depends on how close the closest object is
localtotalWeight, totalDegs, totalDistance=0, 0, 0
for_, deginipairs(self.directionsDeg) do
locald=self.sensors[deg]:getClosestObjectDistance()
ifd<self.rangethen
localweight= (self.range-d) /self.range
totalWeight=totalWeight+weight
-- the direction should be in the tractor's system, therefore we need to compensate here with the
-- current rotation of the pack
totalDegs=totalDegs+weight* (deg+math.deg(self.rotation))
totalDistance=totalDistance+weight*d
end
ifd<closestDistancethen
closestDistance=d
closestRootVehicle=self.sensors[deg]:getClosestRootVehicle()
closestObject=self.sensors[deg]:getClosestObject()
end
end
ifclosestRootVehicle==self.vehiclethen
self:adjustForwardPosition()
end
returnclosestDistance, closestRootVehicle, closestObject, totalDegs/totalWeight, totalDistance/totalWeight
end
returnmath.huge, nil, deg
end
functionProximitySensorPack:disableRightSide()
for_, deginipairs(self.directionsDeg) do
ifdeg<=0then
self.sensors[deg]:disable()
end
end
end
functionProximitySensorPack:enableRightSide()
for_, deginipairs(self.directionsDeg) do
ifdeg<=0then
self.sensors[deg]:enable()
end
end
end
---@classForwardLookingProximitySensorPack:ProximitySensorPack
ForwardLookingProximitySensorPack=CpObject(ProximitySensorPack)
--- Pack looking forward, all sensors are in the middle of the vehicle
functionForwardLookingProximitySensorPack:init(vehicle, ppc, node, range, height)
ProximitySensorPack.init(self, 'forward', vehicle, ppc, node, range, height,
{0, 15, 30, 60, 80, -15, -30, -60, -80},
{0, 0, 0, 0, 0, 0, 0, 0, 0})
end
---@classWideForwardLookingProximitySensorPack:ProximitySensorPack
WideForwardLookingProximitySensorPack=CpObject(ProximitySensorPack)
--- Pack looking forward, but sensors distributed evenly through the width of the vehicle
functionWideForwardLookingProximitySensorPack:init(vehicle, ppc, node, range, height, width)
localdirectionsDeg= {80, 60, 30, 15, 0, -15, -30, -60, -80}
localxOffsets= {}
-- spread them out evenly across the width
localdx=width/#directionsDeg
forxOffset=width/2-dx/2, -width/2+dx/2-0.1, -dxdo
table.insert(xOffsets, xOffset)
end
ProximitySensorPack.init(self, 'wideForward', vehicle, ppc, node, range, height,
directionsDeg, xOffsets)
end
---@classBackwardLookingProximitySensorPack:ProximitySensorPack
BackwardLookingProximitySensorPack=CpObject(ProximitySensorPack)
functionBackwardLookingProximitySensorPack:init(vehicle, ppc, node, range, height)
ProximitySensorPack.init(self, 'backward', vehicle, ppc, node, range, height,
{120, 150, 180, -150, -120},
{0, 0, 0, 0, 0})
end