This is main difference in code between Container #4 and Container #5
Container #5 has these fixes:
First IF detects if ball is stuck, same place it was in last loop.
Then it detects if ball is collided with 1 line or more than one.
If one line, stick with the plan it will work it's way out, notice same code is used as if it weren't stuck.
If more than one line we have the problem of hitting 2 lines simultaneously or nearly so, so I took the average of the normals of the 2 lines. This calc is not perfect so I compare it to the angle of the ball to the center of the container if the calc is off by more than 90 degrees I head the ball to the center of the container and issue a message to that effect with the message box.
With the message box, I could see that the calc ave normal is wrong and choosing to head ball to center is smart way out of getting stuck.
Looking at screen shot, I see I was counting normal reflections off wall, the times where I had to use an average of normal angles because more than one line was intersected and the number of times I had to fix the average because it was pointed out not in.
Container #5 has these fixes:
Code: (Select All)
If Abs(bx - saveBx) > 1 Or Abs(by - saveBy) > 1 Then ' Ball is moving right along
diff = Boundaries(saveL).dN - ba + 180
ba = Boundaries(saveL).dN + diff ' >>>> new direction
delayT = 1: nr = nr + 1
Else ' could be stuck
If totLinesHit = 1 Then
diff = Boundaries(saveL).dN - ba + 180
ba = Boundaries(saveL).dN + diff ' >>>> new direction
delayT = 1: nr = nr + 1
ElseIf totLinesHit > 1 Then
'If totLinesHit > 1 Then
' new 2022-10-21 fix angle to average of normals hit totN is total or all Normals / total lines hit
' new ball direction is average of normals
ba = totN / totLinesHit ' >>>>>>>>>>>>>>>>>>>>>>> new direction from line segment perpendicular angle
delayT = 3: Beep: anr = anr + 1
If Abs(ba - DAtan2(bx, by, x0, y0)) > 90 And Abs(ba - 360 - DAtan2(bx, by, x0, y0)) > 90 Then
Beep ' indicate by sound that the alternate angle for ball was used
mBox "Multiple Line hits", "Ave of norms looks wrong:" + Str$(totN / totLinesHit) + ", fixed using:" + Str$(DAtan2(bx, by, x0, y0))
ba = DAtan2(bx, by, x0, y0)
fixr = fixr + 1
End If
End If
End If
First IF detects if ball is stuck, same place it was in last loop.
Then it detects if ball is collided with 1 line or more than one.
If one line, stick with the plan it will work it's way out, notice same code is used as if it weren't stuck.
If more than one line we have the problem of hitting 2 lines simultaneously or nearly so, so I took the average of the normals of the 2 lines. This calc is not perfect so I compare it to the angle of the ball to the center of the container if the calc is off by more than 90 degrees I head the ball to the center of the container and issue a message to that effect with the message box.
With the message box, I could see that the calc ave normal is wrong and choosing to head ball to center is smart way out of getting stuck.
Looking at screen shot, I see I was counting normal reflections off wall, the times where I had to use an average of normal angles because more than one line was intersected and the number of times I had to fix the average because it was pointed out not in.
b = b + ...