Jump to content

[C++] How can the throttle and brakes of a vehicle be programmatically controlled?


Recommended Posts

First, is there a direct way of doing it or does it have to be done through simulating player input? Would one method even have any foreseeable advantages over the other? Either way, can someone point me in the right direction?

 

Regarding the former method, I have considered the following natives:

VEHICLE::SET_VEHICLE_FORWARD_SPEEDVEHICLE::_SET_VEHICLE_HALT

Under certain circumstances, the first native makes the vehicle fly. Therefore, it is not ideal unless I can find a way to prevent that. In one case, it is okay for the vehicle to instantly be set to a certain speed; I do not care about accelerating there "naturally". It would be useful to be able to constantly keep the vehicle at a certain speed by controlling its speed directly instead of its throttle. However, in another case, I do need it to accelerate naturally (though this case is less important).

 

The second native does not make the vehicle stop in a natural manner; I need the vehicle to stop at the distance it would stop at if the brakes were applied, not some arbitrary distance I specify.

 

Regarding the latter method, I have looked at some natives:

CONTROLS::_SET_CONTROL_NORMALCONTROLS::ENABLE_CONTROL_ACTION

However, after looking through their documentation, I am still unclear about their purpose and how they would be implemented to simulate player input. Another concern is the brake control also controlling reverse. I suppose I could apply the control until the vehicle's speed is at/near zero, but I do not think that would be ideal for my purposes.

Edited by Markk

CONTROLS::_SET_CONTROL_NORMAL simulates player input and can be used to to programmatically control the players' vehicle/movement.

 

There aren't natives for accelerating/braking naturally afaik, but you should be able to assign tasks to peds.

 

You can override throttle and brake in memory but the game keeps overriding those. I haven't checked how it affects non-player vehicles.

CONTROLS::_SET_CONTROL_NORMAL simulates player input and can be used to to programmatically control the players' vehicle/movement.

 

There aren't natives for accelerating/braking naturally afaik, but you should be able to assign tasks to peds.

 

You can override throttle and brake in memory but the game keeps overriding those. I haven't checked how it affects non-player vehicles.

 

hey,

 

there is a way to simulate acceleration or braking. You can use the void TASK_VEHICLE_TEMP_ACTION(Ped driver, Vehicle vehicle, int action, int time) native.

Combined with key presses, you can control a vehicle (with a driver) without even having to control the ped itself. :panic:

 

CONTROLS::_SET_CONTROL_NORMAL simulates player input and can be used to to programmatically control the players' vehicle/movement.

 

There aren't natives for accelerating/braking naturally afaik, but you should be able to assign tasks to peds.

 

You can override throttle and brake in memory but the game keeps overriding those. I haven't checked how it affects non-player vehicles.

 

hey,

 

there is a way to simulate acceleration or braking. You can use the void TASK_VEHICLE_TEMP_ACTION(Ped driver, Vehicle vehicle, int action, int time) native.

Combined with key presses, you can control a vehicle (with a driver) without even having to control the ped itself. :panic:

 

 

That native seems to be exactly what I need. What is function of the time parameter? My guess is the duration, in ms, of the action to be executed.

Yes it's the actual action duration in miliseconds.

I would recommend you to use short values (100ms to 500ms) if you're using key events.

I intend to use it for unknown periods of time. For example, accelerating until a given speed is reached. What would be a good way to implement this - just call the native every frame until the speed is reached?

 

Yes it's the actual action duration in miliseconds.

I would recommend you to use short values (100ms to 500ms) if you're using key events.

I intend to use it for unknown periods of time. For example, accelerating until a given speed is reached. What would be a good way to implement this - just call the native every frame until the speed is reached?

 

Sounds like a good plan. Check if the vehicle is on all wheels, has not collided with anything and keep calling the native (with short ms again, to avoid the function to be called outside your loop) until it reaches the defined limit. If the speed is suddenly higher (happens during last frame) you can adjust it by setting the vehicle speed directly to it's limit (VEHICLE::SET_VEHICLE_FORWARD_SPEED(float limit); to create a smooth effect.

Edited by ins1de

 

 

Yes it's the actual action duration in miliseconds.

I would recommend you to use short values (100ms to 500ms) if you're using key events.

I intend to use it for unknown periods of time. For example, accelerating until a given speed is reached. What would be a good way to implement this - just call the native every frame until the speed is reached?

 

Sounds like a good plan. Check if the vehicle is on all wheels, has not collided with anything and keep calling the native (with short ms again, to avoid the function to be called outside your loop) until it reaches the defined limit. If the speed is suddenly higher (happens during last frame) you can adjust it by setting the vehicle speed directly to it's limit (VEHICLE::SET_VEHICLE_FORWARD_SPEED(float limit); to create a smooth effect.

 

Thanks for all the help. I have got something working, for the most part. The collision check does not seem to do anything:

void update() {	Vehicle vehicle = PED::GET_VEHICLE_PED_IS_IN(playerPed, false);		if (VEHICLE::IS_VEHICLE_ON_ALL_WHEELS(vehicle) && !ENTITY::HAS_ENTITY_COLLIDED_WITH_ANYTHING(vehicle)) {		AI::TASK_VEHICLE_TEMP_ACTION(playerPed, vehicle, 32, 200);	}}

The vehicle continues to accelerate even if it is in contact with another vehicle.

Great! Well I guess it [ENTITY::HAS_ENTITY_COLLIDED_WITH_ANYTHING] doesn't return true because it is checking if your vehicle is on all its wheels at the same time.

if((VEHICLE::IS_VEHICLE_ON_ALL_WHEELS(vehicle) && !ENTITY::HAS_ENTITY_COLLIDED_WITH_ANYTHING(vehicle)){ // we rollin' deep}else if((!VEHICLE::IS_VEHICLE_ON_ALL_WHEELS(vehicle) && ENTITY::HAS_ENTITY_COLLIDED_WITH_ANYTHING(vehicle)){// crashed into something, we hit something and at least one wheel left the ground}

You can also create a system to prevent collisions at all by raycasting in front of the car.

 

For further information about the TEMP_ACTION native : https://github.com/BenjaminFaal/TwoPlayerMod/search?utf8=%E2%9C%93&q=VehicleAction&type=

Edited by ins1de

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • 0 User Currently Viewing
    0 members, 0 Anonymous, 0 Guests

×
×
  • Create New...

Important Information

By using GTAForums.com, you agree to our Terms of Use and Privacy Policy.