/// <summary> /// Adds thurst in the given direction. /// </summary> /// <param name="dir">The direction.</param> public void Thrust(Vector2 dir) { // Rotate the direction. var rotatedForce = new Vector2( dir.X * MathCore.Cos(Body.Rotation) + -dir.Y * MathCore.Sin(Body.Rotation), dir.Y * MathCore.Cos(Body.Rotation) + dir.X * MathCore.Sin(Body.Rotation)); // Add the thrust as acceleration. var velocity = Body.LinearVelocity; if (velocity != Vector2.Zero) { var currentSpeed = velocity.Length(); var velocityDir = Vector2.Normalize(velocity); var forceDir = Vector2.Normalize(rotatedForce); float dotProduct; Vector2.Dot(ref velocityDir, ref forceDir, out dotProduct); var damping = 1.0f - (1.0f / _maxSpeed * currentSpeed) * dotProduct; _force += rotatedForce * damping; } else { _force += rotatedForce; } }
private void GenerateTunnels(Vector2 start, float maxLength = 2000.0f, float turnStrength = 0.2f) { //1234 //12345 //498764867 //582764 var random = new FastRandom(498764867); const float step = 2.0f; var terrainHeight = _terrain.TerrainSize.Y / 2.0f; var maxSize = new Vector2(30.0f); var currentPosition = start; var currentDirection = Vector2.UnitX; var length = 0.0f; while (length < maxLength) { var currentSize = maxSize + new Vector2(Math.Max(0.0f, length + 200.0f - maxLength)) * 1.0f; var brushSize = new Vector2(random.NextFloat() * currentSize.X, random.NextFloat() * currentSize.Y); var brush = new CircleBrush( (brushSize.X + brushSize.Y) / 4.0f, currentPosition + new Vector2(random.NextRangedFloat(), random.NextRangedFloat())); _terrain.SetQuads(brush, false, true); // Add rotation. var rotationDir = random.NextRangedFloat(); // Prevent going outside terrain or going back. if (currentPosition.Y > terrainHeight - 200.0f || (currentDirection.X < 0.0f && currentDirection.Y > 0.0f)) { rotationDir = (rotationDir - 1.0f) * 0.1f; } else if (currentPosition.Y < -(terrainHeight + 200.0f) || (currentDirection.X < 0.0f && currentDirection.Y < 0.0f)) { rotationDir = (rotationDir + 1.0f) * 0.1f; } // Apply rotation. var rotation = rotationDir * turnStrength; currentDirection = new Vector2( currentDirection.X * MathCore.Cos(rotation) + -currentDirection.Y * MathCore.Sin(rotation), currentDirection.Y * MathCore.Cos(rotation) + currentDirection.X * MathCore.Sin(rotation)); currentPosition += currentDirection * step; length += step; } _terrain.Refresh(); }