Jump to content
  • Advertisement
phil67rpg

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

Recommended Posts

I have worked on this problem for several days. I have almost solved my problem. I have got my sprite to move up and  down continuously using the mouse. this done by  using the glutTimerFunc function. I want the mouse to get the sprite to move up and down once. I have googled and debugged my code. I have also taken some JavaScript code and ported it over into opengl. let me  know if you need my  question to be more specific. here is my code so far.

bool onGround = false;

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

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

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

void Loop(int val)
{
	Update();
	drawSprite();
	glutPostRedisplay();
	glutTimerFunc(33, Loop, 0);
}

void mouseClicks(int button, int state, int x, int y)
{
	if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
	{
		StartJump();
	}
	if (button == GLUT_LEFT_BUTTON && state == GLUT_UP)
	{
		EndJump();
	}
}

void handleKeypress(unsigned char key, int x, int y)
{
	switch (key)
	{
	case 27:
		exit(0);
		break;
	case 32:
		Loop(0);
		break;

 

Share this post


Link to post
Share on other sites
Advertisement
Posted (edited)

You're moving in the right direction, Phil. You must set onGround to true in your Update function, because that's when you land.

Also StartJump and EndJump need work, remember that these functions only get called 1 time when you actualy press/release the button. You probably don't need EndJump.

Edit: I removed some thoughts about glutTimerFunc because it seems that is the way glut works. As others have said, glut may not be the best framework to learn common game programming concepts.

Edited by Prototype

Share this post


Link to post
Share on other sites
Posted (edited)

thanks prototype I like freeglut because it is what I used in college. I changed onGround to true in Update function. it still  moves  up and down continuously.  

Edited by phil67rpg

Share this post


Link to post
Share on other sites

1. Find the code that makes it stop jumping.

2. Figure out why that doesn't work, for starters, decide if that code is called at the right time.

 

Code doesn't have a mind of it own, everything it does or doesn't do has been programmed (or not programmed) by you.

In all cases, find the code that is responsible for the bad behavior, and find out why it doesn't do what it is supposed to be doing.

Share this post


Link to post
Share on other sites
20 hours ago, Prototype said:

Also StartJump and EndJump need work, remember that these functions only get called 1 time when you actualy press/release the button. You probably don't need EndJump.

can  you be more specific

Share this post


Link to post
Share on other sites
2 hours ago, phil67rpg said:

can  you be more specific

He doesn't need to be more specific. You need to analyze your code, step through it, and figure out why it doesn't work.He told you where to look.

Share this post


Link to post
Share on other sites
Posted (edited)

ok I will work on my code today, I think my problem is that I don't  understand the glutTimerFunc function very well.

Edited by phil67rpg

Share this post


Link to post
Share on other sites

Okay, make sure you get to understand it well because it's vital to be able to follow the flow of your program. If you can run the code inside your head things will become much easier to figure out. Using a framework that runs on asynchronous timers doesn't really help in that regard.

You know, there are frameworks out there that let you do this kind of thing with six lines of code. You have to ask yourself what it is you actually want to achieve. Do you want to make a finished game? Do you just want to muck around with openGL? Think about that, and then pick the tools that make it possible in the easiest way. You don't learn much from entering dead-end roads all the time.

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!