# How do I copy box2d data from one body to another?

I want to copy all Box2D-related data (position, velocity, rotation, impulse, ...) from one body to another.

This is what I've got working (with box2djsweb):

var t = body.GetTransform();
other_body.SetTransform(t);

var vel = body.GetLinearVelocity();
other_body.SetLinearVelocity(vel);


I think that covers most things, but judging by the behaviour, I've missed something. It's as if it's a frame or tick behind. Also, this isn't placing the other_body in the exact location as the the original body.

Is there a better way to do this?

Box2D has a CreateBody function in the b2World class that takes another body as a definition. Which uses the 'definition body' to create a new body. This does not copy the fixtures/shapes attached to the body.

However, I've not tested this function so I don't know if you'll experience the 'step behind' issue. I imagine that may have to do with when you're creating the copy, but it could also be from box2D queuing up the new body, and it only gets added to the world on the next frame and first simulated on the frame after that (putting it a frame behind).

It'll take some testing, and you might have to simulate a step yourself and make a copy of that or only copy bodies that are at rest.