Adding a shape to a body

Official forum for the Chipmunk2D Physics Library.
Post Reply
Bliddo
Posts: 3
Joined: Mon Jul 27, 2009 11:55 am
Contact:

Adding a shape to a body

Post by Bliddo »

Hi.
I'm trying to figure out how to add at runtime a shape to a body, keeping the distance from the two objects... I'll try to explain :D
I have the player floating on the screen, when it collides with an object, the player can pick up the object by pressing one button.
Right now I'm using this:

Code: Select all

int grabObject(cpShape *a, cpShape *b, cpContact *contacts, int numContacts, cpFloat normal_coef, void *data)
{
	if(_objCaught == false && _beamOn)
	{
		b->body = a->body;

		_objCaught = true;
	}

	return _objCaught;
}
The player and the object are defined as a box with coords in [1, -1]. The player has a cone shape attached to it, that should work as a winch, when an object collides with this cone, the above code is called.
Right now the object is picked up, but it translates right under the player's position, I want it to stay at the relative distance from the player that it is when the player picks it up... so, in other words, I want the object to stay attached to the winch, not under the player.
Any ideas?


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

Re: Adding a shape to a body

Post by slembcke »

You either need to make a new shape with an offset, or set the offset on the original shape. You might also want to adjust the mass and moment of inertia of the player character.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Bliddo
Posts: 3
Joined: Mon Jul 27, 2009 11:55 am
Contact:

Re: Adding a shape to a body

Post by Bliddo »

Thanks.

Though I'm considering using a joint, it would make things easier, but I can't quite figure out where to put the two anchors.
I tried a pinjoint, with the two anchors set to cpvzero, the object is grabbed correctly, but while moving, the object is free to rotate around the player, like it was connected by a rod. How can I make the joint stiff?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Adding a shape to a body

Post by slembcke »

You'll need at least two joints to make it stiff, and that means that it won't really be that stiff. Not using joints would be simpler, faster, and more numerically stable.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Bliddo
Posts: 3
Joined: Mon Jul 27, 2009 11:55 am
Contact:

Re: Adding a shape to a body

Post by Bliddo »

I see.
Which is the best way to set an offset of a shape?
I tried this, but I doesn't work fine

Code: Select all

cpPolyShape *poly = (cpPolyShape *)b;
		
		int num = poly->numVerts;
		cpVect *verts = poly->verts;
		
		cpVect offset;
		offset.x = b->body->p.x - a->body->p.x;
		offset.y = b->body->p.y - a->body->p.y;
		
		for(int i=0; i<num; i++)
			verts[i] = cpvadd(verts[i], offset);
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Adding a shape to a body

Post by slembcke »

You need to take into account the current rotation of the shapes as well.

Also, why this:

Code: Select all

      cpVect offset;
      offset.x = b->body->p.x - a->body->p.x;
      offset.y = b->body->p.y - a->body->p.y;
Instead of just this:

Code: Select all

cpVect offset = cpvsub(b->body->p, a->body->p);
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 15 guests