A little confusion about tutorial 8 homework solution

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
tlefacecat
Posts: 3
Joined: September 4th, 2017, 4:26 am

A little confusion about tutorial 8 homework solution

Post by tlefacecat » September 18th, 2017, 7:57 am

I am confused about the part that makes Poo bounce when it hit the edge

Code: Select all

       const int poo0XOld = poo0X;
		const int poo0YOld = poo0Y;
		poo0X = ClampScreenX(poo0X, pooWidth);
		poo0Y = ClampScreenY(poo0Y, pooHeight);
		if (poo0X != poo0XOld) poo0SpeedX = -poo0SpeedX;
		if (poo0Y != poo0YOld) poo0SpeedY = -poo0SpeedY; 
In the tutorial, Chili said, "We are going to remember the X and Y position of the Poo". I assume that he is talking about the const int poo0XOld and const int poo0YOld. My question is that "What is the value of poo0XOld at the moment that poo hit the edge? (for example the left edge)"

My assumption is that the value of the poo0XOld should be -1 when my poo0 hits the left edge.
Because in the ClampScreenX, it is stated that if x<0, this function will return 0.

Code: Select all

int Game::ClampScreenX(int x, int width)
{
	const int dudeRight = x + width;
	if (x < 0) return 0;
	if (dudeRight > gfx.ScreenWidth) return (gfx.ScreenWidth - 1) - width;
	return x;
}


So at the moment that the poo hits the left edge. The poo0X's value should be less than (something like -1) so that the code would probably work like this

My assumption: At the moment that the poo hits the left edge. The poo0X's value is -1.
const int poo0XOld = -1; // poo0X's value before using the ClampScreenX
const int poo0YOld = poo0Y;
poo0X = 0; // poo0X's value after using the ClampScreenX(poo0X, pooWidth)
poo0Y = ClampScreenY(poo0Y, pooHeight);
if (0 != -1) poo0SpeedX = -poo0SpeedX;
if (poo0Y != poo0YOld) poo0SpeedY = -poo0SpeedY;

Is my assumption correct? Or I misunderstood something. I'm very new to programming. My question might be kind of stupid but I'm really confused. Trying to figure it out for hours but still cannot come up with good explanation. Thank you in advance.

Tlefacecat

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

Re: A little confusion about tutorial 8 homework solution

Post by Yumtard » September 18th, 2017, 8:27 am

xOld and yOld gets set to x and y
we use clampscreen on x and y

if x < 0 then the clamp screen returns 0
now we compare if the xOld is equal to x (0).
In this case it is not, meaning that we've hit the wall so the velocity flips.

say x is within the screen. Then the clampscreen function will simply return x, hence xOld and x will have the same value and we wont change the velocity

tlefacecat
Posts: 3
Joined: September 4th, 2017, 4:26 am

Re: A little confusion about tutorial 8 homework solution

Post by tlefacecat » September 18th, 2017, 9:23 am

Yumtard wrote:xOld and yOld gets set to x and y
we use clampscreen on x and y

if x < 0 then the clamp screen returns 0
now we compare if the xOld is equal to x (0).
In this case it is not, meaning that we've hit the wall so the velocity flips.

say x is within the screen. Then the clampscreen function will simply return x, hence xOld and x will have the same value and we wont change the velocity
Thank you for your answer. So, in this case, it means that the xOld is storing the value that is less than 0, right?

Because I assigned

Code: Select all

 const int poo0XOld = poo0X;
and then later poo0X and poo0XOld are compared.

Code: Select all

 if (poo0X != poo0XOld) poo0SpeedX = -poo0SpeedX;
Because ClampScreen function will return 0 when x<0. So poo0X is now 0 when the poo hits the left edge. And in order for this poo0X != poo0XOld to be true, poo0XOld needs to be -1 or something, right?
I'm not sure about the value of the poo0X before it was assigned to

Code: Select all

 poo0X = ClampScreenX(poo0X, pooWidth);
Is the poo0X's value equal -1 or something before assigning it to the ClampScreen function.

Thank you in advance :D

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

Re: A little confusion about tutorial 8 homework solution

Post by Yumtard » September 18th, 2017, 10:06 am

that is correct.

Say the value of x is -1

Code: Select all

int Game::ClampScreenX(int x, int width) //we're passing -1 to the function
{
   const int dudeRight = x + width;
   if (x < 0) return 0; // x is -1 which is less than 0 so we return 0
   if (dudeRight > gfx.ScreenWidth) return (gfx.ScreenWidth - 1) - width;
   return x;
}
since we returned 0, pooX is now 0 since we assigned poox the returned value of clamp screen when we wrote

Code: Select all

 poo0X = ClampScreenX(poo0X, pooWidth);
say the poo was within the screen when this code was executed. say x = 100, then this would happen

Code: Select all

int Game::ClampScreenX(int x, int width) //we're passing 100 to the function
{
   const int dudeRight = x + width;
   if (x < 0) return 0; // x is NOT less than 0 so we WONT return 0

//dudeRight is NOT bigger than the screenWitdth, so we wont return this eiter
   if (dudeRight > gfx.ScreenWidth) return (gfx.ScreenWidth - 1) - width;

   return x; //since non of the above was true, we simply return x, which is still 100
}

So x will remain unchanged and therefore equal xOld

tlefacecat
Posts: 3
Joined: September 4th, 2017, 4:26 am

Re: A little confusion about tutorial 8 homework solution

Post by tlefacecat » September 18th, 2017, 12:07 pm

Yumtard wrote:that is correct.

Say the value of x is -1

Code: Select all

int Game::ClampScreenX(int x, int width) //we're passing -1 to the function
{
   const int dudeRight = x + width;
   if (x < 0) return 0; // x is -1 which is less than 0 so we return 0
   if (dudeRight > gfx.ScreenWidth) return (gfx.ScreenWidth - 1) - width;
   return x;
}
since we returned 0, pooX is now 0 since we assigned poox the returned value of clamp screen when we wrote

Code: Select all

 poo0X = ClampScreenX(poo0X, pooWidth);
say the poo was within the screen when this code was executed. say x = 100, then this would happen

Code: Select all

int Game::ClampScreenX(int x, int width) //we're passing 100 to the function
{
   const int dudeRight = x + width;
   if (x < 0) return 0; // x is NOT less than 0 so we WONT return 0

//dudeRight is NOT bigger than the screenWitdth, so we wont return this eiter
   if (dudeRight > gfx.ScreenWidth) return (gfx.ScreenWidth - 1) - width;

   return x; //since non of the above was true, we simply return x, which is still 100
}

So x will remain unchanged and therefore equal xOld
Thank you so much again. Your answer is so clear. Really appreciate it. :) :)

Post Reply