Page 1 of 1

Changing entity shape type on the fly?

Posted: Thu Dec 16, 2010 3:57 pm
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?

Re: Changing entity shape type on the fly?

Posted: Thu Dec 16, 2010 4:12 pm
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 :)

Re: Changing entity shape type on the fly?

Posted: Thu Dec 16, 2010 4:27 pm
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! :)