- Home /
Array index confusion - what does [i-1] do exactly???
I'm having a hard time understanding the gearSpeeds[i-1] part of the for loop below. Isn't [i-1] equal to current array index - 1?
var topSpeed : float = 160; var numberOfGears : int = 5; private var engineForceValues : float[]; private var gearSpeeds : float[];
function SetupGears() { engineForceValues = new float[numberOfGears]; gearSpeeds = new float[numberOfGears];
var tempTopSpeed : float = topSpeed;
for(var i = 0; i < numberOfGears; i++)
{
if(i > 0)
{
gearSpeeds[i] = tempTopSpeed / 4 + gearSpeeds[i-1];
Debug.Log("gearSpeeds: " + gearSpeeds[i]);
}
}
}
When I debug gearSpeeds[i] = tempTopSpeed / 4 + gearSpeeds[i-1] with Debug.Log("gearSpeeds: " + gearSpeeds[i]) it gives me the following results:
gearSpeeds: 70
gearSpeeds: 92.5
gearSpeeds: 109.375
gearSpeeds: 122.0312
But if I remove + gearSpeeds[i-1] and debug again, I get:
gearSpeeds: 30
gearSpeeds: 22.5
gearSpeeds: 16.875
gearSpeeds: 12.65625
What I can't figure out is how gearSpeeds[i-1] equals 40 in the first index (30+40=70)???
I'm finally starting to understand Arrays and looping through them with for loops, but the [i-1] results are making my brain smoke...lol
What exactly don't you understand? Don't you understand how arrays are indexed in general, or don't you understand how this is used in your specific application?
Answer by Jessy · Sep 18, 2010 at 07:15 PM
In short, array[i - i] gives you the value of the element at the index 1 less than i.
Your code does not generate those values. It either generates 40-160, or just 40, for index 1-4 in gearSpeeds. You obviously have something else going on in your code that's altering what you posted.
Anyway, there's no reason you have to start your for loop at 0. You could clean it up, like this:
var topSpeed : float = 160; var numberOfGears : int = 5; private var gearSpeeds : float[];
function SetupGears () { gearSpeeds = new float[numberOfGears]; for (var i = 1; i < numberOfGears; ++i) { gearSpeeds[i] = topSpeed / (numberOfGears - 1) * i; Debug.Log("gearSpeeds[" + i + "]: " + gearSpeeds[i]); } }
Answer by RSud · Sep 18, 2010 at 07:23 PM
your first time through the loop there is no value set for gearSpeeds[0] since the if statement skips setting gearSpeed[0]. I guess this is a good thing since if it didn't you first time through you would be picking up gearSpeed[-1].
So gearSpeed[0] has no value set. The coding here has a bug since you don't set any value for gearSpeed[0] but expect to use its value the second time through the loop.
This is not true. gearSpeed[0] is set to zero when the array is created. Arrays of floats are set to zero upon construction; it's not like arrays of reference types, where an element may be null.
Answer by ronronmx · Sep 19, 2010 at 05:22 AM
Jessy, you're right there's something else going on in the code. Here's the full function below:
var topSpeed : float = 160; var numberOfGears : int = 5; private var gearSpeeds : float[];
function SetupGears() { gearSpeeds = new float[numberOfGears];
var tempTopSpeed : float = topSpeed;
for(var i = 0; i < numberOfGears; i++)
{
if(i > 0){
gearSpeeds[i] = tempTopSpeed / 4 + gearSpeeds[i-1];
//Debug.Log("gearSpeeds if: " + gearSpeeds[i]);
}
else{
gearSpeeds[i] = tempTopSpeed / 4;
//Debug.Log("gearSpeeds else: " + gearSpeeds[i]);
}
tempTopSpeed -= tempTopSpeed / 4;
}
}
As you can see, tempTopSpeed is being changed at the end of the loop:
tempTopSpeed -= tempTopSpeed / 4;
Can't believe I didn't catch that, sorry for the confusion.
I'm new to coding and especially arrays, so there are a few things that confuse me. For example, why divide topSpeed by (numberOfGears -1) instead of (numberOfGears)? Is it because arrays start at 0, so in order to divide topSpeed by the correct amount of gears in numberOfGears, you have to subtract 1? Sorry if this is a stupid question...it just doesn't register in my head yet :)
Answer by ginryu · Dec 02, 2013 at 02:12 AM
um, first off, you implementation is off. gear speed is based on a gear ration, you make a varible, or an array and make it float[] then set the index to 2 then you make a variable called gear ratio, lets say you make someFloat[0] = 20 and someFloat[1] = 40 you then have gear ratio = somefloat[0]/somefloat[1] then you take your enginepower and divide it by gear ratio,by simply changing someFloat[1] you can get a large variety of outputs. you want to also add a final gear ratio. now this system will allow you to create a very realistic engine/ transmission/ and differential setup. now i do regret having to say that a high ratio will give a weak output, by adding a torque multiplier, to the low gears that decreases as you shift into higher gears where engine power then makes up for low gear power problems. the next step is to apply a gear resistance factor to decrease eng power as pressures slow down the wheel rpms. which would cause the transmission to shift down and increasing gear torque again which compensates for the deceleration caused by climbing hills or sudden increases in drag. however i have yet to find a way to implement this plan. after i finish writing my global control script,i can once again continue testing of the system i just told you. and please, if you figure out this puzzle, i swear you will have created a vehicle control script where just changing 2 values can alter a vehicles performance quite a bit. allowing easy, and very easy to calculate car performance. ill post the code along with explicit directions on setup. also this setting will allow for very realistic rotations of gameobjects, such as drive shafts, camshafts, crankshafts. which i am actually planning on doing. control of All scripts will be done from the global control script. 1 update. and 1 fixed update. im hoping to use Time.deltaTime to apply forces, hopefully this can allow me to avoid fixedUpdate lag. i noticed that having more than a few of these drops performance radically. i must admit though, that that may be due to a mistake in my programming. my email is kristopher.j.logan@gmail.com. and if you need more info on gear ratios and the like howitworks.com is a very good resource on a large variety of subjects. search gear ratios, engines, transmissions, and differentials, torque, and horsepower. i dont understand torque calculations. i studied the content and learned a great deal, even though i was able to implement this knowledge, low gear torque still eludes me. and oh, use rigidbody.velocity.x/y/z to limit the velocity of the vehicle at low gear. oh and use TransformDirection() to switch it to global. as the very high low gear torque multiplier will cause unrealistic low gear car velocitiies. only apply this right at the drive wheels, you don't want to apply it before the differential as it will mess up animation values. ive done alot of thinking on this. and please forgive the typos. im using my android smart phones remote mouse app. as my keyboard broke:( so if this post looks unprofessional, thats why
Your answer
Follow this Question
Related Questions
Error CS0029 Help? (Screenshot of Exact Error) 1 Answer
Find the index of a GameObject within an array 2 Answers
Comparing each random element in an array with each other element 3 Answers
Find specific element when duplicates exist in list. 1 Answer
Outputting data to CSV file from multiple lists at specific Headers in the CSV? 0 Answers