Skip to main content
gamedev.net
🔒 Locked

Timer + wierd error

Started by JasonL220 0 replies 1k views
Original Post
JasonL220
JasonL220
hi, i'm writing some simple programs to test different inegration methods, i've only done euler, with this code: integration.h
#include <iostream>

class State
{
public:
	State() : x(0), v(0) {};
	State( int _x, int _v ) : x(_x), v(_v) {};
	int x;
	int v;

};

euler.h
#include "integration.h"

void Euler(float tend, float dt, float a, State &result);

euler.cpp
#include "integration.h"

void Euler(float tend, float dt, float a, State &result)
{
	State s;
	s.x = 0;
	s.v = 0;

	float tstart = 0;

	while( tstart <= tend )
	{
		s.v = s.v + a*dt;
		s.x = s.x + s.v*dt;
		tstart = tstart + dt;
	}
	result = s;
}

main.cpp
#include "Euler.h"
#include <ctime>

int main()
{
	State st;
	time_t start, end;
	double time;

	std::time(&start);
	Euler( 10, 0.1, 10, st );
	std::time(&end);

	time = difftime( end, start );

	std::cout << st.x << "\n";
	std::cout << time << "\n";

	return 0;
}

the problem is that the time always shows up as 0, is this becuase it run to fast for the timer to pick up a change in time? also when the dt is shortened the output change to 0, but when the timer step is increased the answer increases, what is causing this? is it my implementation or is it standard behaviour? ps should this be here or in maths? **EDIT** when using dt as 0.3 i get 519, not a bad estimate, but when using 0.29 just 1/100th of a second shorter i get 348, quite a large jump an i thought a smaller dt would make the results closer to the real value ( 500 ), is this normal behaviour?
erissian
erissian
#1 Use a time function with a precision of at least milliseconds, because second integration is too big of a step for most applications.

#2 Decide on a minimum time step, and don't call your function until that step has been achieved. Even with millisecond precision, if your function is called every few milliseconds or less, then you won't get many useful updates.

#3 Your error is proportional to the time step you use, so large, unpredictable time steps will result in large, unpredictable errors.
We''re sorry, but you don''t have the clearance to read this post. Please exit your browser at this time. (Code 23)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.