Vertices.CreateCapsule

Jan 22, 2009 at 5:12 PM
here is my code for creating capsule objects which are useful for platform characters because the allow the character to smoothly walk up hills. currently I use PolyGeom for creating capsule shapes and PolyBody for calculating the MOI because I don't know how to calculate the MOI and simplify the formula for capsules.

@genbox: it would be create if you could implement this in the official Farseer release, so i don't have to re-implement it on every update :)

        /// <summary>
        /// Creates an capsule with the specified total height, radius and number of edges.
        /// </summary>
        /// <param name="totalHeight">Total height (inner height + 2 * radius) of the capsule.</param>
        /// <param name="radius">Radius of the capsule ends.</param>
        /// <param name="edges">The number of edges of the capsule ends. The more edges, the more it resembles an capsule</param>
        /// <returns></returns>
        public static Vertices CreateCapsule( float totalHeight, float radius, int edges )
                : this( totalHeight, radius, edges, radius, edges )
        {}

        /// <summary>
        /// Creates an capsule with the specified total height, radius and number of edges.
        /// </summary>
        /// <param name="totalHeight">Total height (inner height + radii) of the capsule.</param>
        /// <param name="topRadius">Radius of the top.</param>
        /// <param name="topEdges">The number of edges of the top. The more edges, the more it resembles an capsule</param>
        /// <param name="bottomRadius">Radius of bottom.</param>
        /// <param name="bottomEdges">The number of edges of the bottom. The more edges, the more it resembles an capsule</param>
        /// <returns></returns>
        public static Vertices CreateCapsule( float totalHeight, float topRadius, int topEdges, float bottomRadius, int bottomEdges )
        {
            Vertices vertices = new Vertices();

            float height05 = (totalHeight - topRadius - bottomRadius) * 0.5f;
            float stepSize;

            // top
            vertices.Add( new Vector2( topRadius, height05 ) );

            stepSize = MathHelper.Pi/topEdges;
            for( int i = 1; i < topEdges - 1; i++ )
            {
                vertices.Add( new Vector2( topRadius * Calculator.Cos( stepSize * i ), topRadius * Calculator.Sin( stepSize * i ) + height05 ) );
            }

            vertices.Add( new Vector2( -topRadius, height05 ) );

            // bottom
            vertices.Add( new Vector2( -bottomRadius, -height05 ) );

            stepSize = MathHelper.Pi/bottomEdges;
            for( int i = 1; i < bottomEdges - 1; i++ )
            {
                vertices.Add( new Vector2( -bottomRadius * Calculator.Cos( stepSize * i ), -bottomRadius * Calculator.Sin( stepSize * i ) - height05 ) );
            }

            vertices.Add( new Vector2( bottomRadius, -height05 ) );

            return vertices;
        }
        
Coordinator
Jan 22, 2009 at 5:32 PM
Thanks yobiv. I will take a look at it later. Do you have a picture of what this capsule-shape looks like?
Jan 23, 2009 at 12:48 PM
Edited Jan 24, 2009 at 9:13 AM
the dragon egg on left (different top and bottom radius) or the character shape on the right (red)
Coordinator
Jan 23, 2009 at 5:11 PM
Thanks yobiv.

Looks useful. I have written it down on my list.
Coordinator
Feb 8, 2009 at 3:05 PM
Your extension has now been implemented into the Vertices class.
It would be great if you downloaded changeset #46574 under Source Code and tested it out. If you have any additions or fixes, feel free to contact me.

Thanks for the contribution. We really appreciate it.
Feb 9, 2009 at 4:40 PM
You're welcome! :)
Thanks for the engine and the extension.