First i fill a Matrix A with some data according like described below, each object has its own line of data.
[[x,y,angle,i/o,force]]->Mat A (you need to add as many [..] lines as there are objects)
In this matrix de first two positions describe the x an y coordinate, For an angle I use fractions of PI() (UnitCircle)
when i want to move the object i use a code like
-----------------------------------------------------
{7,5}-> Dim Mat A
Lbl 1
For 1->I to 7 Step 1
Mat A[I,1]+cos (Mat A[I,3]*PI)->Mat A [I,1]
Mat A[I,2]+sin (Mat A[I,3]*PI)->Mat A [I,2]
PlotOn Mat A[I,1],Mat A[I,2]
Next
Goto 1
----------------------------------------------------
Now the objects (dots) start moving along their angle
Eventually the objects will disappear from the screen, and this can be good, but i want them to bounce within a box.
So we create a box,
---------------------------------------------------
Lbl 0
Cls
F-Line 1,63,100,63
F-Line 1,63,1,1
F-Line 1,1,100,1
F-Line 100,1,100,63
StoPict 1
---------------------------------------------------
Now we have a visual box but the dots do not see it, so we need to check whether there is a border.
Because i want the dots to bounce and no assimilate into the border before reflecting, i make the actual box one dot smaller than the visible box.
For every I in Lbl 1 i now need to check wether its X an Y are within the box.
Mat A[I,1]<2=>1-Mat A[I,3]->Mat A[I,3]
Mat A[I,1]>99=>1-Mat A[I,3]->Mat A[I,3]
Mat A[I,2]<2=>2-Mat A[I,3]->Mat A[I,3]
Mat A[I,2]>62=>2-Mat A[I,3]->Mat A[I,3]
With four sides it is not necessary to put the values into a matrix, but when you have more then four sides it can pay of to put the min and max X and Y values in a matrix and count with another counter J inside the I counter. (see the collision section further)
The code in Lbl 1 becomes,
----------------------------------------------
{7,5}-> Dim Mat A
Lbl 1
For 1->I to 7 Step 1
'Reflection
Mat A[I,1]<2=>1-Mat A[I,3]->Mat A[I,3]
Mat A[I,1]>99=>1-Mat A[I,3]->Mat A[I,3]
Mat A[I,2]<2=>2-Mat A[I,3]->Mat A[I,3]
Mat A[I,2]>62=>2-Mat A[I,3]->Mat A[I,3]
'Movement
Mat A[I,1]+cos (Mat A[I,3]*PI)->Mat A [I,1]
Mat A[I,2]+sin (Mat A[I,3]*PI)->Mat A [I,2]
'Drawdot
PlotOn Mat A[I,1],Mat A[I,2]
Next
Goto 1
----------------------------------------------
We now have bouncing dots but they do not interact so we have to detect if they collide. We have every X and Y of every particle, to check if they are near each other we say that if the distance between two dots is less the 2 dots they collide. I will therefore have to check each object relative to another (X1-X2 and Y1-Y2) and evaluate if they are near each other.
'ColissionDetection
For 1->I To 7 Step 1
For 1->J To 7 Step 1
If I<>J
Then
Mat A[I,1]-Mat A[J,1]->A
Mat A[I,2]-Mat A[J,2]->B
If Abs A=<2 And Abs B=<2
Then Goto 2
IfEnd
IfEnd
Next
Next
We can integrate this also into Lbl 1 (without the I counter because its there already) (i also Add Lbl 0)
----------------------------------------------
{7,5}-> Dim Mat A
Lbl 0
Cls
F-Line 1,63,100,63
F-Line 1,63,1,1
F-Line 1,1,100,1
F-Line 100,1,100,63
StoPict 1
Lbl 1
RclPict 1
For 1->I to 7 Step 1
'ClearParticle
PlotOff Mat A[I,1],Mat A[I,2]
'Reflection
Mat A[I,1]<2=>1-Mat A[I,3]->Mat A[I,3]
Mat A[I,1]>99=>1-Mat A[I,3]->Mat A[I,3]
Mat A[I,2]<2=>2-Mat A[I,3]->Mat A[I,3]
Mat A[I,2]>62=>2-Mat A[I,3]->Mat A[I,3]
'Movement
Mat A[I,1]+cos (Mat A[I,3]*PI)->Mat A [I,1]
Mat A[I,2]+sin (Mat A[I,3]*PI)->Mat A [I,2]
'Drawdot
PlotOn Mat A[I,1],Mat A[I,2]
'ColissionDetection
For 1->J To 7 Step 1
If I<>J ‘prevention of selfdetection
Then
Mat [I,1]-Mat A[J,1]->A
Mat A[I,2]-Mat A[J,2]->B
If Abs A=<2 And Abs B=<2
Then Goto 2
IfEnd
IfEnd
Next
Next
Goto 1
----------------------------------------------
Adding a 'PlotOff Mat A[I,1],Mat A[I,2]' Before the movement section make the dot appear as dot and not as a line.
Now in Label 2 we need to determine what kind of collision we are dealing with. In a head on collision the direction of particle I is directed to particle J and particle I will get reversed (force will be evaluated later on). In a head on collision the angle of impact and the angle of the impact-plane to the center of ball J are each others mirror. (-><- = <- ->)
If the impact angle (Mat A[I,3]) differs from the mirrored impact-plane-angle the particles will each travel with a fraction of 0,25 PI minus or plus the impact direction (0,25PI is 45 degrees, the particles travel like a Y-shape)
When particle I hits particle J head on:
Mat A[I,3]->Mat A[J,3]
Mat A[I,3]+1->Mat A[I,3]
When Particle I hits particle J on the top:
Mat A[I,3]-0.25->Mat A[J,3]
Mat A[I,3]+0.25->Mat A[I,3]
When Particle I hits particle J on the bottom:
Mat A[I,3]+0.25->Mat A[J,3]
Mat A[I,3]-0.25->Mat A[I,3]
But we still do not know where the particles hit each other, let alone the impact-plane-angle. But with A and B from the impact detection and the X and Y coordinate of particle J we can derive what angle it could have been.
Stay tuned
but be warned the next part will be a bit.....difficult