Advertisement

jump algorithm

Started by September 22, 2019 05:32 PM
34 comments, last by phil67rpg 5 years, 2 months ago

 well let me explain that my code  does when I hit the space bar my sprite moves up to a limit and  when I hit the space bar again it moves my sprite down when I keep the space bar down it moves up and down. my question is how do I hit the space bar and  get my sprite  to move up and down of its own accord, I want  it to jump like in video games. I just need a little  hint I have  almost solved my problem.


void spritejump()
{
		if (state == false)
		{
			if (positionY <= 20.0f)
			{
				positionY += 5.0f;
			}
			if (positionY == 20.0f)
			{
				state = true;
			}
		}
		else if (state == true)
		{
			if (positionY >= 0.0f)
			{
				positionY -= 5.0f;
			}
			if (positionY == 0.0f)
			{
				state = false;
			}
		}
}

void handleKeypress(unsigned char key, int x, int y)
{
	switch (key)
	{
	case 27:
		exit(0);
		break;
	case 32:
		for (int i = 0; i <= 10; i++)
		{
			spritejump();
		}
	

 

Why do you call spritejump() ten eleven times? I think the answer might be related to your asking of 'what is a frame?' in the other topic.

Advertisement

well I call spritejump 11 times because I want the jump algorithm to jump up and down and up again but I want it to only jump  up and down like in Mario brothers. I am sorry but it is hard for me to explain what I want my code to do.

The jumping should be integrated into your game loop, so that with every screen update you move one step up or down. The same thing for every other movement. You are running at 60fps so you cannot do a complete jump with a single function call. Hope this makes sense.

I think from your previous snippets you seem to run everything on timers, which isn't really a good idea. Perhaps it's better to take a step back and rework everything into a proper game loop. Have functions that can create and control sprites etc. You cannot make anything without these building blocks. Otherwise the old advice is still valid: use a f* library.

See:

 

Programmer and 3D Artist

 well I watched the video, and I am going to brush  up on my linear algebra and calculus, specifically parabolas. I like the f(x)=1/2*g*t^2+Vo+Po equation. I am going to try to integrate this into my jump algorithm.

Advertisement
22 hours ago, Rutin said:

See:

That is a terrible video for someone just trying to get a basic jump working.

🙂🙂🙂🙂🙂<←The tone posse, ready for action.

I found it to be interesting.

Here is a pseudo game loop that implements jumping in a quick and dirty way. It implicitly describes a parabola.
Maybe this can get you going.
 


void nextFrame() 
{
    if (jumping) 
    {
        if (jumpVelocity < 4.0) jumpVelocity += 0.1;
        playerY += jumpVelocity;
        if (playerY > floorY) jumping = false;
    }
    else if (jumpKeypressed) {
        jumping = true;
        jumpVelocity = -4.0;
    }
          
    // Do other stuff and draw your frame
}

 

2 hours ago, fleabay said:

That is a terrible video for someone just trying to get a basic jump working.

Please do elaborate on why the video is terrible. The video itself provides insightful information that is valuable to anyone looking to improve. Consistent negativity however is counter productive.

Programmer and 3D Artist

This topic is closed to new replies.

Advertisement