Ballz.GameSession.Renderer.GameRenderer.DrawRope C# (CSharp) Method

DrawRope() public method

public DrawRope ( Rope rope ) : void
rope Ballz.GameSession.World.Rope
return void
        public void DrawRope(Rope rope)
        {
            //var segmentPositions = (from s in rope.PhysicsSegments select s.Position).ToArray();
            //var segmentPositions = (from s in rope.PhysicsSegments select s.GetWorldPoint(new Vector2(0, 0.5f))).ToArray();

            var segmentPositionsList = (from s in rope.PhysicsSegmentJoints select s.WorldAnchorA).ToList();
            segmentPositionsList.Insert(0, rope.AttachedPosition);
            segmentPositionsList.Add(rope.AttachedEntity.Position);
            var segmentPositions = segmentPositionsList.ToArray();

            var triangleCount = (segmentPositions.Length - 1) * 2;
            
            VertexPositionColorTexture[] vpc = new VertexPositionColorTexture[triangleCount * 3];
            
            float u = 0;

            for(int i = 0; i < segmentPositions.Length - 1; i++)
            {
                var p0 = segmentPositions[i + 1];
                var p1 = segmentPositions[i];
                var d = Vector2.Normalize(p1 - p0);
                var n = new Vector2(d.Y, -d.X);
                
                var u0 = u - d.Length() * 0.05f;
                var u1 = u + (p1 - p0).Length() + d.Length() * 0.05f;

                p0 -= d * 0.05f;
                p1 += d * 0.05f;

                var t00 = p0 + n * RopeWidth;
                var t10 = p1 + n * RopeWidth;
                var t01 = p0 - n * RopeWidth;
                var t11 = p1 - n * RopeWidth;

                vpc[i * 6 + 0].Color = Color.White;
                vpc[i * 6 + 0].Position = new Vector3(t11, -1);
                vpc[i * 6 + 0].TextureCoordinate = new Vector2(1, u1 / (2f * RopeWidth));
                vpc[i * 6 + 1].Color = Color.White;
                vpc[i * 6 + 1].Position = new Vector3(t10, -1);
                vpc[i * 6 + 1].TextureCoordinate = new Vector2(0, u1 / (2f * RopeWidth));
                vpc[i * 6 + 2].Color = Color.White;
                vpc[i * 6 + 2].Position = new Vector3(t00, -1);
                vpc[i * 6 + 2].TextureCoordinate = new Vector2(0, u0 / (2f * RopeWidth));

                vpc[i * 6 + 3].Color = Color.White;
                vpc[i * 6 + 3].Position = new Vector3(t01, -1);
                vpc[i * 6 + 3].TextureCoordinate = new Vector2(1, u0 / (2f * RopeWidth));
                vpc[i * 6 + 4].Color = Color.White;
                vpc[i * 6 + 4].Position = new Vector3(t11, -1);
                vpc[i * 6 + 4].TextureCoordinate = new Vector2(1, u1 / (2f * RopeWidth));
                vpc[i * 6 + 5].Color = Color.White;
                vpc[i * 6 + 5].Position = new Vector3(t00, -1);
                vpc[i * 6 + 5].TextureCoordinate = new Vector2(0, u0 / (2f * RopeWidth));

                u = u1;
            }
            
            RopeEffect.World = Matrix.Identity;
            RopeEffect.View = Game.Camera.View;
            RopeEffect.Projection = Game.Camera.Projection;
            RopeEffect.CurrentTechnique.Passes[0].Apply();
            GraphicsDevice.SamplerStates[0] = new SamplerState
            {
                AddressU = TextureAddressMode.Wrap,
                AddressV = TextureAddressMode.Wrap
            };

            GraphicsDevice.DrawUserPrimitives<VertexPositionColorTexture>(PrimitiveType.TriangleList, vpc, 0, triangleCount);
        }