1. Simple Raylib Example

Hello All,

I got a little bored, so I modified the bouncing ball example. How with two bouncing balls and collision detection between them! All you need to run this example is Euphoria the Eurayib wrapper.

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 ball2_x = GetScreenWidth() / 2 
atom ball2_y = GetScreenHeight() / 2 
atom ball2_speed_x = 6.0 
atom ball2_speed_y = 5.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}) 
 
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 
		 
		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 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 
	end if 
	frameCounter += 1 
	 
	BeginDrawing() 
	 
	ClearBackground(b) 
	 
	DrawCircleV(ball_x,ball_y,ball_rad,r) 
	DrawCircleV(ball2_x,ball2_y,ball2_rad,g) 
	 
	if paused = 1  and frameCounter / 30 then 
		DrawText("PAUSED",1,20,30,g) 
	end if 
	 
	DrawFPS(1,1) 
	 
	EndDrawing() 
	 
end while 
 
CloseWindow() 
new topic     » topic index » view message » categorize

2. Re: Simple Raylib Example

For fun, I translated this to run under EuGTK in a window. Uses Cairo for the graphics:

https://openeuphoria.org/pastey/308.wc

You'll note that there is an unnatural result when the two collide. Someone want to fix that?

Two things I'd like to do:

1. Add more balls (the program is structured to make this easy)
2. Figure out how to check for collisions when more than 2 are bouncing! (not easy, I suspect!)

new topic     » goto parent     » topic index » view message » categorize

3. Re: Simple Raylib Example

irv said...

For fun, I translated this to run under EuGTK in a window. Uses Cairo for the graphics:

https://openeuphoria.org/pastey/308.wc

You'll note that there is an unnatural result when the two collide. Someone want to fix that?

Two things I'd like to do:

1. Add more balls (the program is structured to make this easy)
2. Figure out how to check for collisions when more than 2 are bouncing! (not easy, I suspect!)

That was fun to look at the GTK example. The collision detection in mine isn't perfect, but it works. It would be very easy to add more balls and maybe even make a little small little game out of it.

new topic     » goto parent     » topic index » view message » categorize

4. Re: Simple Raylib Example

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?

new topic     » goto parent     » topic index » view message » categorize

5. Re: Simple Raylib Example

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() 
 
new topic     » goto parent     » topic index » view message » categorize

6. Re: Simple Raylib Example

For testing, I changed the 60fps timer in my program to use GIdle instead (which runs as fast as possible, whenever there's a free cpu available).

I can get 1900+ fps on my computer. That's with the relatively simple movement and collision detection. So I suspect even more complicated processing would still have plenty of time, if running at a more reasonable 60 fps (which should be fast enough for anyone).

new topic     » goto parent     » topic index » view message » categorize

7. Re: Simple Raylib Example

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?

There are physics libraries that might be usable by a Euphoria program, but I expect Euphoria is fast enough for simple collision-detection jobs.

new topic     » goto parent     » topic index » view message » categorize

8. Re: Simple Raylib Example

irv said...

For testing, I changed the 60fps timer in my program to use GIdle instead (which runs as fast as possible, whenever there's a free cpu available).

I can get 1900+ fps on my computer. That's with the relatively simple movement and collision detection. So I suspect even more complicated processing would still have plenty of time, if running at a more reasonable 60 fps (which should be fast enough for anyone).

That's pretty good! Dang. I'm thinking of using EuGTK for a game app, but I don't like the idea of coding up my own game engine. If anybody has that passion, I'm sure Euphoria could handle it just fine, especially with the compiling option.

new topic     » goto parent     » topic index » view message » categorize

9. Re: Simple Raylib Example

Euphoria is more than capable for game development. I have wrapped several libraries that can aid in game development, such as in the example, Raylib. I have also wrapped SDL2,SFML2,Sigil, etc. Since those wrappers were wrapped using the C ports or were written in C, then compiling them should be fairly straight forward. As for physics, I have tried wrapping Chipmunk physics, but was having trouble.

new topic     » goto parent     » topic index » view message » categorize

10. Re: Simple Raylib Example

Hey, I get an error when I try to load texture using raylib. Anyone knows what is causing this problem?

C:\Euphoria4_1\Projects\Euraylib-master\Euraylib-master\Euraylib.ew:4250 in function gLoadTexture() 
c_proc/c_func: bad routine number (-1) 

without warning 
 
include std/machine.e 
include std/convert.e 
 
include Euraylib.ew 
--include raylibshim.e 
 
constant width = 800 
constant height = 450 
 
atom black = bytes_to_int({0,0,0,0}) 
atom white = bytes_to_int({255,255,255,255}) 
atom yellow = bytes_to_int({255,255,0,255}) 
atom blue = bytes_to_int({0,0,255,255}) 
 
InitWindow(width,height,"Simple Texture") 
 
gLoadTexture("raylib_logo.png")  
 
sequence texture_info = {3,256,256,1,0} 
 
while not WindowShouldClose() do 
 
	BeginDrawing() 
 
	ClearBackground(blue) 
	 
	DrawText("This is a Texture",1,1,20,yellow) 
	 
	DrawTexture(texture_info,10, 50,white) 
	 
	EndDrawing() 
	 
end while 
 
CloseWindow() 
new topic     » goto parent     » topic index » view message » categorize

11. Re: Simple Raylib Example

This example I wrote works and loads a texture for me.

without warning 
 
include std/machine.e 
include std/convert.e 
 
include Euraylib.ew 
--include raylibshim.e 
 
constant width = 800 
constant height = 450 
 
atom black = bytes_to_int({0,0,0,0}) 
atom white = bytes_to_int({255,255,255,255}) 
atom yellow = bytes_to_int({255,255,0,255}) 
atom blue = bytes_to_int({0,0,255,255}) 
 
InitWindow(width,height,"Simple Texture") 
 
 
while not WindowShouldClose() do 
 
	BeginDrawing() 
 
	ClearBackground(blue) 
	 
	DrawText("This is a Texture",1,1,20,yellow) 
	 
	 
    gLoadTexture("raylib_logo.png")  
	 
    sequence texture_info = {3,256,256,1,0} 
     
	DrawTexture(texture_info,10, 50,white) 
	 
	EndDrawing() 
	 
end while 
 
CloseWindow() 
new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu