Ep. 113: Efficient Sprite Rotation - Game Programming

The Cherno · Intermediate ·11y ago

Key Takeaways

The video demonstrates efficient sprite rotation in game programming by moving the rotation logic into the Sprite class and reducing the number of times the rotation method is called, utilizing tools such as the Sprite class and Pixels array. It also showcases rotating a sprite based on the current sprite or the original sprite at a custom angle, using Math and Pi for calculations.

Full Transcript

hey what's up guys my name is Cho welcome to episode 113 of game programming so last time yesterday actually we um we did the Sprite rotation thing where we first of all covered the algorithm which is right over here okay including these two methods over here and uh and we figured out a way to make these arrows actually rotate as you can see the Sprite is rotating towards the direction uh in which it is going now the problem that we faced was performance basically so you can see the frame rate dropping over here all the way down to like 60 and if we check if we take a look at this let me quickly make the console Just a Touch bigger here so that you guys can see it where are we debug console font let's put that up to like 18 okay there we go you can see that it's running at around a million times per second okay now that's a lot so what we want to do is not necessarily do that right we want to be able to say that you know what Sprites don't have to be rotated every time yeah once it's got its direction it's done the same Sprite will be used okay it'll continue going until the Sprite gets destroyed until the entity which is the projectile gets destroyed and that's the end of that okay so there's not there's not going to be a need to rotate it again now I did read the comments from yesterday's video okay from from episode 112 you guys did say what if it like tracks it or what if we still wanted to rotate that's fine right you guys know how to implement that yourselves I'm not going to bother with that um until we unless we you know get up to a point in the game where it's necessary but the idea is that if that's the case still it does not need to get rotated this many times per second okay you can make it track and this new system will also work with that as well so don't worry okay anyway so let's take this rotate method and think about how we can reduce the amount of time it's going to be called so first off um let's get rid of this uh rotate Plus+ thing and in game as well uh cuz we don't need to track the amount of times it actually does get run anymore that was you know old school stuff that was just a test um what we are going to do though is is actually move this stuff into the Sprite class okay so right now I did it in screen just to get it up just to get it up and running right just to make sure it works and to get it working with the I guess the least amount of abstraction right I didn't want to really put it into multiple classes and split it up because that way it would be harder for you guys to actually uh dissect it and learn about how it works so um that's why I put it here but we're actually going to take it away and put it into um into something else okay so we're going to put into the Sprite CL okay why because the things we're rotating are Sprites okay it's going to be actually really simple we're going to just grab this private in rotate okay along with um this double rotation you know X and rotation y methods okay we're going to cut that contr X on Windows in case you guys don't know and then in Sprite okay right under let's just say the Constructor we've got the split method uh we're going to just paste it in here okay that should give us no errors um and then what I'm going to do is I'm actually going to make I'm not going to make a Constructor okay I could but I'm not going to um what I will do is just make a static uh method that returns Sprite I'll call it rotate okay um and let's see so it'll take in first of all an existing Sprite yeah so the Sprite that we want to rotate and it'll also take in uh the angle at which we want to rotate it to all right and then all we're going to do yeah all we're going to do is return yeah a new Sprite that is consistent of uh rotate the rotate method right the Sprite that we're going to be rotating of course um is or not the Sprite we're going to be rotating but this um pixels array that we're actually going to be rotating will be um uh let's see It'll be sprite. pixels okay the width and height will be Sprite do uh width sprite. height we can use all these private m private Fields because obviously we are in the Sprite class in which these fields declared um and also uh the angle will just be the angle which is up here okay so we're grabbing that and uh then we need the width and height so sprite. width and this is for the Constructor of sprite by the way we're out of the rotate method obviously and I'll go through this in just a second Sprite dowith and sprite. high and that should be it okay so rotate let's see uh so we got we got pixels we got width we got height and we got angle so that should be fine uh okay we need to make it static that's right okay and obviously we need to make these two static as well which shouldn't be a problem because obviously they're not going to use any Fields anyway so there we go okay everything's nice and done now um so let's go through this quickly so we've got a Sprite Constructor up here which takes in a pixel array okay width and height and creates a Sprite out of these pixels because what a Sprite really is fundamentally is an array of pixels and a bunch of metadata just to you know describe that array but essentially it's an array of pixels okay um now uh that's the way that works okay um so what we can do is we can say you know what let's take in our rotation pixels because rotate does return an array of pixels okay we've got our width we've got our height we've got our angle and then we obviously need Sprite width and height to complete this Constructor okay and that's it okay so that's all we're doing and we're copying these pixels across as you can see okay even though that wasn't really that necessary cuz this does create a new array doesn't matter it'll work okay that's that done so what can we do now well first of all I'm going to get rid of this render projectile method which takes an angle okay we'll just get rid of that completely we don't need that anymore that was just for desting purposes in Wizard projectile I'll get rid of angle and all I have to do now is say that um this uh Sprite instead of being equal to sprite. projectile Arrow it'll be equal to sprite. rotate okay so the Sprite will rotate is the original projectile arrow and the angle will just be the angle all right and that's that okay so that's what we're setting Sprite to so now if I were to run this okay and we we take a look look at that okay it works exactly the same way as it did before except you can see the frame rate over here does not drop to 60 it drops to about 2500 okay and you can see they're constantly firing okay so that's pretty fast much faster than it was before so there we go that's essentially the end of the episode um if you guys have any questions or whatever let me know this method um what I did mention before actually was the fact that what if you want it to rotate multiple times or for some reason you just want it to um follow you want it to track something and thus rotate based on a trajectory or there's all there's all these different ideas that you guys might have you can still do that all right all you have to do okay all you have to do is somewhere in the update method just go spr Sprite do rotate the current Sprite Which can either be Sprite if you want to rotate it based on the current Sprite okay which will be the already rotated one or you can actually go back to the original Sprite Which is projectile arrow and rotate that and then of course you can rotate it I don't know why you do it based on the fire rate but you can rotate it on at a custom angle okay it doesn't really matter okay so for example yeah to demonstrate this what you could do is say something like this if we wanted it to be rotated we could have a private int time we'll set that equal to zero we'll go time Plus+ yeah we can say that if time so maybe like once oh maybe not once maybe um I don't know let's just say like 10 times per second so we'll say time 6al Z right that that that'll occur um uh 6 times per second 10 times per second sorry um so what we'll do here is we'll say uh probably just we'll just set Sprite equal to sprite. rotate so Sprite our original Sprite and maybe the angle will be something like math. Pi divid by I don't know something small let's say like divid by 20 all right all right there you go so if we do something like that and we take a look at this now you can see the arrow is being rotated okay it looks kind of like a boomerang but we're still running pretty fast okay we're still running quite fast if I go down here and it's being rotated all throughout here you can see this performance is still very very good okay in fact it's even higher than it was before for some reason but the point is it's around 2500 frames it's fine 2700 even okay and it's being rotated that seems like a bit too much we can make it uh happen a lot quicker or more often that might look better you can see that looks pretty weird actually kind of cool though okay so you can play around with that and you can see the performance is fine all right so don't be afraid of that you can still dynamically rotate it it's just that there's no need to do it that often okay um so yeah that's going to wrap up the episode if you guys have any questions just leave a comment below I'll try and reply to as many comments as I can and um be sure to like the video as well if you enjoyed it that always just helps me make sure that I'm making good videos and um yeah I'll see you guys next time I don't know what we'll do next time so if you guys that's another thing I don't know what what we'll do next time so if you guys want to drop any suggestions Down Below in the comments that would be much appreciated and I'll see you guys next time goodbye he [Music]

Original Description

Patreon ► http://www.patreon.com/thecherno Twitter ► http://www.twitter.com/thecherno Website ► http://www.thecherno.com Facebook ► http://www.facebook.com/thecherno ------------------------------------------------------------------------------------------------- In this video: - ------------------------------------------------------------------------------------------------- Website: http://www.thecherno.com Twitter: http://www.twitter.com/thecherno Facebook: http://www.facebook.com/thecherno Subreddit: http://www.reddit.com/r/thecherno/ Steam Group: http://www.steamcommunity.com/groups/thecherno ------------------------------------------------------------------------------------------------- Outro music is by Approaching Nirvana: http://www.youtube.com/approachingnirvana
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from The Cherno · The Cherno · 0 of 60

← Previous Next →
1 3D Game Programming - Episode 1 - Window
3D Game Programming - Episode 1 - Window
The Cherno
2 3D Game Programming - Episode 2 - Game Loop
3D Game Programming - Episode 2 - Game Loop
The Cherno
3 3D Game Programming - Episode 3 - Arrays
3D Game Programming - Episode 3 - Arrays
The Cherno
4 3D Game Programming - Episode 4 - Drawing Pixels!
3D Game Programming - Episode 4 - Drawing Pixels!
The Cherno
5 3D Game Programming - Episode 4.5 - How Rendering Works
3D Game Programming - Episode 4.5 - How Rendering Works
The Cherno
6 3D Game Programming - Episode 5 - Playing with Pixels!
3D Game Programming - Episode 5 - Playing with Pixels!
The Cherno
7 3D Game Programming - Episode 6 - Performance Boosting
3D Game Programming - Episode 6 - Performance Boosting
The Cherno
8 3D Game Programming - Episode 7 - FPS Counter
3D Game Programming - Episode 7 - FPS Counter
The Cherno
9 3D Game Programming - Episode 8 - Alpha Support and More
3D Game Programming - Episode 8 - Alpha Support and More
The Cherno
10 3D Game Programming - Episode 9 - Beginning 3D
3D Game Programming - Episode 9 - Beginning 3D
The Cherno
11 3D Game Programming - Episode 10 - Floors and Animation
3D Game Programming - Episode 10 - Floors and Animation
The Cherno
12 3D Game Programming - Episode 11 - Rotation
3D Game Programming - Episode 11 - Rotation
The Cherno
13 3D Game Programming - Episode 12 - User Input
3D Game Programming - Episode 12 - User Input
The Cherno
14 3D Game Programming - Episode 13 - Render Distance Limiter!
3D Game Programming - Episode 13 - Render Distance Limiter!
The Cherno
15 3D Game Programming - Episode 14 - Basic Mouse Movement
3D Game Programming - Episode 14 - Basic Mouse Movement
The Cherno
16 3D Game Programming - Episode 15 - Textures + More!
3D Game Programming - Episode 15 - Textures + More!
The Cherno
17 3D Game Programming - Episode 16 - Walking, Crouching, Sprinting + More
3D Game Programming - Episode 16 - Walking, Crouching, Sprinting + More
The Cherno
18 3D Game Programming - Episode 16.5 - Exporting Runnable Jars
3D Game Programming - Episode 16.5 - Exporting Runnable Jars
The Cherno
19 3D Game Programming - Episode 17 - Small Adjustments + Birthday!
3D Game Programming - Episode 17 - Small Adjustments + Birthday!
The Cherno
20 3D Game Programming - Episode 17.5 - Creating an Applet
3D Game Programming - Episode 17.5 - Creating an Applet
The Cherno
21 3D Game Programming - Episode 18 - The Beginning of Walls
3D Game Programming - Episode 18 - The Beginning of Walls
The Cherno
22 3D Game Programming - Episode 18.1 - A Few More Things
3D Game Programming - Episode 18.1 - A Few More Things
The Cherno
23 Episode 18.5 - Creating an EXE File in Java
Episode 18.5 - Creating an EXE File in Java
The Cherno
24 3D Game Programming - Episode 19 - Rendering Walls
3D Game Programming - Episode 19 - Rendering Walls
The Cherno
25 3D Game Programming - Episode 20 - Continuing Walls, Fixing Bugs, and Managing Crashes
3D Game Programming - Episode 20 - Continuing Walls, Fixing Bugs, and Managing Crashes
The Cherno
26 3D Game Programming - Episode 21 - Texturing Walls, Fixing Clipping, and Fixing the Mouse
3D Game Programming - Episode 21 - Texturing Walls, Fixing Clipping, and Fixing the Mouse
The Cherno
27 3D Game Programming - Episode 22 - Random Level Generator + Properly Fixing Clipping
3D Game Programming - Episode 22 - Random Level Generator + Properly Fixing Clipping
The Cherno
28 3D Game Programming - Episode 23 - Graphical User Interface (GUI) Launcher
3D Game Programming - Episode 23 - Graphical User Interface (GUI) Launcher
The Cherno
29 3D Game Programming - Episode 24 - Making Our Launcher Work
3D Game Programming - Episode 24 - Making Our Launcher Work
The Cherno
30 3D Game Programming - Episode 25 - Writing and Reading Files
3D Game Programming - Episode 25 - Writing and Reading Files
The Cherno
31 3D Game Programming - Episode 26 - Custom Resolutions
3D Game Programming - Episode 26 - Custom Resolutions
The Cherno
32 3D Game Programming - Episode 27 - Decorating the Launcher
3D Game Programming - Episode 27 - Decorating the Launcher
The Cherno
33 3D Game Programming - Episode 28 - Continuing our Custom Launcher!
3D Game Programming - Episode 28 - Continuing our Custom Launcher!
The Cherno
34 3D Game Programming - Episode 29 - Launching The Game
3D Game Programming - Episode 29 - Launching The Game
The Cherno
35 3D Game Programming - Episode 30 - Colour Processing In-Depth
3D Game Programming - Episode 30 - Colour Processing In-Depth
The Cherno
36 3D Game Programming - Episode 31 - Sprites!
3D Game Programming - Episode 31 - Sprites!
The Cherno
37 3D Game Programming - Episode 32 - Sprite Mapping
3D Game Programming - Episode 32 - Sprite Mapping
The Cherno
38 3D Game Programming - Episode 33 - High Resolution Rendering
3D Game Programming - Episode 33 - High Resolution Rendering
The Cherno
39 3D Game Programming - Episode 34 - Entities
3D Game Programming - Episode 34 - Entities
The Cherno
40 Genesis - My Game for Ludum Dare 24
Genesis - My Game for Ludum Dare 24
The Cherno
41 Vlog + Ludum Dare Results
Vlog + Ludum Dare Results
The Cherno
42 Game Programming - Episode 1 - Resolution
Game Programming - Episode 1 - Resolution
The Cherno
43 Game Programming - Episode 2 - Threads
Game Programming - Episode 2 - Threads
The Cherno
44 Game Programming - Episode 3 - Game Loop
Game Programming - Episode 3 - Game Loop
The Cherno
45 Game Programming - Episode 4 - Window
Game Programming - Episode 4 - Window
The Cherno
46 Episode 5 - Buffer Strategy
Episode 5 - Buffer Strategy
The Cherno
47 Game Programming - Episode 6 - Graphics Initialized
Game Programming - Episode 6 - Graphics Initialized
The Cherno
48 Game Programming - Episode 7 - Buffered Image and Rasters
Game Programming - Episode 7 - Buffered Image and Rasters
The Cherno
49 Game Programming - Episode 8 - The Screen Class
Game Programming - Episode 8 - The Screen Class
The Cherno
50 Game Programming - Episode 9 - Rendering Pixels
Game Programming - Episode 9 - Rendering Pixels
The Cherno
51 Game Programming - Episode 10 - Clearing the Screen
Game Programming - Episode 10 - Clearing the Screen
The Cherno
52 Game Programming - Episode 11 - "Out of Bounds, Baby!"
Game Programming - Episode 11 - "Out of Bounds, Baby!"
The Cherno
53 Game Programming - Episode 12 - Negative Bounds
Game Programming - Episode 12 - Negative Bounds
The Cherno
54 Game Programming - Episode 13 - Timer
Game Programming - Episode 13 - Timer
The Cherno
55 Game Programming - Episode 14 - FPS Counter
Game Programming - Episode 14 - FPS Counter
The Cherno
56 Episode 15 - Tiles
Episode 15 - Tiles
The Cherno
57 Game Programming - Episode 16 - The Map
Game Programming - Episode 16 - The Map
The Cherno
58 The Walls 2 - Minecraft PvP Survival Map
The Walls 2 - Minecraft PvP Survival Map
The Cherno
59 Game Programming - Episode 17 - Key Input
Game Programming - Episode 17 - Key Input
The Cherno
60 Game Programming - Episode 18 - Controlling The Map
Game Programming - Episode 18 - Controlling The Map
The Cherno

This video teaches efficient sprite rotation in game programming by moving the rotation logic into the Sprite class and reducing the number of times the rotation method is called. It also showcases rotating a sprite based on the current sprite or the original sprite at a custom angle. By following the steps outlined in the video, viewers can implement efficient sprite rotation in their own game programming projects.

Key Takeaways
  1. Move the rotation logic into the Sprite class
  2. Create a static method in the Sprite class to rotate a Sprite by a given angle
  3. Create a new Sprite with the rotated pixels
  4. Copy the pixels from the original Sprite and rotate them based on the angle
  5. Remove the render projectile method and replace it with a call to the rotate method
  6. Rotate the Sprite based on the angle and the pixels
  7. Set a private int time equal to zero
  8. Increment time by 1 in a loop
  9. Rotate the sprite based on the current sprite or the original sprite
  10. Set the angle to Pi divided by 20
💡 By moving the rotation logic into the Sprite class and reducing the number of times the rotation method is called, game programmers can optimize their sprite rotation code and improve performance.
Up next
George Hotz | Programming | tinygrad, starting on CLOUD=1 | Part 2
george hotz archive
Watch →