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

Author Topic: *solved* Shape's position resetting when it shouldn't  (Read 1061 times)

0 Members and 1 Guest are viewing this topic.

fab

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
*solved* Shape's position resetting when it shouldn't
« on: February 26, 2021, 05:01:27 pm »
I'm trying to make a custom physics library but when I set, what is effectively time passed, to zero the position resets. There must be something crucial im not understanding but I can't figure out what.
Relevant Main Code:
      if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space))
         jumpStartTime = gameClock.getElapsedTime().asMilliseconds();

      pos = shape.getPosition().y;
      oldPos = pos; //possibly redundant
      deltaY = physics::gravity(pos, 2, (abs(prevTime-jumpStartTime))/1000, pixelsPerMetre);
      shape.move(0, deltaY);

Relevant physics lib code:
   const float g = 9.81;

   float gravity(float oldY, float mass, float t, float PpM)
   {
      float newY = pow(t, 2) * (g * PpM) * mass; //+9.81 not -9.81 because sfml coordinates are reversed in the y direction
      float deltaY = newY - oldY; //- oldY not + oldY becuase sfml coordinates are reversed in the y dir
      return deltaY;
   }


to explain: gameClock is time elapsed since program start, prevTime is time at the start of the current frame, jumpStartTime acts to make the prevTime-jumpStartTime evaluate to zero when the space key is pressed.

oldY is the shape's current position, newY is the new position and deltaY is the change in position. t is the time, g is acceleration due to gravity, mass is the mass of the shape and PpM is Pixels per Metre (currently set to 10)

Here is a video showcasing the issue:
https://imgur.com/a/ZLqwkV6

The circle should be spawned at y = 100 but starts at y = 0 and whenever I press space the acceleration due to gravity should be restarted but the circle shouldn't be reset to zero. I am confused as, in the console, the deltaY value is printed (the small decimal number after after the frameTime in ms (the frameTime is an integer, usually 13)) and it never goes negative so the circle should never move up, much less be reset to y = 0. Previously I was using setPosition with the += operator, but this also suffered from the same issue and I also realised that I could use the move method. The deltaY is never negative (apart from the first frame, although I haven't sorted out that bug yet) and I only use the move() method, never setPosition so I cannot comprehend how the shape would be moving back up to the top of the screen. I have linked both files below incase you want a more indepth look at the code. It is still very WiP so lots of redundant code at the moment.
« Last Edit: March 01, 2021, 07:18:50 pm by fab »

fab

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: *solved* Shape's position resetting when it shouldn't
« Reply #1 on: March 01, 2021, 07:20:32 pm »
I was calculating the deltaY wrong, finally figured it out. Here is the new code:
   
const float g = 9.81f;

   float gravity(float oldY, float t, float mass, float PpM)
   {
      float deltaY = pow(t, 2) * (g * PpM) * mass; //+9.81 not -9.81 because sfml coordinates are reversed in the y direction
      return deltaY;   
   }