Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: A More Realistic Jump  (Read 4842 times)

0 Members and 1 Guest are viewing this topic.

Ionut

  • Jr. Member
  • **
  • Posts: 59
  • Guess Who's Back ?
    • View Profile
    • Email
A More Realistic Jump
« on: June 04, 2017, 02:56:50 pm »
Hello,I have a question,I want to make the player to jump a little bit realistically.I did a jump,but it is a little too simple I mean...the player simply teleports here an then goes down.I don't think you need any code from me,so I wont post the code.
« Last Edit: June 06, 2017, 08:09:16 pm by Boanc »
Guess Who's Back ?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: A More Realistic Jump
« Reply #1 on: June 05, 2017, 11:16:21 am »
You don't want realistic jumps, but you want jumps that "feel right". Realistic game mechanic often make a game not very fun. ;)

Not sure what you expect from us as reply. If you player just "teleports" when moving up, then maybe don't just setPosition them and instead use acceleration, gravity and velocity to define how fast it should go up.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Ionut

  • Jr. Member
  • **
  • Posts: 59
  • Guess Who's Back ?
    • View Profile
    • Email
Re: A More Realistic Jump
« Reply #2 on: June 05, 2017, 12:17:01 pm »
Oh sorry, my expression was wrong(again!),I don't want a "realistic jump",all I want is a jump that is a little bit more than a teleportation,I will try do make the jump with the help with the velocity,acceleration and the gravity.Thanks for the help. ;D
Guess Who's Back ?

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: A More Realistic Jump
« Reply #3 on: June 09, 2017, 12:09:40 am »
I've written jump code long ago that I really like and it's like: each frame when jumping do not apply gravity and apply some predefined up movement from an std::vector (I have it written in a file that is loaded), when something is hit at the top of the bounding box (hitting a ceiling with your head) or the jump values run out (max possible jump height reached) then the character is falling again so gravity applies, etc. It's totally unrealistic but feels good to me and is nicely adjustable. I'm probably explaining it in a messy way but the code itself is one big ball of ifs and messy too. ;D
Back to C++ gamedev with SFML in May 2023

Elias Daler

  • Hero Member
  • *****
  • Posts: 599
    • View Profile
    • Blog
    • Email
Re: A More Realistic Jump
« Reply #4 on: June 09, 2017, 01:45:31 am »
Tomb Painter, Re:creation dev (abandoned, doing other things) | edw.is | @EliasDaler

Ionut

  • Jr. Member
  • **
  • Posts: 59
  • Guess Who's Back ?
    • View Profile
    • Email
Re: A More Realistic Jump
« Reply #5 on: June 11, 2017, 11:37:08 am »
Thanks Elias ;).I made my own "formula" for the jump,but now the BIG problem is that the only last tile is collideble with the player.
THE MINIMAL CODE :
#include <SFML/Graphics.hpp>
using namespace sf;

int main()
{
    //Variables
    bool upkey = false;
    int offsety = 530;
    int velocity = 20;
    int acceleration = 1;
    double gravity = 0.5;
    int DisplayFrame = 0;            //Frames for animations
    float CurrentFrame = 0;
    Clock upkeytimer;               //Clock
    upkeytimer.restart().asSeconds();
    int mapArray[45][5] =           //The tile map.Now I have a little question,how to put it horizontally,because it consumes a lot of lines  
    {
        {1,0,0,0,0},
        {1,0,0,0,0},
        {1,0,0,0,0},
        {1,0,0,0,0},
        {1,0,0,0,0},
        {1,0,0,0,0},
        {1,0,0,0,0},
        {1,0,0,0,0},
        {1,0,0,0,0},
        {1,0,0,0,0},
        {1,0,0,0,0},
        {1,0,0,0,0},
        {1,0,0,0,0},
        {1,0,0,0,0},
        {1,0,0,0,0},
        {1,0,0,0,0},
        {1,0,0,0,0},
        {1,0,0,0,0},
        {1,0,0,0,0},
        {1,0,0,0,0},
        {1,0,0,0,0},
        {1,0,0,0,0},
        {1,0,0,0,0},
        {1,0,0,0,0},
        {1,0,0,0,0},
        {1,0,0,0,0},
        {1,0,0,0,0},
        {1,0,0,0,0},
        {1,0,0,0,0},
        {1,0,0,0,0},
        {1,0,0,0,0},
        {1,0,0,0,0},
        {1,0,0,0,0},
        {1,0,0,0,0},
        {1,0,0,0,0},
        {1,0,0,0,0},
        {1,0,0,0,0},
        {1,0,0,0,0},
        {1,0,0,0,0},
        {1,0,0,0,0},
        {1,0,0,0,0},
        {1,0,0,0,0},
        {1,0,0,0,0},
        {1,0,0,0,0},
        {1,0,0,0,0},
    };
    //In the game loop
    if(Keyboard::isKeyPressed(Keyboard::Up))
    {
       upkey = true;
       upkeytimer.restart().asSeconds();
    }
    else if(!Keyboard::isKeyPressed(Keyboard::Up))
    {
       upkey = false;
    }
    if(upkey == true)
    {
    for(float up = upkeytimer.getElapsedTime().asSeconds();up < 2;up++) //Longer it's pressed longer it jumps
     {
        pos.y -=up;
     }
    }
    if(upkey == false && !SAdventurer.getGlobalBounds().intersects(STileGrass.getGlobalBounds()))  //The Problem
    {
        pos.y += 2;
    }
    if(Keyboard::isKeyPressed(Keyboard::Right))
    {
        SAdventurer.setTextureRect(IntRect(80 * DisplayFrame,111,80,110));
        pos.x += 2.5;
        view.move(2.5,0);
    }
    CurrentFrame += 0.12;
    if(CurrentFrame >= 2) CurrentFrame = 0;       //The animation
    DisplayFrame = (int)CurrentFrame;
    window.clear(Color::White);    //Clearing the window
    SAdventurer.setPosition(pos.x,pos.y);
    window.setView(view);      //Setting the view
    for(int i = 0;i<43;i++)
    {
        for(int j = 0; j<5;j++)
    {
        if(mapArray[i][j] == 1)
        {
            STileGrass.setPosition(i * 70, (j * 70) + offsety);     //The tilemap builder
            window.draw(STileGrass);
        }
    }
    }
    window.draw(SAdventurer);  //Drawing the Player
    window.display();  //Displaying
    }
 
PS:That's the #50th post,so I'm not anymore a Newbie,I'm a Jr.!
« Last Edit: June 11, 2017, 11:41:41 am by Boanc »
Guess Who's Back ?

Ionut

  • Jr. Member
  • **
  • Posts: 59
  • Guess Who's Back ?
    • View Profile
    • Email
Re: A More Realistic Jump
« Reply #6 on: June 11, 2017, 11:53:07 am »
FRex,I've made it so if the player presses up the character will jump.The longer it's pressed the longer it jumps,but not longer than 2 seconds(because I don't want the character to be at the same position with the sun).If the 2 seconds passed the player will start to fall,till it touches the tile.
Guess Who's Back ?