public void Vector2f_InvertY_Converts() { var input = new Vector2f(1, 2); var expected = new Vector2f(1, -2); var result = input.InvertY(); Assert.AreEqual(expected, result); }
/// <summary> /// Creates and returns a track piece shape. /// </summary> /// <param name="position"></param> /// <param name="rotation"></param> /// <returns></returns> private static RectangleShape CreateShape(Vector2f position, float rotation) { return(new RectangleShape { FillColor = FillColor, OutlineColor = OutlineColor, OutlineThickness = OutlineSize, Size = PieceSize, // invert the position and rotation for SFML Position = position.InvertY(), Rotation = rotation * -1f }); }
/// <summary> /// Creates the graphics and physics objects for the body of the car. /// </summary> private void CreateBody() { m_bodyShape = new ConvexShape((uint)Definition.NumBodyPoints) { OutlineColor = OutlineColor, OutlineThickness = OutlineThickness, Position = Car.StartPosition.ToVector2f().InvertY() }; // build the vertex list for the polygon var vertices = new Vertices(Definition.NumBodyPoints); var angleStep = 360f / Definition.NumBodyPoints; var angle = 0f; for (int i = 0; i < Definition.NumBodyPoints; i++) { // the distance this point is from the center var distance = m_definition.CalcBodyPoint(i); // turn the distance into a point centered around (0,0) var point = new Vector2f { X = distance * (float)Math.Cos(MathExtensions.DegToRad(angle)), Y = distance * (float)Math.Sin(MathExtensions.DegToRad(angle)) }; m_bodyShape.SetPoint((uint)i, point.InvertY()); vertices.Add(point.ToVector2()); angle += angleStep; } // build the physics shape m_bodyBody = BodyFactory.CreatePolygon( m_physicsManager.World, vertices, m_definition.CalcBodyDensity(), Car.StartPosition ); m_bodyBody.BodyType = BodyType.Dynamic; m_bodyBody.Friction = 1; m_bodyBody.CollidesWith = ~CollisionCategory; m_bodyBody.CollisionCategories = CollisionCategory; }
/// <summary> /// Randomly generates a new track, replacing an existing track. /// </summary> public void Generate() { Debug.Assert(Random != null); Clear(); var stopwatch = new Stopwatch(); stopwatch.Start(); float maxX = 0; float minX = 0; float maxY = 0; float minY = 0; // first an invisible physics piece to block cars from running off the // track to the left var start = new Vector2f(-10, 0); minX = start.X; var body = BodyFactory.CreateEdge( m_world, start.ToVector2(), start.ToVector2() + new Vector2(0, 10)); body.CollidesWith = Category.All; body.CollisionCategories = CollisionCategory; m_trackBodies.Add(body); // the first visible piece is the starting pad and is positioned so that // the random track starts at (0,0) var rot = 0f; var shape = new RectangleShape { FillColor = FillColor, OutlineColor = OutlineColor, OutlineThickness = OutlineSize, Position = start.InvertY(), Size = new Vector2f(10, PieceSize.Y), Rotation = rot }; var end = CalcEndPoint(start, shape.Size, rot); m_trackShapes.Add(shape); body = CreateBody(start, end); m_trackBodies.Add(body); m_startingLine = (start / 2f).X; m_trackOutline.Append(new Vertex(start.InvertY(), FillColor)); m_trackOutline.Append(new Vertex(end.InvertY(), FillColor)); // place the rest of the pieces for (int i = 0; i < NumPieces; i++) { start = end; // the angle of the piece is randomized var maxAngle = CalcMaxAngle(i); var minAngle = CalcMinAngle(i); var rotSign = rot < 0 ? -1f : 1f; rot = (float)Random.NextDouble() * (maxAngle - minAngle) + minAngle; // with a 40% chance to flip the sign from the last piece if (Random.NextDouble() < 0.4) { rot *= -rotSign; } else { rot *= rotSign; } shape = CreateShape(start, rot); end = CalcEndPoint(start, shape.Size, rot); body = CreateBody(start, end); m_trackOutline.Append(new Vertex(end.InvertY(), FillColor)); m_trackShapes.Add(shape); m_trackBodies.Add(body); maxY = Math.Max(maxY, end.Y); minY = Math.Min(minY, end.Y); } // the finish line sensor is halfway down the finish line piece var sensor = BodyFactory.CreateEdge(m_world, (end + new Vector2f(5, 0)).ToVector2(), (end + new Vector2f(5, 10)).ToVector2() ); sensor.IsSensor = true; sensor.CollisionCategories = FinishLineCategory; sensor.CollidesWith = Car.Entity.CollisionCategory; sensor.OnCollision += FinishLineOnCollision; m_trackBodies.Add(sensor); // create a landing pad at the end of the track start = end; shape = new RectangleShape { FillColor = FillColor, OutlineColor = OutlineColor, OutlineThickness = OutlineSize, Position = start.InvertY(), Size = new Vector2f(10, PieceSize.Y), Rotation = 0f }; end = start + new Vector2f(shape.Size.X, 0); body = CreateBody(start, end); m_trackShapes.Add(shape); m_trackBodies.Add(body); m_trackOutline.Append(new Vertex(end.InvertY(), FillColor)); maxX = end.X; // create a holder so cars don't go off the end of the track start = end; end = start + new Vector2f(0, 10); body = CreateBody(start, end); m_trackBodies.Add(body); Dimensions = new Vector2(maxX - minX, maxY - minY + 10); Center = new Vector2( minX + (Dimensions.X / 2f), minY + (Dimensions.Y / 2f)); m_generated = true; // Log.DebugFormat("Generated {0} track pieces in {1} ms", // m_trackShapes.Count, stopwatch.ElapsedMilliseconds); }