0
\$\begingroup\$

I am working on a game in Game Maker Studio: 2 and am using Delta Time for the time. I understand what it is, how to get it, and how to use it, but I am running into issues when I need to make comparisons that contain values that have been modified by Delta Time.

Example: an item from my game is moving along the x-axis at 2 pixels-per-sec. at 60 frames. The speed the item is moving is then modified by Delta Time, with the x-coord of the item effectively being 'x = 2 * delta'.

Now, I want to check for when the x-coord of the item has reached a certain spot, i.e. 'if(x = "specific spot on axis") {do something}.

The "specific spot on axis" that I am looking for is an integer (no digits after the decimal), but when using delta time, the x-coord is always off by usually miniscule amounts due to adjusting its position relative the speed of change of each frame. Instead of 'x = 12', it is 'x = 12.00387562' or 'x = 11.99943345'.

Right now, I'm thinking that just rounding 'x' at end of each step is all I have to do to fix this, but I also have a nagging feeling that I am possibly missing something right in front of my face. One reason being that, If I round the x-coord at every frame, does that not make a lot of the adjustments made by Delta Time pointless? Is there a (preferably easy and/or concise) way to do comparisons looking for values inside a certain range of digits after the decimal?

All help and insight is appreciated.

\$\endgroup\$
2
\$\begingroup\$

Your feeling is right, don't round your x at every frame because you need that value as an accumulator and it should remain accurate for a consistent simulation. Instead, round the x as a temporary variable when you need to compare it to an integer.

if(round(x) =="specific spot on axis") {do something}

If you need to check if x has only passed the spot, you can just do the comparison without rounding:

if(x > "specific spot on axis") {do something}

You can also use the floor method, which will always "round" down, and never "up". This could give you a more consistent behaviour: you're not at "specific spot on axis" if you haven't reached it. Period.

if(floor(x) =="specific spot on axis") {do something}

Disclaimer: I'm not an expert with GMS.

\$\endgroup\$
  • \$\begingroup\$ Worked like a charm. For me, specifically rounding off 'x' just for the comparison. Super simple. Thanks for the fresh eyes. \$\endgroup\$ – Dr Negative Nov 3 '17 at 21:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.