### Author Topic: nuke ass masters 3d point rotation  (Read 2027 times)

0 Members and 1 Guest are viewing this topic.

#### Nuke

• Ka-Boom!
• 212
• Mutants Worship Me
##### nuke ass masters 3d point rotation
after 4 articals on the subject, 5 lieters of cola and about 12 headache pills i finally figured out matrix rotation, from scratch i needed a way to rotate normals for my turret modifications, but they could apply to anything from drawing gauges in 3d to writing hud elite. mainly i did it to help me better understand orientations and matricies. so i got 3 usefull functions to play with.

im using a 3*3 matrix for simplicity, in an array 1-9 (or table if you insist upon luas terminology). firs function multiplies one 3x3 with another. the second function multiplies the matrix with a vector (this is what they mean when they apply the matrix). both of those are required by the 3rd function. it creates an x,y,and z matrix, multiplies them together, and applies it tio the normal. i might break it down into 2 funcs, one to create the matrix and the other to rotate with. but for now im happy i finally cracked this nut.

keep in mind this, at least according to what i read, suffers from gimbal lock and is the slow way of doing things. theres actually a better way, but for now this suits my needs. i post the funcs to hopefully help people understand what matricies are all about. also keep in mind this doesnt create an orientation object, and will crash any function that operates on an orientation object if you try to use my matrix instead instead.

Code: [Select]
`multmat = function(mat1,mat2) --multiply a couple 3x3 matricies local mat3 = {} mat3 = (mat1 * mat2) + (mat1 * mat2) + (mat1 * mat2) mat3 = (mat1 * mat2) + (mat1 * mat2) + (mat1 * mat2) mat3 = (mat1 * mat2) + (mat1 * mat2) + (mat1 * mat2) mat3 = (mat1 * mat2) + (mat1 * mat2) + (mat1 * mat2) mat3 = (mat1 * mat2) + (mat1 * mat2) + (mat1 * mat2) mat3 = (mat1 * mat2) + (mat1 * mat2) + (mat1 * mat2) mat3 = (mat1 * mat2) + (mat1 * mat2) + (mat1 * mat2) mat3 = (mat1 * mat2) + (mat1 * mat2) + (mat1 * mat2) mat3 = (mat1 * mat2) + (mat1 * mat2) + (mat1 * mat2) return mat3endmatxvec = function(mat,vec) --multiply a vextor by a matrix local x = (mat * vec.x) + (mat * vec.y) + (mat * vec.z) local y = (mat * vec.x) + (mat * vec.y) + (mat * vec.z) local z = (mat * vec.x) + (mat * vec.y) + (mat * vec.z) vec.x,vec.y,vec.z = x,y,z return vecendrotnorm = function(norm,angs) --rotates a normal, in theory local matx = {1,0,0,0,1,0,0,0,1} local maty = {1,0,0,0,1,0,0,0,1} --create 3x3 matrix arrays for x y and z and fill it in with an identity matrix local matz = {1,0,0,0,1,0,0,0,1} matx = math.cos(angs.x) matx = math.sin(angs.x) matx = -math.sin(angs.x) matx = math.cos(angs.x) maty = math.cos(angs.y) maty = -math.sin(angs.y) --fill in the nessicary parts of the matrix for each axis maty = math.sin(angs.y) maty = math.cos(angs.y) matz = math.cos(angs.z) matz = math.sin(angs.z) matz = -math.sin(angs.z) matz = math.cos(angs.z) local mat = multmat(matx,maty) --multiply our matrixies mat = multmat(mat,matz) --to get total matrix norm = matxvec(mat,norm) --apply the matrix return normend`
« Last Edit: February 05, 2007, 07:25:53 am by Nuke »
I can no longer sit back and allow communist infiltration, communist indoctrination, communist subversion, and the international communist conspiracy to sap and impurify all of our precious bodily fluids.

Nuke's Scripting SVN

#### WMCoolmon

##### Re: nuke ass masters 3d point rotation
Given that for indexing the orientation object, the values have to go through the Lua interpreter and grab the values from the C environment, these functions are probably pretty slow. But there's not any alternative builtin to scripting atm, so it's probably just a matter of using them sparingly.
-C

#### Nuke

• Ka-Boom!
• 212
• Mutants Worship Me
##### Re: nuke ass masters 3d point rotation
yea, rendering a cobra mk3 (35 or so points, its actually 12 but i had to make duplicates cause the way my draw model function worked) is about 3 times slower than the 9 point paper airplane model in my other example. but i really didnt write it for model rotation, i wrote it for the arbitrary rotation of vectors and normals. though the idea of a 3d hud is a pretty cool concept. im still trying to understand how to use quaternions to do this, aparently it gets rid of gimbal lock and is faster. arbitrary point rotations is something id like to see built in functions for.
I can no longer sit back and allow communist infiltration, communist indoctrination, communist subversion, and the international communist conspiracy to sap and impurify all of our precious bodily fluids.

Nuke's Scripting SVN