Controling Vehicles

Hi, I have a little trouble, what is the best method to control two cars, but no at same time. I have one primary car (car1) and one secondary car (car2) and I want to control them both, but no both, like a both, both like you are riding car1 and then press key to leave car1 and ride car2 and then you can press key again to ride car1. I tried some method but it wasn’t that I want. Which way you recommend? Do I must create another one control, that will control the cars?
Thanks

So, are you asking of the best approach, or how to do this in code?~

If you want to control 2 cars, but only 1 at the same time, then assing key 1 to car 1 and key 2 to car 2. This way, you can change the active car. Or, you can also use the mouse buttons.

Do something like variables car1, car2 and carActive.

When it presses 1, carActive = car1;

When it presses 2, carActive = car2;

But what are you exactly trying to achieve?

Yes, I’m. That is good method what you are thinking, but in OnAction method I have that: -if player press “W” key- car1.accelerate(value); and I don’t know how to do it here. I want that car1 and car2 will accelerate at same “W” key, and car1 and car2 I have definited like VehicleControl, so I will think about your method, thats helped to me, but I will must define in every -if player press- what car is actualy active. Thank you. And if you help me to do this code I’ll be very happy, and by the time, I want add more and more cars.

if (player.pressKey == X && player.controlledCar == 2){
 controlCar2();
} else if (player.pressKey == X && player.controlledCar == 1){
 controlCar1();
}

?

That’s a nice code, but I’m new and this is much complicated for me, please add some description of it and where I need to put it. Thanks

Can you show us the code that you do have?

http://wiki.jmonkeyengine.org/doku.php/jme3

EDIT: that, and 1 year of being a Java programmer should lead to success…

I don’t know you, so don’t take this personally or anything, but if you aren’t comfortable with java, work on that first.

Let me explain you @normen’s example:

Firstly, you must understand this code won’t run by simply copying and pasting. This code is only logical. That means it’s a “skeleton” of your game logic. This is what you want to achieve.

if (player.pressKey == X && player.controlledCar == 2){

if the player pressed the X key and the controlled car is the second one (2):

controlCar2()

You control the second car.

} else if (player.pressKey == X && player.controlledCar == 1){

But, if the player pressed the X key and the controlled car is the first one (1):

controlCar1();

You control the first car.

That’s it. @normen if this isn’t exactly what you thought then i’m sorry xD

Now, when you implement things in game, you have to structure the logic. If you’re using VehicleControl, go here and learn how to use it. If you’re already using physics, I suppose you have done the tutorials. Anything you need I’m glad to help :wink:

Thank you @Ev1lbl0w, for explaining @normen’s example, I understand all that, but I want to know what is player here and how he mean controlledCar and controlCar1. I have trouble about controling cars, creating and setting won’t be problem for me, I’ll post you some code or pictures of my game if you want.

So I worked on it, and I solve it by creating booleans car1 and car2 and there’s my simple code:

    if(player.equals("Car1")&& !value) {
                 car1 = true;
                 car2 = false;
                 chaseCam.setSpatial(vehicleNode);
            }
    if(player.equals("Car2")&& !value) {
                chaseCam.setSpatial(vehicleNode2);
                 car1 = false;
                 car2 = true;
            }
            if(car2) {
    if (player.equals("Ups")){
            if (value) {
                accelerationValue += accelerationForce;
            } else {
                accelerationValue -= accelerationForce;
            }
            vehicle2.accelerate(0, accelerationValue);
            vehicle2.accelerate(2, accelerationValue);
           }
         } if (car1) {
         if (player.equals("Ups")){
        if (value) {
            accelerationValue += accelerationForce;
        } else {
            accelerationValue -= accelerationForce;
        }
        vehicle.accelerate(0, accelerationValue);
        vehicle.accelerate(2, accelerationValue);
       }     

That’s little example how it work now in OnAction method, ask if you have any questions and reply to me if you know how I can improve this. Thank you guys, you helped me a lot. :wink:

Hi,
I worked on my game and now, there are more than 2 cars, so I want to ask you a question:
Is it possible to simplify my previous code?

  • onAction method

    if(player.equals(“Car1”)) {
    car1 = true;
    car2 = false;
    car3 = false;

    }
    if(player.equals(“Car2”)) {
    car1 = false;
    car2 = true;
    car3 = false;
    }
    if(player.equals(“Car3”)) {
    car1 = false;
    car2 = false;
    car3 = true;
    }
    if(car1) {
    //control car1
    }
    if(car2) {
    //control car2
    }
    if(car3) {
    //control car3
    }
    As you can see, I must disable other cars one by one and control every car indepentent. It’s going to be worse if there will be over 5 cars. So I want to do it something like:

    if(player.equals(“Car1”)) {
    allCars = false;
    car1 = true;
    }
    If there is tutorial for this, I’m sorry, but I didn’t find it, so post it here and I will read it.

@Ev1lbl0w has a good idea, but I don’t know how to do it.

Thanks for help guys.

First off,
use else if or switch statements, which will (at least when it’s not the last car) reduce the number of if checks (also looks better).
Then, you can use car1 = car2 = car3 = false; which will set all of them to false, or use it for as many variables as you want. It basically says: car3 = false, because car2 = car3, car2 = false, because car1 = car2, car1 = false.

On another note,
it might be time to switch to an int-based system like this:
int currentCar = 0;

//On action method
if(player.equals("Car1"))
{
    currentCar = 0; //Computers usually count from zero
}
else if(player.equals("Car2"))
{
    currentCar = 1;
}
//etc.

switch(currentCar)
{
    case 0:
        //Control car 1
    //etc.
}

Or just use OOP…

Have one active object and switch it out. Just call the one that is active.

Thank you @Robbi_Blechdose! It was pretty easy. Now, how to do stuff like currentCar.getSpeed…
@pspeed I don’t know what is OOP and what it means. Can you explain me that?

This link will do more than I could:
https://lmgtfy.com/?q=oop

Ahh, I google it like jmonkey oop. That’s why Google doesn’t find it :grinning_face_with_smiling_eyes: