Game Development Reference
In-Depth Information
double mag, theta, sign, sqdist;
VECTOR pe, pt;
//Check solution is possible
x = target.x - link->pos.x;
y = -target.y - link->pos.y;
totallength = sqrt(x*x + y*y);
//Find the last link and links length
while (link->next){
linkslength += link->length;
lastlink = link;
link = link->next;
}
if (totallength < linkslength){
//Calculate Cyclic Coordinate Descent solution
link = lastlink;
while(link && loopcount<20){
//Calculate current endeffector position
endeffector.x = lastlink->length * lastlink->right.x +
lastlink->pos.x;
endeffector.y = lastlink->length * lastlink->right.y +
lastlink->pos.y;
//Calculate the squared distance from end effector to
//target
x = target.x - endeffector.x;
y = target.y - endeffector.y;
sqdist = x*x + y*y;
if (sqdist < 0.1) break;
//Calculate pivot to target vector
pt.x = target.x - link->pos.x;
pt.y = target.y - link->pos.y;
pt.z = 0;
//Calculate pivot to end effector vector
pe.x = endeffector.x - link->pos.x;
pe.y = endeffector.y - link->pos.y;
pe.z = 0;
Search Nedrilad ::




Custom Search