Skip to content

Confusion regarding lifetimes #1369

Answered by jrouwe
JuanDiegoMontoya asked this question in Q&A
Discussion options

You must be logged in to vote

You cannot create a shape on the stack if you want to attach that shape to a body. The body doesn't make a copy of the shape, it takes a reference. So unless you create and destroy a body within the scope where sphere exists, you'll get crashes.

That said, you can do things like:

SphereShape sphere(1.0f);
sphere.SetEmbedded();
physics_system->GetNarrowPhaseQuery().CollideShape(&sphere, ...);

because then sphere lives long enough for the narrow phase query to be done with it and it is more efficient than:

SphereShape *sphere = new SphereShape(1.0f);
physics_system->GetNarrowPhaseQuery().CollideShape(sphere, ...);
delete sphere;

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@JuanDiegoMontoya
Comment options

Answer selected by JuanDiegoMontoya
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants