<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Maratis forum - Smoothly move the M3Object3d with custom behavior]]></title>
		<link>http://forum.maratis3d.com/viewtopic.php?id=438</link>
		<description><![CDATA[The most recent posts in Smoothly move the M3Object3d with custom behavior.]]></description>
		<lastBuildDate>Fri, 07 Sep 2012 11:21:51 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Re: Smoothly move the M3Object3d with custom behavior]]></title>
			<link>http://forum.maratis3d.com/viewtopic.php?pid=2539#p2539</link>
			<description><![CDATA[<p>Thanks a lot!</p>]]></description>
			<author><![CDATA[dummy@example.com (Petr)]]></author>
			<pubDate>Fri, 07 Sep 2012 11:21:51 +0000</pubDate>
			<guid>http://forum.maratis3d.com/viewtopic.php?pid=2539#p2539</guid>
		</item>
		<item>
			<title><![CDATA[Re: Smoothly move the M3Object3d with custom behavior]]></title>
			<link>http://forum.maratis3d.com/viewtopic.php?pid=2534#p2534</link>
			<description><![CDATA[<p>There is no build-in variable for fps, you have to count the frames every seconds,<br />you can do that in MGame::draw for example, increment a number every frame and when 1 sec is done you&#039;ll have fps.</p><p>But I&#039;ll try to add this feature by default soon.</p>]]></description>
			<author><![CDATA[dummy@example.com (anael)]]></author>
			<pubDate>Fri, 07 Sep 2012 10:08:33 +0000</pubDate>
			<guid>http://forum.maratis3d.com/viewtopic.php?pid=2534#p2534</guid>
		</item>
		<item>
			<title><![CDATA[Re: Smoothly move the M3Object3d with custom behavior]]></title>
			<link>http://forum.maratis3d.com/viewtopic.php?pid=2533#p2533</link>
			<description><![CDATA[<p>I think its ok with&nbsp; MVector3 newPosition = parent-&gt;getPosition() + dpos;&nbsp; &nbsp;, <br />dpos - distance that object moved from last update.</p><p>Code works now, but movement is not smooth. its very jerky.<br />App works very slow.<br />How can I get current fps from engine? I want to experiment with different effects on/off and different meshes</p><p>I will try to use <br />engine-&gt;getSystemContext()-&gt;getSystemTick()<br />also</p>]]></description>
			<author><![CDATA[dummy@example.com (Petr)]]></author>
			<pubDate>Fri, 07 Sep 2012 09:29:51 +0000</pubDate>
			<guid>http://forum.maratis3d.com/viewtopic.php?pid=2533#p2533</guid>
		</item>
		<item>
			<title><![CDATA[Re: Smoothly move the M3Object3d with custom behavior]]></title>
			<link>http://forum.maratis3d.com/viewtopic.php?pid=2532#p2532</link>
			<description><![CDATA[<p>update is called 60 times/sec</p><p>I think the problem is in your algorithm,<br />and should you not do :&nbsp; </p><p>MVector3 newPosition = initialPos + dpos;</p><p>instead of :</p><p> MVector3 newPosition = parent-&gt;getPosition() + dpos;</p><p>Also, to get ticks, use engine-&gt;getSystemContext()-&gt;getSystemTick() instead of gettimeofday (more portable)</p>]]></description>
			<author><![CDATA[dummy@example.com (anael)]]></author>
			<pubDate>Fri, 07 Sep 2012 09:19:14 +0000</pubDate>
			<guid>http://forum.maratis3d.com/viewtopic.php?pid=2532#p2532</guid>
		</item>
		<item>
			<title><![CDATA[Re: Smoothly move the M3Object3d with custom behavior]]></title>
			<link>http://forum.maratis3d.com/viewtopic.php?pid=2531#p2531</link>
			<description><![CDATA[<p>I&#039;ve found and fix&nbsp; the error ,code works now.</p>]]></description>
			<author><![CDATA[dummy@example.com (Petr)]]></author>
			<pubDate>Fri, 07 Sep 2012 09:18:09 +0000</pubDate>
			<guid>http://forum.maratis3d.com/viewtopic.php?pid=2531#p2531</guid>
		</item>
		<item>
			<title><![CDATA[Smoothly move the M3Object3d with custom behavior]]></title>
			<link>http://forum.maratis3d.com/viewtopic.php?pid=2530#p2530</link>
			<description><![CDATA[<p>In my maratis project I have invisible Target object, and Camera, which has Follow Target behavior and Looks At target behavior.<br />I want to move target from my C++ code, thus force Camera follows this object.<br />I&nbsp; want to achieve effect of smooth moving the camera.<br />I create custom MoveTo behavior. Objects are initialized with new position , and the time during which they have to move. (If you familiar with cocos2d actions - I want to realize analogue of CCMoveTo for maratis).<br />I add behavior to my target object:<br /></p><div class="codebox"><pre><code> target = scene-&gt;getObjectByName(&quot;Target&quot;);
 float animationDuration = 1.5;
 MoveToBehaviour *moveToBeh = new MoveToBehaviour(target, categoryTargetPosition, animationDuration);
 target-&gt;addBehavior( moveToBeh );
// delete behavior after delay
 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, animationDuration*1.1 * NSEC_PER_SEC), dispatch_get_current_queue(), ^{
     target-&gt;deleteBehavior(0);
 });</code></pre></div><p>When this code executed, the camera instantly move to new position, without smooth movement.<br />What I&#039;m doing wrong? <br />How to do this correctly?<br />Does MBehavior::update() is called periodically by engine , or should I manually call MBehavior::update() on every frame?</p><p>Code of MoveTo behavior class:<br /></p><div class="codebox"><pre><code>#ifndef _MB_MOVETO_H
#define _MB_MOVETO_H

#include &lt;sys/time.h&gt;

class MoveToBehaviour : public MBehavior
{
public:
    // constructors / destructors
    MoveToBehaviour(MObject3d * parentObject, MVector3 _pos, float _tm);
    MoveToBehaviour(MoveToBehaviour &amp; behavior, MObject3d * parentObject);
    ~MoveToBehaviour(void);
private:

    float tm, elapsedTime;
    MVector3 pos;
    MVector3 initialPos;
    struct timeval lastCallTime;
//    M
public:

    // destroy
    void destroy(void);

    // get new
    static MBehavior * getNew(MObject3d * parentObject);

    // get copy
    MBehavior * getCopy(MObject3d * parentObject);

    // name
    static const char * getStaticName(void){ return &quot;MoveTo&quot;; }
    const char * getName(void){ return getStaticName(); }

    // events
    void update(void);
    void runEvent(int param){}

    // variables
    unsigned int getVariablesNumber(void);
    MVariable getVariable(unsigned int id);
};

#endif</code></pre></div><p>.cpp:<br /></p><div class="codebox"><pre><code>#include &lt;MEngine.h&gt;
#include &quot;MoveToBehaviour.h&quot;

/////////////////////////////////////////////////////////////////////////////////////////////////////////
// Init
/////////////////////////////////////////////////////////////////////////////////////////////////////////

MoveToBehaviour::MoveToBehaviour(MObject3d * parentObject, MVector3 _pos, float _tm) :
MBehavior(parentObject),
tm (_tm),
pos (_pos),
elapsedTime (0.0)
{
    gettimeofday(&amp;lastCallTime, NULL);
    initialPos = MVector3();
    if (parentObject) {
        initialPos = parentObject-&gt;getPosition();
    }
}

MoveToBehaviour::MoveToBehaviour(MoveToBehaviour &amp; behavior, MObject3d * parentObject) :
MBehavior(parentObject),
tm (behavior.tm),
pos (behavior.pos),
elapsedTime (behavior.elapsedTime),
lastCallTime(behavior.lastCallTime),
initialPos (behavior.initialPos)
{}

MoveToBehaviour::~MoveToBehaviour(void) {}

void MoveToBehaviour::destroy(void)
{
    delete this;
}

MBehavior * MoveToBehaviour::getNew(MObject3d * parentObject)
{
    return new MoveToBehaviour(parentObject, MVector3() , 0.0 );
}

MBehavior * MoveToBehaviour::getCopy(MObject3d * parentObject)
{
    return new MoveToBehaviour(*this, parentObject);
}


/////////////////////////////////////////////////////////////////////////////////////////////////////////
// Variables
/////////////////////////////////////////////////////////////////////////////////////////////////////////

unsigned int MoveToBehaviour::getVariablesNumber(void){
    return 2;
}

MVariable MoveToBehaviour::getVariable(unsigned int id)
{
    switch(id)
    {
    default:
        return MVariable(&quot;NULL&quot;, NULL, M_VARIABLE_NULL);
    case 0:
        return MVariable(&quot;time&quot;, &amp;tm, M_VARIABLE_FLOAT);
    case 1:
        return MVariable(&quot;moveToPosition&quot;, &amp;pos, M_VARIABLE_VEC3);
    }
}


/////////////////////////////////////////////////////////////////////////////////////////////////////////
// Events
/////////////////////////////////////////////////////////////////////////////////////////////////////////

void MoveToBehaviour::update(void)
{
    if (elapsedTime &gt;= tm*1000.0) {
        return;
    }
    
    MEngine * engine = MEngine::getInstance();
    MGame * game = engine-&gt;getGame();
    MLevel * level = engine-&gt;getLevel();
    MScene * scene = level-&gt;getCurrentScene();

    MObject3d * parent = getParentObject();

    long lastMillis = lastCallTime.tv_sec * 1000 + lastCallTime.tv_usec / 1000;
    
    struct timeval tv ;
    gettimeofday(&amp;tv, NULL);
    long millis = (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
    long dt = millis - lastMillis;
    lastCallTime = tv;
    
    MVector3 dpos = (pos - initialPos) * ( ((float)dt) / (tm*1000.0) );
    MVector3 newPosition = parent-&gt;getPosition() + dpos;
    parent-&gt;setPosition( newPosition );
    parent-&gt;updateMatrix();
    
    elapsedTime += dt;
    if (elapsedTime &gt;= tm*1000.0) {
        parent-&gt;setPosition( pos );
        parent-&gt;updateMatrix();
    }
}</code></pre></div>]]></description>
			<author><![CDATA[dummy@example.com (Petr)]]></author>
			<pubDate>Fri, 07 Sep 2012 08:36:24 +0000</pubDate>
			<guid>http://forum.maratis3d.com/viewtopic.php?pid=2530#p2530</guid>
		</item>
	</channel>
</rss>
