Changing entity shape type on the fly?

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
Spankenstein
Posts: 249
Joined: Wed Nov 17, 2010 1:49 pm

Changing entity shape type on the fly?

Post by Spankenstein »

I've got a method that can take any entity and change its shape on the fly:

Code: Select all

        protected void CreateBasicObject(Shape shape, Vector3 scale, Vector3 rotation, Vector3 position, float mass)
        {
            if (entity != null)
            {
                space.Remove(entity);
            }

            switch (shape)
            {
                case Shape.Box:
                    {
                        entity = mass == float.MaxValue ? new Box(position, scaleFactor, scaleFactor, scaleFactor) : new Box(position, scaleFactor, scaleFactor, scaleFactor, mass);
                        entity.InternalOrientationMatrix = Matrix.CreateFromYawPitchRoll(rotation.Y, rotation.X, rotation.Z);
                        break;
                    }
                case Shape.Capsule:
                    {
                        entity = mass == float.MaxValue ? new Capsule(position, scaleFactor, scaleFactor * 0.5f) : new Capsule(position, scaleFactor, scaleFactor * 0.5f, mass);
                        entity.InternalOrientationMatrix = Matrix.CreateFromYawPitchRoll(rotation.Y, rotation.X, rotation.Z);
                        break;
                    }
                case Shape.Cone:
                    {
                        entity = mass == float.MaxValue ? new Cone(position, scaleFactor, scaleFactor * 0.5f) : new Cone(position, scaleFactor, scaleFactor * 0.5f, mass);
                        entity.InternalOrientationMatrix = Matrix.CreateFromYawPitchRoll(rotation.Y, rotation.X, rotation.Z);
                        break;
                    }
                case Shape.Cylinder:
                    {
                        entity = mass == float.MaxValue ? new Cylinder(position, scaleFactor, scaleFactor * 0.5f) : new Cylinder(position, scaleFactor, scaleFactor * 0.5f, mass);
                        entity.InternalOrientationMatrix = Matrix.CreateFromYawPitchRoll(rotation.Y, rotation.X, rotation.Z);
                        break;
                    }
                case Shape.Sphere:
                    {
                        entity = mass == float.MaxValue ? new Sphere(position, scaleFactor * 0.5f) : new Sphere(position, scaleFactor * 0.5f, mass);
                        entity.InternalOrientationMatrix = Matrix.CreateFromYawPitchRoll(rotation.Y, rotation.X, rotation.Z);
                        break;
                    }
                default:
                    {
                        entity = mass == float.MaxValue ? new Box(position, scaleFactor, scaleFactor, scaleFactor) : new Box(position, scaleFactor, scaleFactor, scaleFactor, mass);
                        entity.InternalOrientationMatrix = Matrix.CreateFromYawPitchRoll(rotation.Y, rotation.X, rotation.Z);
                        break;
                    }
            }

            space.Add(entity);
        }
The problem is that this method causes the shape to occasionally draw at (0, 0, 0) for a single frame before being drawn in its original place.

How else can I change the shape of an entity without encountering this problem?
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Changing entity shape type on the fly?

Post by Norbo »

You could try calling Space.ForceBufferedStateUpdate() or performing the logic before a frame's space update. The issue is that the buffered states like CenterPosition won't get updated until a buffered states update at the end of a frame (or you force an update).

If you're not making use of the interpolation or asynchronous access features of the buffered states, you could also just ignore them and use the InternalCenterPosition to render.

Also in v0.15.0, dynamically changing shapes is becoming more directly supported :)
Spankenstein
Posts: 249
Joined: Wed Nov 17, 2010 1:49 pm

Re: Changing entity shape type on the fly?

Post by Spankenstein »

Norbo wrote: If you're not making use of the interpolation or asynchronous access features of the buffered states, you could also just ignore them and use the InternalCenterPosition to render.
Perfect!
Norbo wrote: Also in v0.15.0, dynamically changing shapes is becoming more directly supported :)
Gonna be great! :)
Post Reply