Sign in to follow this  
Jaiminho

[SOLVED] Error on class using Inverse Kinematics

Recommended Posts

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]

Share this post


Link to post
Share on other sites
What specifically is the error that's occurring? Check that art1 and art2 are valid pointers, if m_bones.begin() is failing then odds are the m_bones object is corrupt, which could result from Articulation::move() with a bad pointer. For example the following would cause an access violation:

Atriculation* art = 0;
art->move(1,2);

Share this post


Link to post
Share on other sites
I've tested all pointers and all of them point to the right object in any case I tested.

The error occurs in this command line:
vector<Bone*>::iterator iter = m_bones.begin();

I tried, a few minutes after posting here, a version of this class without using std::vector, limiting 2 bones per articulation, and still got an error on the very first command inside the Articulation::move method. GDB crashes anytime I try to run it on debug mode (maybe because of OpenGL -- happened same with some other OpenGL test programs), so I can't check which is the error signal. When print-debugging, I can print stuff to console inside Articulation::move, before that very first command -- this is what I can't figure out: it gets inside the method, runs stuff inside it, but fails to access any of that object's member variables.

Share this post


Link to post
Share on other sites
Just printed everything I could and found out:

- On Bone constructor (only place where I modify m_art1 and m_art2), it all seems fine: at the end of it, all pointers are as they should
- Calling Articulation::move to the first one, Bone address is correct and it calls Bone::move just fine
- Now, inside Bone::move, m_art1 and m_art2 are all messed up. On this printage, I got 0x00004000 and 0x00000000, while this was still correct.

Something messed up these two pointers and I can't figure what did it.

On main(), I was using a std::vector<Bone>. Just tested with a single Bone object and it didn't get me this error. Still, I'm only using push_back() to insert elements and at() to access vector positions, instead of operator[], so I don't know what could cause this.

Share this post


Link to post
Share on other sites
Are you certain the Bone pointers you're inserting into m_bones are correct in the first place? The only other suggestion I can think of is try creating a simple test app that only uses the Bone and Articulation classes, and assuming that works gradually copy across code related to those classes until you can reproduce the problem. Hopefully then you'll be able to use a debugger to step through it.

Share this post


Link to post
Share on other sites
Done what you suggested on making it as simple as possible on tests and it works, now. BTW, I didn't define destructors (somehow, I forgot to) and, who knows, that may have caused the error.

Anyway... thanks for y'all help. It's solved!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this