Sign in to follow this  
Otacon87

Formula 1 Engine simulation

Recommended Posts

Otacon87    100
Hi there guys, that's my first post. I'm coding a Managerial Formula 1 game in Java (but i know also C++,C#, python). I would like to include car's engine RPMs, speed, gearbox with gear ratio. For now i want only to make a *simple* simulation: the car is moving on with at the maximum acceleration (the best acceleration that a pilot can perform without losing grip).
Assuming that i know only that "the pilot is pushing down the accelerator" how can i know car's speed / rpms and gear???
This is the code that i have wrote to make a scratch simulation:
[code]

public class GearBox
{
Gear[] gears;
int gearNumber;
int currentRPM;
float speed;
final int MIN_GEAR_RPM = 2000;
final int MAX_GEAR_RPM = 20000;
final int OPTIMAL_RPM_CHANGE = 18000;

public GearBox(){
gears = new Gear[7];
float Step_in = 18000;
float multiplier = 1;
gears[0] = new Gear(4000, 2.37f);
gears[1] = new Gear(4300, 3.23f);
gears[2] = new Gear(4400, 2.19f);
gears[3] = new Gear(4500, 1.71f);
gears[4] = new Gear(4600, 1.39f);
gears[5] = new Gear(4700, 1.16f);
gears[6] = new Gear(4800, 0.93f);
gearNumber = 0;
currentRPM = 4000;
}

public Gear getGear(int index){
return gears[index];
}

public Gear getCurrentGear(){
return gears[gearNumber];
}

public float getSpeedInKPH()
{
return speed;
}

public int getCurrentGearNumber()
{
return gearNumber+1;
}

public float getSpeedInMPS()
{
return speed / 3.6f;
}

public void update(int milliseconds)
{
if(currentRPM >= OPTIMAL_RPM_CHANGE && gearNumber < gears.length-1)
{
gearNumber++;
currentRPM = (int) (speed*gears[gearNumber].getLength());
}

if(gears[gearNumber].getMinRPM() > currentRPM && gearNumber > 0)
{
gearNumber--;
currentRPM = (int) (getSpeedInKPH()*gears[gearNumber].getLength());
}

currentRPM = gears[gearNumber].updateRPM(currentRPM, milliseconds );
speed = gears[gearNumber].getSpeed();
}

public int getCurrentRPM() {
return currentRPM;
}

public static void main(String[] args)
{
GearBox gearbox = new GearBox();
for(int i = 0; i < 20; i++)
{
gearbox.update(1000);
}
}

}


public class Gear
{
public int minRPM;
public float length;
private float speed;

public Gear(int minRPM, float length)
{
this.minRPM = minRPM;
this.length = length;
}

public Gear()
{
this(4000,10);
}

public int getMinRPM() {
return minRPM;
}

public float getLength() {
return length;
}

public void setMinRPM(int minRPM) {
this.minRPM = minRPM;
}

public void setLength(int length) {
this.length = length;
}

public float getSpeed(){
return this.speed;
}

public int updateRPM(int RPM,int milliseconds)
{
int currentRPM = ???;
speed = (currentRPM * length);
return currentRPM;
}

}
[/code]

length is the "gear ratio".

Share this post


Link to post
Share on other sites
CombatWombat    673
Octacon,

It appears you want to do a (fairly) simple simulation of your car accelerating. This is not too difficult. Things get difficult if you want to add turning and such.

In a summary type fashion, here is what you need to do.

1) Determine forces acting on the car.
2) Integrate the forces to the momentum of the car.
3) Solve for the velocity of the car by taking momentum * invMassOfTheCar
4) Integrate velocity into the position of the car.
5) Display your car.
6) Feed your current speed back into the system. You will use speed*tiresize*gears (roughly) to find engine RPM. This lets you find torque driving the car. This lets you solve for the values you need in step 1.
Repeat as necessary.

I suggest you look into the articles on: [url="http://gafferongames.com/"]GafferOnGames[/url]
This covers the basics of integration and timestep fixation you will need.
This is also a good introduction to simulating car physics: [url="http://www.asawicki.info/Mirror/Car%20Physics%20for%20Games/Car%20Physics%20for%20Games.html"]MarcoMonster Car Physics[/url]

Share this post


Link to post
Share on other sites
Otacon87    100
[quote name='CombatWombat' timestamp='1307829375' post='4822182']
Octacon,

It appears you want to do a (fairly) simple simulation of your car accelerating. This is not too difficult. Things get difficult if you want to add turning and such.

In a summary type fashion, here is what you need to do.

1) Determine forces acting on the car.
2) Integrate the forces to the momentum of the car.
3) Solve for the velocity of the car by taking momentum * invMassOfTheCar
4) Integrate velocity into the position of the car.
5) Display your car.
6) Feed your current speed back into the system. You will use speed*tiresize*gears (roughly) to find engine RPM. This lets you find torque driving the car. This lets you solve for the values you need in step 1.
Repeat as necessary.

I suggest you look into the articles on: [url="http://gafferongames.com/"]GafferOnGames[/url]
This covers the basics of integration and timestep fixation you will need.
This is also a good introduction to simulating car physics: [url="http://www.asawicki.info/Mirror/Car%20Physics%20for%20Games/Car%20Physics%20for%20Games.html"]MarcoMonster Car Physics[/url]
[/quote]


Yep, that's what I've followed and coded after posting this. The only think that i've missed was rpm = speed*tiresize*gear ... i think that it's the best approach, so thank you very much for your interest!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this