SlideShare a Scribd company logo
libGDX: 
Simple 
Anima0on 
Jussi 
Pohjolainen 
Tampere 
University 
of 
Applied 
Sciences
FRAMERATE 
INDEPENDENCE
Framerate 
Indepence 
• Games 
run 
at 
same 
speed 
no 
ma6er 
the 
framerate 
• In 
slow 
computers; 
30 
fps, 
fast 
computers 
60 
fps 
– No 
need 
to 
go 
over 
60 
fps.. 
• Example 
– Fast 
computer, 
60 
fps, 
move 
object 
1 
px 
at 
a 
=me 
– Slow 
computer, 
30 
fps, 
move 
object 
2 
px 
at 
a 
=me 
– => 
constant 
speed 
no 
maNer 
the 
framerate! 
• The 
key 
to 
framerate 
indepence 
is 
delta-­‐'me 
– Time 
in 
seconds 
since 
the 
last 
0ck 
(last 
render() 
call) 
• 100 
fps 
=> 
1/100 
=> 
0.01 
dt
Moving 
Object 
• At 
30 
fps 
vs 
60 
fps 
this 
object 
will 
move 
at 
different 
speeds 
– int speedX = 1; 
– batch.draw(texture, x += speedX, 0); 
• This 
will 
move 
the 
object 
at 
constant 
speed 
regardless 
of 
fps 
– int speedX = 60; 
– batch.draw(texture, x += speedX * deltaTime, 0); 
• If 
fps 
60, 
deltaTime 
60/1 
= 
0.0166 
secs 
– x += 60 * 0.016666, x += 1 
• If 
fps 
30, 
deltaTime 
30/1 
= 
0.0333 
secs 
– x += 60 * 0.033333, x += 2
libGDX, 
delta 
and 
fps 
• Querying 
FPS 
– Gdx.graphics.getFramesPerSecond() 
• Querying 
Delta 
– Gdx.graphics.getDeltaTime()
Anima0on 
• Use 
Anima0on 
class 
– Animation walkAnimation = new 
Animation(frameDuration, frames); 
• Frame 
dura0on? 
1 
/ 
60 
fps 
• Frames? 
– TextureRegion 
array 
• TextureRegion? 
– Part 
of 
texture
TextureRegion
Split 
.png 
into 
TextureRegions 
walkSheet = new Texture(Gdx.files.internal(”image.png")); 
TextureRegion[][] tmp = TextureRegion.split( 
walkSheet, 
walkSheet.getWidth() / FRAME_COLS, 
walkSheet.getHeight() / FRAME_ROWS );
2D 
array 
-­‐> 
1D 
private TextureRegion[] transformTo1D(TextureRegion[][] tmp) { 
TextureRegion [] walkFrames 
= new TextureRegion[FRAME_COLS * FRAME_ROWS]; 
int index = 0; 
for (int i = 0; i < FRAME_ROWS; i++) { 
for (int j = 0; j < FRAME_COLS; j++) { 
walkFrames[index++] = tmp[i][j]; 
} 
} 
return walkFrames; 
}
Rendering 
public void render() { 
// stateTime was initialized to 0.0f 
stateTime += Gdx.graphics.getDeltaTime(); 
// stateTime is used to calculate the next frame 
// frameDuration! 
currentFrame = walkAnimation.getKeyFrame(stateTime, true); 
spriteBatch.begin(); 
spriteBatch.draw(currentFrame, 150, 150); 
spriteBatch.end(); 
}
TIPS
Extend 
Sprites 
• For 
each 
Sprite 
in 
screen, 
create 
own 
class 
– class 
Monster 
extends 
Sprite 
• Add 
Monster 
aNributes 
like 
– speedX, 
speedY, 
sounds, 
… 
• If 
using 
anima0on 
(previous 
slides) 
you 
could 
create 
animate() 
method 
which 
is 
called 
from 
the 
game 
on 
every 
frame 
• When 
crea0ng 
the 
Sprite, 
remember 
to 
call 
also 
setRegion 
to 
set 
the 
ini0al 
region 
for 
the 
sprite

More Related Content

PDF
Creating Games for Asha - platform
PDF
libGDX: User Input and Frame by Frame Animation
PDF
Implementing a Simple Game using libGDX
PPTX
The Enchant.js Animation Engine in 5 Minutes
PDF
libGDX: User Input
PDF
libGDX: Tiled Maps
PPTX
Unity programming 1
PDF
Breathing the life into the canvas
Creating Games for Asha - platform
libGDX: User Input and Frame by Frame Animation
Implementing a Simple Game using libGDX
The Enchant.js Animation Engine in 5 Minutes
libGDX: User Input
libGDX: Tiled Maps
Unity programming 1
Breathing the life into the canvas

What's hot (18)

PDF
Python book
PPTX
HTML5 Animation in Mobile Web Games
PDF
Unity遊戲程式設計(15) 實作Space shooter遊戲
PDF
The Ring programming language version 1.2 book - Part 36 of 84
PDF
Real life XNA
PDF
Box2D and libGDX
PDF
Developing games for Series 40 full-touch UI
PDF
The Ring programming language version 1.5.3 book - Part 48 of 184
PDF
The Ring programming language version 1.3 book - Part 38 of 88
PDF
Custom SRP and graphics workflows - Unite Copenhagen 2019
PDF
14multithreaded Graphics
PDF
Multimedia Video Systems report
PPT
Scmad Chapter07
PPTX
Chapter ii(coding)
PPTX
Week 10 - Introduction to Animation in 3DS Max
PPTX
Silverlight as a Gaming Platform
PDF
Qt Animation
PPTX
Optimizing Games for Mobiles
Python book
HTML5 Animation in Mobile Web Games
Unity遊戲程式設計(15) 實作Space shooter遊戲
The Ring programming language version 1.2 book - Part 36 of 84
Real life XNA
Box2D and libGDX
Developing games for Series 40 full-touch UI
The Ring programming language version 1.5.3 book - Part 48 of 184
The Ring programming language version 1.3 book - Part 38 of 88
Custom SRP and graphics workflows - Unite Copenhagen 2019
14multithreaded Graphics
Multimedia Video Systems report
Scmad Chapter07
Chapter ii(coding)
Week 10 - Introduction to Animation in 3DS Max
Silverlight as a Gaming Platform
Qt Animation
Optimizing Games for Mobiles
Ad

Similar to libGDX: Simple Frame Animation (20)

PPTX
Witekio custom modern qt quick components
PDF
Cocos2d 소개 - Korea Linux Forum 2014
PPTX
The Technology behind Shadow Warrior, ZTG 2014
PPTX
Computer Animation.pptx
PPTX
Game Development for Nokia Asha Devices with Java ME #2
PPT
CS 354 Texture Mapping
PDF
PlayStation: Cutting Edge Techniques
KEY
Intro to Game Programming
PPT
14709302.ppt
PDF
Simple, fast, and scalable torch7 tutorial
PDF
Introduction to Canvas - Toronto HTML5 User Group
PPTX
Introduction to Canvas - Toronto HTML5 User Group
PDF
Write Python for Speed
PPTX
Benoit fouletier guillaume martin unity day- modern 2 d techniques-gce2014
PDF
Monogame and xna
PPT
MIDP: Game API
PPTX
Intro to Canva
PDF
Processing and Processing.js
PDF
XNA L05–Texturing
PDF
Structure Unstructured Data
Witekio custom modern qt quick components
Cocos2d 소개 - Korea Linux Forum 2014
The Technology behind Shadow Warrior, ZTG 2014
Computer Animation.pptx
Game Development for Nokia Asha Devices with Java ME #2
CS 354 Texture Mapping
PlayStation: Cutting Edge Techniques
Intro to Game Programming
14709302.ppt
Simple, fast, and scalable torch7 tutorial
Introduction to Canvas - Toronto HTML5 User Group
Introduction to Canvas - Toronto HTML5 User Group
Write Python for Speed
Benoit fouletier guillaume martin unity day- modern 2 d techniques-gce2014
Monogame and xna
MIDP: Game API
Intro to Canva
Processing and Processing.js
XNA L05–Texturing
Structure Unstructured Data
Ad

More from Jussi Pohjolainen (20)

PDF
Moved to Speakerdeck
PDF
Java Web Services
PDF
libGDX: Screens, Fonts and Preferences
PDF
Intro to Building Android Games using libGDX
PDF
Advanced JavaScript Development
PDF
Introduction to JavaScript
PDF
Introduction to AngularJS
PDF
libGDX: Scene2D
PDF
libGDX: Simple Frame Animation
PDF
Building Android games using LibGDX
PDF
Android Threading
PDF
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
PDF
Intro to Asha UI
PDF
Intro to Java ME and Asha Platform
PDF
Intro to PhoneGap
PDF
Quick Intro to JQuery and JQuery Mobile
PDF
JavaScript Inheritance
PDF
JS OO and Closures
PDF
Short intro to ECMAScript
Moved to Speakerdeck
Java Web Services
libGDX: Screens, Fonts and Preferences
Intro to Building Android Games using libGDX
Advanced JavaScript Development
Introduction to JavaScript
Introduction to AngularJS
libGDX: Scene2D
libGDX: Simple Frame Animation
Building Android games using LibGDX
Android Threading
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Intro to Asha UI
Intro to Java ME and Asha Platform
Intro to PhoneGap
Quick Intro to JQuery and JQuery Mobile
JavaScript Inheritance
JS OO and Closures
Short intro to ECMAScript

Recently uploaded (20)

PDF
Advanced IT Governance
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
PDF
Sensors and Actuators in IoT Systems using pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
PPTX
Big Data Technologies - Introduction.pptx
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
KodekX | Application Modernization Development
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
AI And Its Effect On The Evolving IT Sector In Australia - Elevate
PDF
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
PDF
GamePlan Trading System Review: Professional Trader's Honest Take
PDF
Electronic commerce courselecture one. Pdf
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Advanced IT Governance
Understanding_Digital_Forensics_Presentation.pptx
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
Sensors and Actuators in IoT Systems using pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Empathic Computing: Creating Shared Understanding
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
Big Data Technologies - Introduction.pptx
20250228 LYD VKU AI Blended-Learning.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
KodekX | Application Modernization Development
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
AI And Its Effect On The Evolving IT Sector In Australia - Elevate
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
GamePlan Trading System Review: Professional Trader's Honest Take
Electronic commerce courselecture one. Pdf
Bridging biosciences and deep learning for revolutionary discoveries: a compr...

libGDX: Simple Frame Animation

  • 1. libGDX: Simple Anima0on Jussi Pohjolainen Tampere University of Applied Sciences
  • 3. Framerate Indepence • Games run at same speed no ma6er the framerate • In slow computers; 30 fps, fast computers 60 fps – No need to go over 60 fps.. • Example – Fast computer, 60 fps, move object 1 px at a =me – Slow computer, 30 fps, move object 2 px at a =me – => constant speed no maNer the framerate! • The key to framerate indepence is delta-­‐'me – Time in seconds since the last 0ck (last render() call) • 100 fps => 1/100 => 0.01 dt
  • 4. Moving Object • At 30 fps vs 60 fps this object will move at different speeds – int speedX = 1; – batch.draw(texture, x += speedX, 0); • This will move the object at constant speed regardless of fps – int speedX = 60; – batch.draw(texture, x += speedX * deltaTime, 0); • If fps 60, deltaTime 60/1 = 0.0166 secs – x += 60 * 0.016666, x += 1 • If fps 30, deltaTime 30/1 = 0.0333 secs – x += 60 * 0.033333, x += 2
  • 5. libGDX, delta and fps • Querying FPS – Gdx.graphics.getFramesPerSecond() • Querying Delta – Gdx.graphics.getDeltaTime()
  • 6. Anima0on • Use Anima0on class – Animation walkAnimation = new Animation(frameDuration, frames); • Frame dura0on? 1 / 60 fps • Frames? – TextureRegion array • TextureRegion? – Part of texture
  • 8. Split .png into TextureRegions walkSheet = new Texture(Gdx.files.internal(”image.png")); TextureRegion[][] tmp = TextureRegion.split( walkSheet, walkSheet.getWidth() / FRAME_COLS, walkSheet.getHeight() / FRAME_ROWS );
  • 9. 2D array -­‐> 1D private TextureRegion[] transformTo1D(TextureRegion[][] tmp) { TextureRegion [] walkFrames = new TextureRegion[FRAME_COLS * FRAME_ROWS]; int index = 0; for (int i = 0; i < FRAME_ROWS; i++) { for (int j = 0; j < FRAME_COLS; j++) { walkFrames[index++] = tmp[i][j]; } } return walkFrames; }
  • 10. Rendering public void render() { // stateTime was initialized to 0.0f stateTime += Gdx.graphics.getDeltaTime(); // stateTime is used to calculate the next frame // frameDuration! currentFrame = walkAnimation.getKeyFrame(stateTime, true); spriteBatch.begin(); spriteBatch.draw(currentFrame, 150, 150); spriteBatch.end(); }
  • 11. TIPS
  • 12. Extend Sprites • For each Sprite in screen, create own class – class Monster extends Sprite • Add Monster aNributes like – speedX, speedY, sounds, … • If using anima0on (previous slides) you could create animate() method which is called from the game on every frame • When crea0ng the Sprite, remember to call also setRegion to set the ini0al region for the sprite