jump sprite

Started by
7 comments, last by Tom Sloper 4 years, 7 months ago

well my question is how do I 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;

 

Advertisement

Draw the sprite at the position where it is supposed to be when you draw the frame.

4 minutes ago, Alberth said:

draw the frame.

what do you mean by frame?

I'm not sure what your goal is with this. Are you asking how to make a sprite animate while jumping or do an actual jump movement? Your code seems to work on the premise of the jump being controlled by loop iterations. I'm not sure what kinda game you are making but in most cases a jump shouldn't be controlled by time because in the real world your jump can be interrupted by a variety of obstacles. If you think early Mario games for example, you can hit your head on a block or land on something that is higher up when you fall, or even fall farther. Usually it'd be better to separate the three acts of: simulating velocity from movement, applying an impulse to create a jump, and handling animation based on state. Generally all of those shouldn't really be coupled together.

6 minutes ago, Satharis said:

Are you asking how to make a sprite animate while jumping or do an actual jump movement?

I am trying to make a sprite animate while jumping.

here is my  stubbed out code for my jumping algorithm. am I on the right track?


#include<iostream>

using namespace std;

float positionX = 0.0f;
float positionY = 0.0f;
float gravity = -0.5f;
float velocityX = 0.0f;
float velocityY = 0.0f;
float time = 0.0f;
void Update(float);
int main()
{
	cout << "PositionX: ";
	cin >> positionX;
	cout << endl;
	cout << "PositionY: ";
	cin >> positionY;
	cout << endl;
	cout << "VelocityX: ";
	cin >> velocityX;
	cout << endl;
	cout << "VelocityY: ";
	cin >> velocityY;
	cout << endl;
	cout << "Time: ";
	cin >> time;
	cout << endl;
	Update(time);
	system("pause");
	return 0;
}

void Update(float time)
{
	for (int i = 0; i < time; i++)
	{
		positionX += velocityX * time;
		positionY += velocityY * time;
		velocityY += gravity * time;
		cout << "positionX: " << positionX << endl;
		cout << "positionY: " << positionY << endl;
		cout << "velocityY: " << velocityY << endl;
		cout << endl;
	}
}

 

9 minutes ago, phil67rpg said:

am I on the right track?

TRY YOUR OWN CODE.

If it works as you want, great! If it doesn't; change it. Then read this post again.

Hello to all my stalkers.

1 hour ago, Lactose said:

TRY YOUR OWN CODE.

If it works as you want, great! If it doesn't; change it. Then read this post again.

Sounds good to me. Thread closed.

-- Tom Sloper -- sloperama.com

This topic is closed to new replies.

Advertisement