First, a little screenshot:
The demo that comes with this tutorial allows to drive the rotation of a torus by changing
the 3 Euler's angles: pitch, yaw and roll. The interesting thing is that the Euler's angles
modification is done by a remote all-purposes TCP client:
The TCP client's functioning is hyper-simple: just enter the server name (characters string
representing the hostname or IP address - ex: 127.0.01 or HyperionServer), the server's port
and the message to be sent. The TCP client adds automatically a '\n' at the end of the message
in order to respect the server protocol. Then, [Send Message] allows to send the message to the server.
The checkbox allows to wait for a server reply. The demo's TCP server returns to the client the
HYP_OK string if the client has sent a valid command or HYP_UNKNOWN_COMMAND in
case of invalid command.
Load the DEMO_LUA_TCP_Server_Coroutine.xml file in Hyperion and start the TCP client.
Then send, from the client, the following command:
pitch +100.0
The torus angular speed is now equal to 100.0 degrees / second. The general format of the command
is:
pitch +/-[value] +/-yaw [value] +/-roll [value]
The following commands are all valid:
pitch +100.0 roll -20.0
yaw -50.0
This little demo allows us to use a very handy function of the LUA string lib:
string.gmatch(). This function allows to analyze the client message and look up
the Euler's angles (in 3 global variables):
function extractEulerAngles( command )
local ok = 0;
for angle_name, angle_value in string.gmatch(command, "(%a+) (%p%d+%p%d+)") do
if angle_name=="pitch" then
g_pitch_step = tonumber(angle_value);
ok = 1;
elseif angle_name=="yaw" then
g_yaw_step = tonumber(angle_value);
ok = 1;
elseif angle_name=="roll" then
g_roll_step = tonumber(angle_value);
ok = 1;
end
end
return(ok);
end
Each call to string.gmatch() analyzes the command string pattern by pattern.
In our case, the pattern is (%a+) (%p%d+%p%d+).
The pattern (%a+) allows to extract the angle's name (pitch, yaw or roll) and stores
this name in the angle_name variable. The pattern (%p%d+%p%d+) allows to extract
the angle's value in order to store it in angle_value.
For example, the pitch +100.0 roll -20.0 yaw +10.0 command contains 3 patterns and
pitch +100.0 contains only one.
I don't get deeper into the detail, please see the LUA 5.1 reference guide available at
www.lua.org for more
information about patterns.