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

Author Topic: Keeping PointLight shader centered on player  (Read 5120 times)

0 Members and 1 Guest are viewing this topic.

LedLoaf

  • Newbie
  • *
  • Posts: 16
    • View Profile
    • Email
Keeping PointLight shader centered on player
« on: October 30, 2020, 02:23:55 am »
Hello,

First off, this isn't specifically an SFML question, more of the appropriate math. I hope this is where I am supposed to post this and if not moving it to the correct forum section would be great. I'm not sure if we are supposed to even ask non SFML questions here. So I apologize if the latter is true.

I have been learning about shaders from a spectacular book (highly recommend) "Mastering SFML Game Development" I'm at a point where we are going over dynamic lighting and there is an example with what I believe is called a Point Light.

shader->setUniform("LightPosition",
                               sf::Glsl::Vec3(context->m_wind->GetWindowSize().x / 2.f,
                                              context->m_wind->GetWindowSize().y / 2.f, lightPos.z));
 

Now, it follows the player since the view is centered on the player, to begin with as the world scrolls. But when I get towards the edges of the world and the screen stops scrolling, the player is now offset and not centered on the screen. So I think the best thing to do is use some math to always stick to where the player is and I'm wondering how I could accomplish this correctly?

Thanks, everyone.

PS: I could just check if the view is no longer scrolling and get the offset from the view's center, but I'm more curious about how to solve this mathematically. Something that has hindered me as I improve with game programming.

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Keeping PointLight shader centered on player
« Reply #1 on: October 30, 2020, 07:30:27 am »
Hey.

You can use mapCoordsToPixel to convert the position of your player in the world into its position in the window, thus getting the light position.