I essentially have a long stick jointed to a large stand. At one end of the stick I jointed a heavy weight so that the stick gets turned upwards very quickly. Sounds simple enough, here are my two problems:
1. For this to work, the weight has to be much havier than the long stick. I did this by changing it's mass to 5 times that of the stick. When I start this setup, everything happens as planned and the stick moves up quickly - but the stick is spazzing out (e.g. moving violently around the joint).
I guess the reason for this is that the force applied to the joint is too big (?). What is the correct way to do this?
2. Once something touches the weight, the weight keeps on slowly rotating at a constant speed and seems to be completely ignoring gravity. This might indicate what I have done incorrectly (?).
Anyways, here is the code:
Code: Select all
//global parameters
float all_e = 0.1f;
float all_u = 1.0f;
float all_w = 1.0f;
float all_m = 1.0f; // the first problem can be somewhat changed by the moment, but it still persists. also it's not much of a catapult if the arm moves slowly. :)
int vertnumarm = 4;
cpVect vertsarm[] = {
cpv(-175,-10),
cpv(-175, 10),
cpv( 175, 10),
cpv( 175,-10),
};
int vertnumstand = 4;
cpVect vertsstand[] = {
cpv(-125,-225),
cpv(-15, 125),
cpv( 15, 125),
cpv( 125,-225),
};
int vertnumweight = 3;
cpVect vertsweight[] = {
cpv(-50,-100),
cpv( 0, 0),
cpv( 50,-100),
};
cpShape *shape;
//create the weight
cpBody *stand;
stand = cpBodyNew(all_w, cpMomentForPoly(all_m, vertnumstand, vertsstand, cpvzero));
stand->p = cpvzero;
cpSpaceAddBody(space, stand);
shape = cpPolyShapeNew(stand, vertnumstand, vertsstand, cpv(0,0));
shape->e = all_e; shape->u = all_u;
stand->cpBodySetMass
shape->data = standgameobj;
shape->collision_type = 9000;//this type does not collide with itself
cpSpaceAddShape(space, shape);
//create the arm (aka long stick)
cpVect armoffset = cpv (-120,80);
cpBody *arm;
arm = cpBodyNew(all_w, cpMomentForPoly(all_m, vertnumarm, vertsarm, cpvzero));
arm->p = armoffset;
cpSpaceAddBody(space, arm);
shape = cpPolyShapeNew(arm, vertnumarm, vertsarm, cpvzero);
shape->e = all_e; shape->u = all_u;
shape->data = armgameobj;
shape->collision_type = 9000;
cpSpaceAddShape(space, shape);
//create the weight
cpVect weightoffset = cpv(50,armoffset.y);
cpBody *weight;
weight = cpBodyNew(8.0f, cpMomentForPoly(all_m, vertnumweight, vertsweight, cpvzero));//notice the 8.0f! this is supposed to be heavy!
weight->p = weightoffset;
cpSpaceAddBody(space, weight);
shape = cpPolyShapeNew(weight, vertnumweight, vertsweight, cpvzero);
shape->e = all_e; shape->u = all_u;
shape->data = weightgameobj;
shape->collision_type = 9000;
cpSpaceAddShape(space, shape);
//join everything - Just ignore the offsets, they might be a little confusing without context. ;)
cpJoint *joint1 = cpPinJointNew(stand, arm, cpv(0,armoffset.y), cpv(-armoffset.x,0));
cpSpaceAddJoint(space, joint1);
cpJoint *joint2 = cpPinJointNew(arm, weight, cpv(weightoffset.x-armoffset.x,0), cpv(0,0));//<- This is the problematic one
cpSpaceAddJoint(space, joint2);
