126 lines
4.1 KiB
C#
126 lines
4.1 KiB
C#
using System.Drawing;
|
|
namespace MetoderUppgift
|
|
{
|
|
public partial class Form1 : Form
|
|
{
|
|
float velocity = 0; // [m/s]
|
|
float vMax; // [m/s]
|
|
float aMax; // [m/s^2]
|
|
float engineBraking = 2; // [m/s^2]
|
|
float dt = 0.1f; // [s]
|
|
float stripeOffset = 0; // horizontal offset for stripes
|
|
|
|
public Form1()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
void UpdateUI(float acceleration)
|
|
{
|
|
lblAcc.Text = "Acceleration: " + acceleration.ToString("0.00") + " [m/s²]";
|
|
lblVel.Text = "Hastighet: " + velocity.ToString("0.00") + " [m/s]";
|
|
lblThr.Text = "Gaspådrag: " + GetThrottlePercentage().ToString("0.00") + " [%]";
|
|
}
|
|
|
|
void ImplementSpecifications()
|
|
{
|
|
if (float.TryParse(tbxAcceleration.Text, out float result)) // tal från sträng, värden blir NaN om input inte fungerar
|
|
{
|
|
aMax = result;
|
|
}
|
|
if (float.TryParse(tbxVelocity.Text, out float result2))
|
|
{
|
|
vMax = result2;
|
|
}
|
|
}
|
|
void Simulate()
|
|
{
|
|
ImplementSpecifications(); // Uppdatera specifikationer av bilen angivna av textboxes
|
|
|
|
float acceleration = GetAcceleration(Throttle.Value); // Hämta acceleration
|
|
velocity += acceleration * dt; // Uppdatera Acceleration
|
|
|
|
UpdateUI(acceleration); // Uppdatera kontroller
|
|
UpdateStripes(); // Uppdatera strecken på vägen för att simulera grafisk hastighet
|
|
|
|
}
|
|
|
|
void UpdateStripes()
|
|
{
|
|
stripeOffset += 10 * velocity * dt; // Uppdatera avstånd från initial plats för varje streck på vägen
|
|
}
|
|
float GetThrottlePercentage() //Omvanlda gaspådrag (0-4) till %
|
|
{
|
|
float percentage = (100 / Throttle.Maximum) * Throttle.Value;
|
|
return percentage;
|
|
}
|
|
float GetAcceleration(int throttle) // Uppdatera accelerationen från gaspådrag
|
|
{
|
|
if (velocity < 0)
|
|
{
|
|
velocity = 0;
|
|
return 0; // Undvik negativ hastighet
|
|
}
|
|
|
|
float ratio = (float)throttle / (float)Throttle.Maximum;
|
|
float acceleration = ratio * aMax * (1.1f - velocity / vMax); // Formel för att göra fysik mer realistisk
|
|
float netAcceleration = acceleration - engineBraking;
|
|
|
|
return netAcceleration;
|
|
}
|
|
|
|
private void btnStart_Click(object sender, EventArgs e) //Start och stopp knapp
|
|
{
|
|
if (!Timer.Enabled)
|
|
{
|
|
Timer.Enabled = true;
|
|
btnStart.Text = "Stopp";
|
|
}
|
|
else
|
|
{
|
|
Application.Exit();
|
|
}
|
|
}
|
|
|
|
protected override void OnPaint(PaintEventArgs e) // Paint funktion för grafik
|
|
{
|
|
base.OnPaint(e);
|
|
Graphics g = e.Graphics;
|
|
|
|
int roadX = 800;
|
|
int roadWidth = 150;
|
|
g.FillRectangle(Brushes.DimGray, roadX, 0, roadWidth, ClientSize.Height);
|
|
Car.Location = new Point(800, ClientSize.Height / 2);
|
|
|
|
// Streck
|
|
int stripeWidth = 10;
|
|
int stripeHeight = 30;
|
|
int gap = 200;
|
|
int stripesCount = 5;
|
|
int stripeX = roadX + roadWidth / 2 - stripeWidth / 2;
|
|
int stripeY = 0;
|
|
|
|
for (int i = 0; i < stripesCount; i++)
|
|
{
|
|
g.FillRectangle(Brushes.White, stripeX, stripeY + stripeOffset, stripeWidth, stripeHeight); // Offset uppdateras varje tick. Förändringshastighet av hastighet.
|
|
stripeY += gap;
|
|
if ((stripeOffset) > 200)
|
|
{
|
|
stripeOffset = 0; // Flytta tillbaka strecken om de hamnar utanför rutan.
|
|
}
|
|
}
|
|
|
|
}
|
|
private void Timer_Tick(object sender, EventArgs e)
|
|
{
|
|
Simulate(); // Simulera fysik och grafik
|
|
Invalidate(); // uppdatera grafik
|
|
}
|
|
|
|
private void Form1_Load(object sender, EventArgs e)
|
|
{
|
|
}
|
|
|
|
}
|
|
}
|