Help with Collision code - :(

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
MrSneekz
Posts: 9
Joined: September 6th, 2019, 12:07 am

Help with Collision code - :(

Post by MrSneekz » September 11th, 2019, 10:48 pm

I have been having some issues with tutorial 5 and 6. I cannot find out why my code is not allowing the cursor to change color as it collides or goes over the box. I think its my if statement, but i have spent hours [6 - 8+] on this one. I come here to ask for help as my last resort .I am feeling a bit discouraged and I really do not want to lose my momentum in learning c++. Any help appreciated!

Code: Select all

	if (left_mobile < right_fixed &&
		right_mobile > left_fixed &&
		top_mobile < bottom_fixed &&
		bottom_mobile > top_fixed)
		{
		colliding = true;
		}
		else
		{
			colliding = false;
		}

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Help with Collision code - :(

Post by chili » September 12th, 2019, 9:35 am

i don't see any problem with this code, so it's gotta be somewhere else. try posting all the code you wrote
Chili

MrSneekz
Posts: 9
Joined: September 6th, 2019, 12:07 am

Re: Help with Collision code - :(

Post by MrSneekz » September 12th, 2019, 10:22 pm

chili wrote:
September 12th, 2019, 9:35 am
i don't see any problem with this code, so it's gotta be somewhere else. try posting all the code you wrote
Here is the entire code. If this is not sufficient let me know... Thanks Chili!
Attachments
CodeSolution.zip
(4.92 MiB) Downloaded 137 times

MrSneekz
Posts: 9
Joined: September 6th, 2019, 12:07 am

Re: Help with Collision code - :(

Post by MrSneekz » September 12th, 2019, 10:43 pm

and here is the code outright for reference if thats all you need. Thanks again...

Code: Select all

#include "MainWindow.h"
#include "Game.h"

Game::Game( MainWindow& wnd )
	:
	wnd( wnd ),
	gfx( wnd )
{
}

void Game::Go()
{
	gfx.BeginFrame();	
	UpdateModel();
	ComposeFrame();
	gfx.EndFrame();
}

void Game::UpdateModel()
{
	if (wnd.kbd.KeyIsPressed(VK_UP)) //key is pressed
	{
		if (inhibitUp) //checks inhibition
		{

		}
		else //finds no inhibition
		{
			vy = vy - 1;
			inhibitUp = true; //maintains velocity while key is pressed
		}
	}
	else
	{
		inhibitUp = false; //once key is released
	}
	if (wnd.kbd.KeyIsPressed(VK_DOWN))
	{
		if (inhibitDown)
		{

		}
		else
		{
			vy = vy + 1;
			inhibitDown = true;

		}
	}
	else
	{
		inhibitDown = false;
	}
	if (wnd.kbd.KeyIsPressed(VK_LEFT))
	{
		if (inhibitLeft)
		{
		}
		else
		{
			vx = vx - 1;
			inhibitLeft = true;

		}
	}
	else
	{
		inhibitLeft = false;
	}
	if (wnd.kbd.KeyIsPressed(VK_RIGHT))
	{
		if (inhibitRight)
		{
		}
		else
		{
			vx = vx + 1;
			inhibitRight = true;

		}
	}
	else
	{
		inhibitRight = false;

	}

	
	
	if (wnd.kbd.KeyIsPressed(VK_UP))
	{
		reticle_y = reticle_y - 1;	
	}
	if (wnd.kbd.KeyIsPressed(VK_DOWN))
	{
		reticle_y = reticle_y + 1;
	}
	if (wnd.kbd.KeyIsPressed(VK_LEFT))
	{
		reticle_x = reticle_x - 1;
	}
	if (wnd.kbd.KeyIsPressed(VK_RIGHT))
	{
		reticle_x = reticle_x + 1;
	}

	reticle_x = reticle_x + vx;
	reticle_y = reticle_y + vy;

	if (reticle_x + 6 >= gfx.ScreenWidth)
	{
		reticle_x = gfx.ScreenWidth - 7;
		vx = 0;
	}
	if (reticle_x - 6 < 0) 
	{
		reticle_x = 6;
		vx = 0;
	}
	if (reticle_y + 6 >= gfx.ScreenHeight)
	{
		reticle_y = gfx.ScreenHeight - 7;
		vy = 0;
	}
	if(reticle_y - 6 < 0)
	{
		reticle_y = 6;
		vy = 0;
	
	}

	const int left_mobile = reticle_x - 5;
	const int right_mobile = reticle_x + 5;
	const int top_mobile = reticle_y - 5;
	const int bottom_mobile = reticle_y + 5;

	const int left_fixed = x_boxFixed - 5;
	const int right_fixed = x_boxFixed + 5;
	const int top_fixed = y_boxFixed - 5;
	const int bottom_fixed = y_boxFixed + 5;
	
	if (left_mobile < right_fixed &&
		right_mobile > left_fixed &&
		top_mobile < bottom_fixed &&
		bottom_mobile > top_fixed)
		{
		colliding = true;
		}
		else
		{
			colliding = false;
		}

	

	shapeIsChanged = wnd.kbd.KeyIsPressed(VK_SHIFT);
}

void Game::ComposeFrame()
{
	

	if (colliding)
	{
		reticle(reticle_x, reticle_y, 255, 0, 0);

	}
	else
	{
		reticle(reticle_x, reticle_y, 255, 255, 255);
	}
	

}
void Game::DrawBox(int x_boxFixed, int y_boxFixed, int r, int g, int b)
	{

		gfx.PutPixel(x_boxFixed + 50, y_boxFixed - 50, r, g, b);
		gfx.PutPixel(x_boxFixed + 50, y_boxFixed + 50, r, g, b);
		gfx.PutPixel(x_boxFixed - 50, y_boxFixed + 50, r, g, b);
		gfx.PutPixel(x_boxFixed - 50, y_boxFixed - 50, r, g, b);
		gfx.PutPixel(x_boxFixed + 50, y_boxFixed, r, g, b);
		gfx.PutPixel(x_boxFixed - 50, y_boxFixed, r, g, b);
		gfx.PutPixel(x_boxFixed, y_boxFixed + 50, r, g, b);
		gfx.PutPixel(x_boxFixed, y_boxFixed - 50, r, g, b);
	
	}
void Game::reticle(int reticle_x, int reticle_y, int r_mob, int g_mob, int b_mob)
	{
		gfx.PutPixel(reticle_x + 6, reticle_y, r_mob, g_mob, b_mob);
		gfx.PutPixel(reticle_x, reticle_y - 6, r_mob, g_mob, b_mob);
		gfx.PutPixel(reticle_x - 6, reticle_y, r_mob, g_mob, b_mob);
		gfx.PutPixel(reticle_x - 6, reticle_y + 6, r_mob, g_mob, b_mob);
		gfx.PutPixel(reticle_x - 6, reticle_y - 6, r_mob, g_mob, b_mob);
		gfx.PutPixel(reticle_x + 6, reticle_y - 6, r_mob, g_mob, b_mob);
		gfx.PutPixel(reticle_x, reticle_y + 6, r_mob, g_mob, b_mob);
		gfx.PutPixel(reticle_x + 6, reticle_y + 6, r_mob, g_mob, b_mob);
		gfx.PutPixel(reticle_x, reticle_y, r_mob, g_mob, b_mob);
	}

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Help with Collision code - :(

Post by Yumtard » September 12th, 2019, 10:50 pm

I don't see a box to collide with.

Anyways, x_botFixed and y_botFixed is set to 0 in the h file
then

const int left_fixed = x_boxFixed - 5;
const int right_fixed = x_boxFixed + 5;
const int top_fixed = y_boxFixed - 5;
const int bottom_fixed = y_boxFixed + 5;

So the reticle changes color when it goes to the top left corner of the screen

MrSneekz
Posts: 9
Joined: September 6th, 2019, 12:07 am

Re: Help with Collision code - :(

Post by MrSneekz » September 13th, 2019, 12:28 am

ah, i see I deleted the box.
The box was initially a called object from the function "DrawBox" in the header file.
I added it to the lines below.

Code: Select all

void Game::ComposeFrame()
{
	DrawBox(200, 250, 200, 30, 60);

	if (colliding)
	{
		reticle(reticle_x, reticle_y, 255, 0, 0);

	}
	else
	{
		reticle(reticle_x, reticle_y, 255, 255, 255);
	}
	

}
And I initially just declared the variables x_boxFixed and y_boxFixed with no equivalence.
When I create an object from the function with new dimensions, I get the box drawn in the window. But the compiler does not use the defined values such as
"DrawBox(200, 250, 200, 30, 60);" in the code as an object for collision...

Code: Select all

void Game::DrawBox(int x_boxFixed, int y_boxFixed, int r, int g, int b)
	{

		gfx.PutPixel(x_boxFixed + 50, y_boxFixed - 50, r, g, b);
		gfx.PutPixel(x_boxFixed + 50, y_boxFixed + 50, r, g, b);
		gfx.PutPixel(x_boxFixed - 50, y_boxFixed + 50, r, g, b);
		gfx.PutPixel(x_boxFixed - 50, y_boxFixed - 50, r, g, b);
		gfx.PutPixel(x_boxFixed + 50, y_boxFixed, r, g, b);
		gfx.PutPixel(x_boxFixed - 50, y_boxFixed, r, g, b);
		gfx.PutPixel(x_boxFixed, y_boxFixed + 50, r, g, b);
		gfx.PutPixel(x_boxFixed, y_boxFixed - 50, r, g, b);
	
	}

Post Reply