# Moving Ball in Labview

Joined Feb 16, 2014
2
Hi

I need some help solving my HW problem.

I need to write a program in Labview where a ball is surrounded by four wall (XY with value 0-100). When the ball hit the wall it bounces back.

I started with a case structure and with a fixed XY position (say x=10,y=80) for simplicity. The x value will increase each time until it reaches 100. Once it reaches 100 it will start decreasing. But the problem is when it become 100 it is going to the true case structure and value is decrease by 1 (x value then 99). But again it goes to false case (as x<100) and value increased by one (x=100). So the ball bounces on the wall not moving forward.

How to overcome this problem. Attach is the snapshot of the block diagram.

Thanks.

#### Attachments

• Niranjana

#### shteii01

Joined Feb 19, 2010
4,644
Use a second variable to keep track of value of x. You start with x is zero, second variable is also 0. Once x is 100, set second variable to 1. So now you "do stuff" to x, but second variable is 1 so you know you should be decreasing x and ignore any increases to x. I guess you need to get x back to zero, so when x gets back to zero, you change second variable from 1 to 0, this tells you that you need to increase x and ignore any attempts to decrease x.

#### djsfantasi

Joined Apr 11, 2010
8,912
What you need is not a case structure, but another vector indicating direction of the bouncing ball whose sign (ie direction on each axis) is toggled upon a wall collision (eg, the location variable becomes 100 (or 0 or other border conditions.)

To simplify the example, let's consider a line. X starts at 1 and is going to move to the left until it gets to 100. Then it will return to 0.

We need another scalar variable. For fun, let's call it dX. It starts as one. The next value of x = x + dX. Now what happens to dX as long as x is <100? And what happens to it when x =100? NOW what happens to dX when 0 < x < 100? Or when x becomes zero?

If you play around with my experiment, you should see what happens in 2D.

From there, easy peasy.

For example, what happens if the balls don't move at a 45 angle to the walls?

Joined Feb 16, 2014
2
Use a second variable to keep track of value of x. You start with x is zero, second variable is also 0. Once x is 100, set second variable to 1. So now you "do stuff" to x, but second variable is 1 so you know you should be decreasing x and ignore any increases to x. I guess you need to get x back to zero, so when x gets back to zero, you change second variable from 1 to 0, this tells you that you need to increase x and ignore any attempts to decrease x.
The issue is say i introduce a 2nd variable(say z). so when x=100, it will set z=1. then we decrease the value of x and it became 99. So x=100 will be false and it will set z value to 0. Means same problem like before. It is easy in some programming language but in Labview it is quite a bit different.

I am using select function to compare the 2nd variable with x. so if x!=100 it will set that value of Z to 0 just after it changes from 100 to 99.

#### WBahn

Joined Mar 31, 2012
28,504
Look at it from a basic physics standpoint. You have a position and velocity. At each time increment you update the position using the current position and the velocity. When a wall is encountered, you don't change the position, you change the velocity. You ONLY change the velocity when a wall is encountered.