Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By HadesMinion

Thread: [Tutorial] Guide to Jumping and Gravity in OSLib

  
  1. #1
    HadesMinion's Avatar
    HadesMinion is offline -Hacks Smarty
    Join Date
    Oct 2010
    Location
    Wisconsin
    Posts
    176
    Rep Power
    10914

    Default [Tutorial] Guide to Jumping and Gravity in OSLib

    A while back I made a guide on how to code jumping in OSLib. Well, it was stupid and naive of me to think that I knew enough to write that; At that point I hadn't even coded gravity yet. So without further ado, my new guide on jumping and gravity.

    [The following tutorial may contain explicit language. This is because I don't give a fuck about swearing. They are words, thus they are not harmful unless we give them power. Therefor if you don't recognize a swear word as a bad word it's just a normal word.]

    This tutorial will assume you have programmed before, and I will be using C++, it should be easy to convert back to C (If at all necessary, I'm not sure because I haven't wrote the guide yet).

    If you have any questions feel free to comment. Normally I'd say there are no stupid questions, but there are stupid questions, sorry, it's a fact.

    Alright, here's how this will go. I will post a function, then afterwards explain how it works. Remember, you should have a basic (at least) understanding of how to program for the psp. I'm not going over the main function or anything else for that matter, just the functions.

    Part 1: Gravity

    The reason I'm doing gravity first is simple. Gravity can be used to do the work of half of a normal jumping function, and make it easier. Meaning for jumping you will only need to make your player go up, and gravity will take care of the rest. This also prevents things like jumping onto a higher platform and going down past it.

    Code:
    //Define all the variables:
    
    int collision, jumping;
    float gravity = 1; //Floats allow for decimals, as opposed to regular integers.
    //No, it isn't necessary, but it allows for slower increase of gravity without
    //adding even more variables to monitor it.
    
    //Now the function:
    
    void playerGravity()
    {
    	if ((collision == 0) && (jumping == 0))
    	{
    		sprite->y += gravity;
    		gravity += 0.5;
    		if (gravity > 7) 
    			gravity = 7;
    		jumping = false;
    	}
    	else
    	{
    		gravity = 1;
    	}
    }
    This should be an easy one. Regardless, here we go, line by line (pretty much, and by the way I'm skipping variable definitions, you shouldn't need explanations for those):
    NOTE ~ You will need to have something that will set collision to one. If need be, make it so that when your sprite gets to a certain area on the screen collision is set to one, EG: if (sprite->y >= 100) collision = 1;
    This should just be temporary, as you should have things that you will actually collide with later in your game.

    On to the code:
    First, we start off with an if statement checking to make sure you're not colliding with anything and not jumping. Next, you increase the Y variable (PSP's Y-Axis is inverted) of your sprite by the gravity variable. Then, you increase the gravity variable by a user-chosen amount. I chose .5 because it will take twice as long to reach the maximum gravitational pull than if you used a whole number such as 1.
    Then you make sure gravity never goes above another user-chosen amount. I chose 7 because I feel that with the size of my sprites in my game it shouldn't be moving faster than 7 pixels at a time. Feel free to play around with all of the variables. Now you make sure that jumping never activates while you're falling. If you could jump in midair life would be pretty cool, unfortunately that's not how it works; likewise you wouldn't want players to be able to keep pressing jump. This is more or less just a failsafe though, because we'll check for collision in the jumping function. Regardless, it's good to have a backup if for some reason you mess up when writing the jump function.
    Next, there is an else statement. If you don't know what this means or does, leave this tutorial now and go read an easier one you whiney little bitch (What now, huh? WHAT NOW?!).
    [End of psychotic brakedown)
    Okay, so we have an else statement saying that if the above if statement isn't true gravity is equal to 1. Why? Because, you want that little, slightly realistic acceleration and deceleration when you start falling and start jumping. This allows for that so you don't just start out falling at maximum speed.

    If you didn't understand anything, suck my dick (Kidding, unless... nevermind). Seriously though, if you don't understand anything post here and I'll do my best to answer your questions, as long as they provide full information on your problem and are NOT stupid questions.

    On to the jumping section of the tutorial:

    Code:
    //Define all the variables:
    int jumpCounter = 20, jumpDist = 20, jumping, collision;
    float jumpSpeed = 10, speedMax = jumpSpeed;
    
    //Now the function:
    
    void playerJump()
    {
    	if (spriteBG->y >= 100) //Temporary, used to test
    	{  //gravity and jumping without needing objects.
    		collision = 1;
    		spriteBG->y = 100;
    	}
    	else collision = 0;
    	if (jumpCounter < jumpDist) //If the counter is reset.
    	{
    		jumping = 1;
    		sprite->y -= jumpSpeed;
    		jumpSpeed -= .5;
    		jumpCounter ++;
    	}
    	else if (jumpCounter >= jumpDist) //If the counter is
    	{ //At its max.
    		jumping = 0;
    		jumpSpeed = speedMax;
    	}
    	if ((osl_keys->pressed.cross) && (collision == 1))
    	{ //Reset the counter
    		jumpCounter = 0;
    	}
    }
    Before I explain the code, here's what the variables do:
    jumpCounter = 20 -> This should always be equal to jumpDist!
    jumpDist = 20 -> The distance to jump. This isn't pixels to jump, but rather a sort of time-dependent way to limit how high you jump.
    jumping -> Just a true/false variable for the player's jump.
    collision -> Like jumping, but for the player's collision.
    jumpSpeed = 10 -> This ALWAYS has to be AT LEAST half of jumpDist. It's how fast you jump. You'll also go higher because the character is moving more pixels per call of the function.
    speedMax = jumpSpeed -> This holds jumpSpeed's max so you can keep track of it even while jumpSpeed is being altered without entering more numbers (This way all you have to do is change the variable definitions and you don't have to mess with anything else. EG: Say you

    want to jump faster, all you do is set jumpSpeed to a higher value, the rest is taken care of by this variable and the jumping function.

    Alright, so it should be at least somewhat apparent what this does, just like the gravity function; especially because this one is commented (somewhat).
    First of all the top if statement in the function is just in case you don't have anything that sets collision. This way you can test it without programming collision, although it's not that hard.
    Now this order will be confusing. I'm going to explain the functions out of the order you see them at here. They are color coded for this purpose:

    PINK:
    (Don't talk shit about pink)

    This is basically a counter initialization. Think of it as a timer, but instead of starting at the minimum value, it starts at the maximum value and has to wait to be reset to the minimum before the actual timer can start. When X is pressed, it sets the timer - which is stopped at jumpDist - back to the minimum. Because of this the next statement is initialized when the code loops back around.


    LIGHT BLUE:

    After the counter is set to 0 this statement's requirements have been met. Now it sets jumping equal to one, meaning the player is jumping. This isn't really used in this function but is necessary if you plan on ever checking if the player is jumping.
    NOTE ~ The jumping variable is ONLY for when the player is going up, NOT WHEN HE/SHE IS FALLING, I.E. THE DOWNFALL OF THE JUMP.
    Next the Y location of your sprite is increased (Remember, Y is inverted on the PSP) by the user-defined jumpSpeed variable.
    Now jumpSpeed is lowered by .5 for the same reason that gravity was. This gives jumping that nice semi-realistic slow down just like if you were to jump right now, just on a larger, less-possible scale.
    Finally, the jumpCounter is increased. This makes it possible for jumping to end. Now on to the last part of the jumping function.


    GREEN:

    This is what stops and resets the whole jump. First the if statement checks to see if jumpCounter has met or passed jumpDist; the maximum distance you want your character to jump.
    Now jumping is set to 0 and the speed is reset. I told you there'd be a reason for having speedMax This may seem like a very small ending function compared to the rest, but this is why I had you create gravity first. Now gravity takes care of the rest!




    Well, now you're done! I will NOT be releasing a sample for this unless enough people request it. The reason for this is that I have too many projects I should be working on right now and this is meant to be implemented into already-existent code. Also, this is a platformer-physics function, as you should be able to tell.


    Thanks for reading the guide!
    (Feel free to leave comments and ask questions. You can flame if you want, personally I don't give a fuck what you have to say)

    bloodles likes this.

  2. #2
    Zasisem's Avatar
    Zasisem is offline -Hacks Enthusiast
    Join Date
    May 2010
    Posts
    316
    Rep Power
    9

    Default

    I would like to say amazing lol. I'm wondering why not a lot of people took notice of this cause this is very useful in games.. It took me about 2 weeks to actually understand some " gravity/ jumping / falling/ yadayadayada". Off-Topic but How I did my "gravity-based-jumps-yada" was I set different timers, and states (collision states as well if I was dealing with blocks) for my Player when jumping. As for stuff like falling I would continuously keep adding the players "falling speed" until it hits something beneath it that would make the players jump state "ground".
    i.e. :
    Code:
    //0 for ground
    //1 for jumping
    //2 for falling
    
    
    if ( Player.y + Player.width > Block.y && collide == 1 ) {
      jumpstate = 0
    }
    
    //code for ground states..
    if ( jumpstate == 0 ) {
      jumpspeed = 0 //the variable that is added to the Player.Y speed, but in this case the Player.Y would remain where ever it landed
    }
    You see where I'm going.. but yes. Thanks for the tut as well, hope others find it very useful

  3. #3
    HadesMinion's Avatar
    HadesMinion is offline -Hacks Smarty
    Join Date
    Oct 2010
    Location
    Wisconsin
    Posts
    176
    Rep Power
    10914

    Default

    Haha, thanks. It took me a while at first too, I guess it's not one of those things you can just jot down and "get." I get how you did yours, I can definitely see potential for more useful output, as with mine there is virtually no output and no falling variable (Maybe I should add one, because right now it's basically if collision is 0 or if jumping is 1). Thanks for responding

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •