# WC3 JASS.com"The Jass Vault" plus vJASS and Zinc

 Warcraft III: Maps Models Skins Icons Spells / Systems Tools Tutorials vJASS & Zinc DocumentationFor the latest documentation about how it works vJASS and Zinc language layers for Warcraft III, please follow these links: Jasshelper documentation - Zinc documentation - WC3 Optimizer documentation

### Math Codes & Snippets

Started by
moyack

0 Members and 1 Guest are viewing this topic.

##### Math
on: January 19, 2012, 02:57:58 PM
Category: Geometry, Variables
Language: vJASS

A library for math functions. Manages Matrix manipulation and more.

Code: jass
1. library Math initializer Init
2.
3. function R2I_n takes real r returns integer
4.     local integer i = R2I(r)
5.     if (r < I2R(i)+0.5) then
6.         return i
7.     else
8.         return i+1
9.     endif
10. endfunction
11.
12. struct VECTOR3
13.     static VECTOR3 Zero
14.     static VECTOR3 oneX
15.     static VECTOR3 oneY
16.     static VECTOR3 oneZ
17.     real x
18.     real y
19.     real z
20.
21.     static method New_0 takes nothing returns VECTOR3
22.         local VECTOR3 this = VECTOR3.create()
23.         set .x = 0
24.         set .y = 0
25.         set .z = 0
26.         return this
27.     endmethod
28.
29.     static method New_1 takes real x, real y, real z returns VECTOR3
30.         local VECTOR3 this = VECTOR3.create()
31.         set .x = x
32.         set .y = y
33.         set .z = z
34.         return this
35.     endmethod
36.
37.     static method New_2 takes VECTOR3 v returns VECTOR3
38.         local VECTOR3 this = VECTOR3.create()
39.         set .x = v.x
40.         set .y = v.y
41.         set .z = v.z
42.         return this
43.     endmethod
44.
45.     method SetValues takes real x, real y, real z returns VECTOR3
46.         set .x = x
47.         set .y = y
48.         set .z = z
49.         return this
50.     endmethod
51.
52.     method Length takes nothing returns real
53.         return SquareRoot(.x*.x+.y*.y+.z*.z)
54.     endmethod
55.
56.     method LengthSq takes nothing returns real
57.         return .x*.x+.y*.y+.z*.z
58.     endmethod
59.
60.     method ToString takes nothing returns string
61.         return "Vector3 id "+I2S(this) + "\nx = "+R2S(.x)+"   y = "+R2S(.y)+"   z = "+R2S(.z)
62.     endmethod
63.
64. endstruct
65.
66. function Vec3Add takes VECTOR3 Output, VECTOR3 v1, VECTOR3 v2 returns VECTOR3
67.     set Output.x = v1.x + v2.x
68.     set Output.y = v1.y + v2.y
69.     set Output.z = v1.z + v2.z
70.     return Output
71. endfunction
72.
73. function Vec3Subtract takes VECTOR3 Output, VECTOR3 v1, VECTOR3 v2 returns VECTOR3
74.     set Output.x = v1.x - v2.x
75.     set Output.y = v1.y - v2.y
76.     set Output.z = v1.z - v2.z
77.     return Output
78. endfunction
79.
80. function Vec3Scale takes VECTOR3 Output, VECTOR3 v, real r returns VECTOR3
81.     set Output.x = v.x * r
82.     set Output.y = v.y * r
83.     set Output.z = v.z * r
84.     return Output
85. endfunction
86.
87. function Vec3Division takes VECTOR3 Output, VECTOR3 v, real r returns VECTOR3
88.     set Output.x = v.x / r
89.     set Output.y = v.y / r
90.     set Output.z = v.z / r
91.     return Output
92. endfunction
93.
94. function Vec3Length takes VECTOR3 v returns real
95.     return SquareRoot(v.x*v.x+v.y*v.y+v.z*v.z)
96. endfunction
97.
98. function Vec3LengthSq takes VECTOR3 v returns real
99.     return v.x*v.x+v.y*v.y+v.z*v.z
100. endfunction
101.
102. function Vec3Normalize takes VECTOR3 Output, VECTOR3 v returns VECTOR3
103.     local real len = SquareRoot(v.x*v.x+v.y*v.y+v.z*v.z)
104.     set Output.x = v.x/len
105.     set Output.y = v.y/len
106.     set Output.z = v.z/len
107.     return Output
108. endfunction
109.
110. function Vec3Dot takes VECTOR3 v1, VECTOR3 v2 returns real
111.     return v1.x*v2.x+v1.y*v2.y+v1.z*v2.z
112. endfunction
113.
114. function Vec3Cross takes VECTOR3 Output, VECTOR3 v1, VECTOR3 v2 returns VECTOR3
115.     local real y  = v1.z * v2.x - v1.x * v2.z
116.     local real z = v1.x * v2.y - v1.y * v2.x
117.     set Output.x = v1.y * v2.z - v1.z * v2.y
118.     set Output.y = y
119.     set Output.z = z
120.     return Output
121. endfunction
122.
123. function Vec3Transform_1 takes VECTOR3 Output, VECTOR3 v, MATRIX3 m returns VECTOR3
124.     local real y = v.x*m.m12+v.y*m.m22+v.z*m.m32
125.     local real z = v.x*m.m13+v.y*m.m23+v.z*m.m33
126.     set Output.x = v.x*m.m11+v.y*m.m21+v.z*m.m31
127.     set Output.y = y
128.     set Output.z = z
129.     return Output
130. endfunction
131.
132. function Vec3Transform_2 takes VECTOR3 Output, VECTOR3 v, MATRIX4 m returns VECTOR3
133.     local real y = v.x*m.m12+v.y*m.m22+v.z*m.m32+m.m42
134.     local real z = v.x*m.m13+v.y*m.m23+v.z*m.m33+m.m43
135.     local real w = v.x*m.m14+v.y*m.m24+v.z*m.m34+m.m44
136.     set Output.x = (v.x*m.m11+v.y*m.m21+v.z*m.m31+m.m41)/w
137.     set Output.y = y/w
138.     set Output.z = z/w
139.     return Output
140. endfunction
141.
142.
143. struct MATRIX3
144.     static MATRIX3 Zero
145.     static MATRIX3 E
146.     real m11
147.     real m12
148.     real m13
149.     real m21
150.     real m22
151.     real m23
152.     real m31
153.     real m32
154.     real m33
155.
156.     static method New_0 takes nothing returns MATRIX3
157.         local MATRIX3 this = MATRIX3.create()
158.         set .m11 = 0
159.         set .m12 = 0
160.         set .m13 = 0
161.         set .m21 = 0
162.         set .m22 = 0
163.         set .m23 = 0
164.         set .m31 = 0
165.         set .m32 = 0
166.         set .m33 = 0
167.         return this
168.     endmethod
169.
170.     static method New_1 takes real r11, real r12, real r13, real r21, real r22, real r23, real r31, real r32, real r33 returns MATRIX3
171.         local MATRIX3 this = MATRIX3.create()
172.         set .m11 = r11
173.         set .m12 = r12
174.         set .m13 = r13
175.         set .m21 = r21
176.         set .m22 = r22
177.         set .m23 = r23
178.         set .m31 = r31
179.         set .m32 = r32
180.         set .m33 = r33
181.         return this
182.     endmethod
183.
184.     static method New_2 takes MATRIX3 m returns MATRIX3
185.         local MATRIX3 this = MATRIX3.create()
186.         set .m11 = m.m11
187.         set .m12 = m.m12
188.         set .m13 = m.m13
189.         set .m21 = m.m21
190.         set .m22 = m.m22
191.         set .m23 = m.m23
192.         set .m31 = m.m31
193.         set .m32 = m.m32
194.         set .m33 = m.m33
195.         return this
196.     endmethod
197.
198.     method SetValues takes real r11, real r12, real r13, real r21, real r22, real r23, real r31, real r32, real r33 returns MATRIX3
199.         set .m11 = r11
200.         set .m12 = r12
201.         set .m13 = r13
202.         set .m21 = r21
203.         set .m22 = r22
204.         set .m23 = r23
205.         set .m31 = r31
206.         set .m32 = r32
207.         set .m33 = r33
208.         return this
209.     endmethod
210.
211.     method ToString takes nothing returns string
212.         return "Matrux3 id "+I2S(this)+"\n"+R2S(.m11)+"  "+R2S(.m12)+"  "+R2S(.m13)+"\n"+R2S(.m21)+"  "+R2S(.m22)+"  "+R2S(.m23)+"\n"+R2S(.m31)+"  "+R2S(.m32)+"  "+R2S(.m33)
213.     endmethod
214.
215. endstruct
216.
217. function Matrix3Multiply takes MATRIX3 Output, MATRIX3 M1, MATRIX3 M2 returns MATRIX3
218.     return Output.SetValues(M1.m11*M2.m11 + M1.m21*M2.m12 + M1.m31*M2.m13,M1.m12*M2.m11 + M1.m22*M2.m12 + M1.m32*M2.m13,M1.m13*M2.m11 + M1.m23*M2.m12 + M1.m33*M2.m13,M1.m11*M2.m21 + M1.m21*M2.m22 + M1.m31*M2.m23,M1.m12*M2.m21 + M1.m22*M2.m22 + M1.m32*M2.m23,M1.m13*M2.m21 + M1.m23*M2.m22 + M1.m33*M2.m23,M1.m11*M2.m31 + M1.m21*M2.m32 + M1.m31*M2.m33,M1.m12*M2.m31 + M1.m22*M2.m32 + M1.m32*M2.m33,M1.m13*M2.m31 + M1.m23*M2.m32 + M1.m33*M2.m33)
219. endfunction
220.
221. function Matrix3Scaling takes MATRIX3 Output, real x, real y, real z returns MATRIX3
222.     return Output.SetValues(x,0,0,0,y,0,0,0,z)
223. endfunction
224.
225. function Matrix3RotationX takes MATRIX3 Output, real a returns MATRIX3
226.     return Output.SetValues(1,0,0,0,Cos(a),-Sin(a),0,Sin(a),Cos(a))
227. endfunction
228.
229. function Matrix3RotationY takes MATRIX3 Output, real a returns MATRIX3
230.     return Output.SetValues(Cos(a),0,Sin(a),0,1,0,-Sin(a),0,Cos(a))
231. endfunction
232.
233. function Matrix3RotationZ takes MATRIX3 Output, real a returns MATRIX3
234.     return Output.SetValues(Cos(a),-Sin(a),0,Sin(a),Cos(a),0,0,0,1)
235. endfunction
236.
237. function Matrix3RotationAxis takes MATRIX3 Output, VECTOR3 v, real a returns MATRIX3
238.     local real cosa = Cos(a)
239.     local real sina = Sin(a)
240.     return Output.SetValues(cosa + (1-cosa)*v.x*v.x,(1-cosa)*v.x*v.y-sina*v.z,(1-cosa)*v.x*v.z + sina*v.y,(1-cosa)*v.y*v.x + sina*v.z,cosa + (1-cosa)*v.y*v.y,(1-cosa)*v.y*v.z-sina*v.x,(1-cosa)*v.z*v.x-sina*v.y,(1-cosa)*v.z*v.y + sina*v.x,cosa + (1-cosa)*v.z*v.z)
241. endfunction
242.
243. function Matrix3RotationYawPitchRoll takes MATRIX3 Output, real Yaw, real Pitch, real Roll returns MATRIX3
244.     local real cosa = Cos(Yaw)
245.     local real sina = Sin(Yaw)
246.     local real cosb = Cos(Pitch)
247.     local real sinb = Sin(Pitch)
248.     local real cosy = Cos(Roll)
249.     local real siny = Sin(Roll)
250.     return Output.SetValues(cosa*cosb,cosa*sinb*siny-sina*cosy,cosa*sinb*cosy + sina*siny,sina*cosb,sina*sinb*siny + cosa*cosy,sina*sinb*cosy-cosa*siny,-sinb,cosb*siny,cosb*cosy)
251. endfunction
252.
253. struct MATRIX4
254.     static MATRIX4 Zero
255.     static MATRIX4 E
256.     real m11
257.     real m12
258.     real m13
259.     real m14
260.     real m21
261.     real m22
262.     real m23
263.     real m24
264.     real m31
265.     real m32
266.     real m33
267.     real m34
268.     real m41
269.     real m42
270.     real m43
271.     real m44
272.
273.     static method New_0 takes nothing returns MATRIX4
274.         local MATRIX4 this = MATRIX4.create()
275.         set .m11 = 0
276.         set .m12 = 0
277.         set .m13 = 0
278.         set .m14 = 0
279.         set .m21 = 0
280.         set .m22 = 0
281.         set .m23 = 0
282.         set .m24 = 0
283.         set .m31 = 0
284.         set .m32 = 0
285.         set .m33 = 0
286.         set .m34 = 0
287.         set .m41 = 0
288.         set .m42 = 0
289.         set .m43 = 0
290.         set .m44 = 0
291.         return this
292.     endmethod
293.
294.     static method New_1 takes real r11, real r12, real r13, real r14, real r21, real r22, real r23, real r24, real r31, real r32, real r33, real r34, real r41, real r42, real r43, real r44 returns MATRIX4
295.         local MATRIX4 this = MATRIX4.create()
296.         set .m11 = r11
297.         set .m12 = r12
298.         set .m13 = r13
299.         set .m14 = r14
300.         set .m21 = r21
301.         set .m22 = r22
302.         set .m23 = r23
303.         set .m24 = r24
304.         set .m31 = r31
305.         set .m32 = r32
306.         set .m33 = r33
307.         set .m34 = r34
308.         set .m41 = r41
309.         set .m42 = r42
310.         set .m43 = r43
311.         set .m44 = r44
312.         return this
313.     endmethod
314.
315.     static method New_2 takes MATRIX4 m returns MATRIX4
316.         local MATRIX4 this = MATRIX4.create()
317.         set .m11 = m.m11
318.         set .m12 = m.m12
319.         set .m13 = m.m13
320.         set .m14 = m.m14
321.         set .m21 = m.m21
322.         set .m22 = m.m22
323.         set .m23 = m.m23
324.         set .m24 = m.m24
325.         set .m31 = m.m31
326.         set .m32 = m.m32
327.         set .m33 = m.m33
328.         set .m34 = m.m34
329.         set .m41 = m.m41
330.         set .m42 = m.m42
331.         set .m43 = m.m43
332.         set .m44 = m.m44
333.         return this
334.     endmethod
335.
336.     static method New_3 takes MATRIX3 m returns MATRIX4
337.         local MATRIX4 this = MATRIX4.create()
338.         set .m11 = m.m11
339.         set .m12 = m.m12
340.         set .m13 = m.m13
341.         set .m14 = 0
342.         set .m21 = m.m21
343.         set .m22 = m.m22
344.         set .m23 = m.m23
345.         set .m24 = 0
346.         set .m31 = m.m31
347.         set .m32 = m.m32
348.         set .m33 = m.m33
349.         set .m34 = 0
350.         set .m41 = 0
351.         set .m42 = 0
352.         set .m43 = 0
353.         set .m44 = 1
354.         return this
355.     endmethod
356.
357.     method SetValues takes real r11, real r12, real r13, real r14, real r21, real r22, real r23, real r24, real r31, real r32, real r33, real r34, real r41, real r42, real r43, real r44 returns MATRIX4
358.         set .m11 = r11
359.         set .m12 = r12
360.         set .m13 = r13
361.         set .m14 = r14
362.         set .m21 = r21
363.         set .m22 = r22
364.         set .m23 = r23
365.         set .m24 = r24
366.         set .m31 = r31
367.         set .m32 = r32
368.         set .m33 = r33
369.         set .m34 = r34
370.         set .m41 = r41
371.         set .m42 = r42
372.         set .m43 = r43
373.         set .m44 = r44
374.         return this
375.     endmethod
376.
377.     method ToString takes nothing returns string
378.         return "Matrux4 id "+I2S(this)+"\n"+R2S(.m11)+"  "+R2S(.m12)+"  "+R2S(.m13)+"  "+R2S(.m14)+"\n"+R2S(.m21)+"  "+R2S(.m22)+"  "+R2S(.m23)+"  "+R2S(.m24)+"\n"+R2S(.m31)+"  "+R2S(.m32)+"  "+R2S(.m33)+"  "+R2S(.m34)+"\n"+R2S(.m41)+"  "+R2S(.m42)+"  "+R2S(.m43)+"  "+R2S(.m44)
379.     endmethod
380.
381. endstruct
382.
383. function Matrix4Multiply takes MATRIX4 Output, MATRIX4 M1, MATRIX4 M2 returns MATRIX4
384.     return Output.SetValues(M1.m11*M2.m11 + M1.m21*M2.m12 + M1.m31*M2.m13 + M1.m41*M2.m14,M1.m12*M2.m11 + M1.m22*M2.m12 + M1.m32*M2.m13 + M1.m42*M2.m14,M1.m13*M2.m11 + M1.m23*M2.m12 + M1.m33*M2.m13 + M1.m43*M2.m14,M1.m14*M2.m11 + M1.m24*M2.m12 + M1.m34*M2.m13 + M1.m44*M2.m14,M1.m11*M2.m21 + M1.m21*M2.m22 + M1.m31*M2.m23 + M1.m41*M2.m24,M1.m12*M2.m21 + M1.m22*M2.m22 + M1.m32*M2.m23 + M1.m42*M2.m24,M1.m13*M2.m21 + M1.m23*M2.m22 + M1.m33*M2.m23 + M1.m43*M2.m24,M1.m14*M2.m21 + M1.m24*M2.m22 + M1.m34*M2.m23 + M1.m44*M2.m24,M1.m11*M2.m31 + M1.m21*M2.m32 + M1.m31*M2.m33 + M1.m41*M2.m34,M1.m12*M2.m31 + M1.m22*M2.m32 + M1.m32*M2.m33 + M1.m42*M2.m34,M1.m13*M2.m31 + M1.m23*M2.m32 + M1.m33*M2.m33 + M1.m43*M2.m34,M1.m14*M2.m31 + M1.m24*M2.m32 + M1.m34*M2.m33 + M1.m44*M2.m34,M1.m11*M2.m41 + M1.m21*M2.m42 + M1.m31*M2.m43 + M1.m41*M2.m44,M1.m12*M2.m41 + M1.m22*M2.m42 + M1.m32*M2.m43 + M1.m42*M2.m44,M1.m13*M2.m41 + M1.m23*M2.m42 + M1.m33*M2.m43 + M1.m43*M2.m44,M1.m14*M2.m41 + M1.m24*M2.m42 + M1.m34*M2.m43 + M1.m44*M2.m44)
385. endfunction
386.
387. private function Init takes nothing returns nothing
388.    set VECTOR3.Zero = VECTOR3.New_0()
389.    set VECTOR3.oneX = VECTOR3.New_1(1,0,0)
390.    set VECTOR3.oneY = VECTOR3.New_1(0,1,0)
391.    set VECTOR3.oneZ = VECTOR3.New_1(0,0,1)
392.    set MATRIX3.Zero = MATRIX3.New_0()
393.    set MATRIX3.E = MATRIX3.New_1(1,0,0,0,1,0,0,0,1)
394.    set MATRIX4.Zero = MATRIX4.New_0()
395.    set MATRIX4.E = MATRIX4.New_1(1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1)
396. endfunction
397.
398. endlibrary
« Last Edit: December 18, 2017, 06:19:28 AM by moyack »

 Started by moyack Replies: 40Views: 58663 Jass Theory & Questions Started by Wojannnn Replies: 0Views: 1363 Warcraft III Models Started by Wojannnn Replies: 0Views: 1280 Warcraft III Models