Animating to wind direction (ASM tweak)

From FSDeveloper Wiki
Revision as of 12:06, 4 March 2016 by MichaelCox (talk | contribs) (minor updates)
Jump to: navigation, search



When we want an object to rotate to the wind, we tweak the source code so that an additional rotation is added depending on the variable that contains the wind direction. But this kind of tweak does not work for parts that have been animated in GMax. This article describes a different way of tweaking the object that does work with animated parts as well.

The technique described here is based on the technique that is used by CAT to condition animations. But in this tutorial we will do much more, instead of the time we will use the wind direction to drive the animation. Let's get started!

Preparing for the tweak in GMax

To make the source tweaking a little bit easier, you will have to prepare your object a little bit in GMax. I used a model of a windmill (typical Dutch object) to test this technique. In my GMax object I created an animation of the vanes. This was all done in the normal way.

The next step was to add an additional dummy animation for the rotation of the head of the windmill. It does not really matter what this animation does, as we will tweak it later on. I just added a 90 degrees swing to the head. As the other animations, like the vanes, are linked to the rotation of the head, it might look a bit strange in GMax at the moment. But you don't have to worry about that. Don't forget the tick18_ prefix for this dummy object, else the correct code is not generated in the ASM file.

Once you are ready with your animations you can export the MDL file. Make sure that you keep the ASM source files as well, as you will need these to tweak the animation.

Tweaking the ASM file

The basic idea

The basic idea of the tweak we are going to do, is that instead of using the time to determine a frame of the animation table, we are going to use the direction of the wind to do this selection. So the animation table of for example 0 to 100 frames, corresponds with a wind direction between 0 and 360 degrees. To achieve this we will have to change a few things. The first is the animation table itself of course, it must represent a nice rotation between 0 and 360 degrees of the object. But we will also have to change the part of the code that now interpolates the frame number from a timer. This must read the wind direction for us and provide a frame number to use from that.

The original ASM code

Before we start tweaking it is probably useful to show you how the original ASM code looked at my machine. For your animation it could be different of course, but the structure of the code should be the same. Below you find the code of the ANIP section for the dummy animation (I have removed some of the frames from the table to keep it readable):

; Animation parameters for animation: 0
molen_zeldenrust_zuidbarge_1_tick18_mod_1 label word
    dw  42
    dw -32768,0,-29554,3215
    dw -29552,0,-26338,3215
    dw -26336,0,-23122,3215
    dw -23120,0,-19906,3215
    dw -19904,0,-16690,3215
    dw -16688,0,-13474,3215
    dw -13472,0,-10258,3215
    dw -10256,0,-7042,3215
    dw -7040,0,-3826,3215
    dw -3824,0,-610,3215
    dw -608,0,2606,3215
    dw 2608,0,5822,3215
    dw 5824,0,9038,3215
    dw 9040,0,12254,3215
    dw 12256,0,15470,3215
    dw 15472,0,18686,3215
    dw 18688,0,21902,3215
    dw 21904,0,25118,3215
    dw 25120,0,28334,3215
    dw 28336,0,31550,3215
    dw 31552,0,32767,1216
molen_zeldenrust_zuidbarge_1_tick18_mod_2 label word
    dw  32
    dw 0,0,200,200
    dw 201,0,401,200
    dw 402,0,602,200
    dw 603,0,803,200
    dw 804,0,1004,200
    dw 1005,0,1205,200
    dw 1206,0,1406,200
    dw 1407,0,1607,200
    dw 1608,0,1808,200
    dw 1809,0,2009,200
    dw 2010,0,2210,200
    dw 2211,0,2411,200
    dw 2412,0,2612,200
    dw 2613,0,2813,200
    dw 2814,0,3014,200
    dw 3015,0,3215,200
molen_zeldenrust_zuidbarge_1_float_convert_hi  label word
    dw  12               ; num entries
    dw  0,0
    dw  1,16256
    dw  2,16384
    dw  4,16512
    dw  8,16640
    dw  16,16768
    dw  32,16896
    dw  64,17024
    dw  128,17152
    dw  256,17280
    dw  512,17408
    dw  1024,17536
molen_zeldenrust_zuidbarge_quat_1        label word
    dw           3       ; 3: Quaternion (rotation) 
    real4     -1.0       ; Previous panim value
    real4 16 dup (0.0)   ; Cached matrix 
    dw         101       ; number of entries
    real4      0.0,  0.000000,  0.000000,  0.000000,  1.000000 ; frame/x/y/z/w values
    real4      2.0,  0.000000,  0.000000, -0.007854,  0.999969 ; frame/x/y/z/w values
    real4      4.0,  0.000000,  0.000000, -0.015707,  0.999877 ; frame/x/y/z/w values
    ;
    ; few frames removed here
    ;
    real4    196.0,  0.000000,  0.000000, -0.695913,  0.718126 ; frame/x/y/z/w values
    real4    198.0,  0.000000,  0.000000, -0.701531,  0.712638 ; frame/x/y/z/w values
    real4    200.0,  0.000000,  0.000000, -0.707107,  0.707107 ; frame/x/y/z/w values

And here is the original code of the ANIC section for the dummy animation:

; Animation command for animation: 0  output index: 1
bgl_animation_command_start_molen_zeldenrust_zuidbarge_1	label	BGLCODE
    ; usrvar = tick18 mod 201
    LOCAL_BASE_32 molen_zeldenrust_zuidbarge_1_tick18_mod_1
    BGL_INTERPOLATE VAR_BASE_GLOBAL,BGL_TICK18,\
                    VAR_BASE_GLOBAL,usrvar,\
                    VAR_BASE_LOCAL,(offset molen_zeldenrust_zuidbarge_1_tick18_mod_1 - offset molen_zeldenrust_zuidbarge_1_tick18_mod_1)
    LOCAL_BASE_32 molen_zeldenrust_zuidbarge_1_tick18_mod_2
    BGL_INTERPOLATE VAR_BASE_GLOBAL,usrvar,\
                    VAR_BASE_GLOBAL,usrvar,\
                    VAR_BASE_LOCAL,(offset molen_zeldenrust_zuidbarge_1_tick18_mod_2 - offset molen_zeldenrust_zuidbarge_1_tick18_mod_2)
    ; convert usrvar to float and store in usrvr2-usrvr3
    LOCAL_BASE_32 molen_zeldenrust_zuidbarge_1_float_convert_hi
    BGL_INTERPOLATE VAR_BASE_GLOBAL,usrvar,\
                    VAR_BASE_GLOBAL,usrvr3,\
                    VAR_BASE_LOCAL,(offset molen_zeldenrust_zuidbarge_1_float_convert_hi - molen_zeldenrust_zuidbarge_1_float_convert_hi)
    VAR_BASE_32   VAR_BASE_GLOBAL
    SETWRD usrvr2,0
    LOCAL_BASE_32 molen_zeldenrust_zuidbarge_quat_1
    BGL_ANIMATE_INDIRECT   VAR_BASE_GLOBAL,usrvr2,VAR_BASE_LOCAL,(offset molen_zeldenrust_zuidbarge_quat_1 - offset molen_zeldenrust_zuidbarge_quat_1),0.000000,-1.246083,-17.500000,1
 bgl_animation_command_end_molen_zeldenrust_zuidbarge_1	label	BGLCODE

Adding the local variable space

Before we start with the actual tweaking, there is one thing I added. A local variable space. This is not really needed, but doing it has some advantages. By default GMax stores the result of the interpolation to determine the frame number in the global user variables. But we have only 5 of them, so I find it a bit wasteful to use them for something like this. By defining a local variable space, ... \n

private label cosmetics, custom made cosmetic, custom made cream, custom lip stick lip balm

Offers private label cosmetics with custom design. We produce lip balms, serum, creams and more. Easy to buy with eshop. Private label cosmetics, custom made cosmetic, custom made cream, custom lip stick lip balm

[private label cosmetics, custom made cosmetic, custom made cream, custom lip stick lip balm]

[private label cosmetics, custom made cosmetic, custom made cream, custom lip stick lip balm]

private label cosmetics, custom made cosmetic, custom made cream, custom lip stick lip balm

Offers private label cosmetics with custom design. We produce lip balms, serum, creams and more. Easy to buy with eshop. Private label cosmetics, custom made cosmetic, custom made cream, custom lip stick lip balm

[private label cosmetics, custom made cosmetic, custom made cream, custom lip stick lip balm]

[private label cosmetics, custom made cosmetic, custom made cream, custom lip stick lip balm]

private label cosmetics, custom made cosmetic, custom made cream, custom lip stick lip balm

Offers private label cosmetics with custom design. We produce lip balms, serum, creams and more. Easy to buy with eshop. Private label cosmetics, custom made cosmetic, custom made cream, custom lip stick lip balm

[private label cosmetics, custom made cosmetic, custom made cream, custom lip stick lip balm]

[private label cosmetics, custom made cosmetic, custom made cream, custom lip stick lip balm]

private label cosmetics, custom made cosmetic, custom made cream, custom lip stick lip balm

Offers private label cosmetics with custom design. We produce lip balms, serum, creams and more. Easy to buy with eshop. Private label cosmetics, custom made cosmetic, custom made cream, custom lip stick lip balm

[private label cosmetics, custom made cosmetic, custom made cream, custom lip stick lip balm]

[private label cosmetics, custom made cosmetic, custom made cream, custom lip stick lip balm]

private label cosmetics, custom made cosmetic, custom made cream, custom lip stick lip balm

Offers private label cosmetics with custom design. We produce lip balms, serum, creams and more. Easy to buy with eshop. Private label cosmetics, custom made cosmetic, custom made cream, custom lip stick lip balm

[private label cosmetics, custom made cosmetic, custom made cream, custom lip stick lip balm]

[private label cosmetics, custom made cosmetic, custom made cream, custom lip stick lip balm]