Annoying Bug in Chilli's Framework >.<

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
barocxPvP
Posts: 11
Joined: January 1st, 2017, 3:56 pm

Annoying Bug in Chilli's Framework >.<

Post by barocxPvP » February 15th, 2017, 8:39 pm

Hi guys, I have been trying to make a game in Chilli's Framework but I have some problems.
My game needs gravity.
Lets just say that I did y+=1;
The problem occurs when I want to collide my object with an line or a platform.
SOMEHOW object get stuck in Platform/Line..
I debugged it a lot of times and there is no any actual error.
It is just a glich.
After years of trying to solve it I managed.
When you put code in UpdateModel() > it runs properly.
When you put code in Your own function (That you run trough UpdateModel() ) > It runs differently.
Glich was shown in ones of Chilli's tutorials.
https://www.youtube.com/watch?v=xWLcp_yOpww at 24:01
(In that video is shown how my glich behave).
So why is this:

Code: Select all

UpdateModel()
{
If (y>=0)
y= 0+1                            //y is global int defined at Game.h/User variables
}
Different than this:

Code: Select all

UpdateModel()
{
checker(ScreenHeight);
}
void checker(int Var)
{
If (y>=0)
y= Var+1        //y is global int defined at Game.h/User variables 
}
Edit: Ignore Syntax errors and Grammar mistakes please (if I made any) ...
Last edited by barocxPvP on February 15th, 2017, 9:10 pm, edited 1 time in total.

mmmmmm
Posts: 38
Joined: August 12th, 2013, 6:33 am

Re: Annoying Bug in Chilli's Framework >.<

Post by mmmmmm » February 15th, 2017, 8:47 pm

well y is defined again in the line: void checker(int y) so in the function there is no way to acces the gloabl variable y
( local scope beats global scope every time )

barocxPvP
Posts: 11
Joined: January 1st, 2017, 3:56 pm

Re: Annoying Bug in Chilli's Framework >.<

Post by barocxPvP » February 15th, 2017, 9:12 pm

mmmmmm wrote:well y is defined again in the line: void checker(int y) so in the function there is no way to acces the gloabl variable y
( local scope beats global scope every time )
Oh sorry I made mistake. Please check post again I edited it and if you can tell me answer now.
Thanks !

MrGodin
Posts: 721
Joined: November 30th, 2013, 7:40 pm
Location: Merville, British Columbia Canada

Re: Annoying Bug in Chilli's Framework >.<

Post by MrGodin » February 15th, 2017, 10:00 pm

Code: Select all

UpdateModel()
{
checker(ScreenHeight);
}
void checker(int Var)
{
If (y>=0)
y= Var+1        //y is global int defined at Game.h/User variables
}
well here you are passing ScreenHeight into checker as Var, and if y >=0 then you are setting y to be ScreenHeight +1, which puts you out of bounds ?.
what would make more sense to me is if y>= Var (screenHeight) then y -= 1 ?
Curiosity killed the cat, satisfaction brought him back

barocxPvP
Posts: 11
Joined: January 1st, 2017, 3:56 pm

Re: Annoying Bug in Chilli's Framework >.<

Post by barocxPvP » February 17th, 2017, 5:53 pm

MrGodin wrote:

Code: Select all

UpdateModel()
{
checker(ScreenHeight);
}
void checker(int Var)
{
If (y>=0)
y= Var+1        //y is global int defined at Game.h/User variables
}
well here you are passing ScreenHeight into checker as Var, and if y >=0 then you are setting y to be ScreenHeight +1, which puts you out of bounds ?.
what would make more sense to me is if y>= Var (screenHeight) then y -= 1 ?
I did an random example. I actually just wanted to show what I meant by my explanations.
I do not have problem with code but with framework :(

cameron
Posts: 794
Joined: June 26th, 2012, 5:38 pm
Location: USA

Re: Annoying Bug in Chilli's Framework >.<

Post by cameron » February 17th, 2017, 5:59 pm

Post your solution.
Computer too slow? Consider running a VM on your toaster.

barocxPvP
Posts: 11
Joined: January 1st, 2017, 3:56 pm

Re: Annoying Bug in Chilli's Framework >.<

Post by barocxPvP » February 17th, 2017, 6:15 pm

cameron wrote:Post your solution.

Code: Select all

void Game::UpdateModel()
{

	//Gravity
	y += Gspeed;
	//Movement
	int speed = 5;
	if (wnd.kbd.KeyIsPressed('W'))
	{
		y -= speed;
	}
	if (wnd.kbd.KeyIsPressed('A'))
	{
		x -= speed;
	}
	if (wnd.kbd.KeyIsPressed('S'))
	{
		y += speed;
	}
	if (wnd.kbd.KeyIsPressed('D'))
	{
		x += speed;
	}
	//Barrier
	if (x <= 0)x = 0;
	if (x + Width >= gfx.ScreenWidth) x = gfx.ScreenWidth - Width-1;
	if (y <= 0) y = 1;
	if (y + Height >= gfx.ScreenHeight) y = gfx.ScreenHeight - Height-1;

}

void Game::ComposeFrame()
{
	BuildBox(x, y, Width, Height, R);
	PlaceLine(lx, ly, 200);
	Collide(lx, ly, 200);
}

void Game::BuildBox(int xCord, int yCord, int Width, int Height, int R)
{
	for (int x = xCord; x <= Width+xCord; x++)
	{
		for (int y = yCord; y <= Height+yCord; y++)
		{
			gfx.PutPixel(x,y,R,255,255);
		}
	}
}
void Game::PlaceLine(int Nx, int Ny, int Nw)
{
	Collide(Nx, Ny, Nw);
	for (int x = Nx; x <= Nw + Nx; x++)
		gfx.PutPixel(x, Ny, 255, 200, 200);
}
void Game::Collide(int lx, int ly, int lw)
{
	lx + lw;
	if (lx + lw > x)
	{
		if (y + Height >= ly + 1)
		{
			y = ly - Height - 1;
		}
		if (y + Height >= ly)
		{
			y = ly - Height - 2;
		}
	}
}
/*
Game.h
int x=0, y=0,Width=20,Height=20;
int lx = 0, ly = 420;
int Gspeed = 5; // Make this 0 if you want to move free.
int R = 255;
int speed = 5;
void BuildBox(int xCord, int yCord, int Width, int Height, int R);
void PlaceLine(int Nx, int Ny, int Nw);
void Collide(int lx, int ly, int lw);
*/
	
There it is ! The problem is that a box glich in platform :/ Please anyone give me a clue how to fix it.
Edit_0: It works fine while Gravity is less than 2. But In that case a player can just Do ''W'' to avoid gravity :/
Edit_1: Actually it does glich even after setting it to 1 sometimes :/.

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Annoying Bug in Chilli's Framework >.<

Post by albinopapa » February 17th, 2017, 8:13 pm

Ok, the problem isn't with the code, per se, but a problem of the code being out of order.

This is how I fixed it

Code: Select all

void Game::UpdateModel()
{
	//Gravity
	y += Gspeed;
	//Movement
	int speed = 5;
	if( wnd.kbd.KeyIsPressed( 'W' ) )
	{
		y -= speed;
	}
	if( wnd.kbd.KeyIsPressed( 'A' ) )
	{
		x -= speed;
	}
	if( wnd.kbd.KeyIsPressed( 'S' ) )
	{
		y += speed;
	}
	if( wnd.kbd.KeyIsPressed( 'D' ) )
	{
		x += speed;
	}
	//Barrier
	if( x <= 0 )x = 0;
	if( x + Width >= gfx.ScreenWidth ) x = gfx.ScreenWidth - Width - 1;
	if( y <= 0 ) y = 1;
	if( y + Height >= gfx.ScreenHeight ) y = gfx.ScreenHeight - Height - 1;

	Collide( lx, ly, 200 );
}

void Game::ComposeFrame()
{
	BuildBox( x, y, Width, Height, R );
	PlaceLine( lx, ly, 200 );
}

void Game::BuildBox( int xCord, int yCord, int Width, int Height, int R )
{
	for( int x = xCord; x <= Width + xCord; x++ )
	{
		for( int y = yCord; y <= Height + yCord; y++ )
		{
			gfx.PutPixel( x, y, R, 255, 255 );
		}
	}
}
void Game::PlaceLine( int Nx, int Ny, int Nw )
{
	for( int x = Nx; x <= Nw + Nx; x++ )
		gfx.PutPixel( x, Ny, 255, 200, 200 );
}
void Game::Collide( int lx, int ly, int lw )
{
	lx + lw;
	if( lx + lw > x )
	{
		if( y + Height >= ly + 1 )
		{
			y = ly - Height - 1;
		}
		if( y + Height >= ly )
		{
			y = ly - Height - 2;
		}
	}
}
You were doing the collision check after you draw the box. So it was drawing the box below the line, then moving the box up then drawing the line.

It's always a good idea to have your game logic separate from your drawing logic, and this is just one reason why.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Post Reply