From 1524140fd0f6c5a276c62e8dc183b08ce88340ad Mon Sep 17 00:00:00 2001 From: Handtag Date: Mon, 19 Jan 2026 10:48:43 +0100 Subject: [PATCH] Uppdatering --- MetoderUppgift/Form1.Designer.cs | 2 ++ MetoderUppgift/Form1.cs | 29 ++++++++++++++++++----------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/MetoderUppgift/Form1.Designer.cs b/MetoderUppgift/Form1.Designer.cs index ba33ed2..8cd3df7 100644 --- a/MetoderUppgift/Form1.Designer.cs +++ b/MetoderUppgift/Form1.Designer.cs @@ -54,6 +54,7 @@ namespace MetoderUppgift Throttle.Name = "Throttle"; Throttle.Size = new Size(112, 45); Throttle.TabIndex = 0; + Throttle.TabStop = false; Throttle.Value = 2; // // lblThr @@ -150,6 +151,7 @@ namespace MetoderUppgift lblMaxHastighet.Size = new Size(223, 15); lblMaxHastighet.TabIndex = 11; lblMaxHastighet.Text = "Max hastighet [m/s] (Recommended: 30)"; + lblMaxHastighet.Click += lblMaxHastighet_Click; // // BilSimulation // diff --git a/MetoderUppgift/Form1.cs b/MetoderUppgift/Form1.cs index 2537422..01ca840 100644 --- a/MetoderUppgift/Form1.cs +++ b/MetoderUppgift/Form1.cs @@ -7,12 +7,14 @@ namespace MetoderUppgift float vMax; // [m/s] float aMax; // [m/s^2] float engineBraking = 2; // [m/s^2] - float dt = 0.1f; // [s] + float dt = 1 / 50f; // 50 FPS float stripeOffset = 0; // horizontal offset for stripes public BilSimulation() { InitializeComponent(); + + Timer.Interval = (int)(1000 * dt); // s -> ms } void UpdateUI(float acceleration) { @@ -41,12 +43,11 @@ namespace MetoderUppgift 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 + stripeOffset += 20 * velocity * dt; // Uppdatera avstånd från initial plats för varje streck på vägen } float GetThrottlePercentage() //Omvanlda gaspådrag (0-4) till % { @@ -62,7 +63,7 @@ namespace MetoderUppgift } 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 acceleration = ratio * aMax * (1.2f - velocity / vMax); // Formel för att göra fysik mer realistisk float netAcceleration = acceleration - engineBraking; return netAcceleration; @@ -88,9 +89,9 @@ namespace MetoderUppgift int roadX = 400; int roadWidth = 100; g.FillRectangle(Brushes.DimGray, roadX, 0, roadWidth, ClientSize.Height); - - Car.Location = new Point(roadX + roadWidth/2, ClientSize.Height / 2); - Car.Size = new Size(roadWidth/2, roadWidth/2); + + Car.Location = new Point(roadX + roadWidth / 2, ClientSize.Height / 2); + Car.Size = new Size(roadWidth / 2, roadWidth / 2); // Streck @@ -101,14 +102,15 @@ namespace MetoderUppgift int stripeX = roadX + roadWidth / 2 - stripeWidth / 2; int stripeY = 0; + if ((stripeOffset) > 80) + { + stripeOffset = 0; // Flytta tillbaka strecken om de hamnar utanför rutan. + } + 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) > 80) - { - stripeOffset = 0; // Flytta tillbaka strecken om de hamnar utanför rutan. - } } } @@ -121,5 +123,10 @@ namespace MetoderUppgift private void Form1_Load(object sender, EventArgs e) { } + + private void lblMaxHastighet_Click(object sender, EventArgs e) + { + + } } }