Jump to content
  • Advertisement
Sign in to follow this  
phil67rpg

jump sprite

Recommended Posts

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;

 

Share this post


Link to post
Share on other sites
Advertisement

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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;
	}
}

 

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


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

  • 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!