Jumping Script

This is a discussion on Jumping Script within the Game Programming forums, part of the General Programming Boards category; I'm pretty crappy with programming games, but here's a little jumping script I made. Don't pay much attention to DrawGLScene(), ...

  1. #1
    People Love Me
    Join Date
    Jan 2003
    Posts
    412

    Jumping Script

    I'm pretty crappy with programming games, but here's a little jumping script I made. Don't pay much attention to DrawGLScene(), because it doesn't really have much to do with the script to make the character jump. glCreateQuad() isn't defined in this code either, but it just creates a simple square.

    Code:
    float xpos=0; // x position
    float MaxJump=1.5; // max height of jump
    float Gravity=1.05; // manipulates max jump
    float SubMaxJump=MaxJump; //constant that restores MaxJump
    float y=0; // Y position
    float yOrigin=y; // Constant that restores y back to it's origin
    bool Jumping=false; //Jumping flag
    bool Falling=false; //Falling flag
    bool JumpOK=true; //Is it okay to jump?
    
    int DrawGLScene(GLvoid)
    {
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    	glLoadIdentity();
    	glTranslatef(xpos,y,-10);
    	glColor3f(0.7f,0.0f,0.0f);
    
    	glCreateQuad();
    	
    	KeyControls();
    
    	return TRUE;
    }
    
    bool move;
    
    void KeyControls()
    {
    if(keys[VK_RIGHT])
    xpos += .05;
    if(keys[VK_LEFT])
    xpos -= .05;
    
    /*if you press UP, you're not jumping or falling, and it's okay to jump, make Jumping=true, and JumpOK=false*/
    
    if(keys[VK_UP] && !Jumping && !Falling && JumpOK)
    {
    Jumping=true;
    JumpOK=false;
    }
    
    /*If yo're jumping, divide MaxJump by Gravity so it will slowly get smaller, and add MaxJump to y so the character moves up*/
    if(Jumping)
    {
    	MaxJump /= Gravity;
    	y += MaxJump;
    }
    
    /*If MaxJump has run out, our character is no longer jumping, but falling*/
    if(MaxJump <= 0)
    {
    	Falling=true;
    	Jumping=false;
    }
    
    /*Opposite of Jumping*/
    if(Falling)
    {
    	MaxJump *= Gravity;
    	y -= MaxJump;
    }
    
    /*Keeps MaxJump from getting higher than 4, so the character doesn't fall at large intervals.*/
    if(Falling && MaxJump > 3)
    MaxJump = 4;
    
    /*If the block is back to it's y origin, make both jumping and falling false, and assign MaxJump to constant SubMaxJump to restore it back to normal. It's not okay to jump again unless you release the UP key, and then press it again.*/
    if(y <= yOrigin)
    {
    	Jumping=false;
    	Falling=false;
    	MaxJump=SubMaxJump;
    	if(keys[VK_UP]==false)
    		JumpOK=true;
    }
    }
    What do you think? It's definitely far from perfect, but it does sorta work. Any tips you have to make more efficient jumping code?

  2. #2
    Banned
    Join Date
    Dec 2003
    Posts
    367
    And we care that you made a jump script?

  3. #3
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    > And we care that you made a jump script?

    Yes, yes we do (I think). He was just asking for suggestions anyway. So, we should. Yeah, it looks good to me. But seeing as I don't know OpenGL I could be wrong. But from what I do know it looks good enough.

    - SirCrono6
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  4. #4
    mov.w #$1337,D0 Jeremy G's Avatar
    Join Date
    Nov 2001
    Posts
    704
    More realisticly you might not be jumping from from 0 y position. Not all games, in fact very little, have flat playing fields.

    Also, the max jump variable is a little redundent. Rather you should use your gravity variable along with a jump velocity to determine max height, it will also make the jump more fluid and believable.

    Heres some vague code you might want to consider to improve your script.

    Code:
    struct sVelocity  {
         float x,y,z;
    }
    typdef enum PLAYER_STATE { PS_NONE, PS_JUMPING, PS_JUMPING2, PS_FALLING, PS_FIRING, PS_ETC } // jumping2 state could be used for double jumping
    sVelocity gravity = { 0.0f, -9.8f, 0.0f }; // 9.8 newtons = gravity on earth (probably will be adjusted for game sake to say 0.98)
    // gravity is always used to keep player on the ground
    sVelocity jumpVelocity = { 0.0f, 27.3f, 0.0f }; 
    
    // in game loop
    player.velocity += gravity; // take current velocity including new movement commands from the keyboard/mouse and add in gravity. this operator wont work, willhave to do it manualy.
    
    // after detecting the user wants to jump (via keyboard press)
    if( player.onGroundSurface() && player.state == PS_NONE ) {
         player.state = PS_JUMPING;
         player.velocity *= jumpVelocity; // figure in jumping momentum;
    }
    
    
    // after all the velocitys have been added together, you are left with the net velocity. This will determine the players next location
    if( !CheckCollision( player.velocity ) ) {
          player.x += velocity.x;
          player.y += velocity.y;
          player.z += velocity.z;
    } else {
          player.x = snapToCollisionSurface(player.velocity).x 
          player.y = snapToCollisionSurface(player.velocity).y;
          player.z = snapToCollisionSurface(player.velocity).z;
    }
    // this set of collision functions uses a collision method of detecting if moving from current position
    // to the next position (figured from sent velocity) if there will be a collision with a surface, if so we get the point of the surface we will colide with, and snap our position right to it.
    Hope this helps a little.
    c++->visualc++->directx->opengl->c++;
    (it should be realized my posts are all in a light hearted manner. And should not be taken offense to.)

  5. #5
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Originally posted by dbgt goten
    More realisticly you might not be jumping from from 0 y position. Not all games, in fact very little, have flat playing fields.

    Also, the max jump variable is a little redundent. Rather you should use your gravity variable along with a jump velocity to determine max height, it will also make the jump more fluid and believable.

    Heres some vague code you might want to consider to improve your script.

    Code:
    struct sVelocity  {
         float x,y,z;
    }
    typdef enum PLAYER_STATE { PS_NONE, PS_JUMPING, PS_JUMPING2, PS_FALLING, PS_FIRING, PS_ETC } // jumping2 state could be used for double jumping
    sVelocity gravity = { 0.0f, -9.8f, 0.0f }; // 9.8 newtons = gravity on earth (probably will be adjusted for game sake to say 0.98)
    // gravity is always used to keep player on the ground
    sVelocity jumpVelocity = { 0.0f, 27.3f, 0.0f }; 
    
    // in game loop
    player.velocity += gravity; // take current velocity including new movement commands from the keyboard/mouse and add in gravity. this operator wont work, willhave to do it manualy.
    
    // after detecting the user wants to jump (via keyboard press)
    if( player.onGroundSurface() && player.state == PS_NONE ) {
         player.state = PS_JUMPING;
         player.velocity *= jumpVelocity; // figure in jumping momentum;
    }
    
    
    // after all the velocitys have been added together, you are left with the net velocity. This will determine the players next location
    if( !CheckCollision( player.velocity ) ) {
          player.x += velocity.x;
          player.y += velocity.y;
          player.z += velocity.z;
    } else {
          player.x = snapToCollisionSurface(player.velocity).x 
          player.y = snapToCollisionSurface(player.velocity).y;
          player.z = snapToCollisionSurface(player.velocity).z;
    }
    // this set of collision functions uses a collision method of detecting if moving from current position
    // to the next position (figured from sent velocity) if there will be a collision with a surface, if so we get the point of the surface we will colide with, and snap our position right to it.
    Hope this helps a little.
    I don't really understand that too well...I'm not very experienced with C++ or OpenGL or game programming in general. I was just wondering if my method for programming jumps was good or not.

  6. #6
    mov.w #$1337,D0 Jeremy G's Avatar
    Join Date
    Nov 2001
    Posts
    704
    heh, none of that was specifically c++ or opengl.

    I think it's pretty much simple C.
    c++->visualc++->directx->opengl->c++;
    (it should be realized my posts are all in a light hearted manner. And should not be taken offense to.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Script in games
    By Shakti in forum Game Programming
    Replies: 7
    Last Post: 09-27-2006, 01:27 AM
  2. In a game Engine...
    By Shamino in forum Game Programming
    Replies: 28
    Last Post: 02-19-2006, 11:30 AM
  3. how to implementate a registration script
    By TJa in forum C++ Programming
    Replies: 0
    Last Post: 10-28-2005, 03:33 AM
  4. Passing arguments to script.....
    By suwie in forum C Programming
    Replies: 5
    Last Post: 09-26-2004, 12:10 AM
  5. Game structure, any thoughts?
    By Vorok in forum Game Programming
    Replies: 2
    Last Post: 06-07-2003, 02:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21