Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update math snippet #320

Merged
merged 33 commits into from
Aug 20, 2024
Merged
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
ea1ea2c
Add LocalVehicle to LocalPlayer vehicle property (#287)
xxshady Nov 9, 2023
465ba21
feat(natives): Add new natives
xLuxy Dec 19, 2023
82e2431
Merge remote-tracking branch 'origin/new-natives'
xLuxy Dec 19, 2023
d475a9b
Merge branch 'dev'
xLuxy Dec 19, 2023
7860e16
Merge branch 'dev'
xLuxy Dec 20, 2023
841c8c1
Merge branch 'dev'
xLuxy Dec 20, 2023
e9646ef
Merge branch 'dev'
xLuxy Dec 20, 2023
f18883b
Merge branch 'dev'
xLuxy Dec 20, 2023
e5c0388
Merge branch 'dev'
xLuxy Dec 25, 2023
3dd93b7
Merge branch 'dev'
xLuxy Dec 28, 2023
63e77af
fix(client): move steeringAngle property to Vehicle class (#294)
xxshady Dec 29, 2023
6a30e27
feat(server): Add Vehicle.setBadge
xLuxy Dec 29, 2023
7472a35
Merge branch 'dev'
xLuxy Jan 2, 2024
3dd5be7
Merge branch 'dev'
xLuxy Jan 3, 2024
e5df04e
Merge branch 'dev'
xLuxy Jan 3, 2024
a7a77fa
Merge branch 'dev'
zziger Jan 7, 2024
65a0afa
Merge branch 'dev'
zziger Jan 12, 2024
a89a2d6
Merge branch 'dev'
xLuxy Jan 28, 2024
f1cfb52
Merge branch 'master' of https://github.com/altmp/altv-types
xLuxy Jan 28, 2024
d0748ff
Merge branch 'dev'
xLuxy Jan 28, 2024
a7b44a7
Merge branch 'dev'
xLuxy Feb 28, 2024
e7cf0b6
chore: add package.json to first resource article (#302)
xxshady Mar 14, 2024
c0b9904
chore(server): remove outdated inspector property from IServerConfig …
xxshady Mar 14, 2024
28c74ba
Merge branch 'dev'
xLuxy Apr 3, 2024
beeedc2
chore: update altv-pkg link (#311)
xxshady Apr 3, 2024
4f8118b
chore: improve getClosestEntities description (#310)
xxshady Apr 3, 2024
c31ed89
Merge branch 'dev'
xLuxy Apr 3, 2024
b873f89
Merge branch 'dev'
xLuxy May 28, 2024
bc5cd4d
chore: Bump version
xLuxy May 28, 2024
58eb1f3
Merge branch 'dev'
xLuxy May 29, 2024
20c356b
Merge branch 'dev'
xLuxy Jun 13, 2024
cbdb0c6
Merge branch 'dev'
xLuxy Jul 1, 2024
d68bf3c
chore: update math snippet
xxshady Jul 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 13 additions & 81 deletions docs/articles/snippets/math.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,12 @@
# Math & Distance Utility Functions

alt:V by default does not come with default functions for getting distance.

Use these functions on server-side or client-side.

```js
/**
* Get all players in a certain range of a position.
* @param {} pos
* @param {} range
* @param {} dimension=0
* @returns {Array<alt.Player>}
*/
export function getPlayersInRange(pos, range, dimension = 0) {
if (pos === undefined || range === undefined) {
throw new Error('GetPlayersInRange => pos or range is undefined');
}

return alt.Player.all.filter(player => {
return player.dimension === dimension && distance2d(pos, player.pos) <= range;
});
}

/**
* Get the forward vector of a player.
* Get the forward vector from rotation.
* @param {} rot
* @returns {{x,y,z}}
*/
export function getForwardVectorServer(rot) {
export function getForwardVector(rot) {
const z = -rot.z;
const x = rot.x;
const num = Math.abs(Math.cos(x));
Expand All @@ -39,39 +18,7 @@ export function getForwardVectorServer(rot) {
}

/**
* Get the distance from one vector to another.
* Does take Z-Axis into consideration.
* @param {} vector1
* @param {} vector2
* @returns {number}
*/
export function distance(vector1, vector2) {
if (vector1 === undefined || vector2 === undefined) {
throw new Error('AddVector => vector1 or vector2 is undefined');
}

return Math.sqrt(
Math.pow(vector1.x - vector2.x, 2) + Math.pow(vector1.y - vector2.y, 2) + Math.pow(vector1.z - vector2.z, 2)
);
}

/**
* Get the distance from one vector to another.
* Does not take Z-Axis into consideration.
* @param {} vector1
* @param {} vector2
* @returns {{x,y,z}}
*/
export function distance2d(vector1, vector2) {
if (vector1 === undefined || vector2 === undefined) {
throw new Error('AddVector => vector1 or vector2 is undefined');
}

return Math.sqrt(Math.pow(vector1.x - vector2.x, 2) + Math.pow(vector1.y - vector2.y, 2));
}

/**
* Check if a position is between two vectors.
* Check if a position is between two vectors (2D).
* @param {} pos
* @param {} vector1
* @param {} vector2
Expand Down Expand Up @@ -109,38 +56,23 @@ export function getClosestVectorFromGroup(pos, arrayOfPositions) {
return distance(pos, a.pos) - distance(pos, b.pos);
}

return distance(pos, a.pos) - distance(pos, b.pos);
return distance(pos, a) - distance(pos, b);
});

return arrayOfPositions[0];
}

/**
* Get the closest player to a player.
* @param {} player
* @returns {Array<alt.Player>}
*/
export function getClosestPlayer(player) {
return getClosestVectorFromGroup(player.pos, [...alt.Player.all]);
}

/**
* Get the closest vehicle to a player.
* @param {alt.Vector3} player
* @returns {Array<alt.Vehicle>}
*/
export function getClosestVehicle(player) {
return getClosestVectorFromGroup(player.pos, [...alt.Vehicle.all]);
}
```

## Example Usage

```js
const dist1 = { x: 5, y: 2, z: 0 };
const dist2 = { x: 1, y: 1, z: 0 };
import alt from "alt-server";

const pos1 = new alt.Vector3(5, 2, 0);
const pos2 = new alt.Vector3(1, 1, 0);

const dist = distance2d(dist1, dist2);
const closestVehicle = getClosestVehicle(player);
const closestPlayer = getClosestPlayer(player);
```
const dist = pos1.distanceTo(pos2);

// Closest pos to 0, 0, 0
const closest = getClosestVectorFromGroup(alt.Vector3.zero, [pos1, pos2]);
```
Loading