Up:[[Tutorial]]    Previous:[[Distance sensor]]    Next: [[Controlling agent with Kinect]]      

----
#contents

* Eve movement [#q549285c]

In this tutorial, a simple application to move avatar's eye is introduced.

** Controller [#e7c58721]

Please create controller file moveEye.cpp as follows:

#highlight(cpp){{
#include "ControllerEvent.h"
#include "Controller.h"
#include "Logger.h"

#define PI 3.141592
#define DEG2RAD(DEG) ( (PI) * (DEG) / 180.0 )

using namespace std;

class MyController : public Controller
{
  public:
    void onInit(InitEvent &evt);
    double onAction(ActionEvent&);
    void onRecvMsg(RecvMsgEvent &evt);
    void onCollision(CollisionEvent &evt);
};

void MyController::onInit(InitEvent &evt)
{
}

double MyController::onAction(ActionEvent &evt)
{
  return 1.0;
}

void MyController::onRecvMsg(RecvMsgEvent &evt)
{
  string msg = evt.getMsg();
  LOG_MSG(("msg : %s", msg.c_str()));
  SimObj *my = getObj(myname());

  if(strstr(msg.c_str()," "))
    {
      int n = 0;
      n = msg.find(" ");
      string phi_s = msg.substr(0,n);
      string theta_s = msg.substr(n+1);

      double phi = atof(theta_s.c_str());
      double theta = atof(phi_s.c_str());

      my->setJointAngle("LEYE_JOINT1",DEG2RAD(phi));
      my->setJointAngle("REYE_JOINT1",DEG2RAD(phi));
      my->setJointAngle("LEYE_JOINT0",DEG2RAD(theta));
      my->setJointAngle("REYE_JOINT0",DEG2RAD(theta));
    }
}

void MyController::onCollision(CollisionEvent &evt)
{
}

extern "C" Controller * createController()
{
  return new MyController;
}

}}

This controller moves an avatar's eyes.
Eye movement is realized by setJointAngle as well as normal joints.
In this sample, the avatar moves his eyes as the angle information sent by a message.

Please compile the controller.
 $ ./sigmake.sh moveEye.cpp

** World file [#q1d78563]

Make an world file "moveEye.xml"

#highlight(cpp){{
<?xml version="1.0" encoding="utf8"?>
 <world name="newworld">

   <gravity x="0.0" y="-980.7" z="0.0"/>

   <instanciate class="Man-nii.xml">
     <set-attr-value name="name" value="man_0"/>
     <set-attr-value name="language" value="c++"/>
     <set-attr-value name="implementation"
                     value="./moveEye.so"/>
     <set-attr-value name="dynamics" value="false"/>
     <set-attr-value name="x" value="0.0"/>
     <set-attr-value name="y" value="60.0"/>
     <set-attr-value name="z" value="0.0"/>

   </instanciate>
 </world>
}}

Only an avatar will be appeared in this world.

** Execution [#j253acc6]

Type the following command to execute

 $ sigserver.sh -w ./moveEye.xml

You can see an avatar in the SIGViewer.
For example, the avatar moves his eyes to 20[deg] horizontally and 10[deg] vertically, if you input a message "20 10".

#ref(./moveEye.PNG,40%)

#highlight(end)

Up:[[Tutorial]]    Previous:[[Distance sensor]]    Next: [[Controlling agent with Kinect]]

Front page   Edit Diff Backup Upload Copy Rename Reload   New List of pages Search Recent changes   Help   RSS of recent changes