Dorian Dore - 1 year ago 76

Javascript Question

I'm trying to write a game in JavaScript, and it has some simple car physics that determines that RPM, torque, speed of the car, etc. I have some functions that convert RPM/hp values into different units, and when the RPM gets past a certain point the gear is incremented. However, for some reason, when the car passes the RPM at which it's supposed to shift up on the final gear, the functions start returning NaN and the object disappears from the canvas. What could be the problem? Here is the code:

`function gameObject(width, height, color, x, y) {`

this.width = width;

this.height = height;

this.speedX = 0;

this.speedY = 0;

this.x = x;

this.y = y;

this.weight = 2500;

this.health = 100;

this.hp = 276;

this.accelTime = 1;

this.gearRatios = [1.98, 1.64, 1.4, 1.23, 1.04];

this.gear = 1;

this.RPM = 0;

this.shiftUpRPM = 7000;

this.shiftDownRPM = 1100;

ctx = game.canvas.getContext('2d');

ctx.fillStyle = color;

ctx.fillRect(this.x, this.y, this.width, this.height);

this.update = function() {

if (this.RPM >= this.shiftUpRPM && this.gear < 5) {

this.RPM = 1400;

this.gear += 1;

} else if (this.RPM <= this.shiftDownRPM && this.gear > 1) {

this.RPM = 5200;

this.gear -= 1;

}

ctx = game.context;

ctx.fillStyle = color;

ctx.fillRect(this.x, this.y, this.width, this.height);

}

this.newPos = function() {

this.x += this.speedX;

this.y += this.speedY;

};

}

var torqueToNewton = function(torque) {

return torque * 1.36;

}

var calculateTorqueOutput = function(hp, RPM) {

console.log("cTO:" + (hp * 5252) / RPM)

return (hp * 5252) / RPM;

}

var velocity = function(kn_energy, mass) {

var v = Math.sqrt(2*kn_energy/mass);

return v;

}

var detectCollision = function(object1, object2) {

object1.left = object1.x;

object1.right = object1.x + object1.width;

object1.bottom = object1.y

object1.top = object1.y + object1.height;

object2.left = object2.x;

object2.right = object2.x + object2.width;

object2.bottom = object2.y

object2.top = object2.y + object2.height;

}

var updateGame = function() {

game.clear();

if (window.key && window.key == 83) {

yourObject.speedX = -1;

}

if (window.key && window.key == 87) {

yourObject.RPM += 1;

nm_s = torqueToNewton(calculateTorqueOutput(yourObject.hp, yourObject.RPM));

yourObject.speedX += velocity(nm_s, yourObject.weight)/10;

yourObject.RPM /= (1/yourObject.gearRatios[yourObject.gear]);

console.log(yourObject.speedX + ":" + yourObject.RPM + ":" + yourObject.gear);

}

else {

if (yourObject.RPM > 0 && yourObject.speedX > 0) {

yourObject.RPM -= yourObject.speedX * (.25 / yourObject.speedX);

yourObject.speedX -= .25;

console.log(yourObject.speedX + ":" + yourObject.RPM + ":" + yourObject.gear);

} else {

yourObject.RPM = 0;

yourObject.speedX = 0;

}

}

if (window.key && window.key == 38) {yourObject.speedY = -1; }

if (window.key && window.key == 39) {yourObject.speedY = 1; }

yourObject.newPos();

yourObject.update();

Answer Source

While I didn't inspect all your code, one bug is certainly here:

```
&& this.gear < 5
```

This would allow the gear to go up to `5`

, while the last gear you defined is `4`

(you have a total of `5`

gears, and since the indexing is zero based, the last one is not `5`

, but `4`

).

To fix it, change it to

```
&& this.gear < 4
```