LebGeeks

A community for technology geeks in Lebanon.

You are not logged in.

#1 July 29 2014

Johnaudi
Member

Jump using basic physics

Hello,

I'm trying to code a jump for my indie, but I'm having issues.

At first, I made a code that made the jump would be triangular. (meaning, jump -> reached max jump -> go down at same rate, it was visually like a triangle which I didn't like)

I want it to go as smooth as an hyperbole or demi-circle.

So I've changed the system used and implemented the following (C#):

public float updateJump(int j_state, float v_y)
        {

            float n = Math.Abs((float)Math.Sin(frametime * (Math.PI / 180d)) * JUMP_SPEED);

            switch (j_state)
            {
                case 0: // Not Jumping
                    frametime = 0;
                    break;
                    
                case 1: // Jumping Up
                    v_y -= n;
                    v_jump += n;
                    break;

                case 2: // Going Down
                    v_y += n;
                    v_jump -= n;
                    break;
            }

            if (v_jump < 0)
            {
                isjumping = false;
                v_jump = 0;
            }

            frametime++;
            
            return v_y;
        }

The thing is that it's doing it instantaneously or not even doing it, not sure if it's because funcs calls are too fast or some code error.

Here's the full code: http://pastebin.com/7wZHdSQh

Thanks in advance.

Offline

#2 July 31 2014

jsaade
Member

Re: Jump using basic physics

what you are describing here is adding acceleration, velocity and gravity to simulate a jump.

The basic game implementing this is mario, a nice article describing it: http://hypertextbook.com/facts/2007/mariogravity.shtml

As for actual implementation, there are tons of articles (you can check gamedev.net or stackoverflow.com) but a simple answer:
http://stackoverflow.com/a/15184318/557996


It seems that you are developing a 2D Platformer, a nice and informative article about movement:
http://www.gamasutra.com/blogs/YoannPig … elings.php

Last edited by jsaade (July 31 2014)

Offline

#3 July 31 2014

Freddy98
Member

Re: Jump using basic physics

Hi , I will give you an idea of my implementation in my own 3d engine that is working , but I'm not sure it is 100% correct , because I am not coming home before 2 weeks to check it . It is a function in a player class which handles gravity(when falling) and jumping :

 void setGravity(double currentTime , float groundHeight)
 {
      float acceleration = currentTime - t0;//t0 is a member of class player
      acceleration/= framerate;
      If(groundHeight>=position.y)
       {
            position.y = groundHeight ;
            t0 = currentTime ; 
         }
      else
        position.y -= acceleration ;

      If (groundHeight>=position.y && jump==true)
          jump=false; //jump member of player
      
      If(isKeyPressed(Space))
        jump = true ;
      
      If(jump==true)
        position.y += (maximumJumpValue/framerate ); 
} 

Make sure that the minimum acceleration is smaller than your maximum jump value (you can divide it by some number) , and the maximum jump value a little bit bigger than the max tolerated height difference when going upwards on a hill or mountain (or use more conditional statements). This code might contain some bugs , I have no computer at the moment to test it . Feel free to correct me and ask questions .

Last edited by Freddy98 (July 31 2014)

Offline

Board footer