Skip to content

185 mod tool 7 the vehicle assembler

Alex Diakovsky edited this page Nov 23, 2026 · 2 revisions

185. Mod tool 7: the vehicle assembler (stage S7.5)

Question: a vehicle made from blank in S7.4 is one box. What does the mod tool need so the user can build a vehicle's whole look from their own sprites (a body, a turret that turns, wheels that roll), see it as the game will, and place its collision shape and its muzzle, all without the Godot editor (issues #5 and #6; tracking issue #143)?

The mod tool series: stage S7 builds the mod tool into a toolset that makes a whole game by hand. It began as the last stage of the deterministic simulation plan (whose stages S0 to S6 are documents 156 to 178), because netplay is tested on a game made with the tool, but the work is the mod tool's. S7 began with the audit (document 179) and the plan for the rest of S7 (document 180). Document 181 is S7.1, document 182 is S7.2, document 183 is S7.3 and document 184 is S7.4. This document is S7.5, the vehicle assembler.

A vehicle's look is the render descriptor the port already draws Return Fire's vehicles with (documents 48 and 59); the assembler writes that same descriptor, so this stage adds no new drawing to the game, only a way to make the data. The mod tool is the port's own: there is no decompiled code to quote, only the port's.

Glossary

Term Meaning
face One entry of a vehicle's render.parts: a sprite stretched over four corners [x, y, z]. The draw descriptor's part, as the original has it.
corner axes x across (right is +), y along (minus forward: the front is -y), z up, in world units. A tile is 32.
body All of a vehicle's faces, groups and bindings: its render block.
group render.groups.<name>: a node the faces in it turn with, by a channel (a turret by turret_deg). Groups can sit under other groups.
channel A number the vehicle reports (Vehicle.channel(name)): turret_deg, gun_elev_deg, position_x, rotor_speed_steps ...
binding A face's sprites_by: its sprite picked from a list by a channel (a wheel strip by position_x).
marker A point a module reads from its parameters, shown on the body: the cannon's gun pivot and muzzle; the collision polygon's points.
draw order The order of the faces: a later face in the same plane is drawn over an earlier one.

The answer, up front

  1. A Body tab (after Vehicles) draws the selected vehicle with the game's own renderer, VehicleRender3D, in a 3D view that orbits, zooms and switches to the game's camera (G), over a grid, in any team colour. What it shows is what the game draws.
  2. Faces: add a face (lying flat, facing forward, facing sideways), a box (six faces) or a wedge (five). Click a face to select it (shift adds); drag the red, green or blue arrow to move it across, along or up; click one of its corners and move that corner alone; or type the corners. Everything snaps to a quarter-unit grid; arrow keys and Page Up / Page Down nudge by it. Turn and scale about the selection's middle.
  3. Sprites: paint a face with a sprite, or make a new sprite sized to the face (one pixel a unit) and draw it in the Assets tab; a face with no sprite shows a checker; a double-click on a face opens its sprite in the Assets tab (#6).
  4. Mirror (M), Duplicate, Repeat (n copies a step apart), Delete, Earlier / Later (draw order).
  5. Moving parts: a group that turns by a channel about the middle of the faces put in it (a turret by turret_deg, a gun by gun_elev_deg, a rotor by rotor_speed_steps); a face's sprite bound to a channel (a wheel strip by position_x); a slider per channel to try them.
  6. The collision shape from above (Fit it to the faces, then drag its points) and the cannon's markers (gun pivot and muzzle) are dragged with the same arrows; a marker is written back as the module's own parameters.
  7. Every edit is one undo step of ModWorkspace on the vehicle's render block, written by PackWriter into the mod's vehicle.json; Validate checks each body.

How a body is built and drawn: the Body tab's clicks, arrows, buttons and sliders call ModWorkspace, one undo step each, which replaces the vehicle's render block in the live pack and, on Save, in the mod's vehicle.json; VehicleRender3D draws that block in the Body tab's view, exactly as it does in the game, where the vehicle's channels (turret angle, x position) turn its groups and pick its bound frames

Mermaid source
flowchart TB
  UI["Body tab<br/>click a face, drag an arrow,<br/>Add box, Mirror, Make group, Bind,<br/>Fit shape, drag a marker"] --> WS["ModWorkspace<br/>add_box, move_faces, mirror_faces,<br/>add_group, bind_face_sprites, fit_shape, set_marker<br/>one undo step each"]
  WS --> R["the vehicle's render block<br/>parts: sprite over 4 corners<br/>groups: turn by a channel, about a pivot<br/>sprites_by: frame by a channel"]
  R --> F["vehicle.json in the mod (Save)"]
  R --> V1["VehicleRender3D in the Body tab<br/>sliders set the channels"]
  R --> V2["VehicleRender3D in the game<br/>the vehicle sets the channels"]
  WS --> S["shape.poly, shape.z<br/>weapon slot params (muzzle)"]
Loading

Step 1: the body in the workspace

A body edit reads the vehicle's render block, changes a copy, and puts the whole block back through set_vehicle_value, so it is one undo step, a vehicle from a lower layer is copied into the mod first, and the writer and Revert of S7.4 work unchanged. A box is six faces, each corner on the grid, each face team-coloured (flag 8):

# engine editor/workspace.gd
## The six faces of a box `size` (x across, y along, z up) whose bottom centre is `at`: top, front, back, right, left, bottom.
static func box_corners(at: Vector3, size: Vector3) -> Array:
	...
	return [
		[Vector3(x0, y0, hi), Vector3(x1, y0, hi), Vector3(x1, y1, hi), Vector3(x0, y1, hi)],   # top
		[Vector3(x0, y0, hi), Vector3(x1, y0, hi), Vector3(x1, y0, lo), Vector3(x0, y0, lo)],   # front
		...
	]

## Adds faces (each a list of 4 corners) drawn with `sprite`, in `group`, after the others; returns their indices. One undo step.
func add_faces(id: String, corner_sets: Array, sprite := "", group := "", action := "Add a face") -> Array[int]:
	var r := body(id)
	var parts: Array = r.get("parts", [])
	var out: Array[int] = []
	for cs in corner_sets:
		out.append(parts.size())
		parts.append(make_face(cs, sprite, group))
	r["parts"] = parts
	if not set_body(id, r, action):
		out.clear()
	return out

Moving, turning and scaling are one function, a transform in corner axes applied to the faces and snapped; a corner bound to a rig (the stock Tank's barrel tip) is left alone:

# engine editor/workspace.gd
func transform_faces(id: String, indices: Array, xform: Transform3D, action := "Move faces") -> bool:
	return edit_faces(id, indices, func(f: Dictionary):
		var cs: Array = f["corners"]
		for n in cs.size():
			if not (cs[n] is Dictionary):
				cs[n] = snap_corners([xform * corner_of(cs[n])])[0], action)

func rotate_faces(id: String, indices: Array, axis: String, degrees: float, pivot: Vector3) -> bool:
	var b := Basis(axis_vector(axis), deg_to_rad(degrees))
	return transform_faces(id, indices, Transform3D(b, pivot - b * pivot), "Turn faces")

A mirror negates x and reverses the corner order, so the copy's sprite reads the same way round from outside:

# engine editor/workspace.gd
func mirror_faces(id: String, indices: Array) -> Array[int]:
	return _copy_faces(id, indices, func(cs: Array) -> Array:
		var out := cs.map(func(c): return c if c is Dictionary else [-float(c[0]), float(c[1]), float(c[2])])
		out.reverse()
		return out, "Mirror faces")

Step 2: groups, bindings and the pivot

A group spec is the renderer's own. The renderer turns group nodes in mesh axes (y up), and the stock turrets turn the way the heading does, so group_spec maps the corner axis and the sense for the user:

# engine editor/workspace.gd
static func group_spec(channel: String, axis := "z", scale := 1.0, parent := "", rate := 0.0, pivot := Vector3.ZERO) -> Dictionary:
	# the renderer turns groups in mesh axes (y up): corner z is mesh y, corner y is mesh z; a turret turns the way the heading does
	var mesh_axis := {"x": "x", "y": "z", "z": "y"}.get(axis, "y")
	var e := {"axis": mesh_axis, "channel": channel, "scale": -scale if axis == "z" else scale}
	if rate != 0.0:   # accumulated: `rate` degrees a tick for each unit of the channel (the stock rotor: 5.625, a heading step)
		e["rate"] = rate
		e["scale"] = -1.0 if axis == "z" else 1.0
	var g := {"rotate": [e]}
	if pivot != Vector3.ZERO:
		g["pivot"] = snap_corners([pivot])[0]
	...

Every stock group turns about the vehicle's centre, which is where Return Fire's turrets sit. A turret the user puts off the centre line must turn on the spot, so a group can now name a pivot, and the renderer turns it about that point (a group with none draws exactly as before):

# engine game/vehicle_render_3d.gd (_turn_groups)
		var rot := _rotation(g["spec"].get("rotate", []), String(name), delta)
		if g["spec"].has("pivot"):
			var pv: Array = g["spec"]["pivot"]
			var p := Vector3(float(pv[0]), float(pv[2]), float(pv[1]))   # corner axes to mesh axes
			var b := Basis.from_euler(rot * (PI / 180.0), EULER_ORDER_YXZ)   # as rotation_degrees would
			g["node"].transform = Transform3D(b, p - b * p)
		else:
			g["node"].rotation_degrees = rot

Make group takes the selected faces' middle as the pivot and puts them in the group in one step (add_group). A binding is the descriptor's sprites_by; wrap is the frame count, so the frames repeat as the channel grows:

# engine editor/workspace.gd
func bind_face_sprites(id: String, indices: Array, channel: String, sprites: Array, scale := 1.0) -> bool:
	return edit_faces(id, indices, func(f: Dictionary):
		if sprites.is_empty():
			f.erase("sprites_by")
		else:
			f["sprites_by"] = {"channel": channel, "sprites": sprites.duplicate(), "scale": scale, "wrap": sprites.size()}, "Bind a face to %s" % channel)

Step 3: the Body tab

The Body tab on the engine's test pack: the face list, the Add, sprite, mirror, turn, scale, repeat, corner, group, binding and collision controls on the left; on the right a vehicle built from a box, a wedge, four bound wheels and a turret group, the selected face outlined in white with its red, green and blue arrows, the collision polygon in green with its points in yellow, the cannon's gun pivot and muzzle in red, and the turret angle and wheel frame sliders below (the test pack's art is plain colour blocks)

The view is a SubViewport with its own world, the vehicle at heading 270 so its corner axes are the world's (forward is north, up the screen in the game). A click casts a ray from the camera against each face's two triangles, through the face's group, so a turned turret is picked where it is drawn:

# engine editor/body_panel.gd
## The nearest face under view pixel `at`, or -1.
func face_at(at: Vector2) -> int:
	var from := _camera.project_ray_origin(at)
	var dir := _camera.project_ray_normal(at)
	var best := -1
	var best_d := INF
	for i in ws.body_faces(_id).size():
		var w := _face_world(i)
		if w.size() < 4:
			continue
		for tri in [[0, 1, 2], [0, 2, 3]]:
			var p: Variant = Geometry3D.ray_intersects_triangle(from, dir, w[tri[0]], w[tri[1]], w[tri[2]])
			if p != null and from.distance_to(p) < best_d:
				best_d = from.distance_to(p)
				best = i
	return best

The arrows are the tool's own lines (the Godot editor's gizmos are not available at run time). Dragging one turns the mouse motion along the arrow's screen direction into units; the selection is drawn where it would go, and letting go makes the move, snapped, as one step:

# engine editor/body_panel.gd
func drag(_at: Vector2, relative: Vector2) -> void:
	...
	var a := _camera.unproject_position(o)
	var b := _camera.unproject_position(o + _axis_world(_drag_axis))
	var per_unit := b - a
	if per_unit.length() < 0.01:
		return
	_drag_amount += relative.dot(per_unit.normalized()) / per_unit.length()

func release() -> void:
	if _drag_axis == "":
		return
	var by := ModWorkspace.axis_vector(_drag_axis) * snappedf(_drag_amount, ModWorkspace.BODY_SNAP)
	_drag_axis = ""
	_drag_amount = 0.0
	move_selection(by)

The same arrows move a corner, a collision point or a marker, whichever is selected (move_selection). The game camera (G) is the one-player view's: from the south, pitched by the vehicle's camera.rest_pitch (64 steps is straight down), 300 plus rest_height from a point 12 units north of the vehicle, with the game's horizontal field of view.

The same vehicle through the game's own camera (G): from the south at the vehicle's resting pitch and height, as small as it will be in play

The renderer shows a face with no sprite as nothing, and the game does the same; the tab lays a checker over such faces so the shape stays visible. Beside the list, the tab names the sprite actually drawn now: the team colour's (a generated colour shows its @ id), the hit flash's, or the frame a binding picks.

Step 4: new sprites for faces (#6)

New sprite for it makes a blank sprite one pixel a unit along the face's longer edges and gives it to the selected faces, as one undo step: the two edits nest in one action.

# engine editor/workspace.gd
func new_face_sprite(id: String, indices: Array, sprite_id: String) -> bool:
	if indices.is_empty() or sprite_id == "" or pack.sprites.has(sprite_id):
		return false
	undo.create_action("New sprite for a face")   # the two edits below nest in this one step
	new_blank_sprite(sprite_id, face_pixel_size(id, int(indices[0])), "New sprite for a face")
	set_face_sprite(id, indices, sprite_id)
	undo.commit_action()
	return true

With nothing typed, the id is <mod id>.<vehicle>.face<n>, in the mod's own namespace. (Two edits of the same body cannot nest this way: each replaces the whole render block, worked out before the other has run, so the second would undo the first. Make group is one function, add_group, for that reason.) The tab then asks the editor to show the sprite in the Assets tab, where S7.2's canvas draws on it; a double-click on a face does the same for the sprite it has.

Step 5: the collision shape and the markers

Fit it to the faces sets the collision polygon (document 53) to the faces' outline from above (their bounding rectangle), its height range to theirs, and the coarse box to the square that holds it. Its points are then yellow markers to drag.

A weapon module can now say where its points are on the body, and turn a moved point back into its parameters. The cannon's are the barrel's pivot and its tip at level:

# engine game/vehicle_modules/cannon.gd
func markers() -> Array:
	var ahead := f("pivot_ahead")
	return [{"name": "gun pivot", "point": Vector3(0.0, -ahead, f("pivot_height")), "doc": "Where the barrel turns up (pivot_ahead, pivot_height)"},
		{"name": "muzzle", "point": Vector3(0.0, -ahead - f("barrel"), f("pivot_height")), "doc": "Where the shot leaves, level (barrel)"}]

func marker_params(name: String, at: Vector3) -> Dictionary:
	match name:
		"gun pivot":
			return {"pivot_ahead": -at.y, "pivot_height": at.z}
		"muzzle":
			return {"barrel": maxf(0.0, -at.y - f("pivot_ahead"))}
	return {}

The shot still leaves from where the cannon's numbers put it (document 52); the marker is those numbers drawn. The markers sit on the centre line, as the cannon has no sideways offset.

Step 6: body checks

Validate checks each body: every face has four corners of three numbers (or rig references); a face with no sprite and no binding is a warning (the game draws nothing there); a face's group exists; a binding's frames exist; a group's parent exists, and the parents form no loop; a pivot is three numbers.

The checks

Engine, tests/vehicle_body_check.gd (65 checks, the workspace API alone on the synthetic test pack): a vehicle from blank emptied; a box (six faces, the top 12 by 16 at height 7, the front at -y, team-coloured); four code-made blank wheel frames; a wheel face on the right side bound to position_x, repeated 10 units back, the pair mirrored (x negated, corners reversed, binding kept); a turret box in a group written as the renderer's spec; a barrel added into the group; moved, turned 90 degrees and back, scaled; a typed corner snapped; the barrel reordered before the turret; a duplicate deleted; a new sprite for the top at 12 x 16 pixels, refused a second time, undone and redone as one step; the collision shape fitted and a point moved; a cannon with its own projectile type, its gun pivot and muzzle placed, read as pivot_ahead 2, pivot_height 9, barrel 10; no errors, and a bad body's four problems found; saved, the file holding exactly the render table, a fresh Pack reading it back and drawing the same 17 faces (sprite and corners). With turret_deg at 90, the turret's seven faces move and the other ten do not, swinging clockwise from above as the stock turrets do; with a pivot at its middle the turret turns on the spot. position_x 0, 2, 4, 6, 8 picks wheel frames 0, 1, 2, 3, 0. Then every one of the 31 steps is undone: the blank's body again, no cannon, none of the new sprites.

Engine, tests/vehicle_body_ui_check.gd (26 checks, the editor scene): the Body tab after Vehicles, drawing a new vehicle's five faces; a click on the top selecting it (in the list too); shift-click adding a side face; a press on the red arrow holding it, 3.1 units of mouse motion moving the face 3 across, as one undo step; a click on a corner and Page Up raising that corner alone; M mirroring; Add with no sprite showing the checker, Paint removing it; Make group putting the face in a group about its own middle, whose slider turns it and nothing else; every team colour; G giving the game's camera; New sprite for it naming a blank in the mod's own namespace (<mod id>.<vehicle>.face1, so Validate does not warn), giving it to the face and opening it in the Assets tab; a double-click opening the face's sprite in the Assets tab; everything undone.

map_editor_ui_check follows the new tab order.

Suites: see the closing comment on #143 for the run that went with this document.

Try it

Open the mod tool on the S7.3 game. In the Assets tab, draw a body sprite, a turret sprite, and a wheel as a sequence (wheel.00, wheel.01 ...) if you have not.

  1. Vehicles tab: New..., or pick a vehicle of yours. Then the Body tab: it shows that vehicle.
  2. Select the blank's box faces in the list and Delete, or keep them. Type your body sprite under Sprite, set the Size and Add a Box (or a Wedge for a sloped front).
  3. Click a face; drag the coloured arrows to move it, or click one of its corners and drag that. Arrow keys and Page Up / Page Down nudge.
  4. Add a Face facing sideways at a wheel's place, with the first wheel frame as its sprite. Under Frames pick your wheel sequence and position_x, and Bind. Repeat it along the side, then select the wheels and Mirror (M).
  5. Add a box for the turret on top. With its faces selected, name a group, pick turret_deg, Make group. Move the turret_deg slider under the view.
  6. Pick each Team colour; press G for the game's camera.
  7. Collision shape: Fit it to the faces, then drag the yellow points. If the vehicle has a cannon (Vehicles tab), drag its red muzzle marker to the barrel's end.
  8. Save. Test drive it from the Vehicles tab on your map: drive (the wheels roll), turn the turret, fire. Then play your map with it.

Changed from the plan

  • Groups turn about a pivot of their own (new in the descriptor): the plan assumed the stock groups' centre.
  • Rotate and scale are typed (an angle, a factor), not dragged; the move arrows, corner dragging and the nudge keys are the gizmo.
  • New from one of the user's own vehicles is S7.4's Duplicate.

Added after the first try: the rest of the plan

The first version of this stage left out four parts of S7.5's plan, a scope cut of the session's own, not the user's. They were added before S7.6 (engine commit 5a7f268), so nothing of S7.5's plan is left as JSON-only:

  • Every weapon's markers. Each weapon module with a position now says where its points are on the body and turns a moved point back into its parameters, as the cannon does (Step 5): the Heli guns' mounts and bomb flash, the rocket salvo's tubes (salvo_x and the shared offset_y, offset_z) and where the first rocket starts and its back-blast shows at the level pitch (rocket_point, blast_point, turned back through the pitch the module fires at), the lobbed missile's launch height, the mine layer's drop point.
  • Poses (corners_by, the stock MSV rack that slides): Store as pose n keeps the selected faces' corners as pose n picked by a channel; Load pose n puts a pose back into the corners to edit. While a face is being edited the view draws its own corners, not the pose the channel picks (VehicleRender3D.static_corners).
  • Rigs (the stock Tank barrel's tip that follows the gun's elevation): make a rig that tips by a channel about an offset, tie a face's corner to it (the corner becomes one of its points), untie it (it stays where it is drawn), and drag the rig's points with the same arrows.
  • The wreck's look: flat decals the wreck shows once it has landed (wreck.quads, drawn by Wreck3D, sized and placed from above), a burnt sprite per face (wreck_sprite_ids), a group left out of the wreck (a rotor that flies off, wreck.body_hidden_groups), and whether the wreck burns before it lands (mid_descriptor_is_decal).
# engine editor/workspace.gd
## Ties corner `corner` of face `face` to rig `rig`: its point at rest becomes a point of the rig (one it already has, or a new one),
## so the corner follows the rig's channel. One undo step.
func tie_corner(id: String, face: int, corner: int, rig: String) -> bool:

Validate also checks rigs (a corner tied to a rig or point that does not exist), poses (each four corners), the burnt sprites and the decals. Engine, tests/vehicle_body_more_check.gd (47 checks) covers each part, and vehicle_body_ui_check the new controls.

Still not here: the hangar's look of a new vehicle and its rise script (S7.7).

Where the code is

  • Engine editor/workspace.gd: the vehicle bodies section (BODY_SNAP, BODY_CHANNELS, body_faces, set_body, make_face, box_corners, wedge_corners, panel_corners, add_faces, transform_faces, move_faces, rotate_faces, scale_faces, set_corner, set_face_sprite, face_pixel_size, new_face_sprite, mirror_faces, duplicate_faces, repeat_faces, delete_faces, move_face, set_group, group_spec, add_group, set_faces_group, bind_face_sprites, fit_shape, set_shape_point, body_markers, set_marker).
  • Engine editor/body_panel.gd (new): the Body tab. Engine editor/editor_main.gd: the tab, RF_EDITOR_FACES, the jump to the Assets tab.
  • Engine game/vehicle_render_3d.gd: a group's pivot; a face with no sprite draws nothing.
  • Engine game/vehicle_modules/vehicle_module.gd and cannon.gd: markers, marker_params.
  • Engine editor/validator.gd: check_body.
  • Engine tests/vehicle_body_check.gd, tests/vehicle_body_more_check.gd, tests/vehicle_body_ui_check.gd.
  • Engine game/vehicle_modules/heli_guns.gd, rocket_salvo.gd, lobbed_missile.gd, mine_layer.gd: their markers, marker_params.
  • openfire docs/EDITOR_PLAN.md.

Next: S7.6, sounds, world objects and rules.

Clone this wiki locally