How to calculate the pos in front of player's vehicle?

Started by ysc3839, Feb 09, 2015, 04:06 PM

Previous topic - Next topic

ysc3839

I want to spawn a ramp in front of player's vehicle. Can somebody tell me how to calculate the pos? Thanks! :)

DizzasTeR

Also trying to figure this out but can't for some reason the object doesnt appear when i touch its pos.


Thijn

You can calculate the position in front of a player like so:
local pos = pPlayer.Pos;
local rot = pPlayer.Angle, rad = rot * 3.14159265359 / 180.0;

local x = pos.x, y = pos.y;
local x2 = x + 1.0 * cos(rad) - 25.0 * sin(rad);
local y2 = y + 1.0 * sin(rad) + 25.0 * cos(rad);

DizzasTeR

Quote from: Thijn on Feb 09, 2015, 05:22 PMYou can calculate the position in front of a player like so:
local pos = pPlayer.Pos;
local rot = pPlayer.Angle, rad = rot * 3.14159265359 / 180.0;

local x = pos.x, y = pos.y;
local x2 = x + 1.0 * cos(rad) - 25.0 * sin(rad);
local y2 = y + 1.0 * sin(rad) + 25.0 * cos(rad);

To be honest, understood nothing.... maybe my math skills suck.

Thijn

Might give a bit more information: clicky
Quote from: Shadow.// [Ka]Juppi's func from http://forum.liberty-unleashed.co.uk/index.php/topic,398.0.html
// ported to fit your needs
function GetForwardPoint( pos, angle ) // you must pass an angle in degrees
{
local rad = angle * PI / 180; // we convert to radians
                local x = pos.x, y = pos.y;
local x2 = x + 1.0 * cos(rad) - 25.0 * sin(rad); // we calculate
local y2 = y + 1.0 * sin(rad) + 25.0 * cos(rad);
                return Vector( x2, y2, pos.z ); // return a vector
}

usage:

player.Pos = GetForwardPoint( FindVehicle(0) /* example */, FindVehicle(0).EulerAngle.z );

Sebastian

Here is a ramp system made by Juppi, but for LU.
Take from there the needed info, but give credits for Juppi.

ysc3839

Quote from: Thijn on Feb 09, 2015, 07:07 PMMight give a bit more information: clicky
Quote from: Shadow.// [Ka]Juppi's func from http://forum.liberty-unleashed.co.uk/index.php/topic,398.0.html
// ported to fit your needs
function GetForwardPoint( pos, angle ) // you must pass an angle in degrees
{
local rad = angle * PI / 180; // we convert to radians
                local x = pos.x, y = pos.y;
local x2 = x + 1.0 * cos(rad) - 25.0 * sin(rad); // we calculate
local y2 = y + 1.0 * sin(rad) + 25.0 * cos(rad);
                return Vector( x2, y2, pos.z ); // return a vector
}

usage:

player.Pos = GetForwardPoint( FindVehicle(0) /* example */, FindVehicle(0).EulerAngle.z );
But there's also something wrong...

Gudio

function GetForwardPoint( pos, angle )
{
  local x = pos.x + 1.0 * cos(angle) - 25.0 * sin(angle)
  local y = pos.y + 1.0 * sin(angle) + 25.0 * cos(angle);
  return Vector( x, y, pos.z );
}

Don't pass player's angle to GetForwardPoint function while driving the vehicle, because only vehicle's angle is changed!
See my next post!

ysc3839

Quote from: Gudio on Feb 10, 2015, 10:12 AMThere is no point to pass an angle in degrees since 0.4 supports radians.
function GetForwardPoint( pos, angle )
{
  local x = pos.x + 1.0 * cos(angle) - 25.0 * sin(angle)
  local y = pos.y + 1.0 * sin(angle) + 25.0 * cos(angle);
  return Vector( x, y, pos.z );
}

Don't pass player's angle to GetForwardPoint function while driving the vehicle, because only vehicle's angle is changed!
GetForwardPoint( vehicle.Pos, vehicle.EulerAngle.z );

I haven't tested it.
The same issue. :(

Thijn


Gudio

Hey, I've written this function for you. Use it with updated squirrel module, because previous module returns vehicle's rotation with wrong order.

function CVehicle::GetRadiansAngle()
{
local angle;
angle = ::asin( this.Rotation.z ) * 2;

return this.Rotation.w < 0 ? 3.14159 - angle : 6.28319 - angle;
}

Usage: GetForwardPoint( vehicle.Pos, vehicle.GetRadiansAngle() );
Hopefully it works well now.

Sebastian

Sounds good, Gudio.
We should be able to set the distance also. Add a parameter for that. :)

Fuzzie

Off:
Quote from: Gudio on Feb 11, 2015, 09:28 PMHey, I've written this function for you. Use it with updated squirrel module, because previous module returns vehicle's rotation with wrong order.
Please use the Server & Plugin Updates board so it would easier for us to be updated with any changes to the modules. :(

Gudio

@Fuzzie, I have no rights to create a topic there.
@sseebbyy, you can do that. Increase someting in GetForwardPoint function :>

ysc3839

Quote from: Gudio on Feb 11, 2015, 09:28 PMHey, I've written this function for you. Use it with updated squirrel module, because previous module returns vehicle's rotation with wrong order.

function CVehicle::GetRadiansAngle()
{
local angle;
angle = ::asin( this.Rotation.z ) * 2;

return this.Rotation.w < 0 ? 3.14159 - angle : 6.28319 - angle;
}

Usage: GetForwardPoint( vehicle.Pos, vehicle.GetRadiansAngle() );
Hopefully it works well now.
This is right:
function GetRadiansAngle(Rotation)
{
local angle;
angle = ::asin( Rotation.z ) * -2;
return Rotation.w < 0 ? 3.14159 - angle : 6.28319 - angle;
}