How can I make my character jump?

Hello,

I'm trying to make my character jump when espace key is pressed.
However when I press espace key nothing happin.
When I run the program It doesn't show error. Can you help me please?



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  class CharacterMotionListerner : public VEEventListener {

// variables 
	float CharacterGravity = 14.0f;
	float CharacterVelosity = 10.0f;
	float CharacterJump = 22.0f;
	float time = 0.0f;
	bool CharacteronGround = true;

	

       public:
	CharacterMotionListerner() : VEEventListener() {};
	bool onKeboard(veEvent event) {

	// Get Entity 7 and when I press 
	if (event.idata1 == GLFW_KEY_ESCAPE) {
	VEEntity*e7 = getSceneManagerPointer()->getEntity("dog");
	time += event.dt;

	if (CharacterMotionListerner:: CharacteronGround) {
					
	CharacterVelosity = -CharacterGravity * event.dt;
	if (GLFW_KEY_ESCAPE)
		{
		CharacterVelosity = CharacterJump;
		return true;
		}
				
	         }

		}
		return false;
		};


	};


int main() {

// declare the new EventListener!
new CharacterMotionListerner();
mve.registerEventListener("CharacterMotionListerner", listener);
}
> bool onKeboard(veEvent event)
Just a guess, but if you're overriding base class functions, you need to be able to spell them properly.

onKeyboard perhaps?

But then again, you posted here as well, and there the spelling was better.
https://stackoverflow.com/questions/54393668/how-can-i-make-my-chacacter-perform-jumping-with-c-vulkan

In common with both posts, there is insufficient information to help you.
http://sscce.org/

Let me take a quick guess.
I'm trying to make my character jump when espace key is pressed.

I don't know if by "espace" you mean space or escape, To me space would be the logical key to chose over escape for jumping in a game.

If it is space you meant to use, if (event.idata1 == GLFW_KEY_ESCAPE) you're checking for a key event for the key escape

If it is escape you meant to use, then I guess we'd need to see more code to be able to help you.
Hello,

Thank you both for your reply.
I read sscce.org and in the future when I ask for help I will comply accordindly.

I made the changes you mentioned spelled correcly onKeyboard

also I updated if function to be

f (event.idata1 == GLFW_KEY_SPACE && event.idata3 == GLFW_PRESS

if (GLFW_KEY_SPACE == GLFW_RELEASE)

when I release and press SPACE it doesn't do anything.
Can you guys help me?

Thanks in advance
Start with this in a new project.
1
2
3
4
5
int main() {
    // declare the new EventListener!
    CharacterMotionListerner  mve();
    mve.registerEventListener("CharacterMotionListerner", listener);
}

Now add the MINIMUM number of lines possible to make that program compile such that it produces a single text messages like "Space pressed" or "Space released".

The very act of trying to produce the minimal example might help you discover for yourself where you're going wrong. Even if you don't find out yourself, what you produce will satisfy the SSCCE requirement that would allow us to simply copy/paste into an IDE and try it for ourselves.
Hello,

I try to do what to told me. I'm not getting any error but It output on the message.
I remonved any object from my sceneI only left one cube otherwise doen't load.

Below is the what I wrote

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class CharacterMotionListerner : public VEEventListener {
	public:
		CharacterMotionListerner() : VEEventListener() {};
		bool onKeyboard(veEvent event) {
			if (event.idata1 == GLFW_KEY_SPACE && event.idata3 == GLFW_PRESS) {

				if (GLFW_KEY_SPACE == GLFW_RELEASE)
				{
					std::cout << "button pressed";
					return true;
				}
			}

		} 
		
	};

I know nothing about GLFW but according to their ressource (https://www.glfw.org/docs/latest/input_guide.html)

if (event.idata1 == GLFW_KEY_SPACE && event.idata3 == GLFW_PRESS)
Do check that event.indata1 is an int key and that event.indata3 is an int action




1
2
3
4
5
if (GLFW_KEY_SPACE == GLFW_RELEASE)
				{
					std::cout << "button pressed";
					return true;
				}


int GLFW_KEY_SPACE is equal to 32 and GLFW_RELEASE is equal to 0, so you will never satisfy the conditions for that if statement.

I think what you're trying to do is something like this:
1
2
3
4
if (key == GLFW_KEY_SPACE && action == GLFW_RELEASE)
{
    //your logic
}
Last edited on
Hello,

Thank you for your help.
I haven't found a solution yet but I keep trying.
Hello,

So I tried again to output a text on the screen and this time was ok.
The main problem was that I had register two event listerner.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
lass CharacterMotionListerner : public VEEventListener {
	public:
		CharacterMotionListerner() : VEEventListener() {};

		bool onKeyboard(veEvent event) {
		
			if (event.idata1 == GLFW_KEY_SPACE) {
				if (GLFW_KEY_SPACE) {

					std::cout << "button pressed";

				}

			}
			else
			return false;
		
		}

	}; 


However I still have problem with the jumpinh
The program that I want to create is when I hit space the character jump and when I relise the character goes buck to the ground. Can You give me a hint how should I proceed??

From your first post it looks like you already started to implement some of the physics for jumping. Jumping follows a parabolic arc. The x-direction is a constant speed (could be 0), and the y-direction has an initial positive speed, but a constant negative acceleration due to gravity.

You need to keep track of the position and velocity of your character each timestep, and updating the position based on velocity, and updating the velocity based on the constant negative acceleration due to gravity.

I would suggest trying to program this logic in its own program before you try to integrate it with a game.

https://www.physicsclassroom.com/class/1DKin/Lesson-6/Kinematic-Equations

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>

struct Vector2 {
    double x;
    double y;
    
    Vector2()
    : x(0), y(0)
    { }
    
    Vector2(double x, double y)
    : x(x), y(y)
    { }
};

Vector2 operator+(const Vector2& a, const Vector2& b)
{
    return Vector2(a.x + b.x, a.y + b.y);
}

Vector2 operator*(const double k, const Vector2& v)
{
    return Vector2(k * v.x, k * v.y);
}

std::ostream& operator<<(std::ostream& os, const Vector2& v)
{
    return os << "(" << v.x << ", " << v.y << ")";
}

struct Player {
  
    Player()
    : jumping(false),
      position {0.0, 0.0},
      velocity {0.0, 0.0}
    {
        
    }
  
    void jump()
    {
        jumping = true;
        velocity = Vector2(10.0, 10.0);
    }
    
    void update(double g)
    {
        double delta = 0.1; // time delta [s]
        
        if (jumping)
        {
            velocity = velocity + delta * Vector2(0.0, g); // V_f = V_i + a * t
            position = position + delta * velocity; // distance [m] = speed [m/s] * time [s]
            
            if (position.y <= 0.0)
            {
                position.y = 0.0;
                jumping = false;
            }
        }
    }
    
    bool jumping;
    Vector2 position;
    Vector2 velocity;
};

int main()
{
    double g = -9.8; // acceleration due to gravity
    Player player;

    // User input:
    player.jump();
    
    std::cout << player.position << '\n';
    
    while (player.jumping)
    {
        // Update player based on world:
        player.update(g);

        // Draw player to screen:
        std::string offset( int(10 * player.position.y), ' ');
        std::cout << offset << player.position << '\n';
    }
}


(0, 0)
         (1, 0.902)
                 (2, 1.706)
                        (3, 2.412)
                              (4, 3.02)
                                   (5, 3.53)
                                       (6, 3.942)
                                          (7, 4.256)
                                            (8, 4.472)
                                             (9, 4.59)
                                              (10, 4.61)
                                             (11, 4.532)
                                           (12, 4.356)
                                        (13, 4.082)
                                     (14, 3.71)
                                (15, 3.24)
                          (16, 2.672)
                    (17, 2.006)
            (18, 1.242)
   (19, 0.38)
(20, 0) // landed!
Last edited on
Hello,

Thank you for your reply, the link for the jump and for the code. I read the link you wrote they were very help full.

I would like to ask you something and I hope it's not a stupid question. What do you mean to program it in own program this logic?

thank you in advance
I think what Ganado meant is that you try to figure out physics, or in this case character jumping, in a separate program, where you only focus on that task. So that you don't have to worry about errors outside of what you're really focused on.
thank you very much.. I will start right away!
Topic archived. No new replies allowed.