0
\$\begingroup\$

I'm a beginner in java and im suffering to make a 2d game.
I already wrote a simple code that makes the character or the sprite jump by increment his Yposition when key is pressed and do the reverse when released ,but it doesn't satisfy me because i don't want the sprite to fall the same speed he fly with it, i want to make fall slowly ,i mean speed (Y increment) of fllying is separated from the falling one.

this may explain what im saying :
1-fllying https://jsfiddle.net/54xfevoc/embedded/result/
2- falling https://jsfiddle.net/poL9cowz/embedded/result/

thank you.

\$\endgroup\$

2 Answers 2

0
\$\begingroup\$

Well, the fix I would suggest would be adding this to your update method:

if (velocityY <= 0)
{
    gravity = 0.3;        
}
else
{
    gravity = 0.1;
}

Pretty straight-forward in my opinion. I looked at your falling program and it seemed to lack acceleration. Was that on purpose? If you really want to do that, you can change your update method to:

function Update()
{
    if(gravity == 0.0)
    {
        positionY += 0.3
    }
    else
    {
        velocityY += gravity;
        positionY += velocityY;
    }
    if(positionY > 175.0)
    {
        positionY = 175.0;
        velocityY= 0.0;
        onGround = true;
    }
    if (velocityY <= 0)
    {
        gravity = 0.3;        
    }
    else
    {
        gravity = 0.0;
    }

    if(positionX < 10 || positionX > 190)
        velocityX *= -1;
}

but in my opinion that's not very realistic at all, and it's really easy to get stuck in the clouds (way too far above the screen) in my opinion.

I suggest you take a look at some physics documentation for acceleration, it should help you understand more of the velocity-type problems you're having in the future.

Hope this helps!

\$\endgroup\$
1
  • \$\begingroup\$ I'm sorry for the way i wrote the question, the code i have put in the link is not mine i take it from link italic bold code just to explain. but Yes your suggestion helps me. \$\endgroup\$
    – GoForIt
    Mar 14, 2015 at 9:00
1
\$\begingroup\$

When I make a physics part of a game, Personally I don't like to fix or modify basic physics constants. ex) gravity.

So, I suggest you to add delta time concept in your physics.
When the object jumps up, time is passing fast. When the object goes down, the time become slow.

I modified your js fiddle sample source. this is not exactly what I want to say. but it could be a example.

Run Sample
JS Source

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .