void Update() { // For now: this just puts the ball at the mouse position: _ball.Step(); NLineSegment line = getEarlyestCol(); //TODO: calculate correct distance from ball center to line //compare distance with ball radius if (line != null) { ballReset(line); } else { _ball.SetColor(0, 1, 0); } _text.Clear(Color.Transparent); _text.Text("Distance to line: " + currentDist, 0, 0); }
public MyGame() : base(800, 600, false, false) { ball = new Ball(30, new Vec2(width / 2, height / 2), new Vec2(-2, 1)); AddChild(ball); text = new EasyDraw(250, 25); text.TextAlign(CenterMode.Min, CenterMode.Min); AddChild(text); lineSegment = new NLineSegment(new Vec2(500, 500), new Vec2(100, 200), 0xff00ff00, 3); lineSegment2 = new NLineSegment(new Vec2(100, 200), new Vec2(500, 500), 0xff00ff00, 3); wallLeft = new NLineSegment(new Vec2(0, height), new Vec2(0, 0), pLineWidth: 2); wallRight = new NLineSegment(new Vec2(width, 0), new Vec2(width, height), pLineWidth: 2); wallTop = new NLineSegment(new Vec2(0, 0), new Vec2(width, 0), pLineWidth: 2); wallBottom = new NLineSegment(new Vec2(width, height), new Vec2(0, height), pLineWidth: 2); AddChild(lineSegment); AddChild(lineSegment2); AddChild(wallLeft); AddChild(wallRight); AddChild(wallTop); AddChild(wallBottom); Lines = new List <NLineSegment> { lineSegment, lineSegment2, wallLeft, wallRight, wallTop, wallBottom }; Restart(); PrintInfo(); }
public MyGame() : base(800, 600, false, false) { _ball = new Ball(30, new Vec2(width / 2, height / 2)); AddChild(_ball); _text = new EasyDraw(250, 25); _text.TextAlign(CenterMode.Min, CenterMode.Min); AddChild(_text); lines = new List <NLineSegment>(); _lineSegment1 = new NLineSegment(new Vec2(500, 500), new Vec2(100, 200), 0xff00ff00, 3); _lineSegment2 = new NLineSegment(new Vec2(800, 100), new Vec2(500, 600), 0xff00ff00, 3); _lineSegment3 = new NLineSegment(new Vec2(50, 250), new Vec2(800, 100), 0xff00ff00, 3); AddChild(_lineSegment1); AddChild(_lineSegment2); AddChild(_lineSegment3); lines.Add(_lineSegment1); lines.Add(_lineSegment2); lines.Add(_lineSegment3); Vec2 testVelocity = new Vec2(2, -11); Vec2 testNormal = new Vec2(0.8f, 0.6f); testVelocity.Reflect(1, testNormal); Console.WriteLine(testVelocity.x == 10f && testVelocity.y == -5f); }
void AddLine(Vec2 start, Vec2 end) { NLineSegment line = new NLineSegment(start, end, 0xff00ff00, 4); AddChild(line); _lines.Add(line); }
public Wall(Vec2 start, Vec2 end) : base(start, end) { //wallSprite = new Sprite(filename, false, false); lineSegment = new NLineSegment(start, end, 0xff00ff00, 3); //wallSprite.SetXY(start.x, start.y); //_collisionZone = lineSegment.start - lineSegment.end; //wallSprite.rotation = _collisionZone.GetAngleDegrees(); }
public MyGame() : base(800, 600, false, false) { _ball = new Ball(30, new Vec2(width / 2 - 80, height / 6)); AddChild(_ball); _text = new EasyDraw(250, 25); _text.TextAlign(CenterMode.Min, CenterMode.Min); AddChild(_text); _lineSegment = new NLineSegment(new Vec2(500, 500), new Vec2(100, 200), 0xff00ff00, 3); AddChild(_lineSegment); }
/*----------------------------------------- * createLines() * ----------------------------------------*/ private void createLines() { LineTop = new NLineSegment(new Vec2(300, 0), new Vec2(width, 0), 0xff00ff00, 3); LineBottom = new NLineSegment(new Vec2(width - 300, height), new Vec2(0, game.height), 0xff00ff00, 3); LineRight = new NLineSegment(new Vec2(width, 0), new Vec2(width, height - 150), 0xff00ff00, 3); LineLeft = new NLineSegment(new Vec2(0, height), new Vec2(0, 150), 0xff00ff00, 3); LineAngleRight = new NLineSegment(new Vec2(width, height - 150), new Vec2(width - 300, height), 0xff00ff00, 3); LineAngleLeft = new NLineSegment(new Vec2(0, 150), new Vec2(300, 0), 0xff00ff00, 3); AddChild(LineTop); AddChild(LineBottom); AddChild(LineRight); AddChild(LineLeft); AddChild(LineAngleLeft); AddChild(LineAngleRight); }
void Reset() { if (Input.GetKeyDown(Key.R)) { _ball.Destroy(); RemoveChild(_ball); _lineSegment.Destroy(); RemoveChild(_lineSegment); _ball = new Ball(30, new Vec2(width / 2 - 80, height / 6)); AddChild(_ball); _lineSegment = new NLineSegment(new Vec2(500, 500), new Vec2(100, 200), 0xff00ff00, 3); AddChild(_lineSegment); _bounciness = Utils.Random(0.0f, 1.0f); } }
private NLineSegment getEarlyestCol() { currentLine = null; float smallestDistance = 50f; for (int i = 0; i < lines.Count; i++) { Vec2 a = _ball.position - lines[i].start; float ballDistance = a.Dot((lines[i].end - lines[i].start).Normal()); if (ballDistance < _ball.radius) { _ball.SetColor(1, 0, 0); if (ballDistance < smallestDistance) { currentLine = lines[i]; currentDist = ballDistance; } } } return(currentLine); }
private void ballReset(NLineSegment currentLine) { _ball.x += (-currentDist + _ball.radius) * (currentLine.end - currentLine.start).Normal().x; _ball.y += (-currentDist + _ball.radius) * (currentLine.end - currentLine.start).Normal().y; _ball.velocity.Reflect(1, (currentLine.end - currentLine.start).Normal()); }
CollisionInfo FindEarliestCollision() { GameObject current = null; Vec2 currentNormal = new Vec2(); smallestToi = 2f; MyGame myGame = (MyGame)game; // Check other movers: for (int i = 0; i < myGame.GetNumberOfMovers(); i++) { Ball mover = myGame.GetMover(i); if (mover != this) { Vec2 relativePosition = position - mover.position; //if (relativePosition.Magnitude() < radius + mover.radius) //{ // TODO: compute correct normal and time of impact, and // return *earliest* collision instead of *first detected collision*: Vec2 u = _oldPosition - mover.position; float a = velocity.Magnitude() * velocity.Magnitude(); float b = 2 * u.Dot(velocity); float c = u.Magnitude() * u.Magnitude() - (radius + mover.radius) * (radius + mover.radius); float delta = b * b - 4 * a * c; float t; t = (-b - Mathf.Sqrt(delta)) / (2 * a); if (c < 0) { if (b < 0) { t = 0; //Vec2 normal = (_oldPosition - mover.position).Normalized(); //return new CollisionInfo(normal, mover, t); } else { continue; } } if (a != 0) //{ // return null; //} //else { if (delta >= 0) //{ // return null; //} //else { Vec2 pointOfImpact = _oldPosition + velocity * t; Vec2 normal = (pointOfImpact - mover.position).Normalized(); if (0 <= t && t < 1) { if (t < smallestToi) { smallestToi = t; current = mover; currentNormal = normal; } } //else return null; } } //Vec2 normal = relativePosition.Normalized(); //float overlap = radius + mover.radius - relativePosition.Magnitude(); //return new CollisionInfo(normal, mover, overlap); //} } } for (int i = 0; i < myGame.GetNumberOfLines(); i++) { NLineSegment line = myGame.GetLine(i); Vec2 differenceVector = _oldPosition - line.start; float ballDistance = differenceVector.Dot((line.end - line.start).Normal()); //if (ballDistance < radius) //{ Vec2 normal = (line.end - line.start).Normal(); Vec2 dist = position + (-ballDistance + radius) * normal; float difference = (dist - position).Magnitude(); float b = -velocity.Dot(normal); //float a = b - difference; float a = ballDistance - radius; if (b > 0) //{ // return null; //} //else { float t = 3f; if (a >= 0) //{ // return null; //} //else { t = a / b; } else if (a >= -radius) { t = 0; } else { continue; } if (t <= 1f) { Vec2 POI = _oldPosition + t * velocity; float d = (POI - line.start).Dot((line.end - line.start).Normalized()); if (0 <= d && d <= (line.end - line.start).Magnitude()) { if (t < smallestToi) { smallestToi = t; current = line; currentNormal = normal; } } // else return null; } } //} } if (current != null) { return(new CollisionInfo(currentNormal, current, smallestToi)); } else { return(null); } // TODO: Check Line segments using myGame.GetLine(); // return null; }