[solver after noobish mistake!] TestFancyCar.java - Reversing?

So I am looking towards creating a driving game. I have been studing the TestFancyCar.java file for a better understanding of how everything works.

This here is the section that controlls the cars steering and braking and all that. But there is no code to make it reverse.


public void onAction(String binding, boolean value, float tpf) {
if (binding.equals(“Lefts”)) {
if (value) {
steeringValue += .5f;
} else {
steeringValue += -.5f;
}
player.steer(steeringValue);
} else if (binding.equals(“Rights”)) {
if (value) {
steeringValue += -.5f;
} else {
steeringValue += .5f;
}
player.steer(steeringValue);
} //note that our fancy car actually goes backwards…
else if (binding.equals(“Ups”)) {
if (value && accelerationValue < 4000 ) {
accelerationValue -= 800;
} else {
accelerationValue += 800;
}
player.accelerate(accelerationValue);
player.setCollisionShape(CollisionShapeFactory.createDynamicMeshShape(findGeom(carNode, “Car”)));
else if (binding.equals(“Downs”)) {
if (value) {
player.brake(40f);
} else {
player.brake(0f);
} }else if (binding.equals(“Reset”)) {
if (value) {
System.out.println(“Reset”);
player.setPhysicsLocation(Vector3f.ZERO);
player.setPhysicsRotation(new Matrix3f());
player.setLinearVelocity(Vector3f.ZERO);
player.setAngularVelocity(Vector3f.ZERO);
player.resetSuspension();
} else {
}
}
}

What I tried to do is basically make a new listener that inverts the “Ups” statement to make the car reverse. It doesn’t work. I have tried playing aroung with the values and things, but can anyone help me with this. Im sure there is something obviouse that I am missing.


inputManager.addMapping(“Backs”, new KeyTrigger(KeyInput.KEY_E));
inputManager.addListener(this, “Backs”);

else if (binding.equals(“Backs”)) {
if (value) {
accelerationValue += 200;
} else {
accelerationValue -=200;
}
}

A link for anyone whos interested:
jmonkeyengine/TestFancyCar.java at master · jMonkeyEngine/jmonkeyengine · GitHub.

Sinceyou wrote the original file I am looking at you @normen

If you go by the @author tag in the file it seems I wrote 50% of all code people post here ^^

Well, if you didn’t write it, then why is your name in the code?

Because somebody copy-pasted a test case?

I’m confused by this

else if (binding.equals(“Backs”)) {
if (value) {
accelerationValue += 200;
} else {
accelerationValue -=200;
}
}

Does this not reverse? Why is accelerationValue being increased when you press the key if you want to reverse? I have a car, and I just set accelerationValue to a negative number and I can reverse

I guess you simply forgot the part which happens after setting the accelerationValue namely Player.accelerate

1 Like

Well what do I know! What a noob! That is exactly what I did. Thanks @Darkchaos, for shedding light on my pathetic mistake!

Because if it isn’t increased, then it does exactly the same thing as go go forward

I… :confused: