Box2d: Set active and inactive
Posted
by Rosarch
on Stack Overflow
See other posts from Stack Overflow
or by Rosarch
Published on 2010-04-07T03:33:53Z
Indexed on
2010/04/07
3:43 UTC
Read the original article
Hit count: 1153
I'm writing an XNA game in C# using the XNA port of Box2d - Box2dx.
Entities like trees or zombies are represented as GameObjects. GameObjectManager
adds and removes them from the game world:
/// <summary>
/// Does the work of removing the GameObject.
/// </summary>
/// <param name="controller">The GameObject to be removed.</param>
private void removeGameObjectFromWorld(GameObjectController controller)
{
controllers.Remove(controller);
worldState.Models.Remove(controller.Model);
controller.Model.Body.SetActive(false);
}
public void addGameObjectToWorld(GameObjectController controller)
{
controllers.Add(controller);
worldState.Models.Add(controller.Model);
controller.Model.Body.SetActive(true);
}
controllers
is a collection of GameObjectController
instances.
worldState.Models
is a collection of GameObjectModel
instances.
When I remove GameObjects from Box2d this way, this method gets called:
void IContactListener.EndContact(Contact contact)
{
GameObjectController collider1 = worldQueryUtils.gameObjectOfBody(contact.GetFixtureA().GetBody());
GameObjectController collider2 = worldQueryUtils.gameObjectOfBody(contact.GetFixtureB().GetBody());
collisionRecorder.removeCollision(collider1, collider2);
}
worldQueryUtils:
// this could be cached if we know bodies never change
public GameObjectController gameObjectOfBody(Body body)
{
return worldQueryEngine.GameObjectsForPredicate(x => x.Model.Body == body).Single();
}
This method throws an error:
System.InvalidOperationException was unhandled
Message="Sequence contains no elements"
Source="System.Core"
StackTrace:
at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
at etc
Why is this happening? What can I do to avoid it? This method has been called many times before the body.SetActive()
was called. I feel that this may be messing it up.
© Stack Overflow or respective owner