Dark Bit Factory & Gravity

PROGRAMMING => General coding questions => Topic started by: Pot Noodle on May 09, 2012

Title: The need for speed!
Post by: Pot Noodle on May 09, 2012
I will try and explain the problem to you

Car racing game, and i have the Rpm that when you press the up key goes from 0 to 55.
Then at the same time i need the Mph to go from 0 to 30.
It's when slowing down the car there is a problem as the Mph goes in to negative numbers before the Rpm reaches 0
I have tryed all sorts but i just can't sort it out.
Thanks for any help guys.


#Minimum_Rpm = 20
Global Gear = 1

Enumeration
  #Accelerate
  #Brake
  #SlowDown
EndEnumeration

Structure GearAccel
  GearUpAt.f                                                 ; RPM for next gear (up)
  GearDownAt.f                                               ; RPM for next gear (down)
  AccelUp.f                                                  ; RPM to add when accelerating
  AccelBreak.f                                               ; RPM to subtract when braking
  AccelDown.f                                                ; RPM to subtract when automatic slow down
  AccelDiff.f                                                ; RPM difference between gears
  SpeedUp.f                                                  ;
  SlowDown.f                                                ;
EndStructure

Global Dim GearTable.GearAccel(5)

; Init GearTable
GearTable(1)\AccelUp    = 0.5 : GearTable(1)\GearUpAt   = 50 ; change up at
GearTable(1)\AccelDown  = 0.5 : GearTable(1)\GearDownAt = 5  ; change down at
GearTable(1)\AccelBreak = 0.5 : GearTable(1)\AccelDiff  = 20 ; rpm between change
GearTable(1)\SpeedUp    = 0.1 : GearTable(1)\SlowDown  = 0.1

GearTable(2)\AccelUp    = 0.4 : GearTable(2)\GearUpAt   = 55
GearTable(2)\AccelDown  = 0.4 : GearTable(2)\GearDownAt = 15
GearTable(2)\AccelBreak = 0.4 : GearTable(2)\AccelDiff  = 20
GearTable(2)\SpeedUp    = 0.1 : GearTable(2)\SlowDown  = 0.1

GearTable(3)\AccelUp    = 0.3 : GearTable(3)\GearUpAt   = 60
GearTable(3)\AccelDown  = 0.3 : GearTable(3)\GearDownAt = 25
GearTable(3)\AccelBreak = 0.3 : GearTable(3)\AccelDiff  = 20
GearTable(3)\SpeedUp    = 0.1 : GearTable(3)\SlowDown  = 0.1

GearTable(4)\AccelUp    = 0.2 : GearTable(4)\GearUpAt   = 65
GearTable(4)\AccelDown  = 0.2 : GearTable(4)\GearDownAt = 35
GearTable(4)\AccelBreak = 0.2 : GearTable(4)\AccelDiff  = 20
GearTable(4)\SpeedUp    = 0.1 : GearTable(4)\SlowDown  = 0.1

GearTable(5)\AccelUp    = 0.1 : GearTable(5)\GearUpAt   = 70
GearTable(5)\AccelDown  = 0.1 : GearTable(5)\GearDownAt = 45
GearTable(5)\AccelBreak = 0.1 : GearTable(5)\AccelDiff  = 20
GearTable(5)\SpeedUp    = 0.1 : GearTable(5)\SlowDown  = 0.1

Procedure Transmission(accel)

  Shared Motor

  If accel = #Accelerate
    If Rpm >= GearTable(Gear)\GearUpAt And Gear < 5          ; Accelerate - Gear Up
      Rpm - GearTable(Gear)\AccelDiff
      Gear + 1
    Else
     
      Rpm + GearTable(Gear)\AccelUp                          ; Accelerate
      Mph + GearTable(Gear)\SpeedUp                         ; Speed up
       
      If Gear = 5 And Rpm > 50 : Rpm = 50 : EndIf
      If Gear = 5 And Mph > 30 : Mph = 30 : EndIf
     
    EndIf
  ElseIf accel = #Brake
   
    Rpm - GearTable(Gear)\AccelBreak                         ; Brake
    Mph - GearTable(Gear)\SlowDown                          ; Slow down
   
    If Rpm <= GearTable(Gear)\GearDownAt And Gear > 1        ; Brake - Gear Down
      Rpm + GearTable(Gear)\AccelDiff
      Gear - 1
    ElseIf Gear = 1
      If Rpm < #Minimum_Rpm : Rpm = #Minimum_Rpm : EndIf
      If Mph < #Minimum_Mph : Mph = #Minimum_Mph : EndIf
    EndIf
  Else
   
    Rpm - GearTable(Gear)\AccelDown                          ; Slow Down
    Mph - GearTable(Gear)\SlowDown
   
    If Rpm <= GearTable(Gear)\GearDownAt And Gear > 1        ; Slow Down - Gear Down
      Rpm + GearTable(Gear)\AccelDiff
      Gear - 1
    ElseIf Gear = 1                                          ; Slow Down - 1st Gear minimum Rpm
      If Rpm < #Minimum_Rpm : Rpm = #Minimum_Rpm : EndIf
      If Mph < #Minimum_Mph : Mph = #Minimum_Mph : EndIf
    EndIf
  EndIf

  ; Change the engine freq acording to Rpm
  SoundFrequency(Motor,Rpm*400)
  ; Return Mph
  ProcedureReturn Mph

EndProcedure

Title: Re: The need for speed!
Post by: Raizor on May 09, 2012
Can you post the project please Pot Noodle.

If you have a DropBox or Google Drive account, they're very handy for posting projects and stuff. Both are free for 5GB storage.
Title: Re: The need for speed!
Post by: hellfire on May 09, 2012
Quote from: Pot Noodle on May 09, 2012
i have the Rpm that when you press the up key goes from 0 to 55.
Then at the same time i need the Mph to go from 0 to 30.
How about
mph= rpm * 30 / 55;
?

Title: Re: The need for speed!
Post by: Pot Noodle on May 09, 2012
Thanks hellfire, the problem with that is i don't wont the MPH to drop & rise with gear change etc..
Raizor here is the link

http://dl.dropbox.com/u/71849177/Jaguar%20XJ220.rar (http://dl.dropbox.com/u/71849177/Jaguar%20XJ220.rar)
Title: Re: The need for speed!
Post by: Raizor on May 09, 2012
Thanks Pot Noddle. I'll take a look.
Title: Re: The need for speed!
Post by: Pot Noodle on May 10, 2012
Cheers Raizor, looking forward to see what you come up with  ;D
Title: Re: The need for speed!
Post by: Canopy on May 12, 2012
isn't the answer something like

mph= (rpm * gearing_modifier) *30 / 55;

thats how it is in a real car.. you'll have to play with values in gearing_modifier to stop it going wild
Title: Re: The need for speed!
Post by: Raizor on May 20, 2012
Pot Noodle,

I've had a look at this and noticed a few things. When the gear shifts up, the speed actually decreases as the gear shifts, which is a bit odd. The way you're handling the whole transmission system seems a little over complex to me. I'd probably rewrite it and try to make it simpler if I were you. You should be able to totally do away the GearTable table and the GearAccel stuff. I've had a play around with it in its current state, but any changes I make have a lot of knock-on effect throughout the program (track flying at crazy speed or running really slowly).

I'd probably have a global acceleration variable that acts as an RPM modifier. As the UP key is held down, the acceleration factor gradually increases up to a set threshold, and gradually decreases when the UP key is released. When the DOWN key is held (decelerate), the acceleration factor would decrease at a greater rate. You might want to have both an acceleration factor and deceleration factor variable and use these together to determine the rate at which the RPM is modified, but this might not be necessary, I'd try without it first.

As far as gear changes, I'd check the current RPM and if it's above a certain level, change the gear up, below a certain level, change the gear down. The RPM rate that causes a gear change would be different depending on current gear, so you'll need to take the current gear into account when determining this threshold. You should be able to come up with a formula to control this without relying on lookup tables.

I'd concentrate on getting the numbers working properly and ignore the effect on the track speed at first. Once the RPM, Gears and MPH are playing nicely together, you can adjust the track movement to suit.

I hope this helps a little bit, sorry I don't have a magic fix :(

p.s. It's coming along nicely though and looks great so far.



Title: Re: The need for speed!
Post by: Jim on May 20, 2012
The problem is that in a real car rpm and speed are not related.  I can be doing 100mph or 10mph at 3000rpm depending what gear I'm in, and I can even be doing 100mph with almost 0rpm if I was doing 120mph and took my foot off the gas and completely.  Or put my foot on the clutch and jam the gas down, I can be doing 6000rpm and 0mph ;)

Jim
Title: Re: The need for speed!
Post by: Pot Noodle on May 21, 2012
Thanks for your time Raizor  :'( I have to be truthfull and say I was hopping for an answer or even a rewrite!..
O'well looks like another one for the trash can as I just can't move forward with this project.
Thanks Jim for your input.
Title: Re: The need for speed!
Post by: Raizor on May 22, 2012
Quote from: Pot Noodle on May 21, 2012
Thanks for your time Raizor  :'( I have to be truthfull and say I was hopping for an answer or even a rewrite!..
O'well looks like another one for the trash can as I just can't move forward with this project.
Thanks Jim for your input.

It's quite a specific project, so getting my head around it and rewriting it would likely take me quite a while. Besides, if I did rewrite the whole lot for you it would defeat the object a little as you wouldn't really learn much from that. Don't be so quick to admin defeat, I'm sure you can get it working if you stick with it :)
Title: Re: The need for speed!
Post by: Pot Noodle on May 22, 2012
OK Raizor i think i have it in the bag  :D I stopped up last night and started from scratch, putting what i have learned in to Perspective and few tweaks her and there and it's now looking like the real thing.  :updance:
I will let you know what i did to sort it out once i have finished it.
Thanks.
Title: Re: The need for speed!
Post by: Raizor on May 22, 2012
Quote from: Pot Noodle on May 22, 2012
OK Raizor i think i have it in the bag  :D I stopped up last night and started from scratch, putting what i have learned in to Perspective and few tweaks her and there and it's now looking like the real thing.  :updance:
I will let you know what i did to sort it out once i have finished it.
Thanks.

Great news! :D
Title: Re: The need for speed!
Post by: Shockwave on May 22, 2012
Glad you haven't trashed this as it has so much potential.

Keep on going with it :)
Title: Re: The need for speed!
Post by: Kirl on May 22, 2012
Ditto, what I saw looked really cool!  :)
Title: Re: The need for speed!
Post by: Pot Noodle on May 22, 2012
Thanks for the advice guys i will Carry on.