Remove triangleMesh from space

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
Foreigner
Posts: 3
Joined: Fri Aug 20, 2010 3:49 pm

Remove triangleMesh from space

Post by Foreigner »

Is there a difference between a entity (Box, Sphere etc.) and a triangle mesh, if you want to delete them
from physics space? When i delete them after a collision, i see this error a few updates later: "Cannot
remove entity from this space as it belongs to a different or no space"
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Remove triangleMesh from space

Post by Norbo »

A TriangleMesh is just a data structure, and a StaticTriangleGroup that uses a TriangleMesh is just a manager of entities, not an entity itself.

When you see that exception, that means something is trying to remove the same entity more than once. If you manually remove a Triangle that is managed by a StaticTriangleGroup, the StaticTriangleGroup will get confused. If you want to remove the StaticTriangleGroup instead of individual triangles, try setting the StaticTriangleGroup's tag to itself. Individual triangles managed by the group will have it as their tag property. If you need to protect against removing the StaticTriangleGroup multiple times, test the group's space first. If it's null, it's been removed.

In v0.15.0, the way entities and StaticTriangleGroups are handled are going to blur quite a bit which will make this sort of thing more straightforward.
Foreigner
Posts: 3
Joined: Fri Aug 20, 2010 3:49 pm

Re: Remove triangleMesh from space

Post by Foreigner »

If i set "triangleGroup.tag = triangleGroup" instead "triangleGroup.tag = staticModel " , then i cant use it with "space.remove(other)"
I don't now exactly what this mean. How do I delete a group instead of an entity?

My code:
(in LoadContent)
physicsSphere.eventManager.addEventHook(new EventHandlerInitialCollisionDetected(handleSphereCollision));
...
StaticTriangleGroup.StaticTriangleGroupVertex[] vertices;
int[] indices;
StaticTriangleGroup.getVerticesAndIndicesFromModel(meshModel, out vertices, out indices);
TriangleMesh triangleMesh = new TriangleMesh(vertices, indices);
StaticTriangleGroup triangleGroup = new StaticTriangleGroup(triangleMesh);
triangleGroup.worldMatrix = Matrix.CreateTranslation(new Vector3(0, 0, 0)) * Matrix.CreateScale(3f);
staticModel = new StaticModel(meshModel, triangleMesh.worldMatrix, Game);
dGame.sceneManager.Add(staticModel);
triangleGroup.tag = staticModel;
//triangleGroup.tag = triangleGroup;
myGame.physicSpace.add(triangleGroup);
...

public void handleSphereCollision(Entity sender, Entity other, CollisionPair pair)
{
myGame.physicSpace.remove(other);
dGame.sceneManager.Remove((IEntity)other.tag);
}
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Remove triangleMesh from space

Post by Norbo »

Space.remove removes entities and a variety of other types, like the StaticTriangleGroup. All you need to do is call Space.remove and give it the StaticTriangleGroup. The question is how you get that data into your event handler; if your tag needs to be a StaticModel, consider adding a StaticTriangleGroup field to the StaticModel so that you can reference the StaticTriangleGroup using a StaticModel tag. Pass the StaticTriangleGroup reference to the Space.remove method.

Obviously, you'd want to verify that what you're colliding with is actually a StaticTriangleGroup before doing any of this. If "other" is not a triangle or if its tag isn't the right type, then you should ignore it.

By the way, dynamically messing with StaticTriangleGroups isn't recommended. It works, but there's a better option in most cases depending on what you're trying to do.
Foreigner
Posts: 3
Joined: Fri Aug 20, 2010 3:49 pm

Re: Remove triangleMesh from space

Post by Foreigner »

Sometimes I wonder how I can use a computer. I forgot the cast from object to StaticTriangleGroup.
I extended my StaticModel.cs too. Thank you!

public void handleSphereCollision(Entity sender, Entity other, CollisionPair pair)
{
StaticModel sm = (StaticModel)other.tag;
StaticTriangleGroup stg = (StaticTriangleGroup)sm.tag;
if (stg.space != null)
myGame.physicSpace.remove(stg);
dGame.sceneManager.Remove((IEntity)other.tag);
}
Post Reply