Sign in to follow this  
Mimr3w

[Tutorial/Snippet] Teleport in x, y-axis by heading angle

Recommended Posts

Posted (edited)

Basically, this tutorial is for teleporting in x, y-axis by getting player heading angle (rotation).
- Note: If our entity is a vehicle and the player is the driver or passanger, we can use player's heading due they are same.

We can behave with heading angle just like a compass and its points, 

300px-Compass_rose.png
a compass with marked angles

The angle of player heading is exactly behave like the angle of a compass in map. But how we can go front or rear in 10 units while we have x and y axis?

We need to have the angles' ranges, the below table is calculated by me:

N > NW = 0.0 - 22.5
NW = 22.5 - 67.5
W = 67.5 - 112.5
SW = 112.5 - 157.5
S = 157.5 - 202.5
SE = 202.5 - 247.5
E = 247.5 - 292.5
NE = 292.5 - 332.5
NW > N = 332.5 - 360.0

Then, we have to calculate X and Y axis from the angle,  consider amount as the units we need to move in that axis

N > NW = Y + Amount
NW = X - Amount / 2 | Y + Amount / 2
W = X - Amount
SW = X - Amount / 2 | Y - Amount / 2
S = Y - Amount
SE = Y - Amount / 2
E = X + Amount
NE = X + Amount / 2
NW > N = Y + Amount / 2

Based on conditions, we can use entity::position (vehicle / player or anything that we want to teleport in x, y axis)
- Note: We need to create a Vector3 with changed amounts of x and y axis:

player.position = new mp.Vector3(player.position.x + num, player.position.y + num, player.position.z);

At the end; for your work convenience, you can use the snippet of this tutorial:

let pa = player.heading;
    if(player.vehicle) {
        if(pa >= 0.0 && pa <= 22.5) player.vehicle.position = new mp.Vector3(player.vehicle.position.x, player.vehicle.position.y + num, player.vehicle.position.z); //n
        if(pa >= 22.5 && pa <= 67.5) player.vehicle.position = new mp.Vector3(player.vehicle.position.x - (num / 2), player.vehicle.position.y + (num / 2), player.vehicle.position.z); //nw
        if(pa >= 67.5 && pa <= 112.5) player.vehicle.position = new mp.Vector3(player.vehicle.position.x - num, player.vehicle.position.y, player.vehicle.position.z); //w
        if(pa >= 112.5 && pa <= 157.5) player.vehicle.position = new mp.Vector3(player.vehicle.position.x - (num / 2), player.vehicle.position.y - (num / 2), player.vehicle.position.z); //sw
        if(pa >= 157.5 && pa <= 202.5) player.vehicle.position = new mp.Vector3(player.vehicle.position.x, player.vehicle.position.y - num, player.vehicle.position.z); //s
        if(pa >= 202.5 && pa <= 247.5) player.vehicle.position = new mp.Vector3(player.vehicle.position.x, player.vehicle.position.y - (num / 2), player.vehicle.position.z); //se
        if(pa >= 247.5 && pa <= 292.5) player.vehicle.position = new mp.Vector3(player.vehicle.position.x + num, player.vehicle.position.y, player.vehicle.position.z); //e
        if(pa >= 292.5 && pa <= 332.5) player.vehicle.position = new mp.Vector3(player.vehicle.position.x + (num / 2), player.vehicle.position.y + (num / 2), player.vehicle.position.z); //ne
        if(pa >= 332.5 && pa < 360.0) player.vehicle.position = new mp.Vector3(player.vehicle.position.x, player.vehicle.position.y + (num / 2), player.vehicle.position.z); //n
    }
    else {
        if(pa >= 0.0 && pa <= 22.5) player.position = new mp.Vector3(player.position.x, player.position.y + num, player.position.z); //n
        if(pa >= 22.5 && pa <= 67.5) player.position = new mp.Vector3(player.position.x - (num / 2), player.position.y + (num / 2), player.position.z); //nw
        if(pa >= 67.5 && pa <= 112.5) player.position = new mp.Vector3(player.position.x - num, player.position.y, player.position.z); //w
        if(pa >= 112.5 && pa <= 157.5) player.position = new mp.Vector3(player.position.x - (num / 2), player.position.y - (num / 2), player.position.z); //sw
        if(pa >= 157.5 && pa <= 202.5) player.position = new mp.Vector3(player.position.x, player.position.y - num, player.position.z); //s
        if(pa >= 202.5 && pa <= 247.5) player.position = new mp.Vector3(player.position.x, player.position.y - (num / 2), player.position.z); //se
        if(pa >= 247.5 && pa <= 292.5) player.position = new mp.Vector3(player.position.x + num, player.position.y, player.position.z); //e
        if(pa >= 292.5 && pa <= 332.5) player.position = new mp.Vector3(player.position.x + (num / 2), player.position.y + (num / 2), player.position.z); //ne
        if(pa >= 332.5 && pa < 360.0) player.position = new mp.Vector3(player.position.x, player.position.y + (num / 2), player.position.z); //n
    }

 


The output message of the action will be:

${player.name} went ${(num >= 0 ? "front" : "rear")} by ${(num >= 0 ? num : -num)}m


- Note: We need to mirror the amount if we are teleporting rear in x, y axis in message.

 

Edited by Mimr3w
  • Like 2

Share this post


Link to post
Share on other sites

Trigonometry simplifies this a ton.

 

            float distance = 2.0f; // Some distance to travel
            Vector3 pos = player.Position; // Current player pos

            pos.X += distance * (float)Math.Sin(-player.Heading);
            pos.Y += distance * (float)Math.Cos(-player.Heading);

            if (!player.IsInVehicle)
                player.Position = pos;
            else
                player.Vehicle.Position = pos;

 

That should work the same.

Edited by Static

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.