Browsing RMweb today (looking for I don’t know what) I came across this idea by Andy Gautrey on his Yakima Valley layout. It is for HO, but I see no reason it won’t work in N.
“There are also a few 2mm round magnets embedded around the layout, and matching magnets glued to the engine block on vehicles helps to stop them from rolling off the edge.”
Better than my practice to date of setting the tyres in a blob of superglue!