Yet another blending code

Posted By: eleroux

Yet another blending code - 08/23/06 13:56

I just wanted to share this code I came with while playing with ent_blendpose.

I know blendpose is slower than ent_blend, but we are talking about just a few frames, and I think there are other advantages.

What I like in this is that the animation that is blending-in starts animating right away (no wait for the blend to end), and that the code ended up ridiculously simple. (To tell the truth I had no idea it would end this way when I started coding)

Be aware that this is not ready-to-use. It doesn't include the functions for setting up strings and animations, or entity physics - I just provide it as a general idea of using ent_blendpose.

Code:


Function mAnim_Animate_Actor()
{

mAnim_initSceneStrings(); //setup animation strings

var base_scene = 1;
var base_scene_old = 1;
var base_mode = 0; //cyclic, one time, one time no blend
var base_cycle = 0;

var base_anim = 0;
var base_anim_old = 0;

var blending = 0;


//loop while actor exists
while (me)
{
//set animation state
if (my._mActor_movestate == STATE_NONE)
{
//no animation
wait(1);
continue;
}

//Get Base scene and parameters
mAnim_setBaseAnim(); //will determine the scene and other global vars below
base_scene = mAnim_temp_basescene;
base_mode = mAnim_temp_basemode;

base_cycle = mAnim_temp_basecycle; //temp globals set by the function


if (base_cycle == 0)
{
base_cycle = 16; //to avoid possible errors
}

if (base_cycle < 0)
{
//time based animation
my._mAnim_distance += time_step;
}
else
{
//distance based
my._mAnim_distance += (my._mActor_distance); //add phisical distance to animation value
}

//At the end of a cycle
if (my._mAnim_distance >= base_cycle)
{
if (base_mode != 0)
{
//Animation is Once-Only. Reset state
my._mActor_movestate = STATE_MOVE;
my._mActor_movestate_val = STATEVAL_FORWARD;
my._mAnim_distance = 0;

wait(1);
continue; //go back to the loop //what happens to the upper scene?
}

//warp time or distance
if (my._mAnim_distance > abs(base_cycle)) //why while?
{
my._mAnim_distance -= abs(base_cycle);
}
}

//Warp if negative
if (my._mAnim_distance < 0)
{
my._mAnim_distance += abs(base_cycle);
}


if (base_cycle == 0)
{
beep; //error
base_cycle == 1;
}

//calculate a percentage for the time/dist
base_anim = (100* my._mAnim_distance / abs(base_cycle));

//NOTE: should prevent empty pointer errors here (from the "text" array)
ent_animate(me,mAnim_scene_text.string[base_scene],base_anim,ANM_CYCLE *(base_mode ==0));


//THE BLEND CODE

if ( (base_scene != base_scene_old) && ( blending <= 0) )
{
//set up a blend, only if not blending
my.pose = 2;
//store the old pose in pose 2 (only once)
ent_animate(me,mAnim_scene_text.string[base_scene_old],base_anim_old, ANM_CYCLE);
my.pose = 1;

blending = 99;
}


//Execute the blend each cycle, backwards
if (blending >0)
{
ent_blendpose(me,1,2,blending);
blending -=50*time_step;
}


if (blending <=0)
{
//end blend
base_scene_old = base_scene;
base_anim_old = base_anim;
}


wait(1); //wait (redraw) a frame

} //end while (me)


}




Note that the Blend part of the function is about the 15 last lines.

|Emilio|
Posted By: frazzle

Re: Yet another blending code - 08/23/06 14:06

Nice code contribution eleroux !!
As you said, short and not complictated at all ^^
Btw, could you show a small video which shows us the blending code in action

Cheers

Frazzle
Posted By: eleroux

Re: Yet another blending code - 08/23/06 15:03

Better (hopefully...)

I uploaded a demo. If you don't mind the ugly level, models and animations, try it

Use the usual WSAD keys to walk around.

Z X and C will switch to Crawl, Walk and Run respectivelly.

SHIFT will RUN if current mode is WALK.

Please note that the blendspeed is set to SLOW (about 8 frames) , so I can look for bugs. It's intended to be faster (2 or 4 frames blend) just as any other blending code from the templates.

No jump or attack for now. (coming soon). Sorry that it's packed, but the rest of the code is confidential. The reason for this is that it's ugly and ordinary and I feel shame to show it away.
Posted By: eleroux

Re: Yet another blending code - 08/27/06 19:56

I just updated the demo link (the same above) and added new Jump and Fall animation states. However, I didn't make new animations so I am using Stand for jump and a slow Run for jump.

Also, when the character falls from a certain distance, it LANDS (another animation state). I am using the CRAWL animation scene for now.

I am very happy with the blending code: I can add as many animations as I want without having to worry anymore about specific blends.

The main reason I have to use ent_blendpose instead of ent_blend is that I will need to add Base animations and Upper Body animations, like attacking while running etc. This code allows a blend without worrying about the actual animation state of the character, nor the new scene(s) involved.

Emilio
© 2023 lite-C Forums