Cube rotation on a plane around his edges (in relatives directions pressing relatives arrows)

19 November 2015 11:27
Hi to everyone,
i'm new to B4W but i've worked on some projects with it with great results. Now i'm trying to go forward and pass to interactive animation with object (to make simples games).
The guide "Making your first game" on controls with objects so i'm trying to understand that simple question. How i can animate a cube rotation around his edges dinamically by pressing relatives arrows without configuring previous animations on blender file?

I've exported a cube with dynamic phisycs on a plane (this last one also with physics but static).
Thank you very much for time you decide to dedicate to my question.
19 November 2015 17:26
Hello and welcome to the forum!

This task is not so trivial as it may seem, so it will require a little of math.
Say, you want to rotate a cube around its vertical edge which has plane coordinates (x,z).

1) First, you need to rotate it around vertical axis:
var angle = Math.PI / 18 // 10 degrees
var cube = m_scenes.get_object_by_name("Cube");
m_transform.rotate_y_local(cube, angle);

2) Then, you have to change its position in accordance with a distance from the edge to the object's origin:
var trans = m_transform.get_translation(cube);
trans[0] = trans[0] * Math.cos(angle) - trans[2] * Math.sin(angle);
trans[2] = trans[0] * Math.sin(angle) + trans[2] * Math.cos(angle);
m_transform.set_translation(cube, trans)

3) And the last step (if you use physics objects) is to synchronize translation with physics engine.

20 November 2015 04:18
Hi Rodygin and thanks for the response,
math part is not a problem, i've supposed that it would be somthing in PI.

The part a don't understand are settings in blender.
When everithing is loaded, if i anchor transformations on mouse click or other events, nothing happens.

Can you give me some tips to set my blend file before export (in blender there are only a cube on a plane)?
Thanks so much!

I've tested the code without enabling physics and it seems to work. But the question is that i want to make roll the Cube on the plane by pressing respective arrows button on keyboard.


20 November 2015 12:01

Actually, all this can be done as it is described in these tutorials: firstperson (which you have already mentioned), thirdperson.

Here is a quote from the second article (I cleaned it for consistency):
function setup_movement() {
    var key_w     = m_ctl.create_keyboard_sensor(m_ctl.KEY_W);
    var key_s     = m_ctl.create_keyboard_sensor(m_ctl.KEY_S);

    var move_array = [
        key_w, key_down

    var forward_logic  = function(s){return (s[0])};
    var backward_logic = function(s){return (s[1])};

    function move_cb(obj, id, pulse) {
        if (pulse == 1) {
            switch(id) {
            case "FORWARD":
                var move_dir = 1;
            case "BACKWARD":
                var move_dir = -1;
        } else {
            var move_dir = 0;
        m_phy.set_character_move_dir(obj, move_dir, 0);

    m_ctl.create_sensor_manifold(_character, "FORWARD", m_ctl.CT_TRIGGER,
        move_array, forward_logic, move_cb);
    m_ctl.create_sensor_manifold(_character, "BACKWARD", m_ctl.CT_TRIGGER,
        move_array, backward_logic, move_cb);

function setup_rotation() {
    var key_a     = m_ctl.create_keyboard_sensor(m_ctl.KEY_A);
    var key_d     = m_ctl.create_keyboard_sensor(m_ctl.KEY_D);

    var elapsed_sensor = m_ctl.create_elapsed_sensor();

    var rotate_array = [key_a,  key_d, elapsed_sensor];

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

    function rotate_cb(obj, id, pulse) {

        var elapsed = m_ctl.get_sensor_value(obj, "LEFT", 2);

        if (pulse == 1) {
            switch(id) {
            case "LEFT":
                m_phy.character_rotation_inc(obj, elapsed * ROT_SPEED, 0);
            case "RIGHT":
                m_phy.character_rotation_inc(obj, -elapsed * ROT_SPEED, 0);
    m_ctl.create_sensor_manifold(_character, "LEFT", m_ctl.CT_CONTINUOUS,
        rotate_array, left_logic, rotate_cb);
    m_ctl.create_sensor_manifold(_character, "RIGHT", m_ctl.CT_CONTINUOUS,
        rotate_array, right_logic, rotate_cb);

This code is doing exactly what you want. It rotates/moves physical character with WASD keys. If you want to move non-physical objects, you can combine this approach with my first reply.
Please register or log in to leave a reply.