{"assets":[],"author_link":"author\/katamori\/","author_name":"Katamori","cat":"LD #29","categories":["LD #29"],"comments":[],"epoch":1398218520,"event":"LD29","likes":8,"metadata":{"p_key":"80073","p_author":"Katamori","p_authorkey":"0","p_urlkey":"289083","p_title":"Projectile physics for dummies [WARNING: MATH!]","p_cat":"LD #29","p_event":"LD29","p_time":"1398218520","p_likes":"8","p_comments":"0","p_status":"WAYBACK","us_key":null,"us_name":null,"us_username":null,"event_start":"1398384000","event_key":"22","event_name":"LD29"},"source_url":"2014\/04\/23\/projectile-physics-for-dummies-warning-math\/","text":"<p>I can still clearly remember, how many problem I had with making smooth-moving bodies \u00a0in my games back in the day, but I\u2019ve improved a lot since, so I feel I have to share this knowledge.<\/p>\n<p>The amazing fact that only midschool math needs for this stuff, so you are goddamn lucky, if you didn\u2019t play Fruit Ninja\u00a0or 2048 constantly during math classes. \ud83d\ude1b<\/p>\n<p><em><strong>#1 the basic knowledge<\/strong><\/em><\/p>\n<p>Calculating the motion of any bodies needs to be able to calculate its positions in space. <a href=\"https:\/\/en.wikipedia.org\/wiki\/Coordinate_system\" target=\"_blank\">A coordinate-system<\/a> is perfect for this, and you already get one all the time when you have a proper window on-screen. (my only experiences about it are Java and L\u00d6VE2D, but I\u2019m pretty sure it\u2019s also true for every similar programming languages &amp; environments)<\/p>\n<p>In most cases, the (0,0) point on this is the upper left corner of that window.<\/p>\n<p><em><strong>#2 straight moving<\/strong><\/em><\/p>\n<p>So you have a window, and a body with some (x,y) coordinates. The texture that marks the body is displayed at (x,y) on the screen.<\/p>\n<p>If you wanna move this body left, you have to substract a number (whatever you want) from X. To move right, you have to ADD\u00a0a number \u00a0to X. Doing the same with Y makes the body move up (in case of substraction) or down (in case of addition).<\/p>\n<p>It\u2019s also the initial point of WASD moving in case of player characters: you have to give these commands to certain keys \u2013 giving special tasks for key-pressing actions vary on programming\u00a0\u00a0languages &amp; environments.<\/p>\n<p><em><strong>#3 straight moving, in any angles<\/strong><\/em><\/p>\n<p>Yeah, that\u2019s fine yet, but if you want to move on a way that changes both X and Y, then you have to call the angles!<\/p>\n<p>There\u2019s a cool stuff that helps us called trigonometry. To use its rules, we need a triangle: the first one is (x,y), the second one is a random point that lies on that line your body will also lie while moving; the third one is that makes a right triangle from these points.<\/p>\n<p>Let\u2019s be more precise: make a body that follow the mouse cursor. Then you get (x,y) \u2013 the body \u2013 and (mouseX, mouseY) \u2013 the mouse. Two points CLEARLY defines a line; it\u2019s gonna be the track of your body. To make it move, you have to know the slope of this line.<\/p>\n<p>And THIS is where you need a triangle \u2013 the third point is (mouseX, y), and this with the line I mentioned forms a right triangle. And well, the slope of the line equals to the tangent of the angle at the body!<\/p>\n<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/Trigonometric_functions#Right-angled_triangle_definitions\" target=\"_blank\">But hey, we have formulas to calculate it by division!<\/a> It\u2019s gonna be\u00a0the Y distance between mouse and the body \/\u00a0the\u00a0X\u00a0distance between mouse and body. Okay,\u00a0it\u2019s a tangent value, but in the 21 century, you can easily calculate a tangent value back into degrees.<br\/>\n<strong><br\/>\n<\/strong>After that, moving the body on its track is not that hard \u2013 even understanding is kind of easy, but I suck at English, so I definitely wouldn\u2019t try to explain it:\u00a0add cos(slope) to body\u2019s \u201cx\u201d, and also add sin(slope) to body\u2019s \u201cy\u201d \u2013 I mention again, that the result of the division is NOT the slope itself, only its tangent. Theoretically, you can calculate <em>sin<\/em> and <em>cos<\/em> from\u00a0<em>tan\u00a0<\/em>without exchanging back into degrees, but I haven\u2019t experimented with that.<\/p>\n<p><strong>TLDR; TO MOVE A BODY ALONGSIDE OF A CERTAIN LINE:<\/strong><\/p>\n<p><strong>a) GET THE SLOPE<\/strong><br\/>\n<strong>A number in\u00a0degrees\/radian (I guess it matters only in LOVE2D)\u00a0that has the next tangent: (mouseY \u2013 y) \/( mouseX \u2013 x), assuming that all of them are positive numbers.<\/strong><\/p>\n<p>b) IN ANY STEPS IN WHICH YOU WANT TO MOVE THE BODY AWAY:<\/p>\n<p><strong>add\u00a0the cosine of slope to \u201cx\u201d (body\u2019s x coordinate)<\/strong><br\/>\n<strong>add\u00a0the sine of slope to \u201cy\u201d (body\u2019s x coordinate)<\/strong><\/p>\n<p><strong><em>#4\u00a0wind, gravity, and other vectors<br\/>\n<\/em><\/strong><\/p>\n<p>I quickly realised that even without serious knowledge of linear algebra, you can simply use vectors. They are used in physics to show the amount and the direction of forces. They have 3 properties: an \u201cx\u201d, an \u201cy\u201d, and a \u201clength\u201d. x and y shows, where it pushes stuffs away, while length (which is calculated from x and y) shows, how strong it does that. in fact, I didn\u2019t use it, because direction was more important for me.<\/p>\n<p>Yeah, right, that\u2019s fine, but how to use them? Well, it depends on that what do you want? In case of linear moving, without acceleration, just simply add VectorX to BodyX and also add VectorY to BodyX.<\/p>\n<p>Example use: wind (though in my test I used acceleration for this, just for fun\u2026 :P), where WindVector has an y=0, and some x, that you have to add to or substract from BodyX. You already know the difference.<\/p>\n<p>In case of acceleration, you have to store the increasing\u2026ehm\u2026\u201dspeed\u201d. Well, yes, you actually store the speed.<\/p>\n<p><strong>Example use: gravity, where GravityVector has and x=0, and some y, and in every frame you have to do the next:<\/strong><\/p>\n<p><strong>GravityAmount =\u00a0GravityAmount + GravityVectorY<\/strong><br\/>\n<strong>BodyY = BodyY + Gravity ( + sin(slope) of course)<\/strong><\/p>\n<p>In this case, there are two forces on your body: one that makes it move\u00a0algonside a certain line, and one that does it in a straight down direction. The result is a parabola track and a mathematically (I guess?) precise implementation of vertical or oblique projection.<\/p>\n<p><em><strong>#5 endnote<\/strong><\/em><\/p>\n<p>I may be wrong in many aspects; one thing is sure: this method works amazingly well for me. I even hope that I used the right words; sadly I\u2019m not familiar with \u201cmathematical English\u201d. Also, thanks for reading and have a successfull Ludum-ing!<\/p>\n<p>\u2013 Katamori<\/p>\n<p>Tags: <a href=\"http:\/\/ludumdare.com\/compo\/tag\/tutorial\/\" rel=\"tag\">tutorial<\/a><\/p>","time":"April 23rd, 2014 2:02 am","title":"Projectile physics for dummies [WARNING: MATH!]","title_was_empty":false}