Page 1 of 1

Joints Start

Posted: Sat Apr 24, 2010 11:21 am
by Xcode
Hey,

1- I have been googling a lot about joints but couldnt find much tutorials. Does anybody has any nice tutorial on joints and constraints.

2- i want to create a rod hanging from a nail. (Pendulum sort effect) and when rod is hit , i should vibrate. Which joint do i have to apply b/w the nail center and the rod upper point ( I guess it should be pivotJoint)

Thanks,

Re: Joints Start

Posted: Sat Apr 24, 2010 11:29 am
by Xcode
Well i have created a pivot point using
cpSpaceAddConstraint(space, cpPivotJointNew2(hookBody,pendulumBody,cpv(0,0),cpv(0,48))); // hookBody is static and pendulumBody is the Rod

and pivot is created as expected. But when something hits rod it does not rotate so definitely i am not getting the pivot points concept correctly. Secondly pivot is only created if i define pendulumBody as

pendulumBody=cpBodyNew(10.0f,INFINITY);

and changing the INFINITY to any value breaks the pivot point.. i even tried using cpMomentForPoly() function but it doesnt help either. Please Help

Re: Joints Start

Posted: Sat Apr 24, 2010 9:01 pm
by Xcode
Hey,,,i have fixed the pivot point ,,but now i have gotten into another issue,,,i will paste full code here

/* Create a body with inifite mass and moment,,i am not adding any shape for this body bcoz that mite collide with the pendulum rod */
cpBody *hookBody=cpBodyNew(INFINITY, INFINITY);
hookBody->p=cpv(100,330);

/* Create the pendulum rod */

cpVect pendulumVerts []={cpv(96,0),cpv(104,0),cpv(104,-96),cpv(96,-96)};
pendulumBody=cpBodyNew(10.0f,cpMomentForPoly(10.0f, 4, pendulumVerts, cpv(0,0)));
pendulumBody->p=cpv(100,330);
cpSpaceAddBody(space, pendulumBody);
cpShape * pendulumShape=cpPolyShapeNew(pendulumBody, 4 , pendulumVerts, cpv(-100,48));
pendulumShape->e=0.5;pendulumShape->u=3;pendulumShape->collision_type=45;
pendulumShape->data=pendulum;
cpSpaceAddShape(space, pendulumShape);
cpSpaceAddConstraint(space, cpPivotJointNew2(hookBody,pendulumBody,cpv(0,0),cpv(0,0)));

and thats all,,, Now i hit the rod with some other object ( ball in my case) but the rod does not rotate.,,, Infact the shape of rotating rod is not updated,,,i keep colliding the ball and it does show that the rod body (pendulumBody) is rotating but shape is not updated. Please help ,,,Thanx

Re: Joints Start

Posted: Sat Apr 24, 2010 10:35 pm
by Xcode
hmmm...i guess i have to change something here in this line

if([shape->data isKindOfClass:[UIView class]]) {

[(UIView *)shape->data setCenter:CGPointMake(shape->body->p.x, 480-shape->body->p.y)]; //I guess i have to apply transformation to view here depending on body's angle;


}
Well lemme try that and i will post the result

Re: Joints Start

Posted: Sat Apr 24, 2010 10:46 pm
by Xcode
Huhhh Yes that does it :)


So i have implemented my first joint successfully :) Thanks slembcke for chimpunk like great stuff :)

Re: Joints Start

Posted: Sat Apr 24, 2010 11:10 pm
by Xcode
Lol now it wont stop rotating,,, is there any kind of joint friction stuff?

I can anticipate that since hookBody got no shape of its own (meaning no friction) , the pendulum body does not meet any frictional force to stop it...Am i right in saying that?

Re: Joints Start

Posted: Sun Apr 25, 2010 10:45 am
by mobilebros
Wow.... I'm going to post this, but I fear by the time I do you might have already found the answer :) I don't think there is any easy way to apply some kind of rotational friction to a pivot without creating your own type of constraint... I could be wrong tho.

Re: Joints Start

Posted: Sun Apr 25, 2010 12:10 pm
by slembcke
Actually there is an easy way to apply rotational friction. Use a cpSimplMotor with a target speed of 0 (the motor wants to stop any relative rotation) and set the maxForce of it to be the amount of friction that you want to apply.

Re: Joints Start

Posted: Sun Apr 25, 2010 1:56 pm
by mobilebros
Thats awesome! Never would have thought to use the motor like that.