bepu / bepuphysics2

Pure C# 3D real time physics simulation library, now with a higher version number.
Apache License 2.0
2.4k stars 274 forks source link

Shapes.Add() problem #93

Closed CharlesWoodhill closed 4 years ago

CharlesWoodhill commented 4 years ago

in order to implement the struct interfaces for my VoxTree i needed a way to get acess to my classes, but the use of some of the structs as template parameter permits nullable type members so i added a GcHandle (which is a struct) to store my class ref (please tell me if u know a better way :) )

i think it would work IF there wouldnt be that in the Shapes class:

        public TypedIndex Add<TShape>(in TShape shape) where TShape : unmanaged, IShape
        {
            .....
            if (batches[typeId] == null)
            {
                batches[typeId] = default(TShape).CreateShapeBatch(pool, InitialCapacityPerTypeBatch, this);
            }
            .....
        }

here default(TShape) is used instead of the the paramater shape itself, which uses a diffrent shape instead of my "decorated" one :) please tell me its a bug and not a necessity :)

CharlesWoodhill commented 4 years ago

i dared to edit your code, and changed it to the parameter. at least for a not advancing sim, raycasting seem to work now :)

RossNordby commented 4 years ago

Creating a shape batch should be independent of any shape instance, since the shape batch is meant to hold many instances of the same type.

Using the input shape instead of default(TShape) will not directly cause problems, but it does imply a dependency on a particular instance that shouldn't exist. In other words, CreateShapeBatch is one of those functions that would be static if C# allowed that pattern.

Typically references to managed data from an unmanaged context can be handled by a simple integer index rather than a full GC handle. This does require some way to look up the desired information from whatever context uses it, of course- depending on where it's being used, that might be difficult. Biting the bullet on a fully unmanaged shape often works out better.

CharlesWoodhill commented 4 years ago

but my voxelShapes have diffrent data, each voxelShape has its own tree, only "workaround" for this im aware of would be adding each shape with a diffrent type id, and some static/singleton lookup with the typeId.

RossNordby commented 4 years ago

Every Mesh shape can have its own Tree, too. No need for additional type ids- the shape instance just stores the required data (and pointers to data).

CharlesWoodhill commented 4 years ago

i have the data too in the shape instance, but the problem i have is, that for some reason an additional "instance" is used for a call, that one is default(shape) and doesnt have this data. so i get null crash. wait i change back the code so i can tell where it happens.

CharlesWoodhill commented 4 years ago

hmm cant reproduce...have changed much meanwhile...so it must have been some bug in my own code.