private void CreateLines() { _lineVecs = new[] { new Vec2(-105, 20), new Vec2(-70, 10), new Vec2(-40, 5), new Vec2(0, 5), new Vec2(40, 5), new Vec2(70, 10), new Vec2(105, 20) }; _lines = new LineSegment[_lineVecs.Length - 1]; for (var i = 0; i < _lines.Length; i++) { _lines[i] = new LineSegment(_position.Clone().Add(_lineVecs[i]), _position.Clone().Add(_lineVecs[i + 1]), 0x0000FF00); _level.AddChild(_lines[i]); } }
/// <summary> /// Handles Collisions and reflection between the ball and a given line. /// </summary> /// <param name="line"> /// Is the Line that the ball will be checked against. /// </param> /// <param name="reflectionStrength"> /// Is the amount of reflection that will be given to the ball, /// 1 is a perfect bounce, 0 makes the ball not bounce at all. /// </param> /// <returns>Returns true when a collision was actually detected</returns> private bool LineCollisionTest(LineSegment line, float reflectionStrength) { var lineVector = line.End.Clone().Subtract(line.Start); var lineVectorNormalized = lineVector.Clone().Normalize(); var lineNormal = lineVector.Clone().Normal(); var lineLength = lineVector.Length(); var ballToLine = _ball.Position.Clone().Subtract(line.Start); var ballDistance = ballToLine.Dot(lineNormal); if (Math.Abs(ballDistance) < _ball.radius) { var ballDistanceAlongLine = ballToLine.Dot(lineVectorNormalized); if (ballDistanceAlongLine < 0) ballDistanceAlongLine = 0; if (ballDistanceAlongLine > lineLength) ballDistanceAlongLine = lineLength; var closestPointOnLine = line.Start.Clone().Add(lineVectorNormalized.Scale(ballDistanceAlongLine)); var difference = _ball.Position.Clone().Subtract(closestPointOnLine); if (difference.Length() < _ball.radius) { var normal = difference.Clone().Normalize(); var separation = _ball.radius - difference.Length(); _ball.Position.Add(normal.Clone().Scale(separation)); _ball.Velocity.Reflect(normal, reflectionStrength); _ball.Step(); return true; } } return false; }
private void CreateLines() { var relativePoints = new Vec2[_points.Length]; for (var i = 0; i < _points.Length; i++) { relativePoints[i] = new Vec2(); relativePoints[i].x = _points[i].x + _realPosX; relativePoints[i].y = _points[i].y + _realPosY; } for (var i = 0; i < relativePoints.Length; i++) { if (i < relativePoints.Length - 1) { if (UtilitySettings.DebugMode) Console.WriteLine("Creating Line " + i + " with coords: " + relativePoints[i] + relativePoints[i + 1]); _lines[i] = new LineSegment(relativePoints[i], relativePoints[i + 1], this, _color); if (!UtilitySettings.DebugMode) _lines[i].Color = 0x00000000; } else { if (UtilitySettings.DebugMode) Console.WriteLine("Creating Line " + i + " with coords: " + relativePoints[relativePoints.Length - 1] + relativePoints[0]); _lines[i] = new LineSegment(relativePoints[relativePoints.Length - 1], relativePoints[0], this, _color); if (!UtilitySettings.DebugMode) _lines[i].Color = 0x00000000; } } }