]> _ Git - website-v3/.git/commitdiff
finished the second part of the yap
authorparking_turkeys <roya1060wien@gmail.com>
Wed, 15 Jul 2026 00:08:41 +0000 (20:08 -0400)
committerparking_turkeys <roya1060wien@gmail.com>
Wed, 15 Jul 2026 00:08:41 +0000 (20:08 -0400)
pages/2026-07-14_raylib_platformer.html

index 290daca..af10e47 100644 (file)
                </DETAILS>
                <P>After this, the program sets the variable <CODE>direction</CODE> to 1 for right, -1 for left, or 0 for neither. It does this by looking at what keys are pressed. Then, <CODE>player.velocity.x</CODE> is set to <CODE>SPEED * direction</CODE>, and that's the end of the function. (It returns 1 in case I want to add errors later.)</P>
                <H3><CODE>physics()</CODE></H3>
+               <P>The <CODE>physics()</CODE> function does multiple things, but its main thing is that it runs the <CODE>move_player()</CODE> function in the same file. This is the function that translates the velocity of the player into its new x/y coordinates.</P>
+               <P>First, it figures out how far the player actually has to move. This is done by literally just <CODE>delta*velocity</CODE> for both x/y. Then this is rounded away from 0, so that the player never moves 0 pixels.</P>
+               <P>The way the function actually moves the player while making sure that they don't just go into a platform is simple. The program moves them in smaller steps; if the player has collided into a platform, they then are rewinded one step. After this, the program just moves them one pixel in both x/y until they collide with a wall so that it doesn't end with them 1 pixel above the ground and unable to jump.</P>
+               <P>The amount of steps and how large they are is done so that neither x nor y is less than 1 pixel. This is done with a bunch of if statements.<P>
+               <OL>
+                       <LI>If both the x distance and the y distance are 0, there are... 0 steps and all steps are 0 pixels, because uh. they don't have to be moved at all.</LI>
+                       <LI>If only x is 0, the amount of steps is |y| (since negative steps wouldn't work), and each step for y is <CODE>distance.y/steps_phys</CODE> (steps physics, idk why i think i was worried steps would conflict with something). This is so that if <CODE>distance.y</CODE> is negative, <CODE>stepy</CODE> is still negative.</LI>
+                       <LI>If only y is 0, it's the same thing, but with x instead of y. so <CODE>steps_phys = abs(distance.x); stepx = distance.x/steps_phys;</CODE></LI>
+                       <LI>If neither is 0, but the absolute value of x is greater than that of y, <CODE>steps_phys</CODE> becomes the absolute value of <CODE>distance.y</CODE>. This is so that <CODE>stepy</CODE> isn't less than 1/-1. Then, <CODE>stepy</CODE> becomes <CODE>distance.y/steps_phys</CODE>, so -1 if distance.y is negative, or 1 if it's positive. <CODE>stepx</CODE> becomes <CODE>ceil(distance.x/steps_phys</CODE>, or literally just the amount needed to accomplish the distance within the steps given.</LI>
+                       <LI>If the absolute value of y is greater, it's literally the same as the last one, just swap "x" and "y".</LI>
+               </OL>
+               <P>After figuring out the steps and how large they are, there is a for loop that loops for each step and adds <CODE>stepx</CODE> to <CODE>position.x</CODE> and <CODE>stepy</CODE> to <CODE>position.y</CODE>. After that, it checks if the player is in the ground. If this is true, it subtracts <CODE>stepx</CODE> from <CODE>position.x</CODE> and <CODE>stepy</CODE> from <CODE>position.y</CODE>, and enters another loop.</P>
+               <P>This second loop uses the same variable (<CODE>i</CODE>) as the first one, so it only continues for as many loops as the first one had left. It literally just adds or subtracts 1 (based on whether stepx or stepy was positive or negative) from <CODE>position.x</CODE> and <CODE>position.y</CODE> until the player goes into a wall, where it then moves back 1 in both directions. The idea of this loop is so that the player doesn't end up just floating a couple pixels off of any platform. Then, the function is ended.</P>
+               <P>The rest of <CODE>physics()</CODE> is very simple. All it does is check if the player is off the right side of the screen. If so, it changes to the next level, sets <CODE>frozen</CODE> to 1 in order to allow dialogue to play, and moves the player up to the top left of the screen. <I>Also, it breaks the game somehow. When I added the <CODE>frozen=1</CODE>it started closing the window on reaching it. This doesn't happen when <CODE>main()</CODE> does this, so I have no idea why it happens here.</I> (that's also partially why I'm writing this, I hope i can figure it out...)</P>
+               <H2>Drawing on the screen</H2>