admin管理员组文章数量:1389883
Inside of the Update function, if 2 bodies collide I want to remove them (or mark them as needing to be removed, and remove them at the end of the time step). How would I acplish this?
In the Update function I try
var bodyA = this.m_fixtureA.m_body;
...
bodyA.m_world.DestroyBody(bodyA);
However, they don't get deleted. It seems that when I am trying to delete them, this.IsLocked() is set to true.
Inside of the Update function, if 2 bodies collide I want to remove them (or mark them as needing to be removed, and remove them at the end of the time step). How would I acplish this?
In the Update function I try
var bodyA = this.m_fixtureA.m_body;
...
bodyA.m_world.DestroyBody(bodyA);
However, they don't get deleted. It seems that when I am trying to delete them, this.IsLocked() is set to true.
Share Improve this question asked Jan 13, 2013 at 22:36 BoundlessBoundless 2,4642 gold badges26 silver badges40 bronze badges1 Answer
Reset to default 9The world will not remove bodies if the world.IsLocked() function returns true. And world.IsLocked() will return true while the world is in a step. Removing a body during a step could cause issues, so the correct way of destroying bodies after collisions is to register them in a variable and then destroy them after the step is pleted.
//Pseudo code:
var destroy_list = [];
// Your contact listener
var listener = function () {
// Push the body you wish to destroy into an array
destroy_list.push(body);
}
// The game interval function
var update = function () {
// Destroy all bodies in destroy_list
for (var i in destroy_list) {
world.DestroyBody(destroy_list[i]);
}
// Reset the array
destroy_list.length = 0;
}
本文标签: javascriptHow do I remove a body in Box2dWeb after a collisionStack Overflow
版权声明:本文标题:javascript - How do I remove a body in Box2dWeb after a collision - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744581234a2613932.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论