Re: Simple Raylib Example
- Posted by Icy_Viking Aug 18, 2019
- 1010 views
irv said...
Comparing two objects for collision is easy, but how to efficiently compare many objects, any two (or more) of which could be in collision, seems like it would be tricky - and require a lot of processing.
I'm sure routines to that have already been worked out, since games do this all the time.
Is Euphoria fast enough to do the job?
Well I'm sure Euphoria would be fast enough. I've already added another ball and it still runs fine. I'm sure you could keep adding balls until the program slows down significantly.
NOTE: Ignore the color changing ball for now, that was just for a test. If you want to add or improve upon it, feel free.
without warning include std/machine.e include std/convert.e include flags.e include Euraylib.ew atom w = 800, h = 450 InitWindow(w,h,"Bouncing Ball") atom ball_x = rand(GetScreenWidth()) / 2 atom ball_y = rand(GetScreenHeight()) / 2 atom ball_speed_x = 5.0 atom ball_speed_y = 4.0 atom ball_rad = 20 atom ball2_rad = 20 atom ball3_rad = 20 atom ball2_x = GetScreenWidth() / 2 atom ball2_y = GetScreenHeight() / 2 atom ball2_speed_x = 6.0 atom ball2_speed_y = 5.0 atom ball3_x = rand(GetScreenWidth()) / 2 - 3 atom ball3_y = rand(GetScreenHeight()) / 2 - 3 atom ball3_speed_x = 8.0 atom ball3_speed_y = 6.0 integer paused = 0 integer frameCounter = 0 SetTargetFPS(60) atom b = bytes_to_int({0,0,0,0}) atom g = bytes_to_int({0,255,0,255}) atom r = bytes_to_int({255,0,0,255}) atom blu = bytes_to_int({0,0,255,255}) atom whi = bytes_to_int({255,255,255,255}) atom yel = bytes_to_int({255,255,0,255}) atom pur = bytes_to_int({128,0,128,255}) sequence cols = {blu,whi,yel,pur,g,r} constant MAX_COLS = 6 while not WindowShouldClose() do if IsKeyPressed(KEY_SPACE) and paused = 0 then paused = 1 elsif IsKeyPressed(KEY_SPACE) and paused = 1 then paused = 0 end if if paused = 0 then ball_x += ball_speed_x ball_y += ball_speed_y ball2_x += ball2_speed_x ball2_y += ball2_speed_y ball3_x += ball3_speed_x ball3_y += ball3_speed_y if ((ball_x >= (GetScreenWidth() - ball_rad)) or (ball_x <= ball_rad)) then ball_speed_x *= -1.0 elsif ((ball_y >= (GetScreenHeight() - ball_rad)) or (ball_y <= ball_rad)) then ball_speed_y *= -1.0 end if if ((ball2_x >= (GetScreenWidth() - ball2_rad)) or (ball2_x <= ball2_rad)) then ball2_speed_x *= -1.0 elsif ((ball2_y >= (GetScreenHeight() - ball2_rad)) or (ball2_y <= ball2_rad)) then ball2_speed_y *= -1.0 end if if ((ball3_x >= (GetScreenWidth() - ball3_rad)) or (ball3_x <= ball3_rad)) then ball3_speed_x *= -1.0 elsif ((ball3_y >= (GetScreenHeight() - ball3_rad)) or (ball3_y <= ball3_rad)) then ball3_speed_y *= -1.0 end if if CheckCollisionCircles(ball_x,ball_y,ball_rad,ball2_x,ball2_y,ball2_rad) then ball_speed_x *= -1.0 ball_speed_y *= -1.0 ball2_speed_x *= -1.0 ball2_speed_y *= -1.0 end if if CheckCollisionCircles(ball2_x,ball2_y,ball2_rad,ball3_x,ball3_y,ball3_rad) then ball2_speed_x *= -1.0 ball2_speed_y *= -1.0 ball3_speed_x *= -1.0 ball3_speed_y *= -1.0 end if if CheckCollisionCircles(ball3_x,ball3_y,ball3_rad,ball_x,ball_y,ball_rad) then ball3_speed_x *= -1.0 ball3_speed_y *= -1.0 ball_speed_x *= -1.0 ball_speed_y *= -1.0 end if end if frameCounter += 1 BeginDrawing() ClearBackground(b) DrawCircleV(ball_x,ball_y,ball_rad,r) DrawCircleV(ball2_x,ball2_y,ball2_rad,g) DrawCircleV(ball3_x,ball3_y,ball3_rad,blu) --for i = 1 to MAX_COLS do -- DrawCircleV(10,90,20,rand(cols[i])) --end for if paused = 1 and frameCounter / 30 then DrawText("PAUSED",1,20,30,g) end if DrawFPS(1,1) EndDrawing() end while CloseWindow()