www.jpct.net

jPCT - a 3d engine for Java => Support => Topic started by: raft on May 30, 2007, 05:28:01 am

Title: Animater: a class which can coordinate animation during unit movement
Post by: raft on May 30, 2007, 05:28:01 am
hello,

as Todd mentioned about unfulfilled promises to post code i felt guilty. here is the Animater class i use in karga to animate avatar movements. it coordinates animations deciding when to switch animation and during completion either rollback the animation or go till end. you cannot directly compile it but it will give an idea

say your avatar is walking forwards and you animate accordingly. then movement stopped but you're in the middle of walk animation, what will you do now ? leave the avatar as it is (one foot top) or play animation till end, or play it backwards ? what will you do when avatar decided to strafe left/right while he was walking forward ? how will you smootly switch between walk forward and strafe animation ? Animater class takes care of these. it does the low level dirty job..

the typical usage is as follows: this code piece is called every frame. seconds is passed seconds since last frame

Code: [Select]
// clamping mode is important here, as we dont want roundings at ends
Avatar.getAnimationSequence().setClampingMode(Animation.USE_CLAMPING);
if (moving) {
    Animater.Frame frame = animater.getNext(step, seconds);
    animate(frame.index, getSequenceNumber(frame.sequence));
} else {
    if (animater.hasNext()) {
        Animater.Frame frame = animater.getNext(seconds);
        animate(frame.index, getSequenceNumber(frame.sequence));
    }
}
Util3D.Step is an enum defining what step avatar makes during movement, one of:

Code: [Select]
NONE, FORWARD, BACKWARD,  JUMP_UP, JUMP_FALL, FALL_DOWN,
LEFT, RIGHT, // strafe
TURN_LEFT, TURN_RIGHT, // turn around
FAST_FORWARD; // run

AvatarInfo.Animation stores information about animation sequences of avatar. the only relevant part used here is period, how long the sequence lasts. the enum Interval is the segments of animation sequences. as forum settings doesnt allow to post such a long piece of code i placed it in karga site  ::) here is the Animater class (http://www.aptalkarga.com/download/Animater.java)

hope this helps  ;)
r a f t

Title: Re: Animater: a class which can coordinate animation during unit movement
Post by: ToddMcF2002 on May 30, 2007, 07:19:08 pm
Nice code raft! First off - its so much cleaner than my meat and potatoes coding style with its just keep on going exception handling  ;D.

I never even considered reversing a sequence.  Nice concept!

Thanks for contributing too of course.  The more the merrier!

Title: Re: Animater: a class which can coordinate animation during unit movement
Post by: raft on May 30, 2007, 07:29:12 pm
thanks and you're welcome ;)
the code took that shape after several tries and trial and errors. mine was a piece of spagetti too at the beginning  ::)
Title: Re: Animater: a class which can coordinate animation during unit movement
Post by: cyberkilla on June 04, 2007, 02:45:33 pm
I can't remember what I was doing to mimic this :D I should really check.