## Beginner HW 8: My solution

The Partridge Family were neither partridges nor a family. Discuss.
perpetual
Posts: 3
Joined: April 4th, 2020, 7:39 am

### Beginner HW 8: My solution

Add the velocities the p00 positions.

Code: Select all

``````    poo0_x += poo0_v_x;
poo0_y += poo0_v_y;
poo1_x += poo1_v_x;
poo1_y += poo1_v_y;
poo2_x += poo2_v_x;
poo2_y += poo2_v_y;
``````
Store poo positions for comparison

Code: Select all

``````    int poo0_temp_x = poo0_x;
int poo0_temp_y = poo0_y;
int poo1_temp_x = poo1_x;
int poo1_temp_y = poo1_y;
int poo2_temp_x = poo2_x;
int poo2_temp_y = poo2_y;
``````
Clamp the p00s to the max coordinates of the screen.

Code: Select all

``````    poo0_x = Clamp(poo0_x, 25, gfx.ScreenWidth);
poo0_y = Clamp(poo0_y, 25, gfx.ScreenHeight);
poo1_x = Clamp(poo1_x, 25, gfx.ScreenWidth);
poo1_y = Clamp(poo1_y, 25, gfx.ScreenHeight);
poo2_x = Clamp(poo2_x, 25, gfx.ScreenWidth);
poo2_y = Clamp(poo2_y, 25, gfx.ScreenHeight);
``````
Check the calculated value the clamped. if there is a difference we hit the side of the screen. if so return and set the negated Velocity, otherwise return the velocity unchanged.

Code: Select all

``````    poo0_v_x = negateBound(poo0_temp_x, poo0_x,poo0_v_x);
poo0_v_y = negateBound(poo0_temp_y, poo0_y,poo0_v_y);
poo1_v_x = negateBound(poo1_temp_x, poo1_x,poo1_v_x);
poo1_v_y = negateBound(poo1_temp_y, poo1_y,poo1_v_y);
poo2_v_x = negateBound(poo2_temp_x, poo2_x,poo2_v_x);
poo2_v_y = negateBound(poo2_temp_y, poo2_y,poo2_v_y);

``````

Code: Select all

``````int Game::negateBound(int prev, int current, int vel)
{
if (prev != current)
return -vel;
return vel;
}
``````

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

### Re: Beginner HW 8: My solution

Interesting approach.

You can even shorten the negateBound function

Code: Select all

``````int Game::negateBound(int prev, int current, int vel)
{
return ( prev == current ) ? vel : -vel;
}
``````
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