Solving differential equations using numeric methods

Click For Summary
SUMMARY

The forum discussion centers on the implementation of a movement system in Game Maker: Studio, specifically addressing the behavior of speed calculations using differential equations. The original code structure, which adds speed while a key is pressed and subsequently reduces it by a quarter, results in a maximum speed of 6 pixels per step. In contrast, placing the entire differential equation within the key press condition yields a maximum speed of 8 pixels per step, aligning with analytical predictions. The discrepancy arises from the timing of the speed reduction relative to the key press, suggesting that the speed reduction should be placed in an else clause to achieve the desired effect.

PREREQUISITES
  • Understanding of Game Maker: Studio programming syntax
  • Basic knowledge of differential equations
  • Familiarity with numerical methods for solving equations
  • Concept of steady-state behavior in dynamic systems
NEXT STEPS
  • Explore Game Maker: Studio's event handling and conditional statements
  • Study numerical methods for solving differential equations
  • Learn about steady-state analysis in dynamic systems
  • Investigate the impact of time-step size on numerical simulations
USEFUL FOR

Game developers, programmers using Game Maker: Studio, and anyone interested in implementing movement systems based on numerical methods and differential equations.

DarkBabylon
Messages
72
Reaction score
10
Hello, I have been working on a little movement system in a program called Game Maker: studio.
The code works fine on the programming perspective, but something I did not expect happened:
When I ran the code by adding to the speed while pressing a key, and every step passively subtracting from the speed I got a certain number I did not expect to find.
Essentially the code run like this:
if key=pressed {speed+=2};
speed-=0.25*speed;
Which according to the game maker syntax should add to the speed while the key is pressed by 2 pixels per step, and afterwards it will subtract from the speed what would be equal a quarter of the speed previously calculated.
With this method I got top speed of 6 pixels per step.
However when I just put out the entire differential equation under the key press 'if' statement, it returned a top speed of 8 pixels per step, which was exactly as an analytical analysis would predict.

Question is, why is it so? Can't seem to get why would that matter at least not at hindsight.
 
Technology news on Phys.org
perhaps you want the speed -= speed*0.25 to be in an else clause

that way pressing the key increments the speed and not pressing it reduces the speed.

Right now when pressing the key it adds 2 then then effectively subtracts a quarter of the speed.
Code:
loop 1: 10     -> 12    -> 9
loop 2: 9      -> 11    -> 8.25
loop 3: 8.25   -> 10.25 -> 7.68
 
The value of speed = 6 is a steady state when the key is pressed. If speed=6 before the code, then speed + 2 - .25 *( speed+2) = 6 + 2 - .25*(6+2) = 6 + 2 - 2 = 6 after the code. If you put a print statement after the 'if' line, then speed=8 at that point. Is that what you expected?
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
4
Views
4K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 32 ·
2
Replies
32
Views
3K