Jumping sprites

#1

I am trying to get a sprite to jump.

void spritejump()
{
	if (state == false)
	{
		jump++;
		if (jump >= 20.0f)
		{
			state = true;
		}
	}
	else if (state == true)
	{
		jump--;
		if (jump <= 0.0f)
		{
			state = false;
		}
	}
//	cout << "jump: " << jump << endl;
}

void handleKeypress(unsigned char key, int x, int y)
{
	int animate = 0;

	switch (key)
	{
	case 27:
		exit(0);
		break;
	case 32:
		animate = !animate;
		if (animate)
		{
			glutIdleFunc(spritejump);
		}
		else
		{
			glutIdleFunc(NULL);
		}
		break;
#2

Again, you didn’t actually ask a question.

Please read all of Posting Guideline #4 here:

#3

well I am working on 2d platformer. what I want to do is get my sprite to jump up and down.

#4

I think his point is that a sprite jumping up and down doesn’t really have anything to do with OpenGL specifically. If you can draw the sprite in one location, at one orientation, and you can draw it in a different location, in a different orientation, then you have all of the OpenGL tools to make the sprite jump up and down. Any other problems you have aren’t really OpenGL problems.

Also, you didn’t explain how your code is trying to accomplish jumping or how it is unable to do so or whatever problem you’re having with it at the time. That makes it kind of difficult for anyone to help you with it.

#5

I am still working on jumping can you look at this code and tell me if I am on the right track.

#include<iostream>

using namespace std;

float speedY = 0.0f;
float positionY = 0.0f;
float currentY = -0.75f;

int main()
{
	for (int i = 0; i < 20; i++)
	{
		if (speedY == 0.0f)
		{
			speedY = 0.0035f;
		}

		if (positionY > currentY)
		{
			speedY -= 0.00001f;
		}

		if (positionY < currentY)
		{
			speedY = 0.0f;
			positionY = currentY;
		}
		positionY++;
		speedY++;
		cout << "speedY: " << speedY << endl;
		cout << "positionY: " << positionY << endl;
	}
	system("pause");
	return 0;
}
#6

I have tried this code but with no luck.

#7

I have got my code to jump up and down continuously when I press the space bar but I want it to only jump up and down once when I hit the space bar.

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 handleKeypress(unsigned char key, int x, int y)
{
	switch (key)
	{
	case 27:
		exit(0);
		break;
	case 32:
		EndJump();
		Loop(0);
		StartJump();
		break;