Advertisement

how do I get my sprite to move up and down once

Started by October 02, 2019 04:52 PM
75 comments, last by Tom Sloper 5 years, 4 months ago
3 hours ago, phil67rpg said:

thanks joe, I am have changed my code, it  jumps up to 25.0f and  stays there, I will work on it some more.

I assume the ground is at height 25. So maybe you unintendently start inside the ground, and staying at 25 after that would be correct.

Can you then jump again, followed by falling down and resting at 25 as expected? (likely not.)

What are your initial values of gravity and positionY?

Let's remain focused on fixing the main loop before we continue to jump around literally. It's useless to debug an algorithm if the overall program flow is broken and/or misunderstood.

Advertisement

 

11 minutes ago, Prototype said:

Let's remain focused on fixing the main loop before we continue to jump around literally. It's useless to debug an algorithm if the overall program flow is broken and/or misunderstood.

I think the main loop is fine now as he added a additional variable for the jump key, following my suggestion.

After that we no longer execute game logic code from GLUT callbacks, of which we do not know when and how often they might happen, and the program flow is clear by following loop().

At this point it seems just some math bug that prevents that jump from finally working.

But if you see still a flaw with game loop, what is it?


	                        if (positionY > 25.0f)
                        {
                            positionY = 25.0f;
                            velocityY = 0.0f;
                            onGround = true; // <- bug fix: previously the have been set to false
                        }
 
	

Yay, i have tested it usig code below and it works. The dot jumps up and falls down to ground if i click my button. :)


struct PhilsGame
{
	float positionX = 0;
	float positionY = 30;
	float velocityX = 0;
	float velocityY = 0;
	float gravity = 1;

	bool onGround = false;
	bool key_jump = false;

	void StartJump()
	{
		if (onGround)
		{
			velocityY = -12.0f;
			onGround = false;
		}
	}

	void EndJump()
	{
		if (velocityY < -6.0f)
		{
			velocityY = -6.0f;
		}
	}

	void Update()
	{
		if (key_jump)
			StartJump();
			velocityY += gravity;
			positionY += velocityY;
			positionX += velocityX;
			if (positionY > 25.0f)
			{
				positionY = 25.0f;
				velocityY = 0.0f;
				onGround = true;
			}
	}

	void Loop(int val)
	{
		Update();
		//drawSprite();
		RenderPoint (vec2(positionX, positionY), 1,1,1);
		RenderArrow (vec2(positionX, positionY), vec2(velocityX, velocityY), 0.1f, 1,1,1);
		//glutPostRedisplay();
		//glutTimerFunc(33, Loop, 0);
	}
};

static PhilsGame game;
if (ImGui::Button("JUMP!"))
	game.StartJump();
game.Loop(0);
ImGui::Text("p %f v %f", game.positionY, game.velocityY);

 

4 hours ago, Prototype said:

I reworked your code a little to reflect these changes.

I see you had fixed the onGround bug already, so it should work as well.

I'd still recommend using the additional variable(s) for the keys to be sure about program control flow, but may be a matter of taste, depending on GLUTs inner workings.

31 minutes ago, JoeJ said:

 

I think the main loop is fine now as he added a additional variable for the jump key, following my suggestion.

After that we no longer execute game logic code from GLUT callbacks, of which we do not know when and how often they might happen, and the program flow is clear by following loop().

At this point it seems just some math bug that prevents that jump from finally working.

But if you see still a flaw with game loop, what is it?

The issue here is that Phil has been known to copy and paste code without actually understanding what is going on and how to reproduce the same result without references. This is why we're seeing the same issues popup since 2006... This is also why I don't provide him with actual code examples anymore and in the past I provided either pseudocode examples or in layman terms but he could not take those examples and actually code it.

https://www.google.com/search?q=phil67rpg+opengl+site%3Agamedev.net

If Phil cannot explain in his own words why something works then he really shouldn't be moving forward. If this is too complex for him then he should just use something like Game Maker Studio. We're doing no service to Phil by spoon feeding.

Programmer and 3D Artist

Advertisement
3 minutes ago, Rutin said:

This is why we're seeing the same issues popup since 2006...

Ha, ok, did not know that. I hear you...

That's for certain a long time. After that neither tutorials nor forum help should be necessary for such simple things.

 

1 hour ago, JoeJ said:

But if you see still a flaw with game loop, what is it?

In the last snippet there was still code that creates additional timers, had EndJump implemented for no reason (the jump is ended when you hit the floor), and several other things that were superfluous or unneeded. I pasted a trimmed down version.

The point is, this uncertainty hints at guesswork. As long as Phil is building on shaky grounds it is never going to work as intended.

well I have been reading about game loops and time steps and  have been slowly learning about them. is there a difference between using others code and reinventing the wheel?

37 minutes ago, phil67rpg said:

well I have been reading about game loops and time steps and  have been slowly learning about them. is there a difference between using others code and reinventing the wheel?

It's not about re-inventing the wheel or using pre-made libraries (which many of us do), it's about understanding how things work. Using other people's code with zero understanding of what is going on will never develop you into a good programmer. If you don't want to "re-invent" the wheel then stop working with OpenGL and pick up Game Maker Studio and work with pre-made templates.

You've been at this for over 14+ years now and have almost nothing to show for it.

If you continue to stay on this track by copying and pasting other people's code and coming to the forums looking for help you will only create additional frustrations for yourself and other people. You're also robbing yourself of time which you'll never get back. I would suggest aligning yourself with something you're good at and something you have passion for as I'm extremely confused on your end goal here... I genuinely would like to see you succeed but it's very apparent programming isn't for you, but at the end of the day you'll do what you want regardless.

Programmer and 3D Artist

This topic is closed to new replies.

Advertisement