04. Hello Floor
Your first haptic effect: a virtual horizontal floor that pushes back when the cursor presses into it. The force is a simple penalty spring — stiffness × penetration_depth — applied along Z via set_cursor_force.
What you'll learn:
- Using
set_cursor_forceto apply a penalty-spring force - Reading
cursor_positionand computing force in real time - (Python) Setting a workspace preset (
arm_front_centered) so the origin sits at workspace centre - (Python) Interactive keyboard tuning of floor height and stiffness via the
keyboardpackage
Workflow
- Open a WebSocket to
ws://localhost:10001and wait for the first state frame. - On the first frame: register the session profile. The Python variant additionally sends
configure.preset: arm_front_centeredso the origin sits at the middle of the workspace; the C++ variants use whatever configuration is already active on the device. - On every frame: read
cursor_position.z, computeforce_z = max(0, (floor_pos - z) * stiffness), and send it back as aset_cursor_forcecommand. - Subsequent ticks send only the force command — the session profile is a one-shot handshake.
- (Python) Each tick also polls keyboard arrows and updates
floor_pos/stiffnesslive.
Parameters
| Name | Default | Purpose |
|---|---|---|
floor_pos | 0.10 m | Z-coordinate of the virtual floor plane |
stiffness | 1000 N/m | Spring constant (1 mm penetration → 1 N) |
PRINT_EVERY_MS | 100–200 | Telemetry throttle |
| Session profile name | co.haply.inverse.tutorials:hello-floor | Identifies this simulation in Haply Hub |
The Python variant uses the keyboard package (elevated privileges required on Linux):
↑/↓— raise / lower the floor plane←/→— decrease / increase stiffnessR— reset to defaults
Forces are additive on the service tick — summed across sources before being sent to the device. A tutorial like this can coexist with other force producers without either blocking the other.
State fields read
From data.inverse3[i].state:
cursor_position.z—vec3, used to compute penetration depthcurrent_cursor_force— reported for telemetry
Send / receive
Each tick: read the cursor's Z, compute force_z = max(0, (floor_pos - z) * stiffness), and send a set_cursor_force. The first outgoing message also carries the session profile (all variants) and, for Python, configure.preset: arm_front_centered.
- Python
- C++ (nlohmann)
- C++ (Glaze)
Single async loop. Handshake on first frame carries profile + configure.preset so floor_pos = 0.1 aligns with workspace-centre coordinates.
async with websockets.connect(URI) as websocket:
while True:
msg = await websocket.recv()
data = json.loads(msg)
if first_message:
first_message = False
device_id = data["inverse3"][0]["device_id"]
# Handshake: profile + preset (one-shot)
request_msg = {
"session": {"configure": {"profile": {"name": "co.haply.inverse.tutorials:hello-floor"}}},
"inverse3": [{
"device_id": device_id,
"configure": {"preset": {"preset": "arm_front_centered"}}
}]
}
else:
# Per tick: compute force along Z, send set_cursor_force
z = data["inverse3"][0]["state"]["cursor_position"]["z"]
force_z = 0.0 if z > floor_pos else (floor_pos - z) * stiffness
request_msg = {
"inverse3": [{
"device_id": device_id,
"commands": {"set_cursor_force":
{"vector": {"x": 0.0, "y": 0.0, "z": force_z}}}
}]
}
await websocket.send(json.dumps(request_msg))
libhv callback model — onmessage runs on the WebSocket I/O thread; the main thread blocks on ENTER. C++ variants ship the minimal handshake (session profile only, no preset).
ws.onmessage = [&](const std::string &msg) {
const json data = json::parse(msg);
// If no Inverse3 yet, ask the service to re-send the full state
if (!data.contains("inverse3") || data["inverse3"].empty()) {
ws.send(R"({"session":{"force_render_full_state":{}}})");
return;
}
json request = {};
if (first_message) {
first_message = false;
request["session"] = {{"configure", {{"profile",
{{"name", "co.haply.inverse.tutorials:hello-floor"}}}}}};
}
request["inverse3"] = json::array();
for (auto &el : data["inverse3"].items()) {
const float z = el.value()["state"]["cursor_position"]["z"].get<float>();
const float force_z = z > floor_pos ? 0.0f : (floor_pos - z) * stiffness;
request["inverse3"].push_back({
{"device_id", el.value()["device_id"]},
{"commands", {{"set_cursor_force",
{{"vector", {{"x", 0.0}, {"y", 0.0}, {"z", force_z}}}}}}},
});
}
ws.send(request.dump());
};
ws.open("ws://localhost:10001");
while (std::cin.get() != '\n') {} // block main thread
Same libhv callback model — only the body changes. Typed structs for both state and commands. std::optional<session_cmd> holds the one-shot profile — Glaze omits it from the serialized JSON when unset.
// Struct models
struct vec3 { float x{}, y{}, z{}; };
struct inverse_state { vec3 cursor_position{}, current_cursor_force{}; };
struct inverse_device { std::string device_id; inverse_state state; };
struct devices_message { std::vector<inverse_device> inverse3; };
struct set_cursor_force_cmd { vec3 vector; };
struct commands_message {
std::optional<session_cmd> session;
std::vector<device_commands> inverse3;
};
// Send / receive
ws.onmessage = [&](const std::string &msg) {
devices_message data{};
if (glz::read<glz_settings>(data, msg)) return;
if (data.inverse3.empty()) {
ws.send(R"({"session":{"force_render_full_state":{}}})");
return;
}
commands_message out_cmds{};
if (first_message) {
first_message = false;
out_cmds.session = session_cmd{ /* profile = hello-floor */ };
}
for (const auto &dev : data.inverse3) {
const float z = dev.state.cursor_position.z;
const float force_z = z > floor_pos ? 0.0f : (floor_pos - z) * stiffness;
device_commands dc{ .device_id = dev.device_id };
dc.commands.set_cursor_force = set_cursor_force_cmd{{0.0f, 0.0f, force_z}};
out_cmds.inverse3.push_back(std::move(dc));
}
std::string out_json;
(void)glz::write_json(out_cmds, out_json);
ws.send(out_json);
};
ws.open("ws://localhost:10001");
while (std::cin.get() != '\n') {} // block main thread
Source: Python · C++ · C++ Glaze
Related: Control Commands (set_cursor_force) · Mount & Workspace (presets) · Types (vec3) · Sessions · Tutorial 07 (Basis & Mount)