public void RotateInYZAround(Vector3 target, float angle) { Matrix rotation = MatrixHelper.CreateFromAxisAngle(Right, angle); Vector3 diff = Position - target; Vector3 newDiff = Vector3.Transform(diff, ref rotation); Position = target + newDiff; Look = Vector3.Transform(Look, ref rotation); Right = Vector3.Transform(Right, ref rotation); Up = Vector3.Transform(Up, ref rotation); UpdateView(); }
public void Rotate(float x, float y, float z) { Matrix rotation; rotation = MatrixHelper.CreateFromAxisAngle(Vector3.Up, y); Look = Vector3.Transform(Look, ref rotation); Right = Vector3.Transform(Right, ref rotation); Up = Vector3.Transform(Up, ref rotation); rotation = MatrixHelper.CreateFromAxisAngle(Right, x); Up = Vector3.Transform(Up, ref rotation); Look = Vector3.Transform(Look, ref rotation); Matrix roll = MatrixHelper.CreateFromAxisAngle(Look, z); Up = Vector3.Transform(Up, ref rotation); Right = Vector3.Transform(Right, ref rotation); UpdateView(); }
public static Triangle[] CreateCircle( Vector3 center, Vector3 radius, Vector3 normal, int pointCount, Color color) { if (pointCount < 3) { throw new ArgumentOutOfRangeException("pointCount", "pointCount must be >= 3."); } float angle = 2 * (float)Math.PI / pointCount; Matrix rotation = MatrixHelper.CreateFromAxisAngle(normal, angle); Triangle[] pieces = new Triangle[pointCount]; for (int i = 0; i < pointCount; i++) { Vector3 newRadius = Vector3.Transform(radius, ref rotation); pieces[i] = new Triangle(center, center + radius, center + newRadius, color); radius = newRadius; } return(pieces); }
public static Triangle[] CreateSphere( float radius, int layerCount, int layerPoints, IEnumerator <Color> colorer) { if (layerCount < 1) { throw new ArgumentOutOfRangeException("layerCount", "layerCount must be >= 1."); } if (layerPoints < 3) { throw new ArgumentOutOfRangeException("layerPoints", "layerPoints must be >= 3."); } if (colorer == null) { throw new ArgumentNullException("colorer"); } List <Triangle> triangles = new List <Triangle>(); Vector3[] layer = new Vector3[layerPoints]; Matrix horizontalRotate = MatrixHelper.CreateFromAxisAngle( Vector3.Right, (float)Math.PI / (layerCount + 1)); Matrix roundRotate = MatrixHelper.CreateFromAxisAngle( Vector3.Up, (float)(2 * Math.PI) / layerPoints); Vector3 top = new Vector3(0, radius, 0); Vector3 bottom = -top; colorer.MoveNext(); // building a top of sphere layer[0] = Vector3.Transform(top, ref horizontalRotate); for (int i = 1; i < layerPoints; i++) { Vector3 lastPoint = layer[i - 1]; Vector3 newPoint = Vector3.Transform(lastPoint, ref roundRotate); layer[i] = newPoint; triangles.Add(new Triangle(top, lastPoint, newPoint, colorer.Current)); } triangles.Add(new Triangle(top, layer[layerPoints - 1], layer[0], colorer.Current)); // building a middle (largest part) of sphere Vector3 currentDown = layer[0]; for (int i = 0; i < layerCount - 1; i++) { colorer.MoveNext(); Vector3 firstUp = layer[0]; currentDown = Vector3.Transform(currentDown, ref horizontalRotate); Vector3 lastDown = Vector3.Zero; for (int j = 0; j < layerPoints - 1; j++) { lastDown = currentDown; currentDown = Vector3.Transform(currentDown, ref roundRotate); Vector3 lastUp = layer[j]; Vector3 currentUp = layer[j + 1]; triangles.Add(new Triangle(currentUp, lastUp, currentDown, colorer.Current)); triangles.Add(new Triangle(currentDown, lastUp, lastDown, colorer.Current)); layer[j] = lastDown; } lastDown = currentDown; currentDown = Vector3.Transform(currentDown, ref roundRotate); triangles.Add(new Triangle(firstUp, layer[layerPoints - 1], currentDown, colorer.Current)); triangles.Add(new Triangle(currentDown, layer[layerPoints - 1], lastDown, colorer.Current)); layer[layerPoints - 1] = lastDown; } colorer.MoveNext(); // building a botom of sphere for (int i = 0; i < layerPoints - 1; i++) { triangles.Add(new Triangle(bottom, layer[i + 1], layer[i], colorer.Current)); } triangles.Add(new Triangle(bottom, layer[0], layer[layerPoints - 1], colorer.Current)); return(triangles.ToArray()); }