VIRUXE/sa-geom
Server-side GTA San Andreas collision: ground height, raycasts, surface normals and objects placed flush on slopes. MTA:SA module, SA-MP/open.mp plugin and CLI, with the whole map built in.
Dodaje jej pliki include, razem z bibliotekami, od których zależy, do kompilatora Pawn w twojej przeglądarce, gotowe do dołączenia w skrypcie.
README.md
sa-geom
GTA San Andreas world collision for servers and tools: ground height, raycasts, surface normals and objects placed flush on slopes, with no game client and no game files.
204 crates dropped on a grid over a Vinewood hillside (25° average slope). Each one is a single
sa-geom ground --model ID --yaw D query, and the pink lines show some of the rays.
One Rust core, three ways to use it:
| Target | What you get | |
|---|---|---|
| MTA:SA server module | ml_sageom.so / .dll (x64, x86) | sageomGround, sageomRaycast, sageomPlaceModel and others in server-side Lua |
| SA-MP / open.mp plugin | sageom.so / .dll (x86) | SAGeom_GetGround, SAGeom_RayCast, SAGeom_PlaceModel and others in Pawn |
| Command-line tool | sa-geom (Linux, Windows, macOS) | One-off and batch queries as JSON or CSV, e.g. snapping a whole list of spawn points offline |
The full vanilla San Andreas collision world (7,760 collision models, 1.6 million triangles and 38,996
placements) is built into every binary, compressed to about 20 MB. Drop the module or plugin in and it works.
You don't need a .hmap, a .cadb or a copy of the game.
-- MTA, server side: a crate on the slope in front of the player, sitting on its base, facing where they face
local x, y, z, rx, ry, rz = sageomPlaceModel(1271, tx, ty, pz + 2, heading)
createObject(1271, x, y, z, rx, ry, rz)
// SA-MP / open.mp: the same
new Float:z, Float:rx, Float:ry, Float:rz;
if (SAGeom_PlaceModel(1271, x, y, pz + 2.0, heading, z, rx, ry, rz))
CreateObject(1271, x, y, z, rx, ry, rz);
$ sa-geom ground -2300 -1700 --model 1271 --yaw 45
{ "hit": true, "z": 478.6807, "nx": 0.0042, "ny": -0.3217, "nz": 0.9468,
"material": 123, "material_name": "P_DIRTROCKY", "model": 18319, "model_name": "cs_landbit_30",
"pos": [-2299.9985, -1700.1125, 479.0121], "rot": [13.6764, 347.0258, 45.0], "water_z": null, ... }
Why
- MTA has no server-side world geometry.
getGroundPositionandprocessLineOfSightonly exist on the client, so anything the server places has to wait for a player to come near and measure it. With sa-geom the server can place items, spawn points and props on the real ground itself. - MapAndreas (SA-MP) is a 2D heightmap. It only gives the topmost height per grid cell, so it can't tell you about bridges, tunnels, floors or interiors, and it doesn't return a surface normal.
- ColAndreas (SA-MP/open.mp) does real collision with Bullet. It needs a generated
.cadbdatabase and doesn't exist for MTA.
sa-geom does real triangle raycasts (bridges, tunnels, several floors, interiors), returns the normal, the surface material and the model hit, and turns all of that into the exact object rotation each server expects.
The Gant Bridge cut open at x = −2680. Three rays at the same spot, started at different heights, find three different surfaces. A heightmap only ever knows the top one.
Install
Download a release from Releases, or build from source.
MTA:SA
- Copy the module into the server's modules folder:
x64/modules/ml_sageom.soon Linux x64,mods/deathmatch/modules/ml_sageom.soon 32-bit Linux, orx64\modules\ml_sageom.dll/mods\deathmatch\modules\ml_sageom.dllon Windows. - Add it to
mods/deathmatch/mtaserver.conf:<module src="ml_sageom.so"/> <!-- ml_sageom.dll on Windows --> - Restart the server. The log shows
MODULE: Loaded "sa-geom"and, about a second later,[sa-geom] world loaded in ... ms.
The functions are global in every resource. See examples/mta for a resource that drops
crates on slopes.
SA-MP / open.mp
- Copy
sageom.so(Linux) orsageom.dll(Windows) intoplugins/. - Load it. SA-MP: add
plugins sageom.sotoserver.cfg. open.mp: add"legacy_plugins": ["sageom"]under"pawn"inconfig.json. - Copy
include/sageom.incinto your includes and#include <sageom>.
Both servers are 32-bit, and so is the plugin. See examples/samp for a filterscript.
CLI
Put the sa-geom binary anywhere and run sa-geom --help.
API
Coordinates are world metres. interior 0 is the outside world, and objects in area 13 are visible from every
interior. Pass -1 to see everything. zFrom is where the downward ray starts. It defaults to 1000, above
everything. Start it just above a point to find the floor under it instead of the roof above it. The world loads
in the background in about a second; until then every function returns false.
Placement rotations are the GTA object Euler angles that createObject/setElementRotation (MTA) and
CreateObject/SetObjectRot (SA-MP, open.mp) take. They were checked against MTA's getElementMatrix and
open.mp's quaternions. The object's up axis follows the surface normal, and its heading seen from above stays the
yaw you asked for (GTA convention: 0 faces north (+Y), 90 faces west).
Lua (MTA, server side)
| Function | Returns |
|---|---|
sageomIsReady() | true once the world is loaded |
sageomGround(x, y [, zFrom, interior, depth]) | z, nx, ny, nz, material, model or false |
sageomRaycast(x1, y1, z1, x2, y2, z2 [, interior]) | true, x, y, z, nx, ny, nz, material, model or false |
sageomPlace(x, y, zFrom, yaw [, lift, interior, width, depth]) | x, y, z, rx, ry, rz, nx, ny, nz or false |
sageomPlaceModel(model, x, y, zFrom, yaw [, lift, interior, restX, restY, restZ]) | x, y, z, rx, ry, rz, nx, ny, nz or false |
sageomGetModelBounds(model) | minX, minY, minZ, maxX, maxY, maxZ or false |
sageomGroundMany({ {x, y [, zFrom]}, ... } [, interior]) | { {z, nx, ny, nz} or false, ... } |
sageomWaterLevel(x, y) | z or false |
sageomAddOverlay(path) | true, summary or false, error (path relative to the server root) |
sageomReload([path]) | true or false, reason: rebuilds the world plus that overlay in the background and swaps it in, dropping every overlay applied before (for a changed overlay, without a server restart) |
sageomReloadStatus() | generation, busy, ok, message: generation counts swapped-in worlds; ok/message are the last finished reload's (false, false before any) |
Live edits apply at once (the next query sees them), survive sageomReload, and belong to the resource that made
them: when it stops, the module drops them. Most servers never call these themselves: the sageom
resource does it for every object on the server.
| Function | Returns |
|---|---|
sageomAddObject(model, x, y, z [, rx, ry, rz, interior, sx, sy, sz]) | a handle, or false when the model has no collision. Rotation in degrees as an MTA object's (.map, setElementRotation); scale as setObjectScale |
sageomAddObjects({ {model, x, y, z [, rx, ry, rz, interior, sx, sy, sz]}, ... }) | { handle or false, ... }, in one batch (24k rows in about 55 ms) |
sageomMoveObject(handle, x, y, z [, rx, ry, rz]) | true or false |
sageomSetObjectScale(handle, sx [, sy, sz]) | true or false |
sageomSetObjectModel(handle, model) | true, or false when the model has no collision: the handle stays valid and the object collides again once a collision for that model is loaded |
sageomRemoveObject(handle) | true or false |
sageomRemoveWorldModel(model, radius, x, y, z [, interior]) | true, count: hides the game's own instances of model around the point, as removeWorldModel (objects added through the module stay, as createObject objects do) |
sageomRestoreWorldModel(model, radius, x, y, z [, interior]) | true, count: undoes the removals of model centred within radius of the point |
sageomReplaceCOL(colData, id) | true or false, error: the first model of the .col file in colData (a Lua string of its bytes) is model id's collision for every instance, now and later. Any id: a game model's, or a new one (18631+, your own). An empty collision model leaves the id without collision |
sageomRestoreCOL(id) | true or false: back to the game's collision (none, for an id the game does not have) |
sageomGetStats() | { objects, objectsWithoutCollision, removals, removedInstances, cols, pending, liveTree, instances, generation, nextHandle, reloading } |
Pawn (SA-MP, open.mp)
native bool:SAGeom_IsReady();
native bool:SAGeom_GetGround(Float:x, Float:y, &Float:z, Float:zFrom = 1000.0, interior = 0,
&Float:nx = 0.0, &Float:ny = 0.0, &Float:nz = 0.0, &material = 0, &model = 0);
native bool:SAGeom_RayCast(Float:x1, Float:y1, Float:z1, Float:x2, Float:y2, Float:z2,
&Float:hx, &Float:hy, &Float:hz, interior = 0, &Float:nx = 0.0, &Float:ny = 0.0, &Float:nz = 0.0,
&model = 0, &material = 0);
native bool:SAGeom_Place(Float:x, Float:y, Float:zFrom, Float:yaw, &Float:z, &Float:rx, &Float:ry, &Float:rz,
Float:lift = 0.0, interior = 0, Float:width = 0.0, Float:depth = 0.0);
native bool:SAGeom_PlaceModel(modelid, Float:x, Float:y, Float:zFrom, Float:yaw, &Float:z, &Float:rx, &Float:ry,
&Float:rz, Float:lift = 0.0, interior = 0, Float:restX = 0.0, Float:restY = 0.0, Float:restZ = 0.0);
native bool:SAGeom_GetModelBounds(modelid, &Float:minX, &Float:minY, &Float:minZ, &Float:maxX, &Float:maxY, &Float:maxZ);
native bool:SAGeom_WaterLevel(Float:x, Float:y, &Float:z);
native bool:SAGeom_AddOverlay(const path[]); // relative to scriptfiles/
SAGeom_GetGround(x, y, z) replaces MapAndreas_FindZ_For2DCoord(x, y, z) directly.
Placing things well
The same 3 × 7 m container (model 2934) on a 30° slope. Left: its origin at the ground height and no tilt,
as most scripts do it. Right: PlaceModel.
*PlaceModelis usually what you want. It reads the model's shape, samples the ground under the corners of its base, tilts it to the plane through them, and lifts its origin by however far the origin sits above the model's lowest point. A long crate across a ridge or a dip rests on it instead of sinking in or floating, and models whose origin is mid-height don't end up half buried.- Shapes come from the render mesh. Every model's
.dffis baked into a small hull (26 extreme points), so models without collision (weapons, cans, ammo boxes, most pickups) and vehicles (whose collision is only the body: the wheels come from the mesh) land right too. Damaged parts, low-detail parts and muzzle flashes are left out. Models without a mesh fall back to their collision bounds. - Rest rotations. Pass
restX, restY, restZ(CLI--rest RX,RY,RZ) for a model that lies in a pose of its own before the heading, e.g.90, 0, 0for a rifle on its side. The lift is measured in that pose and the returned rotation includes it, socreateObject(model, x, y, z, rx, ry, rz)is all it takes. *Placelays something on the ground under a single point. Give itwidth/depthfor a footprint.*Groundgives you the raw height and normal to use however you like.ground --modelalso says whether the model fits:blockednames the first object reaching more than--margin(0.15 m) into its box in that pose (a wall, fence, pole, low roof; the bottom fifth is ignored so the ground, kerbs and wheels don't count; ground-like surfaces below the body's middle and faces lower than a step never count), andgapis how far it is propped above the ground under its origin. A helicopter's main rotor is kept apart from its body: the body box is the fuselage, and the spinning disc is tested as a thin slab at blade height ("part": "rotor"), so a lamp post under the blades doesn't count but one reaching into them does.sa-geom model ID... [--rest RX,RY,RZ]tells you what sa-geom knows about a model: its kind (object, weapon, vehicle), where its shape comes from, its size and how high its origin sits above its lowest point.
CLI
sa-geom ground X Y [--from Z] [--yaw D] [--lift M] [--model ID [--rest RX,RY,RZ] | --footprint W,D] [--interior N]
sa-geom model ID... [--rest RX,RY,RZ] # kind, shape, size, origin height
sa-geom ray X1 Y1 Z1 X2 Y2 Z2
sa-geom batch [--format jsonl|csv] [--above M] < points
sa-geom clearance [--above M] < points # which headings boxes fit in, per point
sa-geom info [--at X Y Z --radius R]
sa-geom export --min X Y Z --max X Y Z [--origin X Y Z] -o region.obj # collision as OBJ, grouped by surface
sa-geom placements --gta DIR --min X Y Z --max X Y Z [--margin M] # every map placement in a box, as JSON
sa-geom bake --gta DIR -o world.sagw.zst
Every command accepts --overlay FILE (repeatable), --world FILE (use another baked world) and --gta DIR
(build from a game install in memory, e.g. a modded one).
batch reads one query per line: JSON ({"x":..,"y":..,"z":..,"yaw":..,"model":..,"rest":[rx,ry,rz],"w":..,"d":..,"interior":..},
where z is where the point is stored and the ray starts --above metres higher) or CSV (x,y[,z[,yaw[,lift]]]).
It writes one result per line. About 2,000 queries take 0.2 s after a 1 s load. In the output, z and the normal are the
surface that was hit, and pos/rot (CSV: x, y, z, rx, ry, rz) are where to create the object and how to turn it.
$ printf '2495,-1687,13\n-2300,-1700\n' | sa-geom batch --format csv
x,y,z,nx,ny,nz,rx,ry,rz,material,model
2495.0,-1687.0,12.5204,-0.0032,0.0016,1.0,359.9105,359.8165,0.0,4,17613
-2300.0,-1700.0,478.6807,0.0041,-0.3217,0.9468,18.7641,0.2337,0.0,123,18319
Fixing a spawn list
Most servers have hand-recorded spawn lists: loot points, vehicle spawns, pickups. They were stored at the height of whoever recorded them, flat, for whatever model happened to be used. Each point then spawns many different models, and each needs its own height and tilt: a lying rifle, an ammo box and a pickup truck on the same slope don't share a z.
examples/spawn-points fixes such a list offline in one pass. It takes the points and the
models each can spawn, and writes a placement for every point × model (× heading, for spawn systems that flip
cars), plus a report of the points that need a human: no ground under them, a wall or pole in the way, standing on
something the world doesn't have, under water, or too steep.
$ python3 examples/spawn-points/fix_spawns.py vehicles.csv --models 400,422,461,468 --flip \
--overlay mymaps.toml -o placements.jsonl --report check.csv
2017 points, 63669 placements, 145 points flagged
sa-geom clearance answers the question for anything placed at random later: for a point and a few box sizes
(say small, medium and long items) it tests 16 headings against the collision around and returns, per box, a bitmask
of the headings that are clear. Store the masks with the point and the spawner picks a random heading among the
clear ones, so a rifle never ends up through a wall:
$ echo '{"x":2099.46,"y":-1052.49,"z":27.36,"boxes":[[0.5,0.5,0.4],[1.0,0.5,0.5],[1.8,0.7,0.7]],"lift":0.08}' | sa-geom clearance
{"hit":true,"z":27.3692,"nx":0.1311, ... ,"masks":[65535,1542,1028], ...}
Loot scattered around a point (item i of n at angle i·360/n, a few tens of centimetres out) has a small,
fixed set of slots. --scatter R --max N measures each one, so items that roll off a table edge or down a step
can be pulled back in.
Leave the module for what only happens in game: a player dropping an item, a crate landing, a car being put back on a road.
Your own maps and models
The built-in world is vanilla San Andreas: every placement from the IPLs in gta.dat and their binary
streams. The script-only IPL groups (the story-mode bridge barriers, crack, carter, truthsfarm) are left
out, because multiplayer servers never load them. Anything you add goes in as an overlay:
- an MTA
.map(<object>,<removeWorldObject>) - a Pawn file (
CreateObject,CreateDynamicObject,RemoveBuildingForPlayer) - an
.ipl, a.col, an.ide, a.dffor an.img(its.cols add collision, its.dffs add shapes) - a TOML manifest that combines any of these, with wildcards
sageomAddOverlay("mods/deathmatch/resources/mymap/mymap.map")
SAGeom_AddOverlay("maps/mymap.pwn");
$ sa-geom ground 2495 -1687 --overlay mymaps.toml
Objects only collide if the world has collision for their model. Models you place by shape need a .dff
the world knows (in an .img, or loose .dff files listed in a manifest's dff entries). Vanilla models do. For SA-MP's added objects
(ids 18631 and up), add the SA-MP client's SAMPCOL.img and SAMP.ide. With those, 781 SA-MP models gain
collision. Do the same for custom models with their .col files.
Keeping the world in sync with your server (MTA)
The module knows the game's map and your overlays. The objects your server creates while it runs (map resources,
scripted doors and gates, buildings) are kept in step by the sageom resource in mta/sageom:
- Copy
mta/sageominto your server'sresourcesfolder. - Give it two rights, in
acl.xml(or approve itsaclrequestwithaclrequest allow sageom allin the console):function.addDebugHook: it watchescreateObject,setElementPositionand the other functions that change objects in every resource;general.ModifyOtherObjects:replaceCOL(":otherresource/file.col", id)reads another resource's file withfileOpen, which MTA only allows with this right, even to read. Without the first it still sends what exists when it starts and what each resource brings when it starts, and drops destroyed elements, but misses later changes (it says so in the log).
- Start it before your other resources (
<resource src="sageom" startup="1" />early inmtaserver.conf), so it sees theirremoveWorldModelcalls.
From then on every object and building in dimension 0 collides on the server where it is: objects from .map files,
createObject, createBuilding, cloneElement and loadMapData, moved by setElementPosition/Rotation,
moveObject (where it stops), attachments to other objects, setObjectScale, setElementModel,
setElementInterior, setElementDimension (only dimension 0 counts) and setElementCollisionsEnabled. Objects
attached to vehicles or peds have no server collision while attached. removeWorldModel / restoreWorldModel calls
and maps' <removeWorldObject> are mirrored. A sageomReload keeps all of it.
What only clients do, the server cannot see; tell the resource:
blendercolandreascollisiongame-servergta-sagta-san-andreasheightmapluamapandreasmta-saopen-mppawnraycastrustsampsan-andreas
Ta strona jest zbudowana z tego, co GitHub publikuje o repozytorium. samp.directory nie hostuje kodu. Zgłoś problem z tym wpisem