Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.