Homework #5 Help

The Partridge Family were neither partridges nor a family. Discuss.
Ziltwix
Posts: 39
Joined: January 3rd, 2017, 5:12 pm

Homework #5 Help

Post by Ziltwix » February 10th, 2017, 5:10 pm

Okay ladies and gentlemen! How is everyone today? I have a question I'd like to ask in regards to any advice for tips for homework five.
This bottom code is in the UpdateModel and I was wondering what method can I use to approach when our reticle collides with the stationary reticle? I don't want the answer, but some advice where I can implement my own style.
I was thinking about using the OR operator, but when I try using both the ax or x variable, nothing changes when the reticle collides with the box.

Here is my code:

Code: Select all

//Code Below Ensures That The Reticle Is Green Outside Of Set Parameters.
	
	if (ax <= 200)
	{
		rb = 0;
	}
	else
	{
		int rb = 255;
	}

	//The Code Below With Change The Color of X/Y If AX/AY Collides With It.

	if (ax + 5 >= x)
	{
		gb = 0;
	}
This next code is in the ComposeFrame just so I can show you guys the stationary reticle that I have drawn.

Code: Select all

        gfx.PutPixel(-5 + ax, -5 + ay, rb, 255, rb);
	gfx.PutPixel(-5 + ax, -4 + ay, rb, 255, rb);
	gfx.PutPixel(-5 + ax, -3 + ay, rb, 255, rb);
	gfx.PutPixel(-4 + ax, -5 + ay, rb, 255, rb);
	gfx.PutPixel(-3 + ax, -5 + ay, rb, 255, rb);
	gfx.PutPixel(-5 + ax, 5 +  ay, rb, 255,  rb);
	gfx.PutPixel(-5 + ax, 4 +  ay, rb, 255,  rb);
	gfx.PutPixel(-5 + ax, 3 +  ay, rb, 255,  rb);
	gfx.PutPixel(-4 + ax, 5 +  ay, rb, 255,  rb);
	gfx.PutPixel(-3 + ax, 5 +  ay, rb, 255,  rb);
	gfx.PutPixel(5 + ax, -5 +  ay, rb, 255,  rb);
	gfx.PutPixel(5 + ax, -4 +  ay, rb, 255,  rb);
	gfx.PutPixel(5 + ax, -3 +  ay, rb, 255,  rb);
	gfx.PutPixel(4 + ax, -5 +  ay, rb, 255,  rb);
	gfx.PutPixel(3 + ax, -5 +  ay, rb, 255,  rb);
	gfx.PutPixel(5 + ax, 5 +   ay, rb,  255,  rb);
	gfx.PutPixel(5 + ax, 4 +   ay, rb,  255,  rb);
	gfx.PutPixel(5 + ax, 3 +   ay, rb,  255,  rb);
	gfx.PutPixel(4 + ax, 5 +   ay, rb,  255,  rb);
	gfx.PutPixel(3 + ax, 5 +   ay, rb,  255,  rb);
I appreciate the help from every single one of you guys! :D

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

Re: Homework #5 Help

Post by MrGodin » February 10th, 2017, 5:54 pm

look up rectangle collision detection on google, that may lead you into an idea of what might work. Hint, use the extents of the object ;)
Just a thought
Curiosity killed the cat, satisfaction brought him back

Ziltwix
Posts: 39
Joined: January 3rd, 2017, 5:12 pm

Re: Homework #5 Help

Post by Ziltwix » February 11th, 2017, 4:34 pm

Thanks, MrGodin! Will definitely look up!

Ziltwix
Posts: 39
Joined: January 3rd, 2017, 5:12 pm

Re: Homework #5 Help

Post by Ziltwix » February 13th, 2017, 5:16 pm

Hey MrGodin! I was looking up multiple ideas for rectangular collision detection and this Cartesian coordinate code on StackOverFlow seems to be the most popular:

Code: Select all

if (RectA.X1 < RectB.X2 && RectA.X2 > RectB.X1 &&
    RectA.Y1 < RectB.Y2 && RectA.Y2 > RectB.Y1)
I have trouble thinking that if I was to implement this code, would I have to then ensure that every extent of the box that we drew would have to be checked so that each variable (i.e. X1,X2) are correct?

The code that I have now is completely different, but wanted to see what was going on.

Code: Select all

bool checkCollide(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2);

	if (x1 < x2 && x2 > x1 && y1 < y2 && y2 > y1)
	{
		gb = 0;
	}
This also is a problem because I'm still figuring out how each part of the box will detect one another when they finally collide.

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

Re: Homework #5 Help

Post by albinopapa » February 13th, 2017, 6:33 pm

Close, but you aren't taking the width and height into account.

The rect collision is if the
left of RectA is less than the right of RectB AND the right of RectA greater than the left of RectB AND
top of RectA is less than the bottom of RectB AND the bottom of RectA is greater than the top of RectB

You can use an image editor that has layers and draw two rectangles on different layers. Move the rectangles around so that they overlap and see if you can visualize how it works.
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

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

Re: Homework #5 Help

Post by MrGodin » February 13th, 2017, 7:28 pm

pseudo code

Code: Select all

// rectangle structure
struct Rectangle
{
 int left,top,right,bottom;
}
Rectangle A,B;
// assumes the extents are -5 and 5
// a is one box and b is the other
A.left = Ax + -5;A.top = Ay - 5;A.right = Ax + 5;A.bottom = Ay+5;
B.left = Bx + -5;B.top = By - 5;B.right = Bx + 5;B.bottom = By+5;
// collision detection
bool Collision(Rectangle A,Rectangle B)
{
return A.top < B.bottom && A.bottom > B.top && 
			A.left < B.right && A.right > B.left;
}
hope this helps
Curiosity killed the cat, satisfaction brought him back

Ziltwix
Posts: 39
Joined: January 3rd, 2017, 5:12 pm

Re: Homework #5 Help

Post by Ziltwix » February 14th, 2017, 4:25 pm

Thank you gentlemen for the help! I will keep going until I get an understanding and then move on from there.

Cheers! :D

Ziltwix
Posts: 39
Joined: January 3rd, 2017, 5:12 pm

Re: Homework #5 Help

Post by Ziltwix » February 16th, 2017, 2:08 am

Alright gentlemen, I admit. I need help.

So, the code that MrGodin introduced to me helped me understand how this is one of many algorithms, but there is something else.

I have the file attached and I wanted to address the issue here. I changed the members around to fit the variables in my code, the problem that I see is the color.

In this program I am able to hit the green box reticle from the top, bottom and left side. They shortly after disappear when I move the reticle out of the parameters. However, the right side of the green box detects the reticle from further away and does not change back to white until after it reaches a certain point.

Another thing, every time I hit the control but to change the moving reticle to blue....it's more of a sky blue, baby blue, whatever you want to call it 'blue'. I had it set to change to a more darker blue. I understand this because my moving box has different variables that change to 2 different colors. I just do not know how to approach detect how to change it. That is my problem.

Anyways, thank you everyone who takes the time to look at my problem and help me dissect the homework step by step!
Attachments
C++ Homework 5.zip
(2.85 KiB) Downloaded 134 times

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

Re: Homework #5 Help

Post by MrGodin » February 16th, 2017, 2:27 am

Ok, I'm sorry but i may have lead you astray. I reorganized the files to what should work for you.
Sorry about that
another thing is you re-delacred a value

Code: Select all

if (bx <= 200)
	{
		rb = 0;
	}
	else
	{
		int rb = 255;/// should be
               rb = 255;
	}
Attachments
Game.zip
(2.63 KiB) Downloaded 142 times
Curiosity killed the cat, satisfaction brought him back

Ziltwix
Posts: 39
Joined: January 3rd, 2017, 5:12 pm

Re: Homework #5 Help

Post by Ziltwix » February 16th, 2017, 4:23 pm

It's no worries, friend!

I honestly would have never figured out to put the "struct" class in Game.h

I'm trying to make sense of all of this.

I realize when I debug, I must have a return value.

Code: Select all

return Rectangle:: top, bottom, right, left
I have searched the internet for clues how to return the value of this rectangle, however I am unfamiliar with how "return" would work in the code.

You are a GOD MrGodin.

Post Reply