[SOLVED] How to draw the floor in raycasters - scaling problem

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Koxinga
Prole
Posts: 13
Joined: Sun Oct 02, 2022 12:29 am

[SOLVED] How to draw the floor in raycasters - scaling problem

Post 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
Last edited by Koxinga on Thu Oct 13, 2022 10:17 am, edited 4 times in total.
User avatar
zorg
Party member
Posts: 3436
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: precompute images at the launch of the game

Post 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...
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
darkfrei
Party member
Posts: 1169
Joined: Sat Feb 08, 2020 11:09 pm

Re: precompute images at the launch of the game

Post 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.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Koxinga
Prole
Posts: 13
Joined: Sun Oct 02, 2022 12:29 am

Re: precompute images at the launch of the game

Post 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 :crazy:

EDIT : Apparently, there is a more efficient way to draw the floor... I'm learning every minute :awesome: https://lodev.org/cgtutor/raycasting2.html
Attachments
maze.png
maze.png (523.02 KiB) Viewed 4832 times
User avatar
darkfrei
Party member
Posts: 1169
Joined: Sat Feb 08, 2020 11:09 pm

Re: [SOLVED] precompute images at the launch of the game

Post by darkfrei »

:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Koxinga
Prole
Posts: 13
Joined: Sun Oct 02, 2022 12:29 am

Re: [SOLVED] precompute images at the launch of the game

Post by Koxinga »

darkfrei wrote: Thu Oct 06, 2022 9:31 pm See also viewtopic.php?t=85542
Thanks, I guess I'll look at shaders later. That's already a lot to process for me.
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: [SOLVED] precompute images at the launch of the game

Post 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.
Koxinga
Prole
Posts: 13
Joined: Sun Oct 02, 2022 12:29 am

Re: [SOLVED] precompute images at the launch of the game

Post 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 ?
Attachments
phone.png
phone.png (518.55 KiB) Viewed 4602 times
computer.png
computer.png (554.51 KiB) Viewed 4602 times
User avatar
darkfrei
Party member
Posts: 1169
Joined: Sat Feb 08, 2020 11:09 pm

Re: [SOLVED] precompute images at the launch of the game

Post by darkfrei »

I can see the other floor: Maybe it comes from low display resolution with high dpi.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Koxinga
Prole
Posts: 13
Joined: Sun Oct 02, 2022 12:29 am

Re: How to draw the floor in raycasters - scaling problem

Post 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) :
Attachments
computer1.png
computer1.png (579.72 KiB) Viewed 4560 times
computer2.png
computer2.png (570.79 KiB) Viewed 4560 times
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 33 guests