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

feat(extra-natives-five): add Collision Detection system [CLIENT] #3117

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1,071 changes: 1,071 additions & 0 deletions code/components/extra-natives-five/src/ColshapeNatives.cpp

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions ext/native-decls/CreateColshapeCircle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
ns: CFX
apiset: client
game: gta5
---
## CREATE_COLSHAPE_CIRCLE

```c
int CREATE_COLSHAPE_CIRCLE(float x, float y, float z, float radius);
```

Creates a circular shape. The game will check for collision and fire either onPlayerEnterColshape or onPlayerLeaveColshape with the unique identifier.

## Parameters
* **x**: The X coordinate of the center of the circle.
* **y**: The Y coordinate of the center of the circle.
* **z**: The Z coordinate of the center of the circle.
* **radius**: The radius of the circle.

## Return value
The unique identifier for the collision shape if successfully created, -1 if not.
23 changes: 23 additions & 0 deletions ext/native-decls/CreateColshapeCube.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
ns: CFX
apiset: client
game: gta5
---
## CREATE_COLSHAPE_CUBE

```c
int CREATE_COLSHAPE_CUBE(float x1, float y1, float z1, float x2, float y2, float z2);
```

Creates a cube shape. The game will check for collision and fire either onPlayerEnterColshape or onPlayerLeaveColshape with the unique identifier.

## Parameters
* **x1**: The X coordinate of the first corner of the cube.
* **y1**: The Y coordinate of the first corner of the cube.
* **z1**: The Z coordinate of the first corner of the cube.
* **x2**: The X coordinate of the opposite corner of the cube.
* **y2**: The Y coordinate of the opposite corner of the cube.
* **z2**: The Z coordinate of the opposite corner of the cube.

## Return value
The unique identifier for the collision shape if successfully created, -1 if not.
18 changes: 18 additions & 0 deletions ext/native-decls/CreateColshapeCylinder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
ns: CFX
apiset: client
game: gta5
---
## CREATE_COLSHAPE_CYLINDER
```c
int CREATE_COLSHAPE_CYLINDER(float x, float y, float z, float radius, float height);
```
Creates a cylinder shape. The game will check for collision and fire either onPlayerEnterColshape or onPlayerLeaveColshape with the unique identifier.
## Parameters
* **x**: The X coordinate of the center of the cylinder.
* **y**: The Y coordinate of the center of the cylinder.
* **z**: The Z coordinate of the bottom of the cylinder.
* **radius**: The radius of the cylinder.
* **height**: The height of the cylinder.
## Return value
The unique identifier for the collision shape if successfully created, -1 if not.
67 changes: 67 additions & 0 deletions ext/native-decls/CreateColshapePolyzone.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
---
ns: CFX
apiset: client
game: gta5
---
## CREATE_COLSHAPE_POLYZONE

```c
int CREATE_COLSHAPE_POLYZONE(char* polyZoneData, int dataLength, float minZ, float maxZ);
```

Creates a polyzone collision shape defined by a set of vertices along with vertical boundaries.
The `polyZoneData` parameter must contain a msgpack‑encoded array of vertices to define the polygon. Accepted vertex formats include vector2, vector3, and vector4 (only the x and y components are used). Normal tables with keys (e.g. `{ x = 2.0, y = 2.0 }`) and arrays (e.g. `{ [1] = 1.0, [2] = 2.0 }`) are also supported.

If parameters 4 (`minZ`) and 5 (`maxZ`) are empty, the system assumes very large numbers (effectively infinite), so basically we ignore Z.

When a player enters or leaves the collision shape, the game fires the `onPlayerEnterColshape` or `onPlayerLeaveColshape` event with the provided `colShapeId`.

### Parameters
* **polyZoneData**: A pointer to a msgpack‑encoded buffer containing an array of vertices (vector2, vector3, or vector4) that define the polygon.
* **dataLength**: The length of the msgpack‑encoded buffer.
* **minZ**: The explicit minimum Z coordinate for the collision shape. If omitted, an extremely low value is assumed (effectively negative infinity).
* **maxZ**: The explicit maximum Z coordinate for the collision shape. If omitted, an extremely high value is assumed (effectively positive infinity).

### Return Value
The unique identifier for the collision shape if successfully created, -1 if not.

### Examples
In this example, a 2D polygon is defined by four vertices and explicit vertical boundaries are provided:

```lua
-- Define a simple rectangular polyzone using 2D vertices
local vertices = {
{ x = 100.0, y = 100.0 },
{ x = 200.0, y = 100.0 },
{ x = 200.0, y = 200.0 },
{ x = 100.0, y = 200.0 }
}
local packedVertices = msgpack.pack(vertices)
-- Create the polyzone with explicit minZ and maxZ (e.g. 50 and 150)
local colShapeId = CreateColshapePolyzone(packedVertices, packedVertices:len(), 50.0, 150.0)
if colShapeId ~= -1 then
print("Polyzone created successfully! ID: " .. colShapeId)
else
print("Failed to create polyzone.")
end
```

In this second example, vertices include a Z coordinate. Passing empty vertical boundaries (e.g., nil) will assume infinite values:

```lua
-- Define a polyzone with 3D vertices (only x and y are used)
local vertices3D = {
{ x = 100.0, y = 100.0, z = 50.0 }, -- or vector3(100, 100, 50)
{ x = 200.0, y = 100.0, z = 55.0 },
{ x = 200.0, y = 200.0, z = 60.0 },
{ x = 100.0, y = 200.0, z = 50.0 }
}
local packedVertices3D = msgpack.pack(vertices3D)
-- Passing nil for minZ and maxZ assumes infinite vertical boundaries.
local colShapeId = CreateColshapePolyzone(packedVertices3D, packedVertices3D:len(), nil, nil)
if colShapeId ~= -1 then
print("Polyzone created successfully with infinite vertical boundaries! ID: " .. colShapeId)
else
print("Failed to create polyzone.")
end
```
23 changes: 23 additions & 0 deletions ext/native-decls/CreateColshapeRectangle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
ns: CFX
apiset: client
game: gta5
---
## CREATE_COLSHAPE_RECTANGLE

```c
int CREATE_COLSHAPE_RECTANGLE(float x1, float y1, float x2, float y2, float bottomZ, float height);
```

Creates a rectangle shape. The game will check for collision and fire either onPlayerEnterColshape or onPlayerLeaveColshape with the unique identifier.

## Parameters
* **x1**: The X coordinate of the first corner.
* **y1**: The Y coordinate of the first corner.
* **x2**: The X coordinate of the opposite corner.
* **y2**: The Y coordinate of the opposite corner.
* **bottomZ**: The bottom Z coordinate of the rectangle.
* **height**: The height of the rectangle.

## Return value
The unique identifier for the collision shape if successfully created, -1 if not.
20 changes: 20 additions & 0 deletions ext/native-decls/CreateColshapeSphere.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
ns: CFX
apiset: client
game: gta5
---
## CREATE_COLSHAPE_SPHERE

```c
int CREATE_COLSHAPE_SPHERE(float x, float y, float z, float radius);
```

Creates a sphere shape. The game will check for collision and fire either onPlayerEnterColshape or onPlayerLeaveColshape with the unique identifier.
## Parameters
* **x**: The X coordinate of the center of the sphere.
* **y**: The Y coordinate of the center of the sphere.
* **z**: The Z coordinate of the center of the sphere.
* **radius**: The radius of the sphere.

## Return value
The unique identifier for the collision shape if successfully created, -1 if not.
18 changes: 18 additions & 0 deletions ext/native-decls/DeleteColshape.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
ns: CFX
apiset: client
game: gta5
---
## DELETE_COLSHAPE

```c
bool DELETE_COLSHAPE(int colShapeId);
```

Deletes a colshape by its unique identifier

## Parameters
* **colShapeId**: The Identifier of the colshape to be deleted.

## Return value
true if deleted successfully, false if it didn't exist in the first place
18 changes: 18 additions & 0 deletions ext/native-decls/DoesColshapeExist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
ns: CFX
apiset: client
game: gta5
---
## DOES_COLSHAPE_EXIST

```c
bool DOES_COLSHAPE_EXIST(int colShapeId);
```

Checks whether a collision shape exists by its identifier.

## Parameters
* **colShapeId**: The Identifier of the colshape.

## Return value
true if it exists, false if not.
14 changes: 14 additions & 0 deletions ext/native-decls/GetIsColshapeSystemEnabled.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
ns: CFX
apiset: client
game: gta5
---
## GET_IS_COLSHAPE_SYSTEM_ENABLED

```c
bool GET_IS_COLSHAPE_SYSTEM_ENABLED();
```
Checks if the collision management system is active or not.

## Return value
true if enabled, false if not.
15 changes: 15 additions & 0 deletions ext/native-decls/SetColshapeSystemEnabled.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
ns: CFX
apiset: client
game: gta5
---
## SET_COLSHAPE_SYSTEM_ENABLED

```c
void SET_COLSHAPE_SYSTEM_ENABLED(bool activated);
```

Activates the grid-based collision detection system. If enabled, the game will fire `onPlayerEnterColshape` and `onPlayerLeaveColshape` if you've created collision shapes and `PlayerPedId()` collides with those shapes.

## Parameters
* **activated**: Whether the System should be active (true) or not (false)
15 changes: 15 additions & 0 deletions ext/native-decls/SetColshapeSystemUpdateInterval.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
ns: CFX
apiset: client
game: gta5
---
## SET_COLSHAPE_SYSTEM_UPDATE_INTERVAL

```c
void SET_COLSHAPE_SYSTEM_UPDATE_INTERVAL(int updateInterval);
```

Sets the internal collision detection systems update interval. The standard time for this is 100ms. For faster collision detection you can decrease this value.

## Parameters
* **updateInterval**: the update time in milliseconds, minimum is 0ms (every tick)
Loading