Vice City: Multiplayer

Server Development => Scripting and Server Management => Topic started by: habi on Feb 24, 2020, 03:29 PM

Title: strange problem and solution
Post by: habi on Feb 24, 2020, 03:29 PM
hey
i was working with vehicle.Angle and it showed (0,0,0,1) always
But if we go close enough to the vehicle, then it changes.
so i tried print(FindVehicle(1).Angle) it showed (0,0,0,1).

the problem was with the server.conf, i copied it and pasted it on main.nut with strings replaced so that it appeared CreateVehicle(...)

then i got correct values.
print(FindVehicle(1).Angle)
[SCRIPT] (0, 0, -0.696589, 0.71747)
Title: Re: strange problem and solution
Post by: Maximiliano on Feb 24, 2020, 03:58 PM
Quote from: rww on Mar 01, 2019, 04:00 PMU can get correct vehicle angle by this script

function CVehicle::GetRadiansAngle() //by Gudio
{
local angle = ::asin(this.Rotation.z) * -2;
return this.Rotation.w < 0 ? 3.14159 - angle : 6.28319 - angle;
}

I never get problems with values from this code ;)

Example, how to get correct angle by /pos command

onPlayerCommands event:
local veh = player.Vehicle;

if (cmd == "pos")
{
if (!veh) ClientMessage("* Your Cords:[#ffffff] "+player.Pos.x+", "+player.Pos.y+", "+player.Pos.z+", "+player.Angle,player,128,255,0,255);
else ClientMessage("* Your Vehicle Cords:[#ffffff] "+player.Pos.x+", "+player.Pos.y+", "+player.Pos.z+", "+veh.GetRadiansAngle(),player,128,255,0,255);
ClientMessage("* Virtual World:[#ffffff] "+player.World+" [#80ff00]/ Sec World:[#ffffff] "+player.SecWorld+" [#80ff00]/ Unique World: [#ffffff]"+player.UniqueWorld,player,128,255,0,255);
}
Title: Re: strange problem and solution
Post by: habi on Feb 24, 2020, 04:36 PM
hi Maximilliano, i had read it before but when we use it with old conf
print(FindVehicle(1).GetRadiansAngle())
[SCRIPT]  6.28319
print(FindVehicle(2).GetRadiansAngle())
[SCRIPT]  6.28319
this is always the answer.

btw, i am very interested in this vehicle.Rotation. can anyone help me with this.

i tried asin(vehicle.Rotation.z)*2 and i got correct angle like player.Angle.  (note it is not -2 as in fn.)

there is a difference in the radian which the functionGetRadiansAngle() gives. it even gives around 6.28. but player.Angle never gives 6. it is always less that 3.14
i need angle like the that (player.Angle) .

what is Rotation.w? when i tested it is always positive. 0 to 1.
why Gudio might have used condition this.Rotation.w < 0
Title: Re: strange problem and solution
Post by: D4rkR420R on Feb 24, 2020, 05:01 PM
Quote from: habi on Feb 24, 2020, 04:36 PMwhat is Rotation.w? when i tested it is always positive. 0 to 1.
why Gudio might have used condition this.Rotation.w < 0

Perhaps this could help you?
(https://www.researchgate.net/profile/Valerie_Renaudin/publication/262143912/figure/fig4/AS:296806765350928@1447775655957/Illustration-of-a-rotation-with-quaternion-parameters.png)

If you're having such problems, you might as well abandon documenting vehicle data on server.conf and do it via script.
Title: Re: strange problem and solution
Post by: habi on Feb 26, 2020, 01:33 PM
thanks DarkRaZoR^. i googled Quarternions and learned something about it.

the following code started to give me vehicle angle
function getvehicleangle(rotation)
{
return asin(rotation.z)*2;
}

we need not check rotation.w because it is always positive.
i learned that rotation.w = cos(t/2) and rotation.z=sin(t/2)
And our t lies in[-pi,+pi]. ( you know player.Angle is 0 to 3.14 and -0 to -3.14) so t/2 lies in such an interval in which cos(t/2) is always positive. hence rotation.w is always positive.

I had problems with sign of angle, and finally understood and asin returns +ve if angle is positive. and returns -ve if angle is -ve.
so the problem (of my understanding vehicle angle)  is solved.

this function returns angle like player.Angle. and i had tested it with a car. works perfectly.
Title: Re: strange problem and solution
Post by: habi on Feb 26, 2020, 02:30 PM
and i learned that all these vehicle angle are approximations using the z axis alone.

get a pcj 600 and SetStuntBike(true) and lean up on vehicle, and put Announce(vehicle angle) in onvehiclemove. you can see even though the pcj is moving in a certain direction say south and when you lean up, the front of the bike up and angle changes eg. from 3 to 2 or even 1.1

Title: Re: strange problem and solution
Post by: habi on Feb 26, 2020, 03:31 PM
hi guys i made a function to accelerate a car
function accelerate(vehicle)
{
local angle=getvehicleangle(vehicle);
local x=cos(angle);
local y=sin(angle);
local speed=1.0; //0 to 1;
vehicle.AddSpeed(Vector(-y,x,0)*speed);
}
function getvehicleangle(vehicle)
{
return asin(vehicle.Rotation.z)*2;
}

So when the function is called and you are sitting in the car. the car goes straight. :)
Title: Re: strange problem and solution
Post by: Xmair on Feb 27, 2020, 03:50 AM
But you can just use this instead of that big code:
vehicle.RelativeSpeed.y += value;
Title: Re: strange problem and solution
Post by: habi on Feb 27, 2020, 01:57 PM
wow. i tested. both works  8)