# Thread: Direct X really basic help.

1. ## Direct X really basic help.

Ok so I haven't been able to hit this book as much as I wanted to ( Final month or so of high school), I've had to do a lot of stuff the last few weeks. I was doing an example of this book. and I stumble upon this

Code:
```	//move the ball
ptBallPosition.x+=ptBallVelocity.x;
ptBallPosition.y+=ptBallVelocity.y;

//bounds checking
//left side
if(ptBallPosition.x<=0) ptBallVelocity.x=abs(ptBallVelocity.x);
//top side
if(ptBallPosition.y<=0) ptBallVelocity.y=abs(ptBallVelocity.y);
//right side
if(ptBallPosition.x>=(int)dwDisplayWidth-gdicBall.GetWidth()) ptBallVelocity.x=-abs(ptBallVelocity.x);
//bottom side
if(ptBallPosition.y>=(int)dwDisplayHeight-gdicBall.GetHeight()) ptBallVelocity.y=-abs(ptBallVelocity.y);```

I am not really sure what's going in on here with the bounds checking this "abs" business I dont' really know what that is, could someone please clarify this for me? Thanks.

2. abs() returns an absolute value.......negative becomes positive......floating points dissapear..

3. Thanks Fordy, this book keeps introducing things assuming that you already know it.........I just realized something I hate Microsoft at times but I also like Windows very much.

4. Code:
```	ptBallPosition.x+=ptBallVelocity.x;
ptBallPosition.y+=ptBallVelocity.y;

//bounds checking
//left side
if(ptBallPosition.x<=0) ptBallVelocity.x=abs(ptBallVelocity.x);
//top side
if(ptBallPosition.y<=0) ptBallVelocity.y=abs(ptBallVelocity.y);
//right side
if(ptBallPosition.x>=(int)dwDisplayWidth-gdicBall.GetWidth()) ptBallVelocity.x=-abs(ptBallVelocity.x);
//bottom side
if(ptBallPosition.y>=(int)dwDisplayHeight-gdicBall.GetHeight()) ptBallVelocity.y=-abs(ptBallVelocity.y);```
Could you please give me walk through of this right here.......
if(ptBallPosition.x<=0) ptBallVelocity.x=abs(ptBallVelocity.x);

this is what I am thinking........if the X coordinate of the Ball is <=0 then something happens but I don't really know what the velocity has to do with the position of the ball, if you need to see the whole code let me know so I can post it. Thanks in advance.

5. E,
Check out the initialization code. ptBallVelocity.x and .y are each
preset to hold the value 1. During each loop ptBallPosition is
updated by ptBallVelocity. So if ptBallVelocity was -3 and
ptBallPosition's last .x coord was 2 then it would now be -1
(2+(-3)=-1).

If the ball (ptBallPosition) has reached the leftmost point of the
screen (0 or less) then we need to redirect it to the right. To do
this we get the absolute value of ptBallVelocity (in the case of my
example here that would be abs(-1)=1). Then we reset the value
of ptBallVelocity.x to this.
Code:
```//if ptBallPosition.x is equal to or less than zero
if(ptBallPosition.x<=0) ptBallVelocity.x=abs(ptBallVelocity.x);
//if(-1<=0) ptBallVelocity.x=abs(-1);
//ptBallVelocity=1;```
Now (until we reach the rightmost point of the screen) when we
incriment ptBallPosition.x by ptBallVelocity.x it will go to the right.
Code:
`ptBallPosition.x+=ptBallVelocity.x;`
ptBallVelocity.x and .y are just the amount to move the ball
up/down/left/right each iteration of the loop. Change your init
code to set them at a higher number and it'll do the same thing,
just moving the ball faster.

Hope this helps. If not, PM me.

6. Does having a negative on the velocity change the direction on which the ball is moving? Because I figured this would make it go to the opposite side in which the ball is moving if you made it a negative.

7. Originally posted by elchulo2002
Does having a negative on the velocity change the direction on which the ball is moving? Because I figured this would make it go to the opposite side in which the ball is moving if you made it a negative.
Yup.....I'm working on something based on this principle at the moment.....

Code:
```rect1.left += lVolX1;//move a rectangle
rect1.top += lVolY1;
rect1.right += lVolX1;
rect1.bottom += lVolY1;

if(rect1.left <= 0 || rect1.right >= cx)
lVolX1 = -lVolX1;

if(rect1.top <= 0 || rect1.bottom >= cy)
lVolY1 = -lVolY1;```
Each time this code is run, the rect moves across the screen....if it touches any edge of the screen the number that its being incremented by has its signing swapped...this sends it in the opposite direction........