[SOLVED] ConvexHull position incorrect

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
sergiusz308
Posts: 49
Joined: Mon Oct 08, 2012 4:57 pm

[SOLVED] ConvexHull position incorrect

Post by sergiusz308 »

Hi Norbo, I got one of your favorite :)

This is a modified scene from Bepu demos. It has three entities:
bepu_Wtf2.jpg
bepu_Wtf2.jpg (26.01 KiB) Viewed 8710 times
As you can see there is a problem with "A" entity - there's a slight gap between "A" and "B" (blue arrow) - they're supposed to be placed next to each other - side by side.

"B" and "C" are boxes and "A" is a convexhull, created from vertices imported from external source - just Vector3s. All them have positions from external source, set after creation. I'm using bepu model drawer, no other changes were made.

"A" is offseted by about .129 units up and down. I tried to offset it's position by convexhull computedcenter - no luck.

I inspected "A" entity after adding to Space and it's position is set to what I want - so it looks like model drawer is messing here?

Thanks,
S.
Last edited by sergiusz308 on Wed Dec 04, 2013 10:14 am, edited 1 time in total.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: ConvexHull position incorrect

Post by Norbo »

"B" and "C" are boxes and "A" is a convexhull, created from vertices imported from external source - just Vector3s. All them have positions from external source, set after creation. I'm using bepu model drawer, no other changes were made.
...
I inspected "A" entity after adding to Space and it's position is set to what I want
Since convex hulls recenter themselves based on the computed center of mass, the external source's position for the convex hull is probably not consistent.
I tried to offset it's position by convexhull computedcenter - no luck.
In what way did it fail?

Is the convex hull dynamic? If the problem isn't actually related to the standard recentering issues, it may be that the collision margin of the convex hull (not visualized by default by the model drawer) is pushing the entities apart. If you would like to visualize collision margins in convex hulls, change line 65 in the ModelDrawer from

Code: Select all

shapeMeshGetters.Add(typeof(ConvexCollidable<ConvexHullShape>), DisplayConvexHull.GetShapeMeshData);
to

Code: Select all

shapeMeshGetters.Add(typeof(ConvexCollidable<ConvexHullShape>), DisplayConvex.GetShapeMeshData);
If you post a little code for the demos, I could take a direct look too.
sergiusz308
Posts: 49
Joined: Mon Oct 08, 2012 4:57 pm

Re: ConvexHull position incorrect

Post by sergiusz308 »

Code is dead simple, here is CH creation:

Code: Select all

if (indxc > 0)
{
	verts = new List<Vector3>(indxc);
	
											for (int n = 0; n < verts.Capacity; n++)
											{
												// positions
												verts.Add(new Vector3(br.ReadSingle(), br.ReadSingle(), br.ReadSingle()));
												// normals
												//br.ReadSingle();
												//br.ReadSingle();
												//br.ReadSingle();
											}

											ConvexHull ch = new ConvexHull(pos, verts);
											ch.Position = pos;
											ch.Orientation = orient;
											entities.Add(ch);
										}
After that I just loop through entities list and add elements to the Space.

Here are vertices:

Code: Select all

new Vector3(-1.032346,0,1.459618);
new Vector3(-1.032346,0,-1.459618);
new Vector3(1.032346,0,-1.459618);
new Vector3(1.032346,0,1.459618);
new Vector3(-1.032346,0.4363835,1.459618);
new Vector3(1.032346,0.9600016,1.459618);
new Vector3(1.032346,0.9600016,-1.459618);
new Vector3(-1.032346,0.4363835,-1.459618);
new Vector3(-1.032346,0,1.459618);
new Vector3(1.032346,0,1.459618);
new Vector3(1.032346,0.9600016,1.459618);
new Vector3(-1.032346,0.4363835,1.459618);
new Vector3(1.032346,0,1.459618);
new Vector3(1.032346,0,-1.459618);
new Vector3(1.032346,0.9600016,-1.459618);
new Vector3(1.032346,0.9600016,1.459618);
new Vector3(1.032346,0,-1.459618);
new Vector3(-1.032346,0,-1.459618);
new Vector3(-1.032346,0.4363835,-1.459618);
new Vector3(1.032346,0.9600016,-1.459618);
new Vector3(-1.032346,0,-1.459618);
new Vector3(-1.032346,0,1.459618);
new Vector3(-1.032346,0.4363835,1.459618);
new Vector3(-1.032346,0.4363835,-1.459618);
Computed center failed to do any change because it was very small values, like 0.00000035 on each axis.

I think we defenetely need an alternative method that builds convexhull around arbitary position - without that it's impossible to build any kind of structure of other shapes than primitives (box, sphere, cylinder) that hold exact positions.

If I replace "A" entity with another box they hold exact positions, as it comes from 3d package.

I tried changes to ModelDrawer you suggested but it can't find DisplayConvex type.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: ConvexHull position incorrect

Post by Norbo »

Computed center failed to do any change because it was very small values, like 0.00000035 on each axis.
Plopping those vertices into the demos, I get a computed center of {0.1290368, 0.3654585, 0}.

For reference, here's what I did:

Code: Select all

            var vertices = new List<Vector3>
                {
                    new Vector3(-1.032346f,0,1.459618f),
                    new Vector3(-1.032346f,0,-1.459618f),
                    new Vector3(1.032346f,0,-1.459618f),
                    new Vector3(1.032346f,0,1.459618f),
                    new Vector3(-1.032346f,0.4363835f,1.459618f),
                    new Vector3(1.032346f,0.9600016f,1.459618f),
                    new Vector3(1.032346f,0.9600016f,-1.459618f),
                    new Vector3(-1.032346f,0.4363835f,-1.459618f),
                    new Vector3(-1.032346f,0,1.459618f),
                    new Vector3(1.032346f,0,1.459618f),
                    new Vector3(1.032346f,0.9600016f,1.459618f),
                    new Vector3(-1.032346f,0.4363835f,1.459618f),
                    new Vector3(1.032346f,0,1.459618f),
                    new Vector3(1.032346f,0,-1.459618f),
                    new Vector3(1.032346f,0.9600016f,-1.459618f),
                    new Vector3(1.032346f,0.9600016f,1.459618f),
                    new Vector3(1.032346f,0,-1.459618f),
                    new Vector3(-1.032346f,0,-1.459618f),
                    new Vector3(-1.032346f,0.4363835f,-1.459618f),
                    new Vector3(1.032346f,0.9600016f,-1.459618f),
                    new Vector3(-1.032346f,0,-1.459618f),
                    new Vector3(-1.032346f,0,1.459618f),
                    new Vector3(-1.032346f,0.4363835f,1.459618f),
                    new Vector3(-1.032346f,0.4363835f,-1.459618f)
                };

            Vector3 center;
            ConvexHullShape convexHullShape = new ConvexHullShape(vertices, out center);
I think we defenetely need an alternative method that builds convexhull around arbitary position - without that it's impossible to build any kind of structure of other shapes than primitives (box, sphere, cylinder) that hold exact positions.
That is indeed the specific purpose of outputting the center. It allows exact positioning.
I tried changes to ModelDrawer you suggested but it can't find DisplayConvex type.
That implies an old version; try using the latest version. I don't recall any bugs in the older convex hull center computation, but it's worth a try.
sergiusz308
Posts: 49
Joined: Mon Oct 08, 2012 4:57 pm

Re: ConvexHull position incorrect

Post by sergiusz308 »

Could you please test such a scenario

- convexhull and vertices from previous post, Position: {-2,100759, 0, 0}
- box, width: 3.61321259, length: 4.40804768, Position: {0,7381939, 0, 0}

Both with zero mass.

This should result in entities "A" and "B" from original scene - how does it look on your side?

I got the same computed center, tried to set it to CollisionInformation.LocalPosition, substract / add to Position - it's still incorrect, not touching "B" entity.

Here is the same scene with box "A" entity, the same position that convexhull has and how it should look:
bepu_Wtf2a.jpg
bepu_Wtf2a.jpg (16.05 KiB) Viewed 8695 times
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: ConvexHull position incorrect

Post by Norbo »

If the vertices and positions are used directly:

Code: Select all

            Vector3 center;
            ConvexHullShape convexHullShape = new ConvexHullShape(vertices, out center);
            var entity = new Entity(convexHullShape);
            entity.Position = new Vector3(-2.100759f, 0, 0)1;

            var box = new Box(new Vector3(0.7381939f, 0, 0), 3.61321259f, 0.9600016f, 4.40804768f);
gap.png
gap.png (21.14 KiB) Viewed 8692 times
A gap! This is expected, because setting an entity's position puts the entity's center of mass right at that position. The local origin in the external program did not use the center of mass for the position, so there's an offset.

If the computed center is then added to the position of the convex hull:

Code: Select all

            Vector3 center;
            ConvexHullShape convexHullShape = new ConvexHullShape(vertices, out center);
            var entity = new Entity(convexHullShape);
            entity.Position = new Vector3(-2.100759f, 0, 0) + center;

            var box = new Box(new Vector3(0.7381939f, 0, 0), 3.61321259f, 0.9600016f, 4.40804768f);
nogap.png
nogap.png (20.99 KiB) Viewed 8692 times
They now touch! But it's still probably not quite what you want; I assume the wedge should be flush on top and bottom with the box.
Because the convex hull has a base y coordinate of 0, it's probably the case that the box's position is similar- that is, the origin of the box in the external program was at the bottom of the box, not at the center of mass.

If half the height of the box is added to the position of the box:

Code: Select all

            Vector3 center;
            ConvexHullShape convexHullShape = new ConvexHullShape(vertices, out center);
            var entity = new Entity(convexHullShape);
            entity.Position = new Vector3(-2.100759f, 0, 0) + center;

            var box = new Box(new Vector3(0.7381939f, 0, 0), 3.61321259f, 0.9600016f, 4.40804768f);
            box.Position += new Vector3(0, box.HalfHeight, 0);
nooffseteither.png
nooffseteither.png (21.4 KiB) Viewed 8692 times
No gap, no vertical offset!

While I'd like to make this stuff easier and more obvious, it's difficult to address from within the engine.
-The engine can't trust the origin supplied by the external program to be anything near the center of mass. If it did, just getting proper rotational behavior would inherit this complexity.
-Adding additional layers of helpers could help in isolated cases, but the extra layers of apparent magic would make it harder to understand exactly what was going on in the general case.
At its core, this is a content pipeline issue. The content pipeline can produce arbitrary mesh data and positions. The engine has no direct control over the pipeline; all it can do is define a standard for its input and hope the content pipeline satisfies its requirements.
sergiusz308
Posts: 49
Joined: Mon Oct 08, 2012 4:57 pm

Re: ConvexHull position incorrect

Post by sergiusz308 »

Norbo, your reply should be pinned somewhere, IMHO it perfectly walks engine users through various problems related to physics objects positioning and related content pipeline considerations.

Thanks, it works, problem solved, again :)
Post Reply