Need help understanding lesson 7-8 moving reticle code

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
User avatar
DreamBliss
Posts: 30
Joined: March 27th, 2013, 11:48 pm
Location: Lost on the Way
Contact:

Need help understanding lesson 7-8 moving reticle code

Post by DreamBliss » May 17th, 2013, 2:02 am

If you have not completed the homework for lesson 7 do not look at this thread!

UPDATE! The code below has been changed to reflect how it looks after altering it per Chili's instructions in Lesson 8!

OK, so here is my code for lesson 8:

Code: Select all

// Start moving reticle code

	int yTMove = 100; // Defines start of top Y line
	int xRMove = 700; // Defines start of right X line
	int yBMove = 500; // Defines start of bottom y line
	int xLMove = 100; // Defines start of right x line

	if(xPosMove == xLMove && yPosMove > 100)
	{
		yPosMove -= 2;
	}
	
	if(yPosMove == yBMove)
	{
		xPosMove -= 2;
	}
	
	if(xPosMove == xRMove)
	{
		yPosMove += 2;
	}
	
	if(yPosMove == yTMove)
	{
		xPosMove += 2;
	}
	
	gfx.PutPixel(xPosMove-5,yPosMove,red,green,blue);
	gfx.PutPixel(xPosMove-4,yPosMove,red,green,blue);
	gfx.PutPixel(xPosMove-3,yPosMove,red,green,blue);
	gfx.PutPixel(xPosMove+3,yPosMove,red,green,blue);
	gfx.PutPixel(xPosMove+4,yPosMove,red,green,blue);
	gfx.PutPixel(xPosMove+5,yPosMove,red,green,blue);
	gfx.PutPixel(xPosMove,yPosMove,red,green,blue);
	gfx.PutPixel(xPosMove,yPosMove-5,red,green,blue);
	gfx.PutPixel(xPosMove,yPosMove-4,red,green,blue);
	gfx.PutPixel(xPosMove,yPosMove-3,red,green,blue);
	gfx.PutPixel(xPosMove,yPosMove+3,red,green,blue);
	gfx.PutPixel(xPosMove,yPosMove+4,red,green,blue);
	gfx.PutPixel(xPosMove,yPosMove+5,red,green,blue);

// End moving reticle code
Understand that instead of x2 and y2 I have used xPosMove and yPosMove as my user variables. My other user variables are xPos and yPos instead of x and y. xPosMove and yPosMove have each been set to 100. Everything else should be well enough explained. And yes, this code does exactly what the homework assignment specified. It is running, I just don't understand why.

The problem is this code:

Code: Select all

if(yPosMove == yTMove)
{
	xPosMove += 2;
}
For some reason that works fine. But if I used this:

Code: Select all

if(xPosMove < xRMove)
{
	xPosMove += 2;
}
The cursor will not move! This is where I was when I gave up on the assignment and watched the beginning of the next lesson. So I was about 50% there. To my mind this should have moved the reticle from its initial position of 100,100 (x,y) to where I wanted, at 700, 100.

Also when I do this:

Code: Select all

if(xPosMove == yTMove)
{
	xPosMove += 2;
}
I think that is what I tried... Anyhow the reticle goes straight up!

I just don't understand why the first statement works and the others do not. Why do I have to increment xPosMove under the y position statements in order to get the reticle to move? Why wouldn't the reticle move under x statements instead? I didn't get very far into lesson 8, so perhaps Chili explains this, but I would really like to understand what's going on here.

I know its a logic error, an error in my thinking. That somehow my thought process, of getting the reticle to travel from point A to B along a certain axis, x for x and y for y, is wrong for some reason. I want to identify this error and correct my thinking. I want to get inside the head of the computer and see things from its perspective, in a way I can understand it. Could someone help me out here?

Thanks!
Last edited by DreamBliss on May 17th, 2013, 11:46 pm, edited 1 time in total.
You create reality in your mind,
Before experiencing it as reality,
So if you want to change something,
First change what you think about it.

User avatar
DreamBliss
Posts: 30
Joined: March 27th, 2013, 11:48 pm
Location: Lost on the Way
Contact:

Re: Need help understanding lesson 7-8 moving reticle code

Post by DreamBliss » May 17th, 2013, 11:43 pm

Well it got worse, not better. I am about 30 minutes into lesson 8, and now Chili has us switching the if statements around to fix the corner issue. Attached is a picture that should clearly depict how it seems the code is operating to my mind. The way I see it none of the statements should execute because none of them are true. I'm missing something here, I hope someone can help me. I need to understand this.

I would really appreciate it!
Attachments
wtf.jpg
This is how I see the code working now...
wtf.jpg (72.43 KiB) Viewed 3381 times
You create reality in your mind,
Before experiencing it as reality,
So if you want to change something,
First change what you think about it.

User avatar
DreamBliss
Posts: 30
Joined: March 27th, 2013, 11:48 pm
Location: Lost on the Way
Contact:

Hey I still need help with this!

Post by DreamBliss » May 19th, 2013, 12:40 am

So I re-worked my code, made it as clear as I could, tried to stick within the bounds of what Chili has taught so far (but I am fast approaching my maximum skill level.) Using my solution I still can not get the cursor to move, and I can see no reason why it does not do so. Please look at my attached solution and help me understand what I am missing here.

Thank you!
Attachments
Lesson 8.zip
(34.59 KiB) Downloaded 171 times
You create reality in your mind,
Before experiencing it as reality,
So if you want to change something,
First change what you think about it.

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

Re: Need help understanding lesson 7-8 moving reticle code

Post by albinopapa » May 22nd, 2013, 4:22 am

First thing I found when debugging is you are asking if itemLocX "100" is equal to pointA "100" and if itemLocX "100" is not equal to pointAb "700" then move itemLocX to right 2. So if they match it moves 2 pixels to the right. Now itemLocX "102" and pointA "100" aren't going to match so it skips adding and continues to the else if statement. Which would read "Is itemLocX "102" the same as pointBc "100" and is itemLocX "102" not equal to pointDa "500"? ". 102 is not equal to 100 so right there it skips that move as well.
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

User avatar
DreamBliss
Posts: 30
Joined: March 27th, 2013, 11:48 pm
Location: Lost on the Way
Contact:

Re: Need help understanding lesson 7-8 moving reticle code

Post by DreamBliss » May 23rd, 2013, 3:10 am

I think you figured it out! OK, I have to think about this some more, preferably on a day when my mind is not elsewhere and I have had more sleep. But I will go over the loop again and try to figure out how to do this correctly.

Listen I really, honestly and truly appreciate your reply! I knew there had to be a logic error and I needed a fresh pair of eyes to help me see it. Thank you!
You create reality in your mind,
Before experiencing it as reality,
So if you want to change something,
First change what you think about it.

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

Re: Need help understanding lesson 7-8 moving reticle code

Post by albinopapa » May 24th, 2013, 5:37 pm

not a problem, I didn't see the problem immediately either, I had to debug just to see how it was working.
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

User avatar
DreamBliss
Posts: 30
Joined: March 27th, 2013, 11:48 pm
Location: Lost on the Way
Contact:

Re: Need help understanding lesson 7-8 moving reticle code

Post by DreamBliss » May 25th, 2013, 10:24 pm

OK, so I think I understand this now, let me know what you think.

Chili uses the X axis to move Y and the Y axis to move X because it is very difficult, with what he has taught us so far, to move along the same axis for which you are checking. I don't know how to make that statement easier to understand. Just sort of occurred to me last night.

Anyhow I changed my code to this:

Code: Select all

// Start moving reticle code

	if(itemLocX <= pointAb)
	{
		itemLocX += 2;
	}

	if(itemLocY <= pointBc)
	{
		itemLocY += 2;
	}

	if(itemLocX >= pointDa)
	{
		itemLocX -= 2;
	}

	if(itemLocY >= pointA)
	{
		itemLocY -= 2;
	}

	DrawReticle(itemLocX, itemLocY, 255, 255, 255);

	// End moving reticle code
Now it stops at what I assume to be 500 pixels. Will try debug to see what the next issue is. But I feel I have learned what I needed to here. Got it moving along the same axis for which I am checking. Understand the logic error I made. Time to move on!

Maybe I'll come back to this someday with an elegant and beautiful solution.
You create reality in your mind,
Before experiencing it as reality,
So if you want to change something,
First change what you think about it.

Post Reply