Forum

snake like manifold logic

17 September 2016 00:35
Hy blend4web heads!
I'm back in my calback adventures !
I am using manfold sensor inspired by petigor tutorials to control a cube.

function setup_direction(){
  var head = m_scenes.get_object_by_name("Cube");

  var key_up     = m_ctl.create_keyboard_sensor(m_ctl.KEY_T);
  var key_down   = m_ctl.create_keyboard_sensor(m_ctl.KEY_G);
  var key_right    = m_ctl.create_keyboard_sensor(m_ctl.KEY_H);
  var key_left   = m_ctl.create_keyboard_sensor(m_ctl.KEY_F);

  var direction_array = [
      key_up, key_down, key_right, key_left
  ];

  var up_logic = function(s){return (s[0])};
  var down_logic = function(s){return (s[1])};
  var right_logic = function(s){return (s[2])};
  var left_logic = function(s){return (s[3])};

  function moveHead_cb(obj, id, pulse, param){
      if (pulse == 1){
        console.log(id, param);
        var head_pos = m_trans.get_translation(obj);
        console.log(head_pos);
        head_pos[param[0]] += param[1];
        console.log(head_pos);
        m_trans.set_translation(obj, head_pos[0], head_pos[1], head_pos[2]);
      }
      else{
        console.log("no pulse");
      }
    };

    m_ctl.create_sensor_manifold(head, "UP", m_ctl.CT_TRIGGER,
        direction_array, up_logic, moveHead_cb, [2,-1]);
    m_ctl.create_sensor_manifold(head, "DOWN", m_ctl.CT_TRIGGER,
        direction_array, down_logic, moveHead_cb, [2,1]);
    m_ctl.create_sensor_manifold(head, "RIGHT", m_ctl.CT_TRIGGER,
        direction_array, right_logic, moveHead_cb, [0,1]);
    m_ctl.create_sensor_manifold(head, "LEFT", m_ctl.CT_TRIGGER,
        direction_array, left_logic, moveHead_cb, [0,-1]);
}
});


after that i added a js callback in a node setup to get a function called every second. Because the snake only moves or chanes direction every Xmillisecond

function setup_direction(){
    m_logic_nodes.append_custom_callback("SNAKEMOVE", snakemove);
    var head = m_scenes.get_object_by_name("Cube");
    var direction = []

    var key_up     = m_ctl.create_keyboard_sensor(m_ctl.KEY_T);
    var key_down   = m_ctl.create_keyboard_sensor(m_ctl.KEY_G);
    var key_right    = m_ctl.create_keyboard_sensor(m_ctl.KEY_H);
    var key_left   = m_ctl.create_keyboard_sensor(m_ctl.KEY_F);

    var direction_array = [
      key_up, key_down, key_right, key_left
    ];

    var up_logic = function(s){return (s[0])};
    var down_logic = function(s){return (s[1])};
    var right_logic = function(s){return (s[2])};
    var left_logic = function(s){return (s[3])};

    function moveHead_cb(obj, id, pulse, param){
        if (pulse == 1){
            console.log(id, param);
            var head_pos = m_trans.get_translation(obj);
            console.log(head_pos);
            head_pos[param[0]] += param[1];
            console.log(head_pos);
            m_trans.set_translation(obj, head_pos[0], head_pos[1], head_pos[2]);
      }
        else{
            console.log("no pulse");
      }
    };

    function setDirection_cb(obj, id, pulse, param){
            //getdirection = getdirection();
            console.log(param)//, getdirection);

    }
    // function getdirection(){
    //     return true;
    // }

    m_ctl.create_sensor_manifold(head, "UP", m_ctl.CT_TRIGGER,
            direction_array, up_logic, setDirection_cb, [2,-1]);
    m_ctl.create_sensor_manifold(head, "DOWN", m_ctl.CT_TRIGGER,
        direction_array, down_logic, setDirection_cb, [2,1]);
    m_ctl.create_sensor_manifold(head, "RIGHT", m_ctl.CT_TRIGGER,
        direction_array, right_logic, setDirection_cb, [0,1]);
    m_ctl.create_sensor_manifold(head, "LEFT", m_ctl.CT_TRIGGER,
        direction_array, left_logic, setDirection_cb, [0,-1]);

    function snakemove() {
        console.log("move");
    }


}
});


So… I wonder if i could use the JSCALLBACK to trigger a sensor, a pulse or something so that it could help trigger the movement only when the JSCALLBACK has been called. I would prefer not to use a global variable.

Is you want more precision please ask. I am a bit confused and not sure how to explain expose my question
21 September 2016 11:34
Can the JS callback be used as a pulse ?



I mean that i have this function called every second i would like it to be part of the mannifold logic to stay in the B4W programming style.

I have this delay function in the logic nodes that triggers a JS callback. I think i want to use that function as a promie for the logic part of the manifold logic. That way the movement could be triggered only every second. Snake stlyle.
21 September 2016 18:22
I have this delay function in the logic nodes that triggers a JS callback. I think i want to use that function as a promie for the logic part of the manifold logic. That way the movement could be triggered only every second. Snake stlyle.
Hello!
Sorry for the delay!

I think that JS_Callback is not a good solution for your task, at least not the optimal one.
We'll dig a little into your question and will give some advice
Blend4Web Team - developer
Twitter
LinkedIn
04 October 2016 14:18
I have this delay function in the logic nodes that triggers a JS callback. I think i want to use that function as a promie for the logic part of the manifold logic. That way the movement could be triggered only every second. Snake stlyle.
Hello!
Sorry for such a long delay (we were hard working on the latest release stuff)!

So again I think that JS_Callback is not suitable for your task. You can easily perform per second translation with the help of m_time.set_timeout method and global variable.
Something like this:
// global vars
var _is_moving = false;
...

    function moveHead_cb(obj, id, pulse, param){
        if (pulse == 1){
            // to prevent accumulation of translation
            if (!_is_moving)
                per_second_translation(obj, param);
      }
        else{
            console.log("Key unpressed");
      }

...

function per_second_translation(obj, param) {
    _is_moving = true;
    m_time.set_timeout(function() {
                            // console.log(id, param);
                            var head_pos = m_trans.get_translation(obj);
                            // console.log(head_pos);
                            head_pos[param[0]] += param[1];
                            // console.log(head_pos);
                            m_trans.set_translation(obj, head_pos[0], head_pos[1], head_pos[2]);

                            _is_moving = false;
                        }
                        , 1000);
}


Besides you should use m_ctl.CT_CONTINUOUS manifold control type.

I've attached a simple example project
Blend4Web Team - developer
Twitter
LinkedIn
10 October 2016 00:03
Thank you so much. I will check as soon as possible !
23 October 2016 17:08
OK perfect !
Thank you very much. I just have to change the logic so that if i press ones on a key the cube will continue to move in that direction every second. In snake the fact that you never stop is crucial to make the game interesting !
 
Please register or log in to leave a reply.