I'm trying to build an inverse kinematics system, starting with the simplest method, without using any angle restriction or anything else like it. My Bone class takes 2 articulations and these articulations get a link to this bone. Anyway, I've tested this before making it into an organized OO system and it works as it should, but it was limited to 2 bones per articulation. Whenever I move an articulation to a new position it calls bone movement which calculates the other articulation's positions and, then, calls that articulation's movement function.
Summarizing, the math behind this system works as I want it to, but an error occurs and I can't find a solution. Here is the code:
Bone definition:
class Bone
{
public:
Bone(Articulation*,Articulation*);
void move(Articulation&,GLfloat,GLfloat,GLfloat,GLfloat);
void mark(bool);
bool marked() const;
void draw() const;
private:
Articulation *m_art1,*m_art2;
bool m_mark;
};
Bone implementation:
Bone::Bone(Articulation *a1 = 0, Articulation *a2 = 0)
{
m_art1 = a1;
m_art2 = a2;
a1->connect_bone(this);
a2->connect_bone(this);
m_mark = false;
}
void Bone::move(Articulation &art, GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2)
{
GLfloat vx,vy,px,py,dist,mod_temp;
px = art.x();
py = art.y();
vx = x1 - px;
vy = y1 - py;
dist = sqrt(vx*vx + vy*vy);
vx = x2 - px;
vy = y2 - py;
mod_temp = sqrt(vx*vx + vy*vy);
vx *= dist/mod_temp;
vy *= dist/mod_temp;
if(m_art1 == &art) {
m_art2->move(x2 - vx,y2 - vy);
}
else {
m_art1->move(x2 - vx,y2 - vy);
}
}
void Bone::mark(bool x)
{
m_mark = x;
}
bool Bone::marked() const
{
return m_mark;
}
void Bone::draw() const
{
glBegin(GL_LINES); {
glVertex2f(m_art1->x(),m_art1->y());
glVertex2f(m_art2->x(),m_art2->y());
}
glEnd();
}
Articulation definition:
class Articulation
{
public:
Articulation(GLfloat,GLfloat);
void connect_bone(Bone*);
void move(GLfloat,GLfloat);
GLfloat x() const;
GLfloat y() const;
private:
GLfloat m_pos_x,m_pos_y;
vector<Bone*> m_bones;
};
Articulation implementation:
Articulation::Articulation(GLfloat x = 0.0, GLfloat y = 0.0)
{
m_pos_x = x;
m_pos_y = y;
}
void Articulation::connect_bone(Bone *bone)
{
m_bones.push_back(bone);
}
void Articulation::move(GLfloat x, GLfloat y)
{
vector<Bone*>::iterator iter = m_bones.begin();
for( ; iter != m_bones.end(); ++iter) {
if(!(*iter)->marked()) {
(*iter)->mark(true);
(*iter)->move(*this,m_pos_x,m_pos_y,x,y);
}
}
for(iter = m_bones.begin(); iter != m_bones.end(); ++iter) {
(*iter)->mark(false);
}
m_pos_x = x;
m_pos_y = y;
}
GLfloat Articulation::x() const
{
return m_pos_x;
}
GLfloat Articulation::y() const
{
return m_pos_y;
}
Starting movement with articulation.move(x,y) gets me an error on Articulation::move, on the very first line of code. Any ideas on why this happens?
Thanks in advance.
[Edited by - Jaiminho on April 9, 2007 8:29:12 PM]