throttle interpretation
by rennie moffat · in Torque Game Builder · 08/19/2009 (4:13 pm) · 3 replies
Edit:
I think my main issue with this code, is that the author has used a "throttle" to define his forward movement in movemap. However he uses "gasPedal" & "throttle" in his code.
thanks
ren
original post with code.
\\\
In this code, I understand how the throttle works in forward motion. But how/what is the thinking in the second portion (/ if only the 'brake' is pressed and we aren't going forward)specifically for this, where he has programmed the car when only break is being pressed.
also, in the same line.
original code (car physics demo)
I think my main issue with this code, is that the author has used a "throttle" to define his forward movement in movemap. However he uses "gasPedal" & "throttle" in his code.
((%this.brake && !%this.gasPedal) && %this.throttle <= 0). Anyone who could clarify why would be greatly appreciated.
thanks
ren
original post with code.
\\\
In this code, I understand how the throttle works in forward motion. But how/what is the thinking in the second portion (/ if only the 'brake' is pressed and we aren't going forward)specifically for this, where he has programmed the car when only break is being pressed.
/// if only the 'brake' is pressed and we aren't going forward else if((%this.brake && !%this.gasPedal) && %this.throttle <= 0why, if we aren't moving forward, is throttle <= 0? should it not just be = 0? Is this just one programmers interpretation? In other words, my logic, nor his is wrong?
also, in the same line.
(%this.brake && !%this.gasPedal)he uses the reverse in the throttle/forward code.
(%this.gasPedal && !%this.brake)I understand the logic of this, one is on the other is not. But he has no command for gasPedal (moveMap). So what is the core purpose of this?
original code (car physics demo)
function PhysCar::onTimer(%this)
{
// 1. interpret input flags
// Throttle:
// if only the gas pedal is pressed and we aren't reversing
if((%this.gasPedal && !%this.brake) && %this.throttle >=0)
{
// increment trottle (forward)
%this.throttle += 0.005;
// clamp throttle at 1 for full forward
if(%this.throttle > 1)
{
%this.throttle = 1;
}
}
// if only the 'brake' is pressed and we aren't going forward
else if((%this.brake && !%this.gasPedal) && %this.throttle <= 0)
{
// decrement throttle (reverse)
%this.throttle -= 0.005;
// clamp throttle at -1 for full reverse
if(%this.throttle < -1)
{
%this.throttle = -1;
}
}About the author
My thanks to Garage Games and the Garage Games Community combined with owned determination I got one game up, Temple Racer and I am looking to build more interesting, fun games for the mass market of the iOS app store.
#2
So, we have to account for the fact that any given floating-point number might be a tiny amount "off." Writing a test like "%this.throttle <= 0" allows for the possibility that %this.throttle may be a negative number that is very, very close but not precisely equal to zero.
Another common idiom, used to compare two floating-point values, is to check that the difference between them is within a certain tolerance, rather than simply checking for exact equality - for instance, instead of writing "if (%a = %b) { ... }", we'd write "if ((%a - %b) <= 0.000001) { ... }"
The classic paper on this is Goldberg's What Every Computer Scientist Should Know About Floating-Point Arithmetic.
08/19/2009 (4:33 pm)
The answer to your first question has to do with how computers represent floating-point numbers. Some numbers can't be exactly represented in a binary format, for much the same reason that one-third can't be exactly represented in decimal.So, we have to account for the fact that any given floating-point number might be a tiny amount "off." Writing a test like "%this.throttle <= 0" allows for the possibility that %this.throttle may be a negative number that is very, very close but not precisely equal to zero.
Another common idiom, used to compare two floating-point values, is to check that the difference between them is within a certain tolerance, rather than simply checking for exact equality - for instance, instead of writing "if (%a = %b) { ... }", we'd write "if ((%a - %b) <= 0.000001) { ... }"
The classic paper on this is Goldberg's What Every Computer Scientist Should Know About Floating-Point Arithmetic.
#3
08/19/2009 (4:34 pm)
great thank you.
Torque Owner rennie moffat
Renman3000
this works as a mathematical formula like (a+b) +C<=0
so to speak, I know thats rough anyhow,
please carry on.
thanks