Skip to main content
GameDev.net gamedev.net
🔒 Locked

attempt at cubic splines

Started by ronkfist Apr 10, 2006 at 10:57 AM 0 replies 1.8k views
Original Post
ronkfist
ronkfist
Hi, I'm trying to implement an anti-lag system into my 2D engine, by using cubic splines. I started creating the basic system using this article(http://www.gamedev.net/reference/articles/article914.asp), but it wasn't working for me at all. So I started to browse the net and after a dozen of articles, I came up with an improved version but its still not working like it should. The problem seems to be that the more an object moves away from (0,0), the less accurate the spline equation seems to become. The result is that other objects connected to the server, will see this object spiking(jumping) around his position. I thought it had something to do with the extrapolation, but the same thing happens when the object is not moving (velocity=0). So I'm basicly out of ideas and can't seem to find more articles. I'm posting the source of the spline here, so if anybody got any idea whats wrong with it, please let me know.

	//The rate packets get updated at
	#define FREQ 0.05f

	NCubicSpline::NCubicSpline(NObject *obj) {
		this->obj = obj;
		this->lastUpdate = FREQ;
	}

	void NCubicSpline::feed(double x, double y, double vx, double vy) {

		//Get the current time.
		double curTime = obj->getEngine()->getTime();
		//time since last update (this should ideally come from the client)
		double dt = curTime - lastUpdate;
		

		GVector2D<double> *coord0 = (GVector2D<double> *)obj->getPosition()->clone();
		GVector2D<double> *coord1 = new GVector2D<double>(coord0->x+(obj->getVelocity()->x*(FREQ / 3)), coord0->y+(obj->getVelocity()->y*(FREQ / 3)));
		GVector2D<double> *coord3 = new GVector2D<double>(x+(vx*(dt+FREQ)), y+(vy*(dt+FREQ)));
		GVector2D<double> *coord2 = new GVector2D<double>(coord3->x+((vx*-1)*(FREQ/3)), coord3->y+((vy*-1)*(FREQ/3)));

		a = coord0->x;
		b = coord1->x;
		c = 3*(coord2->x-coord0->x) - coord1->x*2 - coord3->x;
		d = 2*(coord0->x-coord2->x) + coord1->x + coord3->x;

		e = coord0->y;
		f = coord1->y;
		g = 3*(coord2->y-coord0->y) - coord1->y*2 - coord3->y;
		h = 2*(coord0->y-coord2->y) + coord1->y + coord3->y;
		
		lastUpdate = curTime;

		delete coord0;
		delete coord1;
		delete coord2;
		delete coord3;
	}

	bool NCubicSpline::follow() {
		//Get the current time.
		double curTime = obj->getEngine()->getTime();
		//Time since last update
		double timeSince = curTime - lastUpdate;

		double t1 = timeSince / FREQ;

		if  (t1 >= 0.0f && t1 <= 1.0f) {
			obj->getPosition()->x = (((d*t1) + c)*t1 + b)*t1 + a;
			obj->getPosition()->y = (((h*t1) + g)*t1 + f)*t1 + e;
			return true;
		}
		return false;
	}

ronkfist
ronkfist
I think I solved it. Due to the severe editing of my code, the original spline equations got turned upside down and I forgot to correct them.

So if someone ever needs cubic splines, I changed the equations from the above post to.

a = coord0->x;b = coord1->x*3 - coord0->x*3;c = coord2->x*3 - coord1->x*6 + coord0->x*3;d = coord3->x - coord2->x*3 + coord1->x*3 - coord0->x;e = coord0->y;f = coord1->y*3 - coord0->y*3;g = coord2->y*3 - coord1->y*6 + coord0->y*3;h = coord3->y - coord2->y*3 + coord1->y*3 - coord0->y;

Topic Locked

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

Sign in to reply to this topic.