Joints Start

Official forum for the Chipmunk2D Physics Library.
Post Reply
Xcode
Posts: 8
Joined: Sat Apr 24, 2010 9:30 am
Contact:

Joints Start

Post 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,
Xcode
Posts: 8
Joined: Sat Apr 24, 2010 9:30 am
Contact:

Re: Joints Start

Post 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
Xcode
Posts: 8
Joined: Sat Apr 24, 2010 9:30 am
Contact:

Re: Joints Start

Post 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
Xcode
Posts: 8
Joined: Sat Apr 24, 2010 9:30 am
Contact:

Re: Joints Start

Post 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
Xcode
Posts: 8
Joined: Sat Apr 24, 2010 9:30 am
Contact:

Re: Joints Start

Post by Xcode »

Huhhh Yes that does it :)


So i have implemented my first joint successfully :) Thanks slembcke for chimpunk like great stuff :)
Xcode
Posts: 8
Joined: Sat Apr 24, 2010 9:30 am
Contact:

Re: Joints Start

Post 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?
mobilebros
Posts: 90
Joined: Tue Aug 04, 2009 9:53 am
Contact:

Re: Joints Start

Post 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.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Joints Start

Post 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.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
mobilebros
Posts: 90
Joined: Tue Aug 04, 2009 9:53 am
Contact:

Re: Joints Start

Post by mobilebros »

Thats awesome! Never would have thought to use the motor like that.
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests