SA-MP Forums Archive
Calculating coords for points behind vehicle (math) - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Calculating coords for points behind vehicle (math) (/showthread.php?tid=658699)



Calculating coords for points behind vehicle (math) - Riwerry - 10.09.2018

Hi guys, what I'm trying to achieve is 4 points (each with x, y, z) behind the vehicle (imagine it as cube). Furthest I got is position behind the vehicle (had function for this). And here's the point of my question, how I can add or substract X Y coordinates from those I got from behind of the vehicle so it would work on different facing angle? Thanks a lot


Re: Calculating coords for points behind vehicle (math) - AmirSavand - 10.09.2018

I don't know the math for this, but I have this function which gets in-front of/behind vehicle.

I think this could help, you just need to move this point to left and right 2 times with different distance.

PHP код:
stock GetInFrontOfVehicle(vehicleid, &Float:x, &Float:yFloat:distance)
{
    new 
Float:a;
    
GetVehiclePos(vehicleidxya);
    
GetVehicleZAngle(vehicleida);
    
+= (distance floatsin(-adegrees));
    
+= (distance floatcos(-adegrees));




Re: Calculating coords for points behind vehicle (math) - Riwerry - 10.09.2018

Yes, I have this too. But from those coords I need to add/substract position based on angle (as you say to the left or right )


Re: Calculating coords for points behind vehicle (math) - Nero_3D - 10.09.2018

You could use the ZAngle but that will only work on flat ground, to do that accurate you need to use GetVehicleRotationQuat
Combine one of these with GetVehicleModelInfo to get behind (+ your offset) of the vehicle

For the ZAngle you could reuse this function
PHP код:
GetInFrontOfAngle(Floatangle, &Float:x, &Float:yFloat:distance) {
    
-= distance floatsin(angledegrees);
    
+= distance floatcos(angledegrees);

PHP код:
new FloatxFloatyFloatzFloata;
GetVehiclePos(vehicleidxyz); 
GetVehicleZAngle(vehicleida); // a is facing to the front
// a + 180 = behind | a - 90 = right | a + 90 = left
GetInFrontOfAngle(180.0xy5.0); // 5.0 behind vehicle center
GetInFrontOfAngle(90.0xy3.0); // 5.0 behind, 3.0 to the right
// save current position in new variables
new FloatfirstX xFloatfirstY y;
GetInFrontOfAngle(90.0xy7.0); // 5.0 behind, 4.0 (3.0 - 7.0) to the left
new FloatsecondX xFloatsecondY y;
// and so on 
For the quat solution search for an include