{"id":18,"date":"2019-09-25T22:23:15","date_gmt":"2019-09-25T20:23:15","guid":{"rendered":"http:\/\/douglashalse.com\/?p=18"},"modified":"2019-10-13T22:51:04","modified_gmt":"2019-10-13T20:51:04","slug":"spherical-coordinates","status":"publish","type":"post","link":"https:\/\/douglashalse.com\/index.php\/2019\/09\/25\/spherical-coordinates\/","title":{"rendered":"Spherical Coordinates for a Third-person perspective"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">The Idea<\/h2>\n\n\n\n<p>I have always been fond of the Third person perspective in video games and after my course about multivariate analysis, I finally understood how I can pull it off.<\/p>\n\n\n\n<div class=\"wp-block-columns has-2-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p>A video game typically uses Cartesian coordinates (X Y Z) for the game logic but in order to have a rotating camera around the player, I implemented a Coordinate system conversion in order to interface with the coordinates of the game. The Cartesian coordinates are converted to Spherical coordinates with the variables now being:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" width=\"507\" height=\"153\" src=\"https:\/\/douglashalse.com\/wp-content\/uploads\/2019\/10\/img194.gif\" alt=\"\" class=\"wp-image-114\"\/><\/figure>\n\n\n\n<p>One thing to keep in mind is that formula for phi does not return the desired angle, for example, x&lt;0. A modified version of Acrtan called Arctan2 is used that always returns the angle for any given x and y.<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image\"><img decoding=\"async\" width=\"425\" height=\"425\" src=\"https:\/\/douglashalse.com\/wp-content\/uploads\/2019\/10\/player_cam.png\" alt=\"\" class=\"wp-image-113\"\/><figcaption>A Third person perspective for a character<\/figcaption><\/figure>\n<\/div>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">The Code<\/h2>\n\n\n\n<div class=\"wp-block-columns has-2-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p>I decided to use the Game engine Unity to make the camera and I started with creating a blank scene and put a plane to represent the ground and a capsule to represent the player.  <\/p>\n\n\n\n<p>I created a script called cameraController and made sure that I could link the camera and the capsule to the script to get information like its world position. If you want to you can read the code <a href=\"https:\/\/www.codepile.net\/pile\/VnXV6g8E\">here<\/a>.<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image\"><img decoding=\"async\" width=\"669\" height=\"333\" src=\"https:\/\/douglashalse.com\/wp-content\/uploads\/2019\/10\/hqCB4ie.png\" alt=\"\" class=\"wp-image-115\"\/><figcaption>The lonely player in the vast open void.<\/figcaption><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p>I also made a Character controller to move the capsule but it had to move in the expected direction as seen from the camera so with some simple vector mathematics I defined the controller <a href=\"https:\/\/www.codepile.net\/pile\/4gwx5d2W\">here<\/a>. <\/p>\n\n\n\n<p>Notice how I determine the desired direction for the buttons A and D with a cross product of the projected vector from the camera to the capsule onto the xz-plane and a vector normal to that plane. This returns the vector orthogonal to those two vectors and is the direction I want the capsule to travel in.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Demo<\/h2>\n\n\n\n<p>Here is a demo of the code running. I did add some slight movement interpolation to smooth out the animation of both the camera movement and the rotation of the camera to make it more presentable.<\/p>\n\n\n\n<p>I made a little course that I used to test the movement and camera controls. <\/p>\n\n\n\n<figure class=\"wp-block-video\"><video height=\"720\" style=\"aspect-ratio: 1280 \/ 720;\" width=\"1280\" controls src=\"https:\/\/douglashalse.com\/wp-content\/uploads\/2019\/10\/Unity-2019.10.13-15.55.52.02.mp4\"><\/video><\/figure>\n\n\n\n<p>That\u00b4s it for this post. If you have any questions about the code ask them below!<\/p>\n\n\n\n<p>Thanks for reading!<\/p>\n\n\n\n<p class=\"has-small-font-size\">Douglas Halse signing off.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Idea I have always been fond of the Third person perspective in video games and after my course about multivariate analysis, I finally understood how I can pull it off. A video game typically uses Cartesian coordinates (X Y Z) for the game logic but in order to have a rotating camera around the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":118,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,11],"tags":[9,8,10],"class_list":["post-18","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-unity","category-video-games","tag-coding","tag-unity","tag-video-games"],"_links":{"self":[{"href":"https:\/\/douglashalse.com\/index.php\/wp-json\/wp\/v2\/posts\/18","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/douglashalse.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/douglashalse.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/douglashalse.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/douglashalse.com\/index.php\/wp-json\/wp\/v2\/comments?post=18"}],"version-history":[{"count":17,"href":"https:\/\/douglashalse.com\/index.php\/wp-json\/wp\/v2\/posts\/18\/revisions"}],"predecessor-version":[{"id":160,"href":"https:\/\/douglashalse.com\/index.php\/wp-json\/wp\/v2\/posts\/18\/revisions\/160"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/douglashalse.com\/index.php\/wp-json\/wp\/v2\/media\/118"}],"wp:attachment":[{"href":"https:\/\/douglashalse.com\/index.php\/wp-json\/wp\/v2\/media?parent=18"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/douglashalse.com\/index.php\/wp-json\/wp\/v2\/categories?post=18"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/douglashalse.com\/index.php\/wp-json\/wp\/v2\/tags?post=18"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}