Copyright (c) 2008-2015 GIANTS Software GmbH, Confidential, All Rights Reserved.
This document is to be published solely by ls-mods.de
1 | -- |
2 | -- AIVehicleUtil |
3 | -- Util class for various ai vehicle functions |
4 | -- |
5 | -- @author Stefan Geiger |
6 | -- @date 25/01/09 |
7 | -- |
8 | -- Copyright (C) GIANTS Software GmbH, Confidential, All Rights Reserved. |
9 | |
10 | AIVehicleUtil = {}; |
11 | |
12 | |
13 | function AIVehicleUtil.driveInDirection(self, dt, steeringAngleLimit, acceleration, slowAcceleration, slowAngleLimit, allowedToDrive, moveForwards, lx, lz, maxSpeed, slowDownFactor) |
14 | |
15 | local angle = 0; |
16 | if lx ~= nil and lz ~= nil then |
17 | -- cur dir = 0 0 1 |
18 | -- desired dir = lx, ly, lz |
19 | local dot = lz; -- = lx*0 + lz*1 |
20 | angle = math.deg(math.acos(dot)); |
21 | if angle < 0 then |
22 | angle = angle+180; |
23 | end; |
24 | |
25 | local turnLeft = lx > 0.00001; |
26 | if not moveForwards then |
27 | turnLeft = not turnLeft; |
28 | end; |
29 | |
30 | local targetRotTime = 0; |
31 | |
32 | if turnLeft then |
33 | --rotate to the left |
34 | targetRotTime = self.maxRotTime*math.min(angle/steeringAngleLimit, 1); |
35 | else |
36 | --rotate to the right |
37 | targetRotTime = self.minRotTime*math.min(angle/steeringAngleLimit, 1); |
38 | end; |
39 | |
40 | if targetRotTime > self.rotatedTime then |
41 | self.rotatedTime = math.min(self.rotatedTime + dt*self.aiSteeringSpeed, targetRotTime); |
42 | else |
43 | self.rotatedTime = math.max(self.rotatedTime - dt*self.aiSteeringSpeed, targetRotTime); |
44 | end |
45 | end |
46 | |
47 | |
48 | if self.firstTimeRun then |
49 | local acc = acceleration; |
50 | if maxSpeed ~= nil and maxSpeed ~= 0 then |
51 | if math.abs(angle) >= slowAngleLimit then |
52 | maxSpeed = maxSpeed * slowDownFactor; |
53 | end; |
54 | --[[ |
55 | if self.cruiseControl.speed ~= maxSpeed then |
56 | self:setCruiseControlMaxSpeed(maxSpeed); |
57 | if g_server ~= nil then |
58 | g_server:broadcastEvent(SetCruiseControlSpeedEvent:new(self, self.cruiseControl.speed), nil, nil, self); |
59 | end; |
60 | end; ]]-- |
61 | self.motor:setSpeedLimit(maxSpeed); |
62 | |
63 | if self.cruiseControl.state ~= Drivable.CRUISECONTROL_STATE_ACTIVE then |
64 | self:setCruiseControlState(Drivable.CRUISECONTROL_STATE_ACTIVE); |
65 | end; |
66 | else |
67 | if math.abs(angle) >= slowAngleLimit then |
68 | --print("use slow angle"); |
69 | acc = slowAcceleration; |
70 | end; |
71 | end; |
72 | if not allowedToDrive then |
73 | acc = 0; |
74 | end; |
75 | if not moveForwards then |
76 | acc = -acc; |
77 | end; |
78 | WheelsUtil.updateWheelsPhysics(self, dt, self.lastSpeedReal, acc, not allowedToDrive, self.requiredDriveMode); |
79 | end; |
80 | end; |
81 | |
82 | function AIVehicleUtil.getDriveDirection(refNode, x, y, z) |
83 | local lx, _, lz = worldToLocal(refNode, x, y, z); |
84 | |
85 | local length = Utils.vector2Length(lx, lz); |
86 | if length > 0.00001 then |
87 | length = 1/length; |
88 | lx = lx*length; |
89 | lz = lz*length; |
90 | end; |
91 | return lx, lz; |
92 | end; |
93 | |
94 | function AIVehicleUtil.getAverageDriveDirection(refNode, x, y, z, x2, y2, z2) |
95 | local lx, _, lz = worldToLocal(refNode, (x+x2)*0.5, (y+y2)*0.5, (z+z2)*0.5); |
96 | |
97 | local length = Utils.vector2Length(lx, lz); |
98 | if length > 0.00001 then |
99 | lx = lx/length; |
100 | lz = lz/length; |
101 | end; |
102 | return lx, lz, length; |
103 | end; |
104 | |
105 | function AIVehicleUtil.setCollisionDirection(node, col, colDirX, colDirZ) |
106 | local parent = getParent(col); |
107 | local colDirY = 0; |
108 | if parent ~= node then |
109 | colDirX, colDirY, colDirZ = worldDirectionToLocal(parent, localDirectionToWorld(node, colDirX, 0, colDirZ)); |
110 | end; |
111 | setDirection(col, colDirX, colDirY, colDirZ, 0, 1, 0); |
112 | end; |
113 | |
114 | function AIVehicleUtil.registerCollisions(self, object) |
115 | for _,implement in pairs(object.attachedImplements) do |
116 | if implement.object ~= nil then |
117 | AIVehicleUtil.registerCollisions(self, implement.object); |
118 | self:addCollisionTrigger(implement.object); |
119 | end; |
120 | end; |
121 | end; |
122 | |
123 | function AIVehicleUtil.unregisterCollisions(self, object) |
124 | for _,implement in pairs(object.attachedImplements) do |
125 | if implement.object ~= nil then |
126 | AIVehicleUtil.unregisterCollisions(self, implement.object); |
127 | self:removeCollisionTrigger(implement.object); |
128 | end; |
129 | end; |
130 | end;
|
Copyright (c) 2008-2015 GIANTS Software GmbH, Confidential, All Rights Reserved.
This document is to be published solely by ls-mods.de