Helloooo
I am so glad to have found these videos. It's really the best way to learn cpp I found so far.
it's really entertaining to learn programming like this.
Would love to understand a few lines from the number8 solution.
I managed to complete the bouncing exercise by just adding a condition if poo0x is bigger or equal to screenwidth-poowidth. PooVelocity will be - 1. Else if is smaller or equal to 0. Velocity will be 1.
It was working. But when I try to create and replace that code for a function void called bouncex, passing the 3 variables. X, width and velocity.
It won't bounce. It will just pass by from right edge and start from left edge(if debugging on release) .
poo0x = bouncex(poo0x, poowidth, Vpoo0x);
And the function code is.
Bouncex(int x, int width, int v) {
If (x >= gfx.Screenwidth - width) v=-1;
Else if (x <= 0) v=1;
}
So when I used the function, the poo doesn't bounce but when I just put the code in updateModel without function it will work... Of course I just tried with the x bouncing of the first poo.
It's not the most elegant way.
Any idea?
Anyway in the 8 solution I saw one line inside the scopes
Right after the statement if oldpoo0x != poo0x
This line poo0xv = -poo0xv; i don't really understand.
How can it switch from negative to positive in case it will bounce on the left edge? Is that used just to switch signs?
What if the x or y movement would start to the left instead.. That line would work? Is that line used to invert the value?
Thanks in advance I hope someone can give me a hand to understand.
Help understanding a line in 8s solutions please.
-
- Posts: 3
- Joined: October 13th, 2019, 2:31 pm
-
- Posts: 4373
- Joined: February 28th, 2013, 3:23 am
- Location: Oklahoma, United States
Re: Help understanding a line in 8s solutions please.
Without seeing the actual code you have, we won't be able to fully understand what your issue is. However, there are some things that don't make sense in your post.
int x = BounceX( poo0x, poo0Width, poo0VelX ) shouldn't work because you can't assign void to anything, so I'm assuming the BounceX function doesn't actually return void.
You'd need to take the parameter by reference:
Passing by reference means whatever you do to the parameters, it affects the variables you pass to the function. Notice that width isn't passed by (&) reference. Because we aren't changing it, we can just make a copy.
If velX is -1, then velX = -velX or velX = -(-1) means velX is now +1.
Negating a negative makes it positive, negating a positive makes it negative.
It's like
1 * (-1) = -1
-1 * (-1) = 1
Functions that have a return value of void, don't actually return a value, so something like:It was working. But when I try to create and replace that code for a function void called bouncex, passing the 3 variables. X, width and velocity.
int x = BounceX( poo0x, poo0Width, poo0VelX ) shouldn't work because you can't assign void to anything, so I'm assuming the BounceX function doesn't actually return void.
You aren't returning anything and the values you change ( v = 1 or v = -1 ) only change the local parameter 'v' not the poo's velocity.Bouncex(int x, int width, int v) {
If (x >= gfx.Screenwidth - width) v=-1;
Else if (x <= 0) v=1;
}
You'd need to take the parameter by reference:
Code: Select all
void BounceX( int& posX, int width, int& velX )
{
if( posX >= 0 && posX + width < Graphics::ScreenWidth )
return; // Return early if posX is not near edge of screen
// If we reach this point, posX is near an edge and needs to be redirected regardless of
// which side it hit
velX = -velX;
// Since we know that posX is near either left or right edge,
// only need to test for one side or the other
if( posX < 0 )
posX = 0;
else
posX = Graphics::ScreenWidth - width;
}
So, if velX is 1, then velX = -velX or velX = -(1) means velX is now -1.This line poo0xv = -poo0xv; i don't really understand.
How can it switch from negative to positive in case it will bounce on the left edge? Is that used just to switch signs?
If velX is -1, then velX = -velX or velX = -(-1) means velX is now +1.
Negating a negative makes it positive, negating a positive makes it negative.
It's like
1 * (-1) = -1
-1 * (-1) = 1
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
-
- Posts: 3
- Joined: October 13th, 2019, 2:31 pm
Re: Help understanding a line in 8s solutions please.
Sorry my bad. Wrote it by memory because I was from the phone and forgot.
Now I remmver i wrote
Bouncexfunction.... Using the variables from the class game.
And then I did this line.
poo0x += Vpoo0x;
Thinking the Vpoo0x will get the new value based on the x and width.
You were totally right that didn't Make sense.
So if I pass by reference should work.
But would like to know why needs to be done by pass by reference? Is because only during the function scope that variable will have the new value? Once function finishes what happens with the variable v or the passed one vpoo0x?
So this is a good example. To use pass by reference.
Wouldn't it be enough to pass just the reference of vpoo0x? Because I saw that you didn't pass the width for some specific reason?
Even if we still haven't learnt it yet about references is nice to know more or less how they work. .
Man thank you so much it was really helpful.
Nice to know that it's used to change the sign like multiplying. 2 negatives is one positive. Exactly.
Thanksssss. All solved.
But if you could clarify those small questions would make everything even clearer. Thank you again man.
Now I remmver i wrote
Bouncexfunction.... Using the variables from the class game.
And then I did this line.
poo0x += Vpoo0x;
Thinking the Vpoo0x will get the new value based on the x and width.
You were totally right that didn't Make sense.
So if I pass by reference should work.
But would like to know why needs to be done by pass by reference? Is because only during the function scope that variable will have the new value? Once function finishes what happens with the variable v or the passed one vpoo0x?
So this is a good example. To use pass by reference.
Wouldn't it be enough to pass just the reference of vpoo0x? Because I saw that you didn't pass the width for some specific reason?
Even if we still haven't learnt it yet about references is nice to know more or less how they work. .
Man thank you so much it was really helpful.
Nice to know that it's used to change the sign like multiplying. 2 negatives is one positive. Exactly.
Thanksssss. All solved.
But if you could clarify those small questions would make everything even clearer. Thank you again man.
-
- Posts: 4373
- Joined: February 28th, 2013, 3:23 am
- Location: Oklahoma, United States
Re: Help understanding a line in 8s solutions please.
As stated, if you don't pass by reference, you are making a copy. If I take a document and make a copy using a photo copier, I can scribble all over the copy and the original is not affected, same concept.why needs to be done by pass by reference?
Local variables and function parameters are pushed onto a stack when the function is called. When the function or scope ends, those variables are destroyed by popping them off the stack creating more room for the next local variable or function parameter called.what happens with the variable v or the passed one vpoo0x?
The BounceX function needs to clamp the x position so you definitely need to pass that by reference. The velocity gets negated, so that needs to be passed by reference. You might look again at what I posted, I did indeed pass width, it's the second parameter.Wouldn't it be enough to pass just the reference of vpoo0x? Because I saw that you didn't pass the width for some specific reason?
void BounceX( int& posX, int width, int& velX )
I didn't pass width by reference because it doesn't need to be changed during the lifetime of that function call. For user defined types ( classes or structs ) that are more than a few bytes in size, you'd normally pass by constant reference if the object isn't going to be changed. Since int is only 4 bytes, making a copy is just as fast as passing by reference or constant reference.
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
-
- Posts: 3
- Joined: October 13th, 2019, 2:31 pm
Re: Help understanding a line in 8s solutions please.
Yes sorry. You passed width... Sorry I didn't specify by reference... but I meant passing by reference.
Now I understand why you didn't. Thank you for explaining, your time and help, mate .
Now I understand why you didn't. Thank you for explaining, your time and help, mate .
-
- Posts: 4373
- Joined: February 28th, 2013, 3:23 am
- Location: Oklahoma, United States
Re: Help understanding a line in 8s solutions please.
welcome, hope it helped.
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