/// <summary> /// Creates a circular shadow hull /// </summary> /// <param name="radius">radius of the circle</param> /// <param name="sides">number of sides the circle will be comprised of</param> /// <returns>A circular shadow hull</returns> public static ShadowHull CreateCircle(float radius, int sides) { // Validate input if (sides < 3) { throw new ArgumentException("Shadow hull must have at least 3 sides."); } ShadowHull hull = new ShadowHull(); hull.MaxRadius = radius; // Calculate number of sides hull.NumPoints = sides * 2; var numTris = hull.NumPoints - 2; hull.NumIndicies = numTris * 3; hull.Points = new ShadowHullPoint[hull.NumPoints]; hull.Indicies = new Int32[hull.NumIndicies]; var angle = (float)(-Math.PI * 2) / sides; // XNA Renders Clockwise var angleOffset = angle / 2; for (int i = 0; i < sides; i++) { // Create vertices var v1 = new ShadowHullPoint(); var v2 = new ShadowHullPoint(); // Vertex Position v1.Position.X = (float)Math.Cos(angle * i) * radius; v1.Position.Y = (float)Math.Sin(angle * i) * radius; v2.Position.X = (float)Math.Cos(angle * (i + 1)) * radius; v2.Position.Y = (float)Math.Sin(angle * (i + 1)) * radius; // Vertex Normal v1.Normal.X = (float)Math.Cos(angle * i + angleOffset); v1.Normal.Y = (float)Math.Sin(angle * i + angleOffset); v2.Normal.X = (float)Math.Cos(angle * i + angleOffset); v2.Normal.Y = (float)Math.Sin(angle * i + angleOffset); // Copy vertices hull.Points[i * 2 + 0] = v1; hull.Points[i * 2 + 1] = v2; } for (int i = 0; i < numTris; i++) { hull.Indicies[i * 3 + 0] = 0; hull.Indicies[i * 3 + 1] = (Int16)(i + 1); hull.Indicies[i * 3 + 2] = (Int16)(i + 2); } return(hull); }
/// <summary> Creates a circular shadow hull </summary> /// <param name="radius">radius of the circle</param> /// <param name="sides">number of sides the circle will be comprised of</param> /// <returns>A circular shadow hull</returns> public static ShadowHull CreateCircle(float radius, int sides) { // Validate input if (sides < 3) { throw new ArgumentException("Shadow hull must have at least 3 sides."); } var hull = new ShadowHull { MaxRadius = radius, NumPoints = sides * 2 }; // Calculate number of sides var numTris = hull.NumPoints - 2; hull.NumIndicies = numTris * 3; hull.Points = new ShadowHullPoint[hull.NumPoints]; hull.Indicies = new Int32[hull.NumIndicies]; var angle = (float)(-Math.PI * 2) / sides; // XNA Renders Clockwise var angleOffset = angle / 2; for (int i = 0; i < sides; i++) { // Create vertices var v1 = new ShadowHullPoint(); var v2 = new ShadowHullPoint(); // Vertex Position v1.Position.X = (float)Math.Cos(angle * i) * radius; v1.Position.Y = (float)Math.Sin(angle * i) * radius; v2.Position.X = (float)Math.Cos(angle * (i + 1)) * radius; v2.Position.Y = (float)Math.Sin(angle * (i + 1)) * radius; // Vertex Normal v1.Normal.X = (float)Math.Cos(angle * i + angleOffset); v1.Normal.Y = (float)Math.Sin(angle * i + angleOffset); v2.Normal.X = (float)Math.Cos(angle * i + angleOffset); v2.Normal.Y = (float)Math.Sin(angle * i + angleOffset); // Copy vertices hull.Points[i * 2 + 0] = v1; hull.Points[i * 2 + 1] = v2; } for (int i = 0; i < numTris; i++) { hull.Indicies[i * 3 + 0] = 0; hull.Indicies[i * 3 + 1] = i + 1; hull.Indicies[i * 3 + 2] = i + 2; } return hull; }