NaughtScape[06] - Let's Get Physical

in #programming7 years ago

On the Docket for Today:

<p dir="auto">My plan for this post was pretty simple: <ul> <li>Generate 3D world from tile map. <li>Spawn in the player <li>Get player "moving" <p dir="auto">To start I needed a class to house all of the code dealing directly with the 3D world, so I created a new C# class called <strong>PhysicalWorld. In the <strong>PhysicalWorld I placed a reference to the tile map, an object to act as parent to the tile objects, a reference to a player game object, and a <strong>TerrainTile. <p dir="auto">The first thing the <strong>PhysicalWorld needed to be able to do was read the tile map I so lovingly created. <h3>Reading the Map <p dir="auto">The method I decided to go with for reading the TileMap was a simple, and quite a familiar one. Once again I am looping through the grid using that same modulo fueled loop that I've used in the <em>FillMap function explained in my <a href="https://steemit.com/programming/@maple-thunder/naughtscape-05-a-small-function-that-will-save-a-lot-of-time" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">last post. Where this loop differs is the offset used for creating the newX and newZ variables, and what it does with each tile it come across. <p dir="auto"><img src="https://images.hive.blog/768x0/https://steemitimages.com/DQmWBtjFSoBzfgGqpAR9PzSvsbXgL3S82Gq7x6YN1kFKsk3/gnerate_world_01.PNG" alt="gnerate_world_01.PNG" srcset="https://images.hive.blog/768x0/https://steemitimages.com/DQmWBtjFSoBzfgGqpAR9PzSvsbXgL3S82Gq7x6YN1kFKsk3/gnerate_world_01.PNG 1x, https://images.hive.blog/1536x0/https://steemitimages.com/DQmWBtjFSoBzfgGqpAR9PzSvsbXgL3S82Gq7x6YN1kFKsk3/gnerate_world_01.PNG 2x" /> <blockquote> <p dir="auto">My first draft of the GenerateWorld function. <p dir="auto">The offset in the other looping functions uses half the width of a tile so that each tile will be placed exactly a tile width apart. In <em>GenerateWorld I change the offset so that instead of being based on the width of a 2D tile texture, it would be based on half the width of the 3D model. This would place the models next to each other instead of overlapping. <p dir="auto">For each tile that is read, the loop grabs the <strong>TerrainTile class attached to the tile and stores it in the <strong>PhysicalWorlds _<em>currTerrainTile. Now that I have the <strong>TerrainTile at a tile location I can see which texture models are associated with it. <h3>Laying the Sod <p dir="auto">Once the _<em>currTerrainTile was set I called a new function <em>BuildGround and passed in the tile's location. <p dir="auto"><img src="https://images.hive.blog/768x0/https://steemitimages.com/DQmYMWwcvZ7jioQzzSLzkei8C7oTesQERkP4uw2CueGYxWP/BuildGround.PNG" alt="BuildGround.PNG" srcset="https://images.hive.blog/768x0/https://steemitimages.com/DQmYMWwcvZ7jioQzzSLzkei8C7oTesQERkP4uw2CueGYxWP/BuildGround.PNG 1x, https://images.hive.blog/1536x0/https://steemitimages.com/DQmYMWwcvZ7jioQzzSLzkei8C7oTesQERkP4uw2CueGYxWP/BuildGround.PNG 2x" /> <blockquote> <p dir="auto">Builds a model from the _currTerrainTile at a given location. <p dir="auto"><em>BuildGround takes in a location and uses that location to create the 3D model that is in _<em>currTerrainTile. Now a <em>TerrainTile has an array to hold models, which means that I need to specify which of the models I want to use. I do have 2 models for the grass, one solid green tile and one with a couple of rocks. I originally had the function grab a random prefab using a <em>Random.Range with the length of the models array as the upper boundary. This ended up too cluttered so for while I was still developing the function I hard coded it to only use the first model in the array. <h3>Time to Test ! <p dir="auto">After getting the ground models spawned in and the tile map disabled I could see a nice grassy field 32x32. But something looked a little ..off. The plane seemed roughly the same size as the tile map, which didn't make sense because the models were bigger than the 2D tiles. I found the problem after selecting 2 different tiles, they were overlapping ! <p dir="auto"><img src="https://images.hive.blog/768x0/https://steemitimages.com/DQmaC2jhReWktYYbie3gM94Q72q1CGrgGAagsayntsx5qc3/overlapping_tiles.PNG" alt="overlapping_tiles.PNG" srcset="https://images.hive.blog/768x0/https://steemitimages.com/DQmaC2jhReWktYYbie3gM94Q72q1CGrgGAagsayntsx5qc3/overlapping_tiles.PNG 1x, https://images.hive.blog/1536x0/https://steemitimages.com/DQmaC2jhReWktYYbie3gM94Q72q1CGrgGAagsayntsx5qc3/overlapping_tiles.PNG 2x" /> <blockquote> <p dir="auto">Because of incorrect positioning, the models ended up instantiating overlapped. <p dir="auto">This issue had me stumped for a little while. Because of the way I have my <code>model>model_object>prefab structure for each model it got pretty confusing. It's probably for the best that I ended up doing this, even if it took me more time than I had planned on because it made me really examine how I was accessing the models and textures. I noticed an error with how the <strong>PhysicalWorld accessed the model's renderer and in fixing that noticed the real problem. <p dir="auto">Earlier when I had changed the looping code to use the size of the offset, I forgot to also change the code from determining the size of the tile to determining the size of the model. <p dir="auto"><img src="https://images.hive.blog/768x0/https://steemitimages.com/DQmPDMTPexiajkV559BQJpYcde3mGqYQv4DHKcM4bqimF97/change_tile_and_offset.PNG" alt="change_tile_and_offset.PNG" srcset="https://images.hive.blog/768x0/https://steemitimages.com/DQmPDMTPexiajkV559BQJpYcde3mGqYQv4DHKcM4bqimF97/change_tile_and_offset.PNG 1x, https://images.hive.blog/1536x0/https://steemitimages.com/DQmPDMTPexiajkV559BQJpYcde3mGqYQv4DHKcM4bqimF97/change_tile_and_offset.PNG 2x" /> <blockquote> <p dir="auto">Updated reference vectors <em>tile and <em>offset. <h2>Moving on down the list <p dir="auto">The Plan: <ul> <li><del>Generate 3D world from tile map. <li>Spawn in the player <li>Get player "moving" <h2>The Hero <p dir="auto">To get the player created in the world I created another function inside the <strong>PhysicalWorld called <em>SpawnPlayer. This new function will handle creating the player data object, and a player game object that will be the visuals. <p dir="auto"><img src="https://images.hive.blog/768x0/https://steemitimages.com/DQmf4H2U9UvgJA9rUVCLRxsScBDVeKmhZUgDuDH5LN6HVzM/spawn_player.PNG" alt="spawn_player.PNG" srcset="https://images.hive.blog/768x0/https://steemitimages.com/DQmf4H2U9UvgJA9rUVCLRxsScBDVeKmhZUgDuDH5LN6HVzM/spawn_player.PNG 1x, https://images.hive.blog/1536x0/https://steemitimages.com/DQmf4H2U9UvgJA9rUVCLRxsScBDVeKmhZUgDuDH5LN6HVzM/spawn_player.PNG 2x" /> <blockquote> <p dir="auto">The <em>SpawnPlayer function. <p dir="auto">I created an empty game object (<em>playerGO), named it "Player_Character", and set it's parent to be the <strong>PhysicalWorld. Then I created a capsule to act as a placeholder for the character model. I set the capsule's parent to be the <em>playerGO and offset it by the size of the tile model divided by 2. I also offset the capsule by 1.0f in the y axis to place it on top of the tiles instead of on the same level. <h3>Hero Info <p dir="auto">Now that I have the <em>playerGO and a temporary model set up I need to add the controller to it, then create a new <strong>Player data class to hold all of the player information. I initialize the <strong>Player class with the enum <code>Player.SKILL_LOADOUT.NEW_PLAYER. This is an enum I made so I could more easily create players with specific skill load outs. <p dir="auto"><img src="https://images.hive.blog/768x0/https://steemitimages.com/DQmUhjGxDkbZsfBj3aw4XhQnYF3hHRXV8jK1Pu7eE1KJThK/player_constructors.PNG" alt="player_constructors.PNG" srcset="https://images.hive.blog/768x0/https://steemitimages.com/DQmUhjGxDkbZsfBj3aw4XhQnYF3hHRXV8jK1Pu7eE1KJThK/player_constructors.PNG 1x, https://images.hive.blog/1536x0/https://steemitimages.com/DQmUhjGxDkbZsfBj3aw4XhQnYF3hHRXV8jK1Pu7eE1KJThK/player_constructors.PNG 2x" /> <blockquote> <p dir="auto">I am currently only worrying about the combat stats. <p dir="auto">With the player data populated, I could add it to the <strong>PlayerController by calling the <em>InitPlayer function and passing along the player data object. The final step in spawning in the player character was to <em>strap the CameraPivot to the player. <p dir="auto">To do this I wrote a small function in the <strong>CameraStrap class to set the transform the camera pivots on to a passed in transform. I then called this function at the end of <em>SpawnPlayer and passed along the <em>playerGO's transform. <p dir="auto"><img src="https://images.hive.blog/768x0/https://steemitimages.com/DQmSCqo3F6vYr2tV7s7uxaJt9cqZdiyS6BcVCV4GbVT1aMQ/attach_camera.PNG" alt="attach_camera.PNG" srcset="https://images.hive.blog/768x0/https://steemitimages.com/DQmSCqo3F6vYr2tV7s7uxaJt9cqZdiyS6BcVCV4GbVT1aMQ/attach_camera.PNG 1x, https://images.hive.blog/1536x0/https://steemitimages.com/DQmSCqo3F6vYr2tV7s7uxaJt9cqZdiyS6BcVCV4GbVT1aMQ/attach_camera.PNG 2x" /> <h2>One Last Point Left <p dir="auto">Updated Plan: <ul> <li><del>Generate 3D world from tile map. <li><del>Spawn in the player <li>Get player "moving" <h3>Moving Around <p dir="auto">I already had a "player" spawned and "moving" in my <a href="https://steemit.com/programming/@maple-thunder/naughtscape-01-first-came-the-land-to-walk-on" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">original post but now I needed to adapt that to my new world generation. I started by adding the <strong>MouseController as a component on the <em>playerGO, and initialize it, passing through a reference to the <strong>PhysicalWorld and the <em>playerGO's <strong>PlayerController. <p dir="auto"><img src="https://images.hive.blog/768x0/https://steemitimages.com/DQmVdisQ7YXyjL1TuZU6ZQo4zycRiD9PN2oBjTmmhjBAe8e/create_and_init_mouse_controller.PNG" alt="create_and_init_mouse_controller.PNG" srcset="https://images.hive.blog/768x0/https://steemitimages.com/DQmVdisQ7YXyjL1TuZU6ZQo4zycRiD9PN2oBjTmmhjBAe8e/create_and_init_mouse_controller.PNG 1x, https://images.hive.blog/1536x0/https://steemitimages.com/DQmVdisQ7YXyjL1TuZU6ZQo4zycRiD9PN2oBjTmmhjBAe8e/create_and_init_mouse_controller.PNG 2x" /> <blockquote> <p dir="auto">Adding the MouseController and initializing it with the PlayerController and PhysicalWorld. <p dir="auto">The <strong>MouseController will one day handle logic around input regarding the mouse. I set it up only deal with left clicks for now, just to get the character moving. I chose to put player movement on the left click because that's how it's done in Old School RuneScape, so that's how it's done in NaughtScape ! <p dir="auto"><img src="https://images.hive.blog/768x0/https://steemitimages.com/DQmYHxNMRfoL7bWR2R1GbmZB3Nyg747MhNMZANi3pZCPav2/CheckUnderMouse.PNG" alt="CheckUnderMouse.PNG" srcset="https://images.hive.blog/768x0/https://steemitimages.com/DQmYHxNMRfoL7bWR2R1GbmZB3Nyg747MhNMZANi3pZCPav2/CheckUnderMouse.PNG 1x, https://images.hive.blog/1536x0/https://steemitimages.com/DQmYHxNMRfoL7bWR2R1GbmZB3Nyg747MhNMZANi3pZCPav2/CheckUnderMouse.PNG 2x" /> <blockquote> <p dir="auto">A Ray is cast out, returns the collider that it hits which is set to the PlayerControllers destination. <p dir="auto">In the <strong>PlayerController I have a <em>MoveToDestination function that gets called in the <em>Update function if the <em>destination is different from the <em>currentTileCoord. <em>MoveToDestination calls a <code>Vector3.Lerp() on the <em>currentTileCoord, <em>destination, and use the <code>Time.deltaTimeas the time parameter. <p dir="auto">It's alright movement for now, but in the future I'll be replacing it with an A* pathfinding algorithm. <p dir="auto"><img src="https://images.hive.blog/0x0/https://media.giphy.com/media/xT1R9F2VPe8mWg8l6U/giphy.gif" alt="player_movement.gif" /> <h3>Until Next Time ! <p dir="auto">If you have any suggestions for improvements, questions, or links to resources that would help me out feel free to leave a comment. I am using <a href="https://unity3d.com/" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Unity as a game engine and <a href="https://www.blender.org/" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Blender for any 3D modeling I may have to do during this project. Upvote if you liked the post or found it helpful, and follow me for future updates on <strong>NaughtScape, sometimes other programming, and occasionally other stuff. All of the source code can be found on my <a href="https://github.com/MapleThunder/NaughtScape" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Github. <p dir="auto">You could also add me in game on OSRS if you play !<br /> My ign is <strong>MapleThunder.
Sort:  

click here.Congratulations @maple-thunder, this post is the forth most rewarded post (based on pending payouts) in the last 12 hours written by a User account holder (accounts that hold between 0.1 and 1.0 Mega Vests). The total number of posts by User account holders during this period was 2546 and the total pending payments to posts in this category was $2246.80. To see the full list of highest paid posts across all accounts categories,

If you do not wish to receive these messages in future, please reply stop to this comment.

Woo! You got a curie vote! Way to go!