Skip to main content
gamedev.net
🔒 Locked

Jumping Script

Started by Krak Jan 10, 2004 at 1:51 AM 0 replies 350+ views
Original Post
Krak
Krak
I''m pretty crappy with programming games, but here''s a little jumping script I made. Don''t pay much attention to DrawGLScene(), because it doesn''t really have much to do with the script to make the character jump. glCreateQuad() isn''t defined in this code either, but it just creates a simple square.
float xpos=0; // x position

float MaxJump=1.5; // max height of jump

float Gravity=1.05; // manipulates max jump

float SubMaxJump=MaxJump; //constant that restores MaxJump

float y=0; // Y position

float yOrigin=y; // Constant that restores y back to it''s origin

bool Jumping=false; //Jumping flag

bool Falling=false; //Falling flag

bool JumpOK=true; //Is it okay to jump?


int DrawGLScene(GLvoid)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glLoadIdentity();
	glTranslatef(xpos,y,-10);
	glColor3f(0.7f,0.0f,0.0f);

	glCreateQuad();
	
	KeyControls();

	return TRUE;
}

bool move;

void KeyControls()
{
if(keys[VK_RIGHT])
xpos += .05;
if(keys[VK_LEFT])
xpos -= .05;

/*if you press UP, you''re not jumping or falling, and it''s okay to jump, make Jumping=true, and JumpOK=false*/

if(keys[VK_UP] && !Jumping && !Falling && JumpOK)
{
Jumping=true;
JumpOK=false;
}

/*If yo''re jumping, divide MaxJump by Gravity so it will slowly get smaller, and add MaxJump to y so the character moves up*/
if(Jumping)
{
	MaxJump /= Gravity;
	y += MaxJump;
}

/*If MaxJump has run out, our character is no longer jumping, but falling*/
if(MaxJump <= 0)
{
	Falling=true;
	Jumping=false;
}

/*Opposite of Jumping*/
if(Falling)
{
	MaxJump *= Gravity;
	y -= MaxJump;
}

/*Keeps MaxJump from getting higher than 4, so the character doesn''t fall at large intervals.*/
if(Falling && MaxJump > 3)
MaxJump = 4;

/*If the block is back to it''s y origin, make both jumping and falling false, and assign MaxJump to constant SubMaxJump to restore it back to normal. It''s not okay to jump again unless you release the UP key, and then press it again.*/
if(y <= yOrigin)
{
	Jumping=false;
	Falling=false;
	MaxJump=SubMaxJump;
	if(keys[VK_UP]==false)
		JumpOK=true;
}
}
What do you think? It''s definitely far from perfect, but it does sorta work. Any tips you have to make more efficient jumping code?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.