strange problem and solution

Started by habi, Feb 24, 2020, 03:29 PM

Previous topic - Next topic

habi

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)

Maximiliano

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);
}

habi

#2
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

D4rkR420R

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?


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

habi

#4
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.

habi

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


habi

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. :)

Xmair

But you can just use this instead of that big code:
vehicle.RelativeSpeed.y += value;

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