Game Programming - Episode 34 - Setting Offsets
Skills:
Delivery Management60%
Key Takeaways
The Cherno's Game Programming series episode 34 covers setting offsets in game development using Java, focusing on calculating and applying offsets for tile positions and map scrolling. This episode provides hands-on experience with game programming concepts, including offset calculation and map movement, using specific methods like setOffset and variables like X offset and Y offset.
Full Transcript
hey what up guys my name is a CH welcome to Episode 34 of game programming so last time we took a look at this awesome void tile that we created and basically now we're actually able to go on and draw or render these tiles now we've handled every kind of exception right we've got the thing saying that if it's if it's zero right if if our random generator level here uh generates a zero it's going generate a random number from 0 to three uh inclusive both uh if it does actually generate uh a zero it's going to place a tile. grass at that coordinate which is great uh a grass tile rather I'm talking on code here um it'll place a grass tile at that coordinate which is great if if it if it's not zero it's obviously not going to execute this code and it's just going to give us a black tile right in other words it's not going to crash the program everything's going to work perfectly now we've handled all that so now we can actually get on to rendering um and the way that we do that is um um I'm sort of going to split that into two parts as well because first of all we actually need to handle offsets now I talked a little about offsets a few episodes ago uh in in the screen class here where I um you know I I said that um that this XP uh the absolute position is going to be y+ this this offset type deal um and today we're actually going to talk about how we apply offsets all right so this is this is the tile position right but the thing is if if we actually take a look at this render TI method in the screen class right now what it tells us is that the Y absolute the the absolute position of the map on the map of the tile right the absolute position on the map of the tile that we're rendering is the actual pixel that is rendering right that gives us a number from 0 to 15 um hang on here yep okay just um yeah so it gives us a number of Sor I was just clearing my throat there um it gives us a number between 0 and 15 right depending on which pixel is up to rendering so don't worry about this first one right that's just that's yeah but the second one gives us the actual y position or you know in this case X position y position it g it gives us the actual y position or the X position of the tile on the map right but think about it this way what about when the player actually moves right so think about it this way when when you actually move the player the map obviously has to scroll the map has to move and if you actually look at these two variables that we Fred they don't actually take in account take they don't actually take into account when the player moves and that's a bit of a problem because yeah we need to basically tell this thing that when the player moves so for example if I open up our game here so I can actually demonstrate it if we move this way we need to be like all right the player has offset set the map I don't know 500 pixels y all right plus y for example um and we actually need to calculate that so that we can then update the locations of the tiles to be um in a different location so to do that we actually need to make a offset um a set offset um method so over here in our screen class um just below the renaile method we're going to Simply do we're just simply going to make a method but first we're actually going to make two variables so up here um right over here we're just going to type public in X offset and then y offset and we're not actually going to give them any values right now we could but that's we don't really need to um because we're going to set them anyway here but yeah public int X offset and Y offset and that's it I'll actually just clean this up a bit um just to make it look nicer but yeah yeah public in X offset and Y offset and those are going to actually store the offset value so if I go into here I'm actually going to make a method a public void uh method called set offset and it's going to accept two parameters X offset and Y offset both integers offset and again what we're simply going to do here is type thisx offset equals x offset and this y offset equals y offset all right so what is this doing um when we actually run this method when when when we call this method we input two parameters two integers X offset and Y offset and then what we do is we actually set these two parameters that are accessible in this screen class equal to the two parameters that we input when we call this method okay so now in other words we've got these variables in this class we can now move on to you know applying them into our into our render tile method to actually calculate what the offset is um and that's kind of simple as well all we have to do here because remember this is the position of the tile we we don't want to mess around with this because that is the pixel that is up to rendering because so we we definitely don't want to touch the pixels otherwise we'll end up you know skewing our our Sprites which we don't want to do um what we do want to do though is adjust this variable right this y position variable and the reason we we sort of want to adjust that is because um you know that is that is the location of the tile on the map so we want to be like all right if the players moved 200 pixels to the right we want to add 200 pixels to this x position now before I actually do that by you know all I'm want to do here is simply add you know offset um the offset to the x x offset to the X position and the Y offset to the Y position before I actually do that help you guys understand why I want do what I want to do now if I open the game you'll see that if I go right the map is moving right okay now that's not cool it should be moving the other way that should be moving right because remember our player going to be right over here when he moves right or in other words when he you know walks right um the map sort of needs to go left if that makes sense otherwise you know cuz cuz in other words what we're sort of doing here is we're not really moving the player we're moving the map all right we're moving his location on the map so if we hit the right Ju Just Right now if I just hit the right AR here on this game you'll see that it looks like we're walking left it looks like we're just moving the map right that's incorrect we should reverse it and to do that all I'm going to do here is instead of going ahead and saying that XP is going to equal XP plus this x offset I'm just going to do the opposite and go like um XP is going to minus equal the X offset um and again that this line of code is the same thing as saying this all right XP minus equal x offset is the same as saying XP = XP minus X offset it's just a shorter way of saying it all right and that is it I'm going to do the same thing for y and that that's it that's as simple as it is all right all I'm doing here is I'm saying that we're adjusting the location the X location of this of the tile um by the offset and same thing with the Y as well we're displacing the Y position of the tile um by the offset whatever the offset may be now that's great we've adjusted this and now we have an awesome awesome system for actually figuring out um the location of the map and the location of the player we've now properly offset all the tiles brilliant now one thing we do we we do need to do is call this method somewhere because this is the method method that actually sets our offset so if I go back to my level class here over here uh where we're rendering all I'm going to do here is simply at the very top here I'm just going to go ahead and say screen and again we can access screen cuz there's a parameter here screen do set offset and then I'm going to set the offset to X scroll and Y scroll and that is it all right all I'm doing here is I'm setting the offset equal to X scroll and Y scroll which of course is the actual location of the player and that's it that is just how simple it is to actually set offsets um and yeah so I hope you enjoyed this episode of game programming if you did please hit the like button and give the video a favorite next time we'll take a look at actually moving on to getting the required tiles here and rendering them onto the screen we'll see you guys next time goodbye a [Music]
Original Description
Support this series and get rewards! ► http://www.patreon.com/thecherno
Welcome to Game Programming, a series in which we take an in depth look at how to make a game from scratch, in Java.
----------------------------------------------------------------------------------------------------------------
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
3D Game Programming - Episode 1 - Window
The Cherno
3D Game Programming - Episode 2 - Game Loop
The Cherno
3D Game Programming - Episode 3 - Arrays
The Cherno
3D Game Programming - Episode 4 - Drawing Pixels!
The Cherno
3D Game Programming - Episode 4.5 - How Rendering Works
The Cherno
3D Game Programming - Episode 5 - Playing with Pixels!
The Cherno
3D Game Programming - Episode 6 - Performance Boosting
The Cherno
3D Game Programming - Episode 7 - FPS Counter
The Cherno
3D Game Programming - Episode 8 - Alpha Support and More
The Cherno
3D Game Programming - Episode 9 - Beginning 3D
The Cherno
3D Game Programming - Episode 10 - Floors and Animation
The Cherno
3D Game Programming - Episode 11 - Rotation
The Cherno
3D Game Programming - Episode 12 - User Input
The Cherno
3D Game Programming - Episode 13 - Render Distance Limiter!
The Cherno
3D Game Programming - Episode 14 - Basic Mouse Movement
The Cherno
3D Game Programming - Episode 15 - Textures + More!
The Cherno
3D Game Programming - Episode 16 - Walking, Crouching, Sprinting + More
The Cherno
3D Game Programming - Episode 16.5 - Exporting Runnable Jars
The Cherno
3D Game Programming - Episode 17 - Small Adjustments + Birthday!
The Cherno
3D Game Programming - Episode 17.5 - Creating an Applet
The Cherno
3D Game Programming - Episode 18 - The Beginning of Walls
The Cherno
3D Game Programming - Episode 18.1 - A Few More Things
The Cherno
Episode 18.5 - Creating an EXE File in Java
The Cherno
3D Game Programming - Episode 19 - Rendering Walls
The Cherno
3D Game Programming - Episode 20 - Continuing Walls, Fixing Bugs, and Managing Crashes
The Cherno
3D Game Programming - Episode 21 - Texturing Walls, Fixing Clipping, and Fixing the Mouse
The Cherno
3D Game Programming - Episode 22 - Random Level Generator + Properly Fixing Clipping
The Cherno
3D Game Programming - Episode 23 - Graphical User Interface (GUI) Launcher
The Cherno
3D Game Programming - Episode 24 - Making Our Launcher Work
The Cherno
3D Game Programming - Episode 25 - Writing and Reading Files
The Cherno
3D Game Programming - Episode 26 - Custom Resolutions
The Cherno
3D Game Programming - Episode 27 - Decorating the Launcher
The Cherno
3D Game Programming - Episode 28 - Continuing our Custom Launcher!
The Cherno
3D Game Programming - Episode 29 - Launching The Game
The Cherno
3D Game Programming - Episode 30 - Colour Processing In-Depth
The Cherno
3D Game Programming - Episode 31 - Sprites!
The Cherno
3D Game Programming - Episode 32 - Sprite Mapping
The Cherno
3D Game Programming - Episode 33 - High Resolution Rendering
The Cherno
3D Game Programming - Episode 34 - Entities
The Cherno
Genesis - My Game for Ludum Dare 24
The Cherno
Vlog + Ludum Dare Results
The Cherno
Game Programming - Episode 1 - Resolution
The Cherno
Game Programming - Episode 2 - Threads
The Cherno
Game Programming - Episode 3 - Game Loop
The Cherno
Game Programming - Episode 4 - Window
The Cherno
Episode 5 - Buffer Strategy
The Cherno
Game Programming - Episode 6 - Graphics Initialized
The Cherno
Game Programming - Episode 7 - Buffered Image and Rasters
The Cherno
Game Programming - Episode 8 - The Screen Class
The Cherno
Game Programming - Episode 9 - Rendering Pixels
The Cherno
Game Programming - Episode 10 - Clearing the Screen
The Cherno
Game Programming - Episode 11 - "Out of Bounds, Baby!"
The Cherno
Game Programming - Episode 12 - Negative Bounds
The Cherno
Game Programming - Episode 13 - Timer
The Cherno
Game Programming - Episode 14 - FPS Counter
The Cherno
Episode 15 - Tiles
The Cherno
Game Programming - Episode 16 - The Map
The Cherno
The Walls 2 - Minecraft PvP Survival Map
The Cherno
Game Programming - Episode 17 - Key Input
The Cherno
Game Programming - Episode 18 - Controlling The Map
The Cherno
More on: Delivery Management
View skill →Related Reads
📰
📰
📰
📰
I Built 3 SaaS Products in 6 Months Using AI — Only 1 Got Users. Here's What the Other 2 Taught Me About Distribution vs. Features
Dev.to · Jack
The One Job, One Income and One Platform Method Wil Be The Downfall Of Millions In 2027
Medium · SEO
The Future of Leadership: Human, Culture, and Authenticity in the Digital Era
Medium · AI
Why Branding Is the Key to Business Success
Medium · Startup
🎓
Tutor Explanation
DeepCamp AI