Homework #4 Solution Questions

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

Homework #4 Solution Questions

Post by Ziltwix » January 15th, 2017, 5:08 pm

Hey Chillz! Hello everyone! Excellent video my friend! I have a few questions in the hopes of clarifying my confusion:

First, when you delcared vx and vy in Game.h, is that what allows our reticle to "move" across the screen by only tapping it? Is this accomplished because of the x = x + xv function?

This y = y + xv; Does this function effect the verticality of how the reticle "moves" almost similar to the above function?

We declare the inhibitor variable in Game.h so it will not die in UpdateModel(), and also, why do we set these boolean inhibitor directions as false? (bool inhibitLeft = false;)

I am having difficulty understanding the nesting when we use the inhibtor function.

Code: Select all

if(wnd.kbd.KeyIsPressed(VK_UP))
{
    if(inhibitUp)
    {
    }
    else
    {
        vy = vy - 1;
        inhibitUp = true;
    }
}
else
{
    inhibitUp = false;
}
This line of code for example is what tripped me up when understanding why the reticle reacts the way it it does.

Thank you all for taking your time to answer my questions! :D

Edit by albinopapa: Added

Code: Select all

[code]
[/code] tags around the code and formatted it in order to make it more readable.

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

Re: Homework #4 Solution Questions

Post by albinopapa » January 16th, 2017, 12:37 am

You should play around with the vars to get a better understanding of the "WHY", the vars are used and set to true/false. Maybe comment out the inhibitUp = false line and see what happens.

Yeah, keeping the variables in the Game.h makes them stay alive as long as the Game object is alive. This is one reason why you have to set true or false, because on the next frame the value would remain the same and certain code paths wouldn't run. Use the debugger and see if you can tell what is happening.

Still confused after doing those suggestions? Come back and let us know, will try explaining a little better.
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
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Homework #4 Solution Questions

Post by chili » January 16th, 2017, 1:23 am

Ziltwix wrote:Hey Chillz! Hello everyone! Excellent video my friend! I have a few questions in the hopes of clarifying my confusion:

First, when you delcared vx and vy in Game.h, is that what allows our reticle to "move" across the screen by only tapping it? Is this accomplished because of the x = x + xv function?
Yup, vx/vy allow us to remember the velocity (speed) of the reticle. We add these amounts to the x and y every frame, regardless of whether the keys are being pressed, and that's how we get the reticle to 'move on its own'. We control vx/vy with the keys.
Ziltwix wrote:This y = y + xv; Does this function effect the verticality of how the reticle "moves" almost similar to the above function?
Well, the y = y + vy; statement updates the y position (verticality) of the reticle every frame based on the velocity, just like the x = x + vx; does for the x position.
Ziltwix wrote:and also, why do we set these boolean inhibitor directions as false? (bool inhibitLeft = false; )
You set it as false, because you want to stop inhibiting detection of key presses when the user releases the key, so that the next time they press the key it will be detected again.

As papa suggest, try messing around with the code, commenting out that line etc. and seeing what happens. If you still have questions, come back here and type empty out your thoughts in text form and we'll try and help you out.
Chili

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

Re: Homework #4 Solution Questions

Post by Ziltwix » January 16th, 2017, 3:55 pm

Thank you gentlemen! I will be back later to tell you what I have noticed!

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

Re: Homework #4 Solution Questions

Post by Ziltwix » January 18th, 2017, 2:00 am

Good Evening, Morning or anything else, gentlemen!

So I ran through the code for the solution of the homework and I have comments alongside to see if I'm on the right track.

Here it is:

Code: Select all

if(wnd.kbd.KeyIsPressed(VK_UP))
{ 
    if(inhibitUp)
    {
    }
    // Changing inhibitUp's boolean value to false will change nothing to the velocity of the reticle 
    // and make it "move" at the rate before the nested if function was called.
    else
    {
        vy = vy - 1;
        inhibitUp = true;
    }
}

//Getting rid of the code from "else" to "}" will make it difficult for the reticle to "move" upwards. 
//Changing the inhibitor boolean function below to true will negate upward movement.

else
{
    inhibitUp = false; 
}
That is essentially what I figured out by fiddling with the code. I apologize if I made any terminology errors. I am going to run through the previous videos to see if I can try to delve a bit deeper as to why things work. It helps build character, haha.

Let me know if I am on to something, gentlemen. I can almost see why if you make one error or completely neglect a piece of code it changes the way things play out immensely.

One last question gentlemen. Why is nesting logic so important? It could be on a grand scale or in this case, an if/else statement.

Thank you all once again for the help! :D

Edited by albinopapa for formatting

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

Re: Homework #4 Solution Questions

Post by albinopapa » January 18th, 2017, 3:29 am

So given the code above, your first frame you probably won't be pressing UP, so the inhibitUP gets set to false, because wnd.kbd.IsPressed(VK_UP) is false so the else{ inhibitUP = false; } runs.

On the second frame, if you were to press UP, then wnd.kbd.IsPressed(VK_UP) would be true, so it moves inside the first if() block. It checks the inhibitUP value, and since it's false, executes the
else{ vy = vy - 1; inhibitUp = true; } branch.

On the third frame, you'd probably still be holding the UP button, unless you can press and release the key in 1/60th of a second. So again, it enters the first if() block. Then it checks the inhibitUP value, it's now true so it executes...wait...it's empty so nothing to do and it leaves all if blocks thinking it's done exactly what you wanted it to do.

So you will get 1 and only 1 frame of update per button press/release pair using the code above.
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

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

Re: Homework #4 Solution Questions

Post by Ziltwix » January 23rd, 2017, 4:46 pm

Thank you, Albino! I appreciate the response.

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

Re: Homework #4 Solution Questions

Post by albinopapa » January 23rd, 2017, 8:25 pm

You're welcome, hopefully it was helpful.
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