Вы находитесь на странице: 1из 54

the

gamedesigninitiative
at cornell university

Lecture 12

2D Animation
Animation Basics: The FilmStrip

— Animation is a sequence of hand-drawn frames


— Smoothly displays action when change quickly
— Also called flipbook animation

— Arrange animation in a sprite sheet (one texture)


— Software chooses which frame to use at any time
— So programmer is actually the one doing animation
the
2 Scene Graphs gamedesigninitiative
at cornell university
Anatomy of AnimationNode Class
/**
* Sets the active frame as the given index.
*
* @param frame the index to make the active frame
*/
void AnimationNode::setFrame(int frame) {
this->frame = frame;
int x = (frame % cols)*bounds.size.width;
int y = (frame / cols)*bounds.size.height;
bounds.origin.set(x,y);
setPolygon(bounds);
}

the
3 Scene Graphs gamedesigninitiative
at cornell university
Anatomy of AnimationNode Class
/**
* Sets the active frame as the given index.
*
* @param frame the index to make the active frame
*/
void AnimationNode::setFrame(int frame) {
this->frame = frame;
int x = (frame % cols)*bounds.size.width; Actual code has
int y = (frame / cols)*bounds.size.height; some minor
bounds.origin.set(x,y); optimizations
setPolygon(bounds);
}

the
4 Scene Graphs gamedesigninitiative
at cornell university
Adjusting your Speed
— Do not want to go too fast
— 1 animation frame = 16 ms
— Walk cycle = 8/12 frames
— Completed in 133-200 ms

— General solution: cooldowns


— Add an int timer to your object
— Go to next frame when it is 0
— Reset it to > 0 at new frame

— Simple but tedious


— Have to do for each object
— Assumes animation is in a loop
the
5 Scene Graphs gamedesigninitiative
at cornell university
Combining Animations
— Characters to a lot of things
Landing Animation — Run, jump, duck, slide
— Fire weapons, cast spells
— Fidget while player AFK

— Want animations for all


— Is loop appropriate for each?
— How do we transition?

— Idea: shared boundaries


— End of loop = start of another
— Treat like advancing a frame
Idling Animation
the
6 Scene Graphs gamedesigninitiative
at cornell university
Combining Animations
— Characters to a lot of things
Landing Animation — Run, jump, duck, slide
— Fire weapons, cast spells
— Fidget while player AFK

Not a Loop — Want animations for all


— Is loop appropriate for each?
— How do we transition?

— Idea: shared boundaries


— End of loop = start of another
— Treat like advancing a frame
Idling Animation
the
7 Scene Graphs gamedesigninitiative
at cornell university
Combining Animations
— Characters to a lot of things
Landing Animation — Run, jump, duck, slide
— Fire weapons, cast spells
— Fidget while player AFK
Transition
— Want animations for all
— Is loop appropriate for each?
— How do we transition?

— Idea: shared boundaries


— End of loop = start of another
— Treat like advancing a frame
Idling Animation
the
8 Scene Graphs gamedesigninitiative
at cornell university
Animation and State Machines
— Idea: Each sequence a state
— Do sequence while in state
idle
— Transition when at end
— Only loop if loop in graph
— A graph edge means…
— Boundaries match up shoot
— Transition is allowable
— Similar to data driven AI
— Created by the designer walk
— Implemented by programmer
— Modern engines have tools
the
9 Scene Graphs gamedesigninitiative
at cornell university
Animation and State Machines
— Idea: Each sequence a state Continuing
— Do sequence while in state Action
idle
— Transition when at end
— Only loop if loop in graph
— A graph edge means…
— Boundaries match up One time shoot
— Transition is allowable action

— Similar to data driven AI


— Created by the designer walk
— Implemented by programmer
— Modern engines have tools
the
10 Scene Graphs gamedesigninitiative
at cornell university
Complex Example: Jumping

stand

stand2crouch

crouch hop

takeoff float

land
the
11 Scene Graphs gamedesigninitiative
at cornell university
Complex Example: Jumping

stand
Jump Press

stand2crouch
Jump Release

crouch hop
Jump Release
takeoff float
Near Ground
land
the
12 Scene Graphs gamedesigninitiative
at cornell university
Complex Example: Jumping

stand
Transition state
needed to align
the sequences stand2crouch

crouch hop

takeoff float

land
the
13 Scene Graphs gamedesigninitiative
at cornell university
Aside: Sync Kills

the
14 Scene Graphs gamedesigninitiative
at cornell university
The Responsiveness Issue

Tightness of stand
the gameplay Additional delay
preventing jump
stand2crouch

crouch hop

takeoff float

land
the
15 Scene Graphs gamedesigninitiative
at cornell university
Fast Transitions: Crossfade Blending

A B
t = 0.0

— Linear interpolation on colors


rc = tra + (1 t)rb
gc = tga + (1 t)gb Note weights sum to 1.0

bc = tba + (1 t)bb
the
16 Scene Graphs gamedesigninitiative
at cornell university
Fast Transitions: Crossfade Blending

A B
t = 0.3

— Linear interpolation on colors


rc = tra + (1 t)rb
gc = tga + (1 t)gb Note weights sum to 1.0

bc = tba + (1 t)bb
the
17 Scene Graphs gamedesigninitiative
at cornell university
Fast Transitions: Crossfade Blending

A B
t = 0.6

— Linear interpolation on colors


rc = tra + (1 t)rb
gc = tga + (1 t)gb Note weights sum to 1.0

bc = tba + (1 t)bb
the
18 Scene Graphs gamedesigninitiative
at cornell university
Fast Transitions: Crossfade Blending

A B
t = 0.8

— Linear interpolation on colors


rc = tra + (1 t)rb
gc = tga + (1 t)gb Note weights sum to 1.0

bc = tba + (1 t)bb
the
19 Scene Graphs gamedesigninitiative
at cornell university
Fast Transitions: Crossfade Blending

A B
t = 1.0

— Linear interpolation on colors


rc = tra + (1 t)rb
gc = tga + (1 t)gb Note weights sum to 1.0

bc = tba + (1 t)bb
the
20 Scene Graphs gamedesigninitiative
at cornell university
Combining With Animation

A B

Cycle the Cycle the Combine


filmstrip filmstrip with alpha
normally normally blending

the
21 Scene Graphs gamedesigninitiative
at cornell university
Related Concept: Tweening

A B

— Act of linear interpolating between animation frames


— Because we cycle filmstrip slower than framerate
— Implements a form of motion blur

— If animation designed right, makes it smoother


the
22 Scene Graphs gamedesigninitiative
at cornell university
Tweening Works for Transforms Too

A B

— Any transform is represented by a matrix


— Can linearly interpolate matrix components
— Gives a reasonable transform “in-between”
— Aside: This is a motivation for quaternions
— Gives smoother interpolation for rotation
the
23 Scene Graphs gamedesigninitiative
at cornell university
Cocos2D Engine Support
— Recall: decoupled renderer
— Update pass modifies scene
— Render pass to SpriteBatch Update
— Draw pass sends to GPU
— Render pass can animate Scene
— Switches frames if needed Graph
— Tweens in-between frames
— Purpose of action API Render
— Give action with duration
— Can be new frame
— Also could be a transform
the
24 Scene Graphs gamedesigninitiative
at cornell university
Executing Actions: Transforms
Action* action;
action = RotateBy::create(2.0f, 90.0f);
action->retain(); // Need it for later
sprite->runAction(action);

if (action->isDone()) {
action->release();
action = nullptr;
// Do other clean-up
}

the
25 Scene Graphs gamedesigninitiative
at cornell university
Executing Actions: Transforms
Action* action;
action = RotateBy::create(2.0f, 90.0f);
action->retain(); // Need it for later
sprite->runAction(action);
How long
to spend

Tweens
rotation
if (action->isDone()) {
action->release();
action = nullptr;
// Do other clean-up
}

the
26 Scene Graphs gamedesigninitiative
at cornell university
Executing Actions: FilmStrips
Action* action;

Vector<SpriteFrame*> theFrames
auto f1 = SpriteFrame::create(im, rec1)
theFrames->push_back(f1);

auto f8 = SpriteFrame::create(im, rec8)
theFrames->push_back(f8);

Animation* seq = Animation::


createWithSpriteFrames(theFrames,0.1f);
action = Animate::create(seq);
action->retain();

sprite->runAction(action)

// Clean up same as before


the
27 Scene Graphs gamedesigninitiative
at cornell university
Executing Actions: FilmStrips
Action* action;

Vector<SpriteFrame*> theFrames
auto f1 = SpriteFrame::create(im, rec1)
theFrames->push_back(f1);

auto f8 = SpriteFrame::create(im, rec8) Does not
theFrames->push_back(f8);
tween
Animation* seq = Animation::
createWithSpriteFrames(theFrames,0.1f);
action = Animate::create(seq);
action->retain();
Seconds
sprite->runAction(action)
per frame
// Clean up same as before
the
28 Scene Graphs gamedesigninitiative
at cornell university
Executing Actions: FilmStrips
Action* action;

Vector<SpriteFrame*> theFrames
auto f1 = SpriteFrame::create(im, rec1)
theFrames->push_back(f1);

auto f8 = SpriteFrame::create(im, rec8)
theFrames->push_back(f8);

Animation* seq = Animation::


createWithSpriteFrames(theFrames,0.1f);
action = Animate::create(seq);
action->retain();
Reason I made
sprite->runAction(action)
AnimationNode
// Clean up same as before
the
29 Scene Graphs gamedesigninitiative
at cornell university
Easing Function
1
— Basic approach to tweening
— Specify duration to animate
t
— Set t = 0 at beginning
— Normalize t = 1 at end
0
— Interpolate value with t start end
Duration
— How does t change?
— Usually done linearly
— Could be some other way
— Easing: how to change t
— Used for bouncing effects
— Best used for transforms
the
30 Scene Graphs gamedesigninitiative
at cornell university
Easing Function
1
— Basic approach to tweening
— Specify duration to animate
t
— Set t = 0 at beginning
— Normalize t = 1 at end
0
— Interpolate value with t start end
Duration
— How does t change? 1
— Usually done linearly
— Could be some other way t
— Easing: how to change t
— Used for bouncing effects 0
start end
— Best used for transforms Duration
the
31 Scene Graphs gamedesigninitiative
at cornell university
Easing Functions in Cocos2D

the
32 Scene Graphs gamedesigninitiative
at cornell university
Problem With Decoupled Animation
Action* action;
action = RotateBy::create(2.0f, 90.0f); What if we change our
action->retain(); // Need it for later
mind before 2 seconds?
sprite->runAction(action);

the
33 Scene Graphs gamedesigninitiative
at cornell university
Problems With Decoupled Animation
Action* action;
action = RotateBy::create(2.0f, 90.0f); Compatible: Combine
action->retain(); // Need it for later
sprite->runAction(action); Incompatible: Replace

the
34 Scene Graphs gamedesigninitiative
at cornell university
Problems With Decoupled Animation
Action* action;
action = RotateBy::create(2.0f, 90.0f); Compatible: Combine
action->retain(); // Need it for later
sprite->runAction(action); Incompatible: Replace

the
35 Scene Graphs gamedesigninitiative
at cornell university
Problems With Decoupled Animation

Transform Tweening

Physical Animation

Complete Disaster
the
36 Scene Graphs gamedesigninitiative
at cornell university
Recall: Modular Animation
— Break asset into parts
— Natural for joints/bodies
— Animate each separately
— Cuts down on filmstrips
— Most steps are transforms
— Very natural for tweening
— Also better for physics
— Several tools to help you
— Example: Spriter
— Great for visualizing design
the
37 Scene Graphs gamedesigninitiative
at cornell university
Recall: Modular Animation
— Break asset into parts
— Natural for joints/bodies
— Animate each separately
— Cuts down on filmstrips
— Most steps are transforms
— Very natural for tweening
— Also better for physics
— Several tools to help you
— Example: Spriter
— Great for visualizing design
the
38 Scene Graphs gamedesigninitiative
at cornell university
Recall: Modular Animation
— Break asset into parts
— Natural for joints/bodies
— Animate each separately
— Cuts down on filmstrips
— Most steps are transforms
— Very natural for tweening
— Also better for physics
— Several tools to help you
— Example: Spriter, Spine
— Great for visualizing design
the
39 Scene Graphs gamedesigninitiative
at cornell university
Recall: Modular Animation
— Break asset into parts Loose hit
boxes
— Natural for joints/bodies
— Animate each separately
— Cuts down on filmstrips
— Most steps are transforms
— Very natural for tweening
— Also better for physics
— Inside hit box can safely
— Several tools to help you — Transform with duration
— Example: Spriter — Tween animations
— Great for visualizing design — Manage multiple actions
the
40 Scene Graphs gamedesigninitiative
at cornell university
Aside: Skinning

the
41 Scene Graphs gamedesigninitiative
at cornell university
Aside: Skinning

Way to get extra usage


of hand-drawn frames

the
42 Scene Graphs gamedesigninitiative
at cornell university
Spine Demo

the
43 Scene Graphs gamedesigninitiative
at cornell university
Basic Idea: Bones

the
44 Scene Graphs gamedesigninitiative
at cornell university
Basic Idea: Bones

the
45 Scene Graphs gamedesigninitiative
at cornell university
Basic Idea: Bones

Orientation Sprite
(y-axis) attached

Pivot
(origin)

Creates implicit
coordinate space
the
46 Scene Graphs gamedesigninitiative
at cornell university
Bones are Heirarchical

Child

Parent

the
47 Scene Graphs gamedesigninitiative
at cornell university
Bones are Heirarchical

Transforms
apply to
children

the
48 Scene Graphs gamedesigninitiative
at cornell university
Bones are Heirarchical

Transforms
do not affect
the parent

the
49 Scene Graphs gamedesigninitiative
at cornell university
Recall: Scene Graph Hierarchy
Device/
Screen
Bounded Layer Coordinates
box inside

Node Node Node

Node Node Node Node Node Node

Coords relative
to parent box the
50 Scene Graphs gamedesigninitiative
at cornell university
Bones are a Scene Graph Visualization

the
51 Scene Graphs gamedesigninitiative
at cornell university
Manage With Multiple State Machines

legs idle arms idle

arms
legs walk
shoot

the
52 Scene Graphs gamedesigninitiative
at cornell university
Manage With Multiple State Machines

legs idle arms idle

Can be independent
or coordinated
arms
legs walk
shoot

the
53 Scene Graphs gamedesigninitiative
at cornell university
Summary
— Standard 2D animation is flipbook style
— Create a sequence of frames in sprite sheet
— Switch between sequences with state machines

— Tweening supports interpolated transitions


— Helpful for motion blur, state transitions
— Transforms can be combined with easing functions

— Professional 2D animation uses modular sprites


— Scene graphs are a simplified form of model rigging
— State machine coordination can be very advanced
the
54 Scene Graphs gamedesigninitiative
at cornell university

Вам также может понравиться