Jump to content
  • Advertisement
Sign in to follow this  
styfir

Gravity in a simple C++ Game

This topic is 4284 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi does anyone know how to implement gravity in a simple platform game. At the min I have a character who walks right and left, jumps and goes up ladders to the next platform - you get the jist. The problem is that if the char. goes up a ladder and say, wants to come off the ladder halfway up/down, or reaches the top of the ladder and steps off to the platform, he just falls through the floor(all the way down)!! there is no gravity in the (basic)code as yet and I feel that putting this into the code would make the game run better. Have a look at the code - I know its not great as I'm only starting out

#include "Globals.h"

void Main_Loop()
{   
	
	HDC hdc;
    hdc = GetDC(GameHandle);

	if (KEYDOWN(VK_ESCAPE))
	{		
	    SendMessage(GameHandle, WM_CLOSE, 0,0);		
	}


	chuckyendtime=(GetTickCount()-chuckystarttime);
	if ((int)chuckyendtime>chuckyspeed)
	{
		chuckystarttime=GetTickCount();
		underChucky=GetPixel(hdc,px+4,py+35);

		/*if((underChucky)!=RGB(50,50,50) && !canJump && !jumping)
		{
			Clear_Chucky();
			py+=3;
			Draw_Chucky();
		}
*/
		if(!jumping && !canJump)
		{
			if((underChucky)!=RGB(50,50,50))
			{
				SetPixel(hdc,200,350,RGB(0,255,255));
				Clear_Chucky();
				py+=3;
				Draw_Chucky();
			}
		}
	

		if((underChucky)==RGB(50,50,50)||(underChucky+1)==RGB(50,50,50))
		{
			canJump=true;
		}
		else
		{
			canJump=false;
		}

		if (KEYDOWN(VK_SHIFT) && canJump)
		{
			SetPixel(hdc,200,300,RGB(0,255,0));
			jumping=true;
		}

		if(jumping)
		{
			if(py>470)
			{
				Clear_Chucky();
				py-=3;
				Draw_Chucky();
				canJump=false;
			}		
			else
			{
				jumping=false;
			}
			
		}
		
		

		


		if (KEYDOWN(VK_RIGHT))
		{
			if (px<774)
			{
				Clear_Chucky();
				px+=3;
				
				// when moving right check to see if theres a ladder //
				if(GetPixel(hdc,px+27,py)==RGB(60,60,60))  // ladder color
				{	
					contact=true;
					canClimb=true;
				}
				if(contact)
				{
					Redraw_Ladder();    // go and redraw corresponding ladder
				}
				Draw_Chucky();
			}
		}

		if (KEYDOWN(VK_LEFT))
		{
			if (px>3)
			{
				Clear_Chucky();
				px-=3;

				// when moving left check to see if theres a ladder //
				if(GetPixel(hdc,px-27,py)==RGB(60,60,60))  // ladder color
				{	
					contact=true;
					canClimb=true;
				}
				if(contact)
				{
					Redraw_Ladder();    // go and redraw corresponding ladder
				}
				Draw_Chucky();
			}
		}
	
		if(KEYDOWN(VK_UP) && canClimb)
		{
			goClimb=true;
			//canClimb=false;
		}
		if(goClimb)
		{
			if(py>391)
			{
				Clear_Chucky();
				py-=1;
				Draw_Chucky();
				goClimb=false;
			}
		}
	}
	
	ReleaseDC(GameHandle,hdc);
	

}


styfir.

Share this post


Link to post
Share on other sites
Advertisement
First, I would recommend refactoring your code. So much of it is tied to Magic Numbers, like the color of a pixel below the character. Magic Numbers are bad because they are difficult to change, and make your code break when you try. It might also be easier to read if you re-partitioned your code into smaller functions.

Keep in mind that I'm not disparaging your efforts thus far. You should be proud that you got anything running at all! Quite a few people don't even make it that far.

As far as answering your actual question, I'm actually not quite sure what you're asking. If you want to know how to implement gravity, it's a pretty simple arrangement of variables. The problem of falling through the floor sounds more like a collision detection issue. Considering your collision detection tests only one pixel, it might be that the character hasn't moved far enough for that test pixel to be over the platform yet. You may have to test each pixel below the bottom of the character's sprite.

Share this post


Link to post
Share on other sites
I think maybe your problem is that the character fell too fast, and in one frame, moved past the pixel you are testing against. I think a better way for this is to use 2 points on the character, one at bottom and one at top. So that way if any ground pixel is between the 2 points, then you know there is collision.

If you want to implement a better gravity into your game, take a look at this article:

http://www.gamedev.net/reference/articles/article694.asp

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!