B4J Tutorial Bouncing a single ray inside an equilateral triangle (100 simultaneous rays in Post #2)

A bit more tricky than what I thought but then my math is a bit rusted. First challenge is to figure out if the moving point has breached the perimeter of the triangle (from inside to outside) and then correct for it and then bounce/reflect it correctly.

For the attached project the start angle is 135 degrees from the centre point of the triangle. It is moving 1 vector length at a time.

Starting point:
    Dim startAngle As Double = 135

Making use of Barycentric coordinates to determine if the moving point is inside or outside the triangle.

First 10 bounces

1.png


Many bounces later.
2.png


Change the "Sleep" at the end of "Sub draw" to Sleep(0) to speed up the drawing.
B4X:
Sleep (0)

The code can in all probability be massively improved but this is what my small brain came up with for now.
 

Attachments

  • b4jTriangleBounce.zip
    3.2 KB · Views: 49
Last edited:

Johan Schoeman

Expert
Licensed User
Longtime User
100 simultaneous rays
Change this line of code to increase/decrease the number or rays
B4X:
numrays = 100

Change this if you want to do more than 500 simultaneous rays

B4X:
    Dim pos(500) As PVector
    Dim speed(500) As PVector
    Dim oldPos(500) As PVector
    
    Dim jump As Int
    
    Dim slope(500) As Double
    Dim startAngle(500) As Double
    Dim outAngle(500) As Double

3.png
 

Attachments

  • b4jTriangleBounce.zip
    3.3 KB · Views: 34
Top