New problem.
I need a way to address different body parts of N different ragdolls.
Like this:
player1.head->f.x=1000;
or
player2.shinR->t=-20000;
My plan was to create a RagDoll structure with cpBodies embedded in it.
typedef struct RagDoll
{
float BodyLength;
float BodyFat;
.... blah blah blah...
float Flexibility;
cpBody *head, *shinR, *thighR... etc etc;
}RagDoll;
Next step was writing a ragdoll-adding function.
void adddoll(RagDoll doll) //there is no space parameter yet, because currently I’m only using one space.
{
float BodyLength=doll.BodyLength;
float BodyFat=doll.BodyFat;
.... blah blah blah...
doll.head = cpBodyNew(0.2, cpMomentForCircle(0.2, 0, abs(SKX)*HeadSize, cpvzero));
doll.head->p = cpv(initX+SKX*(HeadSize/5), initY+SKY*(BodyLength/2+HeadSize));
cpSpaceAddBody(space,doll.head);
}
Ragdoll appears, but I can't address it's head. If I do, error appears "Unhandled exception at 0x004049cc in Chipmunk4MSVC2k3.exe: 0xC0000005: Access violation writing location 0x00000024."
Ok. Plan B.
cpBody *head; //additioanal cpBody
void adddoll(RagDoll doll)
{
.... blah blah blah...
head = cpBodyNew(0.2, cpMomentForCircle(0.2, 0, abs(SKX)*HeadSize, cpvzero));
head->p = cpv(initX+SKX*(HeadSize/5), initY+SKY*(BodyLength/2+HeadSize));
cpSpaceAddBody(space, head);
doll.head=head;
}
Same thing.
Plan C.
void adddoll(RagDoll doll)
{
.... blah blah blah...
cpBody *head; //additioanal cpBody, declared outside the function and structure
head = cpBodyNew(0.2, cpMomentForCircle(0.2, 0, abs(SKX)*HeadSize, cpvzero));
head->p = cpv(initX+SKX*(HeadSize/5), initY+SKY*(BodyLength/2+HeadSize));
cpSpaceAddBody(space, head);
}
player1.head=head;
It works! And it works with any number of players! But that's not very convinient.
adddoll(player1);
player1.head=head;
player1.shinR=shinR;
......................
adddoll(player2);
player2.head=head;
player2.shinR=shinR;
.....................
adddoll(player3);
player3.head=head;
player3.shinR=shinR;
etc etc etc
That sucks.
Why, why
adddoll(player1);
.................
doll.head=head; INSIDE the function
is not equivalent to
adddoll(player1);
.................
player1.head=head; OUTSIDE the function
I know it's not a chipmunk question. But I just don't get it (and can't find the answer in the net). Maybe someone here will help me.
Or maybe you can suggest a different method of addressing different parts of a ragdoll (presuming there are N automatically generated ragdolls)?
p.s.: Sorry for my not-so-good English.