Newtonian Physics at its heart is simply the axiom that the net sum of the forces F = ma. If anything should be included in a physics engine, it should be this relationship. Alternatively, one can start with the definition of momentum p = mv, which can then be used to
derive F = ma. These two notions should be equivalent. One key important point is that Newton's laws only hold in inertial reference frames. However, Game Maker's coordinate system is an inertial reference, so this isn't a problem.
Forces can depend on various things, and often require methods of differential equations to solve. One example is the spring force: The net force when the spring is released is proportional to the distance for which it is stretched. In other words, ma = -kx. (The minus sign is used to indicate direction. k is the constant of proportionality, often referred to as a spring constant.) After realizing that this is the differential equation m d^2x / dt^2 = -kx(t), we can solve this for the spring's position as a function of time:
x(t) = Acos(wt), where w = sqrt(k / m), and is the angular frequency of the object.
When we are trying to mimic physics in Game Maker, there are two approaches. The first is to use (and know) the equations of motion, and perform calculations to where the object should be and how it should react. The other is to try and mimic the differential equation to reproduce the equations of motion. This is more valuable because this essentially is what F = ma is all about -- F = ma tells us how the system will act at a later time.
As an example of these two methods, let's look at an object that starts at y = room_height / 2, x = room_width / 2. We want to mimic the object moving up and down in the y-direction as a wave. The two methods look like this:
Method 1Create Eventmass = 1
time = 0; //Time parameter
x_start = room_width / 2
x = x_start
y_start = room_height / 2
y = y_start
y_wave_amp = room_width / 4
y_wave_period = room_speed * 4
y_wave_frequency = 2 * pi / y_wave_period //this is angular frequency.
Step Eventtime += 1
x = x_start
y = y_start + y_wave_amp * cos(y_wave_frequency * time)
Method 2Create Eventmass = 1
x_start = room_width / 2
x = x_start
x_speed = 0;
x_acceleration = 0;
y_start = room_height / 2
y_speed = 0;
y_wave_amp = room_width / 4
y_wave_period = room_speed * 4
y = y_start + y_wave_amp
y_wave_frequency = 2 * pi / y_wave_period //this is angular frequency.
y_acceleration = 0
Step Eventx_acceleration = 0 //this corresponds to no forces in x direction
x_speed += x_acceleration
x += x_speed
y_acceleration = -sqr(y_wave_frequency) * (y - y_start)
y_speed += y_acceleration
y += y_speed
Try these. They should give identical results, up to small errors. There are key differences between the two. The first equation requires a time parameter, and is quite rigid--it cannot be combined with any other "forces" unless a new position function is generated. The second, however, requires only that it starts an initial amount away, and uses the differential equation to generate the motion, independent of a time parameter. This second method should combine better with forces, simply by adding or subtracting to the acceleration.
If a general physics engine is to be created, then method 2 should be strived for.