How to rotate around a point?

Official forum for the Chipmunk2D Physics Library.
Post Reply
boca_jrs
Posts: 2
Joined: Tue Dec 22, 2009 6:16 am
Contact:

How to rotate around a point?

Post by boca_jrs »

I created the following:

Code: Select all

	cpVect verts[] = {
		cpv(-3.5, -8.0),
		cpv(-3.5, 8.0),
		cpv( 3.5, 8.0),
		cpv( 3.5, -8.0),
	};

	cpFloat mass = 4.0f;	
	cpFloat moment = cpMomentForPoly(mass, 4, verts, cpvzero);
	
	body = cpBodyNew(mass, moment);	
	cpBodySetPos(body, cpv(37.0f, 31.0f));
	cpBodySetAngle(body, -10.0 * M_PI / 180.0f);

	cpSpaceAddBody(space, body);
	
	shape = cpPolyShapeNew(body, 4, riderTorsoVerts, cpvzero);
...
However, I would like to have the body rotate around (0.0, -8.0) when using cpBodySetAngle and not around (0.0, 0.0). I tried using the offset parameter in momentforpoly and polyshapenew, with no results.

How can I do that?

Thanks!
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: How to rotate around a point?

Post by slembcke »

The rigid body will rotate around it's center of gravity at (0,0). If you want the collision shape to rotate around a different point, you will need to use the offset parameters to move where the shape attaches to the body. Did you move the offset in the wrong direction maybe?
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
boca_jrs
Posts: 2
Joined: Tue Dec 22, 2009 6:16 am
Contact:

Re: How to rotate around a point?

Post by boca_jrs »

Thanks for your answer. I just tried it and it works, but I have now a different issue:

- I just used the offset in cpPolyShapeNew, and not on cpMomentForPoly as I just want to change the rotation point and not the behavior of the body. It now rotates around the right side of the object, but when I add a pin constraint, it just stays 'stuck', as if gravity didn't exist. If I remove the pin constraint, it falls to the ground. Is there a way to modify the rotation point without changing the center of gravity.

Thanks again for your help!

Code: Select all

	cpVect offset, v;
	
	// Lower arm
	cpVect verts[] = {
		cpv(-1.5, -5.5),
		cpv(-1.5, 5.5),
		cpv( 1.5, 5.5),
		cpv( 1.5, -5.5),
	};
	
	offset = cpv(0.0f, 5.5f);
	cpFloat mass = 0.5f;	
	cpFloat moment = cpMomentForPoly(mass, 4, verts, cpvzero);
	
	riderLowerArm = cpBodyNew(riderLowerArmMass, riderLowerArmMoment);	
	
	v = cpv(12.0, 12.0);
	v = cpBodyLocal2World(body2, v);
	v = cpBodyWorld2Local(body1, v);
	
	cpBodySetPos(body2, v);
	cpBodySetAngle(body2, -30.0 * M_PI / 180.0f);
	
	cpSpaceAddBody(space, body2);

	cpShape *shape = cpPolyShapeNew(body2, 4, verts, offset);
	shape->u = 0.5;
	shape->layers = 1;
	shape->group = 2;
	
	cpSpaceAddShape(space, shape);

	// Adding pin
	v2 = cpv(0.0, 0.0);
	v1 = cpBodyLocal2World(body2, v2);
	v1 = cpBodyWorld2Local(body1, v1);
	cpSpaceAddConstraint(space, cpPinJointNew(body2, body1, v2, v1));

User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: How to rotate around a point?

Post by slembcke »

For an object in freefall, the center of gravity is the point it rotates around. If you want it to rotate around a different point, you need to cause it to do so using a pivot joint for example.

If the joint you are using is not causing any rotation, then the joint is probably aligned vertically with the center of gravity of the two bodies involved.

I'm not exactly certain what you are trying to do in the code snippet:

Here you are using coordinate conversions to set the position of body2 based on it's previous (presumably initialized) position/rotation.

Code: Select all

   v = cpv(12.0, 12.0);
   v = cpBodyLocal2World(body2, v);
   v = cpBodyWorld2Local(body1, v);
   
   cpBodySetPos(body2, v);
   cpBodySetAngle(body2, -30.0 * M_PI / 180.0f);
Here you are attaching the pin joint to the center of gravity of body2. This will cause no rotation of body2. You are also creating a 0 length pin joint which should be avoided. If you want a zero length pin joint, you really want a pivot joint instead which does the same thing only it does it in a much more stable way. What exactly to body1 and body2 represent?

Code: Select all

   v2 = cpv(0.0, 0.0);
   v1 = cpBodyLocal2World(body2, v2);
   v1 = cpBodyWorld2Local(body1, v1);
   cpSpaceAddConstraint(space, cpPinJointNew(body2, body1, v2, v1));

Did you mean to do something with these variables? They were never used again.

Code: Select all

   cpFloat mass = 0.5f;   
   cpFloat moment = cpMomentForPoly(mass, 4, verts, cpvzero);
   
   riderLowerArm = cpBodyNew(riderLowerArmMass, riderLowerArmMoment);   
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests