Page 1 of 2
[SOLVED] How to draw the floor in raycasters - scaling problem
Posted: Thu Oct 06, 2022 12:54 pm
by Koxinga
Hey guys !
I'm new to löve there but trying to make things work.
I'm creating a game for android (a maze in fake 3D). It was going well until I tried to draw the ground. Turns out trying to draw thousands of individual points one by one is not the way to go.
I had the idea to precompute all the ground configurations (30 or so) and store those in images at the launch of the game.
I suppose it won't be that hard to create ImageData objects storing my data, but how do you create images from ImageData objects ?
Thanks
EDIT : I changed the title due to the change of direction and the new problem
Re: precompute images at the launch of the game
Posted: Thu Oct 06, 2022 4:32 pm
by zorg
Hi and welcome to the forums!
Code: Select all
yourImageData = love.image.newImageData()
-- Fill up imagedata with pretty visuals
yourImage = love.graphics.newImage(yourImageData)
function love.draw()
-- draw out your image
love.graphics.draw(yourImage, 0, 0)
end
Not sure why you're trying to draw anything out with points though...
Re: precompute images at the launch of the game
Posted: Thu Oct 06, 2022 4:59 pm
by darkfrei
Use the Canvas
https://love2d.org/wiki/Canvas
If you want, you can save it as files and you don't need to precompute it before the game.
Re: precompute images at the launch of the game
Posted: Thu Oct 06, 2022 6:56 pm
by Koxinga
Thanks guys.
I suppose I can combine both methods : draw on canvases then store them as images. I'll look into this
zorg wrote:Not sure why you're trying to draw anything out with points though...
I am implementing fake 3D as it was in doom or more specifically in Lands of Lore (cause you move on a grid).When the player is turning the texture of the floor is rotated so I can't draw it as strips as I did for the walls. The only way I found was to draw each point separately.
As you see on the screenshot, the fps count was a bit problematic
EDIT : Apparently, there is a more efficient way to draw the floor... I'm learning every minute
https://lodev.org/cgtutor/raycasting2.html
Re: [SOLVED] precompute images at the launch of the game
Posted: Thu Oct 06, 2022 9:31 pm
by darkfrei
Re: [SOLVED] precompute images at the launch of the game
Posted: Fri Oct 07, 2022 10:01 am
by Koxinga
Thanks, I guess I'll look at shaders later. That's already a lot to process for me.
Re: [SOLVED] precompute images at the launch of the game
Posted: Fri Oct 07, 2022 10:26 pm
by pgimeno
Raycasting and real 3D are fundamentally different. Real 3D is hardware accelerated and cheap for the CPU; true raycasting is CPU-expensive. It's possible to simulate raycasting with real 3D.
Re: [SOLVED] precompute images at the launch of the game
Posted: Mon Oct 10, 2022 8:41 pm
by Koxinga
I finally got it by writing a shader.
Code: Select all
extern Image ground;
extern number playerX;
extern number playerY;
extern number playerA;
extern number D;
vec4 effect( vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords ){
number angle = mod((playerA - atan((love_ScreenSize.x/2.0 - ((love_ScreenSize.x-screen_coords.x)-1.0))/D)) , (2.0*3.14159));
number d = D / cos(angle - playerA);
number cosA = cos(angle);
number sinA = sin(angle);
number distGround = 0.5 * d / (screen_coords.y - love_ScreenSize.y/2.0);
vec4 pixel = Texel(ground, vec2( (mod((playerX + distGround*cosA),1.0)),(mod((playerY - distGround*sinA),1.0)) ));
return pixel;
}
It seems to be working perfectly on my computer, at 60 fps. But when I try to run it on the android emulator or on my phone, the floor texture is stretched.
Any idea why this is happening ? Is there maybe something to do with love-android that I'm using ?
EDIT : Should I rename the thread ?
Re: [SOLVED] precompute images at the launch of the game
Posted: Tue Oct 11, 2022 5:51 am
by darkfrei
I can see the other floor: Maybe it comes from low display resolution with high dpi.
Re: How to draw the floor in raycasters - scaling problem
Posted: Tue Oct 11, 2022 7:40 am
by Koxinga
I removed the texture of the floor and put a gradient instead to see what was going on, and I printed the dpiscale. But unfortunately, I don't have any idea if the problem is because of a change in the DPI scale (1 on my computer and almost 3 on the phone) or because some function in the shader doesn't return the same value on both platforms.
Here are the screenshots if you want to have a look at it (the computer ones are the expected behavior, it is cut at the wrong places on the phone) :