B4J Tutorial Bouncing a single ray inside a square

Sure you will figure it out. A position vector and a speed vector. When it hits a vertical wall we change the direction of the x-position vector and for a horizontal wall we change the direction of the y-position vector. Change direction => position x / y = position x / y * -1

1.png
2.png
 

Attachments

  • b4jSquareBounce.zip
    2.3 KB · Views: 160
Last edited:

Johan Schoeman

Expert
Licensed User
Longtime User
Bouncing 200 rays simultaneously inside a square from the center of the square at increasing angles of 360/numOfRays. Sure you will figure it out how to do more than 200 rays. Code DIM's 1000 and number of rays for the attached project set to 200.

B4X:
    Dim leftTop, rightTop, leftBottom, rightBottom, centre, ray(1000), speed(1000), oldPos(1000) As PVector

B4X:
numOfRays = 200                    'number of simultaneous rays to be drawn and bounced

1.png
2.png
3.png


4.png
5.png
 

Attachments

  • b4jSquareBounce.zip
    3.1 KB · Views: 117
Top