# "Defeating lag with cubic splines" - Help needed!

This topic is 3669 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi there, I've read this article- http://www.gamedev.net/reference/articles/article914.asp When I input the code below, my object moves along the spline, but not correctly, it ends up at the point "Newx + Newxvector" instead of just "Newx". So it curves round correctly, but seems to think that it should end not at the endposition, but at the end position + endvector. Can anyone see any mathmatical errors in the code below (I've grabbed the code from another post, but i've copied it exactly)? function newPosition(positionObject:Object){ var timeNow:Number = getTimer(); var timeDiff:Number = timeNow-lastPositionTime; lastPositionTime = timeNow; // Get new variables from packet var newX = positionObject._x; var newY = positionObject._y; var newRotation = positionObject._rotation; var newSpeed = positionObject.speed; var newXSpeed = Math.cos((_rotation)*Math.PI/180)*newSpeed; var newYSpeed = Math.sin((_rotation)*Math.PI/180)*newSpeed; var newXAcc = (newXSpeed - xSpeed)/timeDiff; var newYAcc = (newYSpeed - ySpeed)/timeDiff; x1 = _x; y1 = _y; x2 = _x+xSpeed; y2 = _y+ySpeed; x3 = newX + (newXSpeed*timeDiff) + (0.5*newXAcc*(timeDiff*timeDiff)); y3 = newY + (newYSpeed*timeDiff) + (0.5*newYAcc*(timeDiff*timeDiff)); x4 = x3 - (newXSpeed + (newXAcc*timeDiff)); y4 = y3 - (newYSpeed + (newYAcc*timeDiff)); A = x4 - (3*x3) + (3*x2) - x1; B = (3*x3) - (6*x2) + (3*x1); C = (3*x2) - (3*x1); D = x1; E = y4 - (3*y3) + (3*y2) - y1; F = (3*y3) - (6*y2) + (3*y1); G = (3*y2) - (3*y1); H = y1; clearInterval(followSplineInterval); currentStep=1; followSplineInterval = setInterval(this, "followSpline", timeDiff/noOfSteps); } function followSpline(){ var t:Number = currentStep/noOfSteps; _x = A*t*3 + B*t*2 + C*t + D; _y = E*t*3 + F*t*3 + G*t + H; currentStep++; if(currentStep==noOfSteps) clearInterval(followSplineInterval); } Thanks, Alex

##### Share on other sites

_x = A*t*3 + B*t*2 + C*t + D;
_y = E*t*3 + F*t*3 + G*t + H;

Following the article:

x = At^3 + Bt^2 + Ct + D
y = Et^3 + Ft^3 + Gt + H

I also wounder if Ft^3 is a typo for Ft^2 .. But that you have to try out :-)

I done an implementation of cubic splines following the article but I haven't got access to that piece of code right now.. Got it working after awhile anyhow :-)

##### Share on other sites
Your right, that bit is wrong. - Although In my code its correct.

I've managed to solve the problem now though. The value t must be set initially at 1. It was set at 0, thus causing the spline to be distorted/out of shape.

Thanks very much for your help though. Does the solution work nicely for you?

##### Share on other sites

This topic is 3669 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628648
• Total Posts
2984036

• 10
• 9
• 9
• 10
• 21