[ad_1]
So an object has a place, velocity, and is affected by gravity.
#embrace <simd/simd.h> // MacOS
struct Object {
vector_float3 Translation; // Models
vector_float3 Velocity; // Models per second
};
The sport loop usually runs 60 occasions per second.
void ObjectUpdate(struct Object *obj) {
obj->Velocity.y -= 9.80655/60;
obj->Translation += obj->Velocity/60;
}
There are a pair issues with this method:
- Lag exists, so the belief of 60 updates per second being embedded within the code is just not nice.
- Floating level sorts are vulnerable to precision errors at far distances from the origin, and are additionally tougher to deal with in some methods than integers.
I tried to unravel these with a fixed-point method, and the usage of clock()
in addition to conserving observe of the time of the final replace.
struct Object {
vector_long3 Translation; // 65536ths of a unit
vector_long3 Velocity; // 65536ths of a unit per second
clock_t LastUpdate;
};
void UpdateObject(struct Object *obj) {
const clock_t present = clock();
const unsigned lengthy delta = (unsigned lengthy)((current-obj->LastUpdate)*(clock_t)65536/CLOCKS_PER_SEC); // Attempt to discover the variety of 65536ths of a second for the reason that final replace, in a approach that it doesn't matter whether or not clock_t is integral or actual
obj->LastUpdate = present;
obj->Velocity.y -= delta*642682>>16; // 642682 = 9.80655*65536
obj->Translation -= delta*obj->Velocity>>16;
}
Nevertheless, this suffers from different issues, most notably, being extraordinarily chunky and unreliable. If I merely add delta
to an accumulator after which print the accumulator, the worth rises at an inconsistent price, usually a lot slower than by 65536 per second.
How ought to I deal with objects that want ‘fixed’ updating and their updates are contingent on time?
[ad_2]