-
Notifications
You must be signed in to change notification settings - Fork 78
/
VectorExtensions.cs
287 lines (238 loc) · 10.3 KB
/
VectorExtensions.cs
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
using System;
using GTA;
using GTA.Math;
using GTA.Native;
namespace MapEditor
{
public static class VectorExtensions
{
public static float Denormalize(this float h)
{
return h < 0f ? h + 360f : h;
}
public static Vector3 Denormalize(this Vector3 v)
{
return new Vector3(v.X.Denormalize(), v.Y.Denormalize(), v.Z.Denormalize());
}
public static float ToRadians(this float val)
{
return (float)(Math.PI / 180) * val;
}
public static float ToDegrees(this float val)
{
return (float) (val*(180/Math.PI));
}
public static Vector3 TransformVector(this Vector3 i, Func<float, float> method)
{
return new Vector3()
{
X = method(i.X),
Y = method(i.Y),
Z = method(i.Z),
};
}
public static Vector3 ToEuler(this GTA.Math.Quaternion q)
{
var pitchYawRoll = new Vector3();
double sqw = q.W * q.W;
double sqx = q.X * q.X;
double sqy = q.Y * q.Y;
double sqz = q.Z * q.Z;
pitchYawRoll.Y = (float)Math.Atan2(2f * q.X * q.W + 2f * q.Y * q.Z, 1 - 2f * (sqz + sqw)); // Yaw
pitchYawRoll.X = (float)Math.Asin(2f * (q.X * q.Z - q.W * q.Y)); // Pitch
pitchYawRoll.Z = (float)Math.Atan2(2f * q.X * q.Y + 2f * q.Z * q.W, 1 - 2f * (sqy + sqz));
pitchYawRoll = pitchYawRoll.TransformVector(ToDegrees);
pitchYawRoll = pitchYawRoll.Denormalize();
pitchYawRoll = new Vector3()
{
Y = pitchYawRoll.Y * -1f + 180f,
X = pitchYawRoll.X,
Z = pitchYawRoll.Z,
};
return pitchYawRoll;
}
public static GTA.Math.Quaternion ToQuaternion(this Vector3 vect)
{
vect = new Vector3()
{
X = vect.X.Denormalize() * -1f,
Y = vect.Y.Denormalize() - 180f,
Z = vect.Z.Denormalize() - 180f,
};
vect = vect.TransformVector(ToRadians);
float rollOver2 = vect.Z * 0.5f;
float sinRollOver2 = (float)Math.Sin((double)rollOver2);
float cosRollOver2 = (float)Math.Cos((double)rollOver2);
float pitchOver2 = vect.Y * 0.5f;
float sinPitchOver2 = (float)Math.Sin((double)pitchOver2);
float cosPitchOver2 = (float)Math.Cos((double)pitchOver2);
float yawOver2 = vect.X * 0.5f; // pitch
float sinYawOver2 = (float)Math.Sin((double)yawOver2);
float cosYawOver2 = (float)Math.Cos((double)yawOver2);
GTA.Math.Quaternion result = new GTA.Math.Quaternion();
result.X = cosYawOver2 * cosPitchOver2 * cosRollOver2 + sinYawOver2 * sinPitchOver2 * sinRollOver2;
result.Y = cosYawOver2 * cosPitchOver2 * sinRollOver2 - sinYawOver2 * sinPitchOver2 * cosRollOver2;
result.Z = cosYawOver2 * sinPitchOver2 * cosRollOver2 + sinYawOver2 * cosPitchOver2 * sinRollOver2;
result.W = sinYawOver2 * cosPitchOver2 * cosRollOver2 - cosYawOver2 * sinPitchOver2 * sinRollOver2;
return result;
}
public static Vector3 ForwardVector(this Vector3 vector, float yaw)
{
Vector3 right;
float cos = (float)Math.Cos(yaw + Math.PI/2.0f);
right.X = (180f/(float)Math.PI)*cos;
right.Y = 0f;
float sin = (float) Math.Sin(yaw + Math.PI/2.0f);
right.Z = (180f/(float) Math.PI)*sin;
return CrossWith(vector, right);
}
public static Vector3 CrossWith(Vector3 left, Vector3 right)
{
Vector3 result;
result.X = left.Y*right.Z - left.Z*right.Y;
result.Y = left.Z*right.X - left.X*right.Z;
result.Z = left.X*right.Y - left.Y*right.X;
return result;
}
public static bool WorldToScreenRel(Vector3 worldCoords, out Vector2 screenCoords)
{
OutputArgument num1 = new OutputArgument();
OutputArgument num2 = new OutputArgument();
if (!Function.Call<bool>(Hash._WORLD3D_TO_SCREEN2D, worldCoords.X, worldCoords.Y, worldCoords.Z, num1, num2))
{
screenCoords = new Vector2();
return false;
}
screenCoords = new Vector2((num1.GetResult<float>() - 0.5f) * 2, (num2.GetResult<float>() - 0.5f) * 2);
return true;
}
public static Vector3 ScreenRelToWorld(Vector3 camPos, Vector3 camRot, Vector2 coord)
{
var camForward = RotationToDirection(camRot);
var rotUp = camRot + new Vector3(10, 0, 0);
var rotDown = camRot + new Vector3(-10, 0, 0);
var rotLeft = camRot + new Vector3(0, 0, -10);
var rotRight = camRot + new Vector3(0, 0, 10);
var camRight = RotationToDirection(rotRight) - RotationToDirection(rotLeft);
var camUp = RotationToDirection(rotUp) - RotationToDirection(rotDown);
var rollRad = -DegToRad(camRot.Y);
var camRightRoll = camRight * (float)Math.Cos(rollRad) - camUp * (float)Math.Sin(rollRad);
var camUpRoll = camRight * (float)Math.Sin(rollRad) + camUp * (float)Math.Cos(rollRad);
var point3D = camPos + camForward * 10.0f + camRightRoll + camUpRoll;
Vector2 point2D;
if (!WorldToScreenRel(point3D, out point2D)) return camPos + camForward * 10.0f;
var point3DZero = camPos + camForward * 10.0f;
Vector2 point2DZero;
if (!WorldToScreenRel(point3DZero, out point2DZero)) return camPos + camForward * 10.0f;
const double eps = 0.001;
if (Math.Abs(point2D.X - point2DZero.X) < eps || Math.Abs(point2D.Y - point2DZero.Y) < eps) return camPos + camForward * 10.0f;
var scaleX = (coord.X - point2DZero.X) / (point2D.X - point2DZero.X);
var scaleY = (coord.Y - point2DZero.Y) / (point2D.Y - point2DZero.Y);
var point3Dret = camPos + camForward * 10.0f + camRightRoll * scaleX + camUpRoll * scaleY;
return point3Dret;
}
public static Vector3 RotationToDirection(Vector3 rotation)
{
var z = DegToRad(rotation.Z);
var x = DegToRad(rotation.X);
var num = Math.Abs(Math.Cos(x));
return new Vector3
{
X = (float)(-Math.Sin(z) * num),
Y = (float)(Math.Cos(z) * num),
Z = (float)Math.Sin(x)
};
}
public static Vector3 DirectionToRotation(Vector3 direction)
{
direction.Normalize();
var x = Math.Atan2(direction.Z, direction.Y);
var y = 0;
var z = -Math.Atan2(direction.X, direction.Y);
return new Vector3
{
X = (float)RadToDeg(x),
Y = (float)RadToDeg(y),
Z = (float)RadToDeg(z)
};
}
public static double DegToRad(double deg)
{
return deg * Math.PI / 180.0;
}
public static double RadToDeg(double deg)
{
return deg * 180.0 / Math.PI;
}
public static double BoundRotationDeg(double angleDeg)
{
var twoPi = (int)(angleDeg / 360);
var res = angleDeg - twoPi * 360;
if (res < 0) res += 360;
return res;
}
public static Vector3 RaycastEverything(Vector2 screenCoord)
{
var camPos = GameplayCamera.Position;
var camRot = GameplayCamera.Rotation;
const float raycastToDist = 100.0f;
const float raycastFromDist = 1f;
var target3D = ScreenRelToWorld(camPos, camRot, screenCoord);
var source3D = camPos;
Entity ignoreEntity = Game.Player.Character;
if (Game.Player.Character.IsInVehicle())
{
ignoreEntity = Game.Player.Character.CurrentVehicle;
}
var dir = (target3D - source3D);
dir.Normalize();
var raycastResults = World.Raycast(source3D + dir * raycastFromDist,
source3D + dir * raycastToDist,
(IntersectOptions)(1 | 16 | 256 | 2 | 4 | 8)// | peds + vehicles
, ignoreEntity);
if (raycastResults.DitHitAnything)
{
return raycastResults.HitCoords;
}
return camPos + dir * raycastToDist;
}
public static Vector3 RaycastEverything(Vector2 screenCoord, Vector3 camPos, Vector3 camRot, Entity toIgnore)
{
const float raycastToDist = 100.0f;
const float raycastFromDist = 1f;
var target3D = ScreenRelToWorld(camPos, camRot, screenCoord);
var source3D = camPos;
Entity ignoreEntity = toIgnore;
var dir = (target3D - source3D);
dir.Normalize();
var raycastResults = World.Raycast(source3D + dir * raycastFromDist,
source3D + dir * raycastToDist,
(IntersectOptions)(1 | 16 | 256 | 2 | 4 | 8)// | peds + vehicles
, ignoreEntity);
if (raycastResults.DitHitAnything)
{
return raycastResults.HitCoords;
}
return camPos + dir * raycastToDist;
}
public static Entity RaycastEntity(Vector2 screenCoord, Vector3 camPos, Vector3 camRot)
{
const float raycastToDist = 100.0f;
const float raycastFromDist = 1f;
var target3D = ScreenRelToWorld(camPos, camRot, screenCoord);
var source3D = camPos;
Entity ignoreEntity = Game.Player.Character;
var dir = (target3D - source3D);
dir.Normalize();
var raycastResults = World.Raycast(source3D + dir * raycastFromDist,
source3D + dir * raycastToDist,
(IntersectOptions)(1 | 16 | 256 | 2 | 4 | 8)// | peds + vehicles
, ignoreEntity);
if (raycastResults.DitHitEntity)
{
return raycastResults.HitEntity;
}
return null;
}
}
}