Author Topic: Questions about triggered animation  (Read 15585 times)

0 Members and 1 Guest are viewing this topic.

Offline Nuke

  • Ka-Boom!
  • 212
  • Mutants Worship Me
Re: Questions about triggered animation
using functions like require and dofile looks for things in fs root. this is generally not the way freespace mods are supposed to work. instead you should use cfile to load your scripts from file, and then run the code with loadstring. theres a thread with a code example here.

ideally these built in functions should be modified to use the game's filesystem, and include some anti cheat and module security features (crced lua files and mod-approved binary modules), which will come in handy if multiplayer ever utilizes scripting. this probibly should be done before loadstring is depricated in future lua versions.

as for world/local transform conversion you just need to multiply your local matrix by its parent's orientation. i want to say its shipOri*subSysOri but im not sure. matrix multiplication is non commutative, so a*b != b*a. so if that dont work try it the other way around. if your transforming vectors you can just use orientation:unrotateVector(vector) or orientation:rotateVector(vector).
« Last Edit: December 06, 2012, 11:19:44 pm 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

 
Re: Questions about triggered animation
Bookmarked it to read latter tonight. I want to have only the necessary code for each ship to come with the model, and the main lua in a seperately held tbm file.  Course there needs to be some editing on whoever uses it, but only trivial adding a few lines.

The way I'm doing it right now every .01 of a second i adjust the rotation the some amount (total rotation / total amount of time [seconds])

Code: [Select]
#Conditional Hooks


$Application: FS2_Open
$On Game Init:
[
lastFrameTime = mn.getMissionTime ()
animationQueue = {}

function CreateOrientation (x, y, z)
return ba.createOrientation (math.rad(x),math.rad(y),math.rad(z))
end

function SetupAnimation(shipName, subsystemName, destOrientation, rotationTime)

if type(shipName) ~= "string" then
return false
end

if type (subsystemName) ~= "string" then
return false
end

local shp = mn.Ships[shipName]

-- Make sure we actually have a ship
if shp == nil then
return false
end

if shp:isValid() then
subsystem = shp[subsystemName]

-- Make sure we actually have a subsystem
if subsystem == nil then
return false
end

if subsystem:isValid () then
-- Simply add it to the animation queue
AddAnimationToQueue (shp, subsystem, destOrientation, rotationTime)
return true
end
end
return false
end

function AddAnimationToQueue (ship, subsystem, destOrientation, timeToLive)

totalNumberOfSteps = timeToLive * 100
animation = {}
animation.ship = ship
animation.subsystem = subsystem
animation.destOrientation = destOrientation
animation.runTime = timeToLive

-- Find the delta difference between the source and destination angles
animation.deltaX = destOrientation.p - subsystem.Orientation.p
animation.deltaY = destOrientation.b - subsystem.Orientation.b
animation.deltaZ = destOrientation.h - subsystem.Orientation.h

-- Number of degrees to move during each animation step
animation.deltaX = (animation.deltaX / totalNumberOfSteps) 
animation.deltaY = (animation.deltaY / totalNumberOfSteps)
animation.deltaZ = (animation.deltaZ / totalNumberOfSteps)

table.insert (animationQueue, animation)
end

function ProcessAnimation (animation)
pitch = animation.subsystem.Orientation.p
bank = animation.subsystem.Orientation.b
heading = animation.subsystem.Orientation.h

newX = pitch + animation.deltaX
newY = bank + animation.deltaY
newZ = heading + animation.deltaZ

animation.subsystem.Orientation = ba.createOrientation (newX, newY, newZ)

end

--require (".\\scripts\\Bearcat.lua")
function LowerBearcatLandingGear (shipName)
SetupAnimation (shipName, "FrontLandingGear", CreateOrientation (-90, 0, 0), 1.5 )
SetupAnimation (shipName, "PortMainLandingGear", CreateOrientation (0, -90, 0), 1.5)
SetupAnimation (shipName, "StarMainLandingGear", CreateOrientation (0, 90, 0), 1.5)
SetupAnimation (shipName, "PortDoor01", CreateOrientation (0, -90, 0), .5)
SetupAnimation (shipName, "PortDoor02", CreateOrientation (0, -90, 0), .5)
SetupAnimation (shipName, "PortDoor03", CreateOrientation (0, -90, 0), .5)
SetupAnimation (shipName, "StarDoor01", CreateOrientation (0, 90, 0), .5)
SetupAnimation (shipName, "StarDoor02", CreateOrientation (0, 90, 0), .5)
SetupAnimation (shipName, "StarDoor03", CreateOrientation (0, 90, 0), .5)
SetupAnimation (shipName, "MissileBayPortDoor", CreateOrientation (0, -90, 0), 2.5)
SetupAnimation (shipName, "MissileBayPortDoor01", CreateOrientation (0, -90, 0), 2.5)
SetupAnimation (shipName, "MissileBayStarDoor", CreateOrientation (0, 90, 0), 2.5)
SetupAnimation (shipName, "MissileBayStarDoor01", CreateOrientation (0, 90, 0), 2.5)

end

function RaiseBearcatLandingGear (shipName)
SetupAnimation (shipName, "FrontLandingGear", CreateOrientation (0, 0, 0), 1.5 )
SetupAnimation (shipName, "PortMainLandingGear", CreateOrientation (0, 0, 0), 1.5)
SetupAnimation (shipName, "StarMainLandingGear", CreateOrientation (0, 0, 0), 1.5)
SetupAnimation (shipName, "PortDoor01", CreateOrientation (0, 0, 0), 3.5)
SetupAnimation (shipName, "PortDoor02", CreateOrientation (0, 0, 0), 3.5)
SetupAnimation (shipName, "PortDoor03", CreateOrientation (0, 0, 0), 3.5)
SetupAnimation (shipName, "StarDoor01", CreateOrientation (0, 0, 0), 3.5)
SetupAnimation (shipName, "StarDoor02", CreateOrientation (0, 0, 0), 3.5)
SetupAnimation (shipName, "StarDoor03", CreateOrientation (0, 0, 0), 3.5)
SetupAnimation (shipName, "MissileBayPortDoor", CreateOrientation (0, 0, 0), 2.5)
SetupAnimation (shipName, "MissileBayPortDoor01", CreateOrientation (0, 0, 0), 2.5)
SetupAnimation (shipName, "MissileBayStarDoor", CreateOrientation (0, 0, 0), 2.5)
SetupAnimation (shipName, "MissileBayStarDoor01", CreateOrientation (0, 0, 0), 2.5)
end


function OpenLandGear(ship)
--if (ship.Class.Name == "F-104D Bearcat") then
LowerBearcatLandingGear (ship)
--end
end

function CloseLandGear(ship)
--if (ship.Class.Name == "F-104D Bearcat") then
RaiseBearcatLandingGear (ship)
--end
end

]

$State: GS_STATE_GAME_PLAY
$On Frame:
[

-- Make sure animationQueue exists and isn't empty (no sense in processing nothing)
if (animationQueue == nil) then
return false
end

if (#animationQueue == 0) then
return
end

currentSecond = mn.getMissionTime ()

-- Make sure the game has moved by X amount of seconds
if (currentSecond < (lastFrameTime+.01)) then
return
end

for loop = 1, #animationQueue do
anim = animationQueue [loop]

-- Make sure the subsystem still exists
if not (anim.subsystem:isValid ()) then
table.remove (animationQueue, loop)
break
end

ProcessAnimation (anim)

-- Decrement the amount of time the animation has left to live
anim.runTime = anim.runTime - .01
if (anim.runTime <= 0) then
-- Animation must be done by now.. deleting it
table.remove (animationQueue, loop)
break
end

end

lastFrameTime = currentSecond
]
#End
 


Edit: Hmmm it looks like Orientation does return a local angle... If I grab the p,b & h from a subsystem before it starts rotating it's 0,0,0.  But it rotates like it's rotating on the parent objects axis instead.

double edit: For the fun of it I put in the ship space angles in and the results were definitely not right!  So I'm not sure what exactly is going wrong. 
triple edit:  Just to see where the rotation goes, I rotated one of the bay doors into a completely different orientation, but the script is still applying the same rotation as before, so it's definitely not using the subobjects local.
« Last Edit: December 07, 2012, 04:47:02 am by Scooby_Doo »
That's cool and ....disturbing at the same time o_o  - Vasudan Admiral

"Don't play games with me. You just killed someone I like, that is not a safe place to stand. I'm the Doctor. And you're in the biggest library in the universe. Look me up."

"Quick everyone out of the universe now!"

 
Re: Questions about triggered animation
You might understand this better than I can but here's some testing:
The subsystem (missilebayportdoor01) world rotation is (8.62, 0, 0)   local is obviously (0,0,0).
When I rotate it (0,-90,0) I get this:


The y rotation is correct (-90), but on the other axis it's incorrect.
Now if I enter 8.62 into the other two axis then it works perfectly.  I can understand why you would need an offset for one of the axis, but for both?!?
That's cool and ....disturbing at the same time o_o  - Vasudan Admiral

"Don't play games with me. You just killed someone I like, that is not a safe place to stand. I'm the Doctor. And you're in the biggest library in the universe. Look me up."

"Quick everyone out of the universe now!"

 
Re: Questions about triggered animation
Some more testing.  I added drawstring debugger info watching the p,b & h for it as it opened up.  The numbers went from 0 to down -90 for banking and stayed 0 for the rest.  This is how it should work... only the banking should change on the local axis.  There must be something I'm completely missing  or just not aware of.
That's cool and ....disturbing at the same time o_o  - Vasudan Admiral

"Don't play games with me. You just killed someone I like, that is not a safe place to stand. I'm the Doctor. And you're in the biggest library in the universe. Look me up."

"Quick everyone out of the universe now!"

 

Offline Nuke

  • Ka-Boom!
  • 212
  • Mutants Worship Me
Re: Questions about triggered animation
i think this is a common problem when using euler angles. the problem of gimbal lock. if you rotate the subsystem 90 degrees on the z axis for example, and then want to pitch it up, you would end up yawing to one side because the frame of reference has changed (its been rotated 90 degrees). what happened here is you started at an orientation that is already rotated 8.62 degrees on the pitch axis. you want to rotate about that non-orthagonal axis represented by the initial rotation, not the z axis, but when you rotate by 0'90'0 your rotating about the z axis, resulting in what happened in the image. my instincts are telling me to take the desired orientation of 0'90'0 and matrix multiply it by the transpose of the original orientation of 8.62'0'0. im not totally sure if thats right, the multiply order may be wrong or may be transposing the wrong thing (or maybe you dont need the transpose). but the idea is to put your desired rotation into the initial frame of reference. then just use the resulting orientation in place of 0'90'0.
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

 
Re: Questions about triggered animation
Solved that problem ughhh....
Also... are there ANY debugging tools that come with this? drawString is getting very annoying after a while.  I'd kill for a gui debugger right about now lol.
« Last Edit: December 08, 2012, 06:03:06 am by Scooby_Doo »
That's cool and ....disturbing at the same time o_o  - Vasudan Admiral

"Don't play games with me. You just killed someone I like, that is not a safe place to stand. I'm the Doctor. And you're in the biggest library in the universe. Look me up."

"Quick everyone out of the universe now!"

 

Offline Nuke

  • Ka-Boom!
  • 212
  • Mutants Worship Me
Re: Questions about triggered animation
run in a window with a debug build and just use ba.print(). you can print anything to the debug console. i just wish you could do the same with a release build. if i ever get around to doing that keyframe animation script i want to do i will probibly also include an in-game animation editor in the lab.
« Last Edit: December 08, 2012, 01:19:46 pm 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

 
Re: Questions about triggered animation
Here's a bit of info.  Before I start the animation sequence I store the Orientation matrix into a table.
Then I spit the table out to the screen (drawstring)
here's what it looks like (math.deg):

Code: [Select]
57.29 0       0
0       57.29 0
0       0        57.29 

Model is (8.62,0,0) not sure where the 57.29 is coming from.

also non-deg version (i assume in radians) is
1 0 0
0 1 0
0 0 1

edit:Hmmm this also happens when i look at the ships orientation matrix (which is also 0,0,0) but it does change as i rotate the ship around
« Last Edit: December 09, 2012, 03:03:40 am by Scooby_Doo »
That's cool and ....disturbing at the same time o_o  - Vasudan Admiral

"Don't play games with me. You just killed someone I like, that is not a safe place to stand. I'm the Doctor. And you're in the biggest library in the universe. Look me up."

"Quick everyone out of the universe now!"

  

Offline Nuke

  • Ka-Boom!
  • 212
  • Mutants Worship Me
Re: Questions about triggered animation
thats what you call an identity matrix. it doesnt contain any rotation, this is the same as 0'0'0. isn't linear algebra fun! :D
« Last Edit: December 09, 2012, 03:42:34 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

 
Re: Questions about triggered animation
thats what you call an identity matrix. it doesnt contain any rotation, this is the same as 0'0'0. isn't linear algebra fun! :D

Ahhhh I was wondering if it was the indentity. LOL I barely remember linear algebra from years ago.  Also I remember doing some reading on euler angles and said screw it LOL
Also [1][2] and [2][1] will change from zero to values as you rotate (i assume rotational values for each axis)

Ok I'm going to do the destinationOrientation * Transpose (worldAxisOrientation) and see what that produces.  There is a slight problem I can already  forsee.  I don't see a way of seeing the (8.62,0,0) ingame.  Ingame the subsystem  shows up as (0,0,0), it's local axis

edit: oh oh
Code: [Select]
__mul: Argument 2 is an invalid type 'table'; type 'orientation' expected

Code: [Select]
function Transpose( m )
local newOrient = {}

newOrient[1] = m[1]
newOrient[2] = m[4]
newOrient[3] = m[7]
newOrient[4] = m[2]
newOrient[5] = m[5]
newOrient[6] = m[8]
newOrient[7] = m[3]
newOrient[8] = m[6]
newOrient[9] = m[9]

return newOrient
end
Code: [Select]
tranpose = Transpose (ba.createOrientation (8.63,0,0))
newDest = destOrientation * transpose
Is there anyway to do a orientation constructor or have newOrient be declared as an orientation?
« Last Edit: December 09, 2012, 04:32:00 am by Scooby_Doo »
That's cool and ....disturbing at the same time o_o  - Vasudan Admiral

"Don't play games with me. You just killed someone I like, that is not a safe place to stand. I'm the Doctor. And you're in the biggest library in the universe. Look me up."

"Quick everyone out of the universe now!"

 

Offline Nuke

  • Ka-Boom!
  • 212
  • Mutants Worship Me
Re: Questions about triggered animation
most porgrammers consider euler angles evil, prefering matrices or quaternions instead. the only reason they are used at all is because most users and artists understand that system.

ba.createOrientation() can take 0, 3 or 9 parameters. with no parameters it creates an identity matrix, with 3 it assumes its a euler angle, with 9 it takes a full matrix. reguardless you end up with an orientation object which is just a 3x3 matrix internally. instead of making your own function i think you can use orientation:getTranspose() to transpose a matrix. tables generally dont have operators defined (but you can if you set a metatable, but thats a different discussion).

this should be allowed.
Code: [Select]
newDest = destOrientation * ( ba.createOrientation(8.63,0,0):getTranspose() )
« Last Edit: December 09, 2012, 12:38:23 pm 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

 
Re: Questions about triggered animation
Unfortunately neither dest*ba.create nor ba.create*dest work.  Both cause the door to excessively spin around on the wrong axis (z)

I've stepped back and starting working with a much simpler model.  So far I've noticed that when I rotate on one axis, it rotates based on the world (ship) axis not the subsystem.  Two when I rotate are more than one axis, things tend to go wrong, I'm guessing that euler issue.  Will report back when I've found more info.

edit: Third, the subsystem.Orientation p/b/h all report 0.  So subsystem.Orientation must be based on it's local axis-space.
That's cool and ....disturbing at the same time o_o  - Vasudan Admiral

"Don't play games with me. You just killed someone I like, that is not a safe place to stand. I'm the Doctor. And you're in the biggest library in the universe. Look me up."

"Quick everyone out of the universe now!"

 
Re: Questions about triggered animation
Another test....

Here's the sample code:
Code: [Select]

if (waitStep == 0) then
animation.subsystem.Orientation = CreateOrientation (45,0,0)
return true
end
if (waitStep == 1) then
animation.subsystem.Orientation = CreateOrientation (0,90,0)
return true
end

waitStep is changed via keypress, that way you only go through the animation sequence once per press. First time it moves the object to world (0,0,0) then rotates it "open"


Here's how the test model is setup, the "door" is -45,0,0 (world)


Here's after the first keypress (45,0,0), Looks good!


And after rotating it 0,90,0... errr something happened bad!
That's cool and ....disturbing at the same time o_o  - Vasudan Admiral

"Don't play games with me. You just killed someone I like, that is not a safe place to stand. I'm the Doctor. And you're in the biggest library in the universe. Look me up."

"Quick everyone out of the universe now!"

 

Offline Nuke

  • Ka-Boom!
  • 212
  • Mutants Worship Me
Re: Questions about triggered animation
the problem here is we need to rotate a thing about an arbitrary axis. heres how you build an arbitrary axis rotation matrix:

Code: [Select]
function arbitraryAxisMatrix(w,x,y,z)
 local cosi = math.cos(w)
 local sine = math.sin(w)
 local tang = 1 - cosi
 local aarot = ba.createOrientation( tang * x^2 + cosi, tang * x * y + sine * z, tang * x * z - sine * y,
     tang * x * y - sine * z,         tang * y^2 + cosi, tang * y * z + sine * x,
     tang * x * z + sine * y,         tang * y * z - sine * x, tang * z^2 + cosi )
 return aarot
end

i havent tested this with freespace though, the math was pulled from my quaternion library (which was likewise untested) for nukesim. so im not sure if the handedness is the same. but x,y,z is a normalized vector which is the axis of rotation, and w is the rotation about that axis (essentially a quaternion). returns an orientation object. in your case the axis would be parallel to the hinge on your missile door. 
« Last Edit: December 10, 2012, 10:47:11 pm 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

 
Re: Questions about triggered animation
Hey I think I'm making some progress...
It's gaudy and inefficient:

Let's say I want to rotate that 90 so i looks like it opened up
Code: [Select]
temp = arbitraryAxisMatrix (math.rad(45), 1,0,0)   --45 90  0
temp = arbitraryAxisMatrix (math.rad (90), 0,0,1) * temp
temp = arbitraryAxisMatrix (math.rad (-45), 1,0,0) * temp
Looks like you have to rotate the object back down to the worlds (0,0,0), apply the rotation you want then reset it back to the original local rotation
That's cool and ....disturbing at the same time o_o  - Vasudan Admiral

"Don't play games with me. You just killed someone I like, that is not a safe place to stand. I'm the Doctor. And you're in the biggest library in the universe. Look me up."

"Quick everyone out of the universe now!"

 
Re: Questions about triggered animation
Oh... I understand how to take something originally rotated (-45,0,0) to (0,0,0) -> (math.rad(45),1,0,0)
how do I do something that's rotated say (-45,90,0)?
Do I have to do this:
(math.rad(45),1,0,0) * (math.rad(-90),0,1,0)    [or reverse whatever it happens to be]

?
That's cool and ....disturbing at the same time o_o  - Vasudan Admiral

"Don't play games with me. You just killed someone I like, that is not a safe place to stand. I'm the Doctor. And you're in the biggest library in the universe. Look me up."

"Quick everyone out of the universe now!"

 

Offline Nuke

  • Ka-Boom!
  • 212
  • Mutants Worship Me
Re: Questions about triggered animation
never hurts to try it. thats my usuall aproach, to tweak the math till it works. if that doesnt work, another thing you can try is you can transform the xyz vector for the second matrix by the first matrix (use orientation:unrotateVector()) and then use the transformed vector in place of the original vector.

Code: [Select]
mat1 = arbitraryAxisMatrix(math.rad(45),1,0,0)
 vec = mat1:unrotateVector(ba.createVector(0,1,0))
 mat2 = arbitraryAxisMatrix(math.rad(-90),vec.x,vec.y,vec.z)
 mat3 = mat1*mat2

of course im not sure if that will do it. the first matrix stores the 45 degree rotation. along the x axis.
the vector for the second matrix is then rotated into the space represented by the first matrix.
this axis is then used for the second matrix. im not quite sure whether mat2 or mat3 is the one you need.
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

 
Re: Questions about triggered animation
For the moment I'll stick with what I can understand  :D
However there seems to be a small problem, at the 90 degree the object "flickers"/switches around axis then resumes normal motion afterwards.  I'm guessing something to do with sin/cos(90)?
I'm going to step by step from about 88 to 92 and look at the matrix entries.

edit:
I've narrowed it down to just that matrix that does the 90 degree mark       arbitraryAxisMatrix (math.rad(p),0,0,1)
At 89:
Code: [Select]
.99      57    0
-57     .99   0
0        0      57

At 90:
Code: [Select]
~0      57     0
-57     ~ 0    0
0         0      57


Everything works out fine in the excel worksheet.   I wonder if it's something in the matrix multiplication i don't understand, because cos(x) looks good.  89 = .017 90 = 0 91 = -.017
« Last Edit: December 12, 2012, 04:19:31 am by Scooby_Doo »
That's cool and ....disturbing at the same time o_o  - Vasudan Admiral

"Don't play games with me. You just killed someone I like, that is not a safe place to stand. I'm the Doctor. And you're in the biggest library in the universe. Look me up."

"Quick everyone out of the universe now!"

 

Offline Nuke

  • Ka-Boom!
  • 212
  • Mutants Worship Me
Re: Questions about triggered animation
flicker is usually caused by a singularity. your better off interpolating across one of those than animating the angles.

matrix multiply can either be row-column or column-row, and also there are left handed and right handed systems. i dont really have much time to go into this, i have to catch a plane in about 30 minutes, but heres a good site for understanding all the game math you would ever use: http://www.euclideanspace.com/maths/algebra/matrix/index.htm
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

 
Re: Questions about triggered animation
Some good news... if the subsystem origins are at (0,0,0) world then everything works great.  Now if you adjust the subystems rotation as part of the pof, when you go to rotate it tends to wobble, not the wobble because of 90 degree stuff.

Now gotta see what's causing that.

edit: Hmmm I wonder if it's because I'm modifying the same axis twice in the matrix (first to reset to 0,0,0, then to get the new angle)


double edit:Ugh I don't know what I was doing before.  Whatever it was was a heck of a lot more work than what was needed.  Apparently the subsystems (0,0,0) is the actual subsystems (0,0,0) [not the ships (0,0,0)].  This makes things 75% easier.  Need to do more testing but this looks very hopeful!
« Last Edit: December 13, 2012, 04:31:50 am by Scooby_Doo »
That's cool and ....disturbing at the same time o_o  - Vasudan Admiral

"Don't play games with me. You just killed someone I like, that is not a safe place to stand. I'm the Doctor. And you're in the biggest library in the universe. Look me up."

"Quick everyone out of the universe now!"