LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Particle in a Box

Hello everyone,

 

I'm very new to LabVIEW and programming languages in general. In our class, we have been asked to create a program that describes the motion of a particle in a 2D box. That particle in to be subjected to user selected forces, acceralerations, etc. The problem is, our instructor pretty much everyone for us to do and didn't really explain how to anything. 

 

For the simple case of a particle moving around in a box, I understant the logic behind it. Just give a particle an initial position. To that initial position, add the displacement that it occurs after time dt, by the linear equation, r(j+1)=r(j)+vdt. Then if the particle reaches the boundrym, reflect the corresponding component of the velocity. Can someone just please explain to me how I would go about putting any of this down?  

 

I've attached a copy of all that I have. Am i going in  the right direction or am I completely wrong?

 

Thank you,

Javier

0 Kudos
Message 1 of 4
(4,015 Views)

Have a look at the pong game implemented here. It should give you some ideas. (I have not looked at your code yet)

0 Kudos
Message 2 of 4
(4,002 Views)

Javier,

 

Welcome to the Forum (and to LabVIEW and to programming).

 

Thank you for mentioning that this is a class assignment. We will not do your work for you but are glad to help you understand how to use the tools (LabVIEW) to do your assignment.

 

You have made a good start. The most important part of any program is to design it before you start writing code. You have done part of that by specifying the equation which defines the behavior.  You have apparently made some other implicit assumptions: the use of x and y coordinates for the particle position (implied by the choice of XY graph). You have not allowed for acceleration other than in the +y direction. Does gravity pull up in your universe? By using the loop iteration terminal "i" as dt you have defined dt = 1. This is fine for simulation purposes. If you want to scale to "real world" behavior, you may also need to scale dt.

 

It is very helpful to document your choices and assumptions. The use of free labels on the block diagram can minimize confusion (as in "Why did I do that?!?!?). A note stating that the two element arrays represent the x and y components of position and velocity at time t = i*dt would assure that someone else did not interpret the data as y and x or r and theta.

 

Another option is to use complex numbers to represent position, velocity, and acceleration. That provides some conveniences in calculations and plotting but is by no means necessary.

 

You need two more things to get your VI started. First is completion of the position and velocity feedback. For each iteration of the loop you need to put the updated values into the right sides of the shift registers.  Second is to accumulate a history of the particle positions and velocities (and accelerations?) for plotting purposes.  The graph requires an array.  The most obvious choice is to use Build Array. That works but LV requires that all the elements of an array be stored in contiguous locations in memory. So an array that grows in size requires frequent re-allocations of memory. If the arrays get large, memory fragmententation may result in memory errors long before the arrays become larger than the total available memory. So it is better to Initialize the arrays outside the loop and to use Replace Array Subset inside to update the data in the array.

 

Lynn

Message 3 of 4
(3,988 Views)

You probably had time to implement some of Lynn's idea.

 

You need to be very careful with the "acceleration", because it can get quite complicated if the speed gets larger than the box (i.e. if you need to calculate several reflections per step). A more sane implementation would be to start fast and implement some sort of friction where the speeds gets reduces slightly with each step (e.g. multiplied by 0.995).

 

Here's a very quick modification of my old pong algorithm quoted above. Maybe it can give you some more ideas. Yes, using a complex scalar for speed and position would typcally be a good idea, except that it would no longer work with the "in range" function and would probably require some kludgy code.

Message 4 of 4
(3,913 Views)