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

Author Topic: Screens goes black after some time  (Read 1401 times)

0 Members and 1 Guest are viewing this topic.

Kapuche

  • Newbie
  • *
  • Posts: 2
    • View Profile
Screens goes black after some time
« on: November 24, 2022, 11:59:38 pm »
Good morning ! (Or good evening)

I am making a little game with sfml, to train in c++, but I have a problem in my projects and cannot find the problem :
When launching the program, after a random duration, the screen will become black.

My code is in several files, but I can post them all by editing this post (it is also in the zip file, but I can understand that there would be some concerns for safety reasons  :P).

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: Screens goes black after some time
« Reply #1 on: November 25, 2022, 03:37:46 am »
I haven't tried running it, but looking through the code there's one possibility I noticed:
In Player::update you have this line:
float gradient = (startX - xTarget) / (startY - yTarget);
If startY (player pos) and yTarget (mouse coords) are the same, then that will cause a divide by zero, and gradient will be a NaN (not a number).
The NaN will then spread to everything gradient is used with, which will set the player position to NaN. That position is then used (back in Engine::update) to move the view, which will also become a NaN and break the rendering.


euscar

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Screens goes black after some time
« Reply #2 on: November 25, 2022, 10:09:35 am »
I confirm what kojack wrote.

I tested the program by inserting an if statement in the Player::update causing the program to terminate with an error message: well, the program terminates at that point whenever the mouse pointer is over the red dot of the spacecraft, causing a division by zero.

You are a great kojack  ;)


P.S. As I continued testing the program, I noticed that division by zero happens even when the pointer is far from the spacecraft, and I deduced that this depends only on the two Y coordinates - I could have gotten there even earlier, how careless I am  :-[
« Last Edit: November 25, 2022, 10:34:08 am by euscar »

Kapuche

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Screens goes black after some time
« Reply #3 on: November 25, 2022, 07:52:07 pm »
Oooh, thank you very much !

Well, I guess it's time for me to dive into math to find a solution. Because I don't really want to do ZQSD (I want an acceleration system where the ship goes toward the direction where the mouse is)

But thank you very much !

 

anything