|
Hermite spline - moving with constant speed
#391574
01/13/12 22:29
01/13/12 22:29
|
Joined: May 2010
Posts: 117 Germany , Dortmund
Bone
OP
Member
|
OP
Member
Joined: May 2010
Posts: 117
Germany , Dortmund
|
Hello, I use the formula from http://cubic.org/docs/hermite.htm for calculating a spline. Now I want to move a model along the spline but with constant speed. The user set the waypoints so i cant place them in the same distance. This is the problem- if the waypoints have a higher distance between each waypoint, the model moves faster along the spline. I have the circa length of the curve. I go through the curve in 100 steps and save the length between each point on the curve. The range of the curve is between 0.0 and 1.0 . How can I calculate the right value between 0.0 and 1.0 in relation to the speed? I tried some things:
var dist += 1 / curveLength * speed * time_step
spline(ent.x, dist);
spline(nextpoint.x,dist);
while(vec_dist(ent.x,nextpoint.x) > 0)
{
c_move(...); // speed * time_step
}
dist += 0.1
and some other ways. But they dont work. The speed is not constant and/or the entity dont move not exactly along the spline. Google shows me some article about this topic but there are no real examples, only complicate formulas that i dont understand. I hope somebody have a tip for me. Good evening
Last edited by Bone; 01/14/12 11:54.
|
|
|
Re: Hermite spline - moving with constant speed
[Re: sivan]
#391618
01/14/12 11:56
01/14/12 11:56
|
Joined: May 2010
Posts: 117 Germany , Dortmund
Bone
OP
Member
|
OP
Member
Joined: May 2010
Posts: 117
Germany , Dortmund
|
I had some problems with path_spline() It need a distance parameter to calculate the position on the spline and the distance is not the real distance on the curve. From manual: The dist parameter corresponds to the distances of straight lines connecting the nodes, and not to the real distance of the curve. Thus, when adding a constant speed to the dist parameter, the real speed along the curve can vary. It is the faster the more the curve deviates from the straight lines between the nodes. If this is undesired, place more nodes to avoid deviations at sharp bends in the path.
Its the same problem, the speed vary. I dont know how can I solve my problem with path_spline and c_move. Do you have another idea?
|
|
|
Re: Hermite spline - moving with constant speed
[Re: Bone]
#391634
01/14/12 14:25
01/14/12 14:25
|
Joined: Sep 2007
Posts: 101 Luxembourg
krial057
Member
|
Member
Joined: Sep 2007
Posts: 101
Luxembourg
|
You have to normalize the direction vector and then scale it with the speed. Here is the code part of my algorithm to follow a bezier curve:
vec_set(dir,currentNode->next->pos);
vec_sub(dir,currentNode->pos);
vec_normalize(dir,speed*time_step);
while(distToNext>=speed*time_step)
{
var percentDone=1-(distToNext/wholeDist);
quat_slerp(&quaternInter,&quaternStart,&quaternEnd,percentDone);
ang_for_quat(my.pan,&quaternInter);
c_move(me,nullvector,dir,IGNORE_MODELS);
distToNext=vec_dist(my.x,currentNode->next->pos);
wait(1);
}
regards Alain
Last edited by krial057; 01/14/12 14:27.
|
|
|
Re: Hermite spline - moving with constant speed
[Re: sivan]
#391868
01/16/12 17:22
01/16/12 17:22
|
Joined: Sep 2007
Posts: 101 Luxembourg
krial057
Member
|
Member
Joined: Sep 2007
Posts: 101
Luxembourg
|
Yes it works, but you have to write them xD. Here are the quaternion functions i used(written by JibbSmart): http://www.opserver.de/ubb7/ubbthreads.php?ubb=showflat&Number=284378&page=5There you have the function quat_nlerp it should work as fine as with an slerp. If you still want to ues an slerp, you can add this function that i created:
function quat_slerp(QUATERNION *q, QUATERNION *u, QUATERNION *v, double t) {
QUATERNION qa, qb;
quat_set(&qa, u);
quat_set(&qb, v);
double cosHalfTheta=qa.w*qb.w + qa.x*qb.x + qa.y*qb.y + qa.z*qb.z;
if(abs(cosHalfTheta)>=1.0)
quat_set(&q,&qa);
else
{
double halfTheta=acos(cosHalfTheta);
double sinHalfTheta=sqrt(1.0 - cosHalfTheta*cosHalfTheta);
if (abs(sinHalfTheta) < 0.001)
{
q.w = (qa.w * 0.5 + qb.w * 0.5);
q.x = (qa.x * 0.5 + qb.x * 0.5);
q.y = (qa.y * 0.5 + qb.y * 0.5);
q.z = (qa.z * 0.5 + qb.z * 0.5);
}
else
{
double ratioA = sin((1 - t) * halfTheta) / sinHalfTheta;
double ratioB = sin(t * halfTheta) / sinHalfTheta;
q.w = (qa.w * ratioA + qb.w * ratioB);
q.x = (qa.x * ratioA + qb.x * ratioB);
q.y = (qa.y * ratioA + qb.y * ratioB);
q.z = (qa.z * ratioA + qb.z * ratioB);
}
}
}
And this will be the reuslt: http://www.youtube.com/watch?v=O07tSEez7scIf rotations are not needed, you can leave out all the quaternion stuff. (first three lines inside the while loop) regards Alain
Last edited by krial057; 01/16/12 17:30.
|
|
|
Re: Hermite spline - moving with constant speed
[Re: krial057]
#391902
01/16/12 20:41
01/16/12 20:41
|
Joined: Mar 2011
Posts: 3,150 Budapest
sivan
Expert
|
Expert
Joined: Mar 2011
Posts: 3,150
Budapest
|
thanx a lot! it is really useful even to make more realistic my grid based pathfinding system! so I dig into it a bit
|
|
|
Re: Hermite spline - moving with constant speed
[Re: sivan]
#392124
01/19/12 14:47
01/19/12 14:47
|
Joined: Jan 2007
Posts: 2,247 Deutsch Niedersachsen
Puppeteer
Expert
|
Expert
Joined: Jan 2007
Posts: 2,247
Deutsch Niedersachsen
|
I use Splines too and what I do is using the first derivation to calculate the spline direction of movement on the spline and move the agent along this normal. You do not have to do exacly that, but what you definatly need is a method to find the current positon of the agent along the spline. This Method works fine for me and it is really fast too. Do not iterate over the whole spline every frame!
|
|
|
Re: Hermite spline - moving with constant speed
[Re: sivan]
#392191
01/19/12 21:51
01/19/12 21:51
|
Joined: May 2010
Posts: 117 Germany , Dortmund
Bone
OP
Member
|
OP
Member
Joined: May 2010
Posts: 117
Germany , Dortmund
|
Thank you krial057, your code works well! Puppeteer, that sounds interesting but Its to complicated for me
Last edited by Bone; 01/19/12 21:54.
|
|
|
|