Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: adventure3d: Simple raycaster with SFML  (Read 13593 times)

0 Members and 1 Guest are viewing this topic.

tms

  • Newbie
  • *
  • Posts: 8
    • View Profile
adventure3d: Simple raycaster with SFML
« on: January 16, 2016, 11:37:11 pm »
Hi everyone,

A few days ago, I decided to make a raycaster in SFML, I used this tutorial: http://lodev.org/cgtutor/raycasting.html

Here's what I made: https://github.com/tmsbrg/adventure3d
Compile using `make' or just compile main.cpp using your favourite compiler.

Don't have textured walls working yet. Will work on that when I feel like it.
Edit: I've got textured walls working now.

Screenshot:
« Last Edit: January 21, 2016, 10:03:52 am by tms »

Mortal

  • Sr. Member
  • ****
  • Posts: 284
    • View Profile
Re: adventure3d: Simple raycaster with SFML
« Reply #1 on: January 17, 2016, 03:29:48 am »
it looks fabulous, good job

i have download it. i will try to compile it in VS14.

many thanks for tutorial it rarely someone can find full documented tutorials out there, most of it some kind of show off more than educated

tms

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: adventure3d: Simple raycaster with SFML
« Reply #2 on: January 17, 2016, 02:11:10 pm »
Thanks, MORTAL! I hope compiling works well. For the record I used clang++ on Linux, with SFML 2.1(default from the repo on Ubuntu).

Yeah, I'm glad I found the tutorial too. I also found he has more tutorials, including one about drawing ceilings and floors with raycasting and drawing sprites. I also found the code license for his tutorial code, which is basically MIT. So I'll add that to the repository.

Resethel

  • Newbie
  • *
  • Posts: 22
    • View Profile
    • Email
Re: adventure3d: Simple raycaster with SFML
« Reply #3 on: January 17, 2016, 08:05:48 pm »
Compiled on mac, tested it, and it works amazingly well!
Great job on this, I'm looking to see how you'll improve it ^^

Elias Daler

  • Hero Member
  • *****
  • Posts: 599
    • View Profile
    • Blog
    • Email
Re: adventure3d: Simple raycaster with SFML
« Reply #4 on: January 18, 2016, 08:27:28 am »
Wow, this looks pretty neat. :)
Would be cool to see some gifs of it in action (I recommend to use LICEcap)
Tomb Painter, Re:creation dev (abandoned, doing other things) | edw.is | @EliasDaler

tms

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: adventure3d: Simple raycaster with SFML
« Reply #5 on: January 18, 2016, 08:52:10 pm »
I actually have a video on my site.

Thanks all. I think I'll add textures very differently than the tutorial does, because I can just specify texture coordinates for my lines and have the GPU pick the pixels. Doesn't seem too hard. It's fun to make something like this - You get to make a simple cool 3d-ish experience way easier than starting with drawing triangles and shaders such. Its so efficient, too!

fer_t

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Re: adventure3d: Simple raycaster with SFML
« Reply #6 on: January 20, 2016, 09:07:03 pm »
Looks great :D

But for me it only works if I switch the windows (some other, e.g. terminal) and then switch back.
Something with hasFocus is wrong... (compiled with sfml 2.3.2 on openSUSE 42.2)

Hapax

  • Hero Member
  • *****
  • Posts: 3346
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: adventure3d: Simple raycaster with SFML
« Reply #7 on: January 20, 2016, 09:31:40 pm »
for me it only works if I switch the windows (some other, e.g. terminal) and then switch back.
Something with hasFocus is wrong.
hasFocus is not being initialised and is only being set when the window focus changes so if the application starts with the window having focus already then hasFocus has the value that it started with (uninitialised). In your case, it's being false.

Try initialising hasFocus to true.
e.g.
bool hasFocus{true}; // C++11 or later
or
bool hasFocus = true; // pre-C++11


p.s. This looks quite cool. I'm always a fan of the faux 3D  :D
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

tms

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: adventure3d: Simple raycaster with SFML
« Reply #8 on: January 21, 2016, 10:12:40 am »
Thanks fer_t for reporting and Hapax for the suggested fix. An oversight on my part! I've fixed it now.

That C++11 style seems a bit weird to me. I've read in "Effective Modern C++" about the {} constructor, but in this case I feel it's more natural to use =(as I've done with other simple initializers), so I went for that one.

C++ is such a mess at times with multiple syntaxes for everything :D

Edit: I do think it could be a problem initializing it to true if for some people the window is started in the background. But I'm not sure how to fix that. Is there anyway to know whether you're being started in the foreground with SFML 2.1?
« Last Edit: January 21, 2016, 10:15:24 am by tms »

Mortal

  • Sr. Member
  • ****
  • Posts: 284
    • View Profile
Re: adventure3d: Simple raycaster with SFML
« Reply #9 on: January 21, 2016, 12:35:19 pm »
latest progress looks even better, it quite noticeable when you implemented the textures.  :)

i also working on this project too ;D, i have added floor and sky after struggle with realistic color of them, somehow i got it 8)

i just added those lines in your code:
sf::RectangleShape sky({ static_cast<float>(screenWidth), screenHeight / 2u });
sky.setFillColor({ 135, 206, 250 });
       
sf::RectangleShape floor({ static_cast<float>(screenWidth), screenHeight / 2u });
floor.setFillColor({ 102, 51, 0 });
floor.setPosition(0, screenHeight / 2.f);


Edit: I do think it could be a problem initializing it to true if for some people the window is started in the background. But I'm not sure how to fix that. Is there anyway to know whether you're being started in the foreground with SFML 2.1?
i didn't face this problem in my OS, windows always start on focus. for those who has different OS , i not so familiar with SFML, but i think it can be done by creating extra Boolean variable just for first run and "hasFocus" can do its job later on,
« Last Edit: January 21, 2016, 12:46:05 pm by MORTAL »

tms

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: adventure3d: Simple raycaster with SFML
« Reply #10 on: January 21, 2016, 03:52:33 pm »
MORTAL,

Yeah, I was thinking of adding sky and floor similarly. Though I'd also like maybe adding floor and ceiling textures as in the second tutorial. However, the way he does it there is very CPU-focused and would require me to render differently then the line-based rendering I'm doing now. I like that rendering, so I'd rather not change it to something slower! I'm wondering if there's a good fast way to do that with the GPU(Maybe shaders, but I don't have much experience with those).

And maybe I'd like to add a skybox kind of thing, where a certain texture is always visible in the sky, and you can see different parts by turning your character but you'll never get closer to it.

I'm not sure what you mean with the extra boolean value. I'd still need a way to ask the window whether it's in focus or not right after it was created. Some people can have their system set so new windows don't steal focus. But it's probably not too important. If you start the raycaster you'll be wanting it in focus anyway.

Mortal

  • Sr. Member
  • ****
  • Posts: 284
    • View Profile
Re: adventure3d: Simple raycaster with SFML
« Reply #11 on: January 21, 2016, 11:06:47 pm »
Yeah, I was thinking of adding sky and floor similarly. Though I'd also like maybe adding floor and ceiling textures as in the second tutorial.
i haven't covered second tutorial yet, i'm sill stuck in first. :-\


However, the way he does it there is very CPU-focused and would require me to render differently then the line-based rendering I'm doing now. I like that rendering, so I'd rather not change it to something slower! I'm wondering if there's a good fast way to do that with the GPU(Maybe shaders, but I don't have much experience with those).
well, the optimization is Evil, i would not recommend it. dont bother yourself with it specially in such early stage of make this game, unless the performance drops down for any reason, that time you may looking for alternatives such as shaders but as far as i know SFML doesn't support vertices shaders, i had made a game similar to what you going to do, i have over +2000 game object i didn't witness any performance except when i implemented the collision as showing here Car racing, it wasn't big deal it can be solved easy. even though the raycaster doesn't cost much. it was used in 286's, 386's computers,


I'm not sure what you mean with the extra boolean value. I'd still need a way to ask the window whether it's in focus or not right after it was created. Some people can have their system set so new windows don't steal focus. But it's probably not too important. If you start the raycaster you'll be wanting it in focus anyway.
sorry i couldn't understand this issue. i thought the problem was if window start without focus then "bool hasFocus" will be initialized to false by "sf::Event::LostFocus", i think this can be solved as i said.

it seems that you have issue with creating a new window. really i can't help much in this issue i haven't done any thing similar :-[

Hapax

  • Hero Member
  • *****
  • Posts: 3346
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: adventure3d: Simple raycaster with SFML
« Reply #12 on: January 22, 2016, 02:03:57 am »
Thanks fer_t for reporting and Hapax for the suggested fix.
You're welcome  :)

I do think it could be a problem initializing it to true if for some people the window is started in the background. But I'm not sure how to fix that. Is there anyway to know whether you're being started in the foreground with SFML 2.1?
window.hasFocus()
It's in the documentation  :P
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

tms

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: adventure3d: Simple raycaster with SFML
« Reply #13 on: January 22, 2016, 09:46:26 am »
window.hasFocus()
It's in the documentation  :P

I actually used that at first, but found it's not in SFML 2.1. I prefer to keep compatible with that version because it's the default in the Debian/Ubuntu repository and I like it being easy to set up.

 

anything