3. #### Suggestions

*DO NOT* SHARE MEDIA OR LINKS TO LEAKED COPYRIGHTED MATERIAL. Discussion is allowed.

# Resurrecting a vehicle in-motion

## Recommended Posts

Hello, I have been trying to figure out a good way to fix a "dead" vehicle so that it is completely repaired. The normal SET_VEHICLE_FIXED() native makes a dead vehicle feel alive, but it has lost a lot of its physics, you cannot go first person in it and peds will not drive it. So I figured that - like with resurrecting peds - you probably have to duplicate and replace the vehicle with a new one to truly fix it. It's very easy for peds because you can just use PED::CLONE_PED(), but I have found no such native for vehicles so I have to copy everything manually.

I have gotten it to the point where I can pretty seamlessly replace my midair-spinning dead vehicle with a new vehicle of the same type, color, mods and transfer the passengers, but the car's spinning/rotation angle will be inverted about half the time you press the 'repair' button . I'm not sure I really understand the math behind how to transfer rotational velocity from the old vehicle to the new one.

Right now I get the old vehicle's quaternion, velocity and rotational velocity and transfer them all to the new vehicle, but I run into problems with the rotational velocity. I've tried a bunch of different combinations of parameters for APPLY_FORCE_TO_ENTITY() and this is what I have now:

`Vector3 velocity = ENTITY::GET_ENTITY_VELOCITY(oldVeh);Vector3 rotationalVelocity = ENTITY::GET_ENTITY_ROTATION_VELOCITY(oldVeh);Vector3 rotation = ENTITY::GET_ENTITY_ROTATION(oldVeh, 2);float x; float y; float z; float w;ENTITY::GET_ENTITY_QUATERNION(oldVeh, &x, &y, &z, &w);...ENTITY::SET_ENTITY_QUATERNION(newVeh, x, y, z, w);ENTITY::SET_ENTITY_VELOCITY(newVeh, velocity.x, velocity.y, velocity.z);if (!ENTITY::IS_ENTITY_UPSIDEDOWN(newVeh))    ENTITY::APPLY_FORCE_TO_ENTITY(newVeh, 5, rotationalVelocity.x, rotationalVelocity.y, rotationalVelocity.z, rotation.x, rotation.y, rotation.z, 2, true, false, true, false, false);   else    ENTITY::APPLY_FORCE_TO_ENTITY(newVeh, 5, -rotationalVelocity.x, -rotationalVelocity.y, -rotationalVelocity.z, rotation.x, rotation.y, rotation.z, 2, true, false, true, false, false);`

...but it is still wrong a lot of the time when the car is spinning wildly in the air, and the new car will be spinning in a different way.

How do you perfectly transfer rotational force from one object to another?

Also the new vehicle is immune to being detected by my raycasting or manipulated by my gravity gun for a few seconds after it appears, even though I can apply the initial velocity instantly. If it collides with something, it becomes detectable. Why is this?

Edited by whorse
##### Share on other sites

What I would try is something like

`Vector3 velocity = ENTITY::GET_ENTITY_VELOCITY(oldVeh);Vector3 rotationalVelocity = ENTITY::GET_ENTITY_ROTATION_VELOCITY(oldVeh);Vector3 rotation = ENTITY::GET_ENTITY_ROTATION(oldVeh, 2);float x; float y; float z; float w;ENTITY::GET_ENTITY_QUATERNION(oldVeh, &x, &y, &z, &w);//...ENTITY::SET_ENTITY_QUATERNION(newVeh, x, y, z, w);ENTITY::SET_ENTITY_ROTATION(newVeh, rotation.x, rotation.y, rotation.z, 2, true);ENTITY::APPLY_FORCE_TO_ENTITY(newVeh, 5, velocity.x, velocity.y, velocity.z, rotationalVelocity.x, rotationalVelocity.y, rotationalVelocity.z, 2, true, false, true, false, false);`

And to fix the raycast maybe you could use one of these natives (no idea if any of them will help)

`STREAMING::REQUEST_COLLISION_FOR_MODEL(Hash model)STREAMING::REQUEST_COLLISION_AT_COORD(float x, float y, float z)// while (!STREAMING::HAS_COLLISION_FOR_MODEL_LOADED(model)) STREAMING::REQUEST_COLLISION_FOR_MODEL(model);VEHICLE::SET_VEHICLE_DAMAGE(Vehicle vehicle, float xOffset, float yOffset, float zOffset, float damage, float radius, BOOL p6) `
Edited by Jitnaught
##### Share on other sites

no point saving rotation and quaternion info, they both store the same info

• 1
##### Share on other sites

I had tried a couple collision related natives, but not those three or setting vehicle damage. I'll let you know if it works.
and

What I would try is something like

`Vector3 velocity = ENTITY::GET_ENTITY_VELOCITY(oldVeh);Vector3 rotationalVelocity = ENTITY::GET_ENTITY_ROTATION_VELOCITY(oldVeh);Vector3 rotation = ENTITY::GET_ENTITY_ROTATION(oldVeh, 2);float x; float y; float z; float w;ENTITY::GET_ENTITY_QUATERNION(oldVeh, &x, &y, &z, &w);//...ENTITY::SET_ENTITY_QUATERNION(newVeh, x, y, z, w);ENTITY::SET_ENTITY_ROTATION(newVeh, rotation.x, rotation.y, rotation.z, 2, true);ENTITY::APPLY_FORCE_TO_ENTITY(newVeh, 5, velocity.x, velocity.y, velocity.z, rotationalVelocity.x, rotationalVelocity.y, rotationalVelocity.z, 2, true, false, true, false, false);`
And to fix the raycast maybe you could use one of these natives (no idea if any of them will help)
`STREAMING::REQUEST_COLLISION_FOR_MODEL(Hash model)STREAMING::REQUEST_COLLISION_AT_COORD(float x, float y, float z)// while (!STREAMING::HAS_COLLISION_FOR_MODEL_LOADED(model)) STREAMING::REQUEST_COLLISION_FOR_MODEL(model);VEHICLE::SET_VEHICLE_DAMAGE(Vehicle vehicle, float xOffset, float yOffset, float zOffset, float damage, float radius, BOOL p6) `

I tried doing the APPLY_FORCE_TO_ENTITY like that first and it was very inconsistent. What seems to be especially hard to account for is the car's corkscrew motion (but not always?), while it is simultaneously spinning about another axis. I tried the upside-down check for a while and it seemed to make little difference, so I removed it.
I'm not really that sure why, but I have had the most success with it by multiplying the rotation and rotational velocity by the vehicle's forward-vector, like this:

`ENTITY::APPLY_FORCE_TO_ENTITY(newVeh, 5, rotationalVelocity.x * fwdVector.x, rotationalVelocity.y * fwdVector.y, rotationalVelocity.z * fwdVector.z, rotation.x * fwdVector.x, rotation.y * fwdVector.y, rotation.z * fwdVector.z, 2, true, false, true, false, false);`

Here's a video clip of what I'm seeing (throwing my own vehicle around with a gravity gun and resurrecting it in midair):
http://webm.land/media/EG36.webm

Most of the replacements are pretty successful in that one (it's usually at most 50%), but you can see what I'm talking about at the end of the video. It's like the rotation of the new car is completely reversed of what the old car had, or something.

no point saving rotation and quaternion info, they both store the same info

Thanks for the heads up. I literally had never heard of a quaternion before seeing it in the natives.h, so I wasn't sure whether the x,y,z values were the same as for rotation. What is w, by the way?

Edited by whorse
##### Share on other sites

Instead of replacing the vehicle, maybe we should figure out how to fix the one we already have? It's likely just a flag set on the vehicle. I'll have some free time next week. If nobody has found it by then, I'll look into it.

Have you tried applying the rotational velocity with isRel set to false?

Edited by InfamousSabre
##### Share on other sites

Instead of replacing the vehicle, maybe we should figure out how to fix the one we already have? It's likely just a flag set on the vehicle. I'll have some free time next week. If nobody has found it by then, I'll look into it.

It has been looked into before, but was not completely figured out. Hopefully you can figure it out

##### Share on other sites

Hello, I have been trying to figure out a good way to fix a "dead" vehicle so that it is completely repaired. The normal SET_VEHICLE_FIXED() native makes a dead vehicle feel alive, but it has lost a lot of its physics, you cannot go first person in it and peds will not drive it. So I figured that - like with resurrecting peds - you probably have to duplicate and replace the vehicle with a new one to truly fix it. It's very easy for peds because you can just use PED::CLONE_PED(), but I have found no such native for vehicles so I have to copy everything manually.

I have gotten it to the point where I can pretty seamlessly replace my midair-spinning dead vehicle with a new vehicle of the same type, color, mods and transfer the passengers, but the car's spinning/rotation angle will be inverted about half the time you press the 'repair' button . I'm not sure I really understand the math behind how to transfer rotational velocity from the old vehicle to the new one.

Right now I get the old vehicle's quaternion, velocity and rotational velocity and transfer them all to the new vehicle, but I run into problems with the rotational velocity. I've tried a bunch of different combinations of parameters for APPLY_FORCE_TO_ENTITY() and this is what I have now:

`Vector3 velocity = ENTITY::GET_ENTITY_VELOCITY(oldVeh);Vector3 rotationalVelocity = ENTITY::GET_ENTITY_ROTATION_VELOCITY(oldVeh);Vector3 rotation = ENTITY::GET_ENTITY_ROTATION(oldVeh, 2);float x; float y; float z; float w;ENTITY::GET_ENTITY_QUATERNION(oldVeh, &x, &y, &z, &w);...ENTITY::SET_ENTITY_QUATERNION(newVeh, x, y, z, w);ENTITY::SET_ENTITY_VELOCITY(newVeh, velocity.x, velocity.y, velocity.z);if (!ENTITY::IS_ENTITY_UPSIDEDOWN(newVeh))    ENTITY::APPLY_FORCE_TO_ENTITY(newVeh, 5, rotationalVelocity.x, rotationalVelocity.y, rotationalVelocity.z, rotation.x, rotation.y, rotation.z, 2, true, false, true, false, false);   else    ENTITY::APPLY_FORCE_TO_ENTITY(newVeh, 5, -rotationalVelocity.x, -rotationalVelocity.y, -rotationalVelocity.z, rotation.x, rotation.y, rotation.z, 2, true, false, true, false, false);`

...but it is still wrong a lot of the time when the car is spinning wildly in the air, and the new car will be spinning in a different way.

How do you perfectly transfer rotational force from one object to another?

Also the new vehicle is immune to being detected by my raycasting or manipulated by my gravity gun for a few seconds after it appears, even though I can apply the initial velocity instantly. If it collides with something, it becomes detectable. Why is this?

I decided to look and figured it out. Infamous was right. It is a simple flag in the vehicle class:

`auto address = getScriptHandleBaseAddress(vehicle);*(BYTE*)(address + 0xD8) &= ~7;`
##### Share on other sites

I decided to look and figured it out. Infamous was right. It is a simple flag in the vehicle class:

`auto address = getScriptHandleBaseAddress(vehicle);*(BYTE*)(address + 0xD8) &= ~7;`

Amazing! Thank you so much! I can just use this with SET_VEHICLE_FIXED and SET_VEHICLE_DEFORMATION_FIXED and the original vehicle is good as new

##### Share on other sites

`Amazing! Thank you so much! I can just use this with SET_VEHICLE_FIXED and SET_VEHICLE_DEFORMATION_FIXED and the original vehicle is good as new`

If you still have the code for saving the vehicle mods and transferring the passengers, would you mind sharing that code?

##### Share on other sites

• 1 year later...
On 11/1/2016 at 2:09 AM, whorse said:

Amazing! Thank you so much! I can just use this with SET_VEHICLE_FIXED and SET_VEHICLE_DEFORMATION_FIXED and the original vehicle is good as new

On 11/3/2016 at 2:33 AM, stillhere said:

If you still have the code for saving the vehicle mods and transferring the passengers, would you mind sharing that code?

You bitch, you wouldn't even give a naturally nice canadi-gander your yet so simple code? He joined in 2006. Do him a favor, give him the code. You whor-se !

-Sent from the library.

-Tasked auto-send this message in exactly, 9 months from november, on the 3rd. at 12:13.

## Create an account

Register a new account

×