This topic was marked solved by Sebastian, on April 19th, 2020, 03:03 PM
strange problem and solution

habi

  • Sr. Member
  • Posts: 281
strange problem and solution
«  »
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
Code: [Select]
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.
Code: [Select]
print(FindVehicle(1).Angle)
[SCRIPT] (0, 0, -0.696589, 0.71747)

Maximiliano

  • Newbie
  • Posts: 34
Re: strange problem and solution
« Reply #1,  »
Quote from rww on March 1st, 2019, 04:00 PM
U can get correct vehicle angle by this script

Code: [Select]
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:
Code: [Select]
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);
}

habi

  • Sr. Member
  • Posts: 281
Re: strange problem and solution
« Reply #2,  »Last edited
hi Maximilliano, i had read it before but when we use it with old conf
Code: [Select]
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
Code: [Select]
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 function
Code: [Select]
GetRadiansAngle()
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
Code: [Select]
this.Rotation.w < 0


DarkRaZoR^

  • Full Member
  • VC:MP Battlefield Head Developer/KF Leader
  • Posts: 226
Re: strange problem and solution
« Reply #3,  »
Quote from habi on February 24th, 2020, 04:36 PM
what is Rotation.w? when i tested it is always positive. 0 to 1.
why Gudio might have used condition
Code: [Select]
this.Rotation.w < 0
Perhaps this could help you?


If you're having such problems, you might as well abandon documenting vehicle data on server.conf and do it via script.

habi

  • Sr. Member
  • Posts: 281
Re: strange problem and solution
« Reply #4,  »Last edited
thanks DarkRaZoR^. i googled Quarternions and learned something about it.

the following code started to give me vehicle angle
Code: [Select]
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.
Re: strange problem and solution
« Reply #5,  »
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

Re: strange problem and solution
« Reply #6,  »
hi guys i made a function to accelerate a car
Code: [Select]
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. :)

Xmair

  • Wiki Contributor
  • Posts: 953

Credits to Boystang!

VU Full Member | VCDC 6 Coordinator & Scripter | EG A/D Contributor | Developer of VCCNR | Developer of KTB | Ex-Scripter of EAD

habi

  • Sr. Member
  • Posts: 281