Forum

User posts Marcos Calvi
05 August 2016 00:44
Finally I got it!

var _move_dir;
var _strafe_dir;

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 key_up    = m_ctl.create_keyboard_sensor(m_ctl.KEY_UP);
    var key_down  = m_ctl.create_keyboard_sensor(m_ctl.KEY_DOWN);

    var move_array = [
        key_w, key_up,
        key_s, key_down,
    ];

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


    function move_cb(obj, id, pulse) {
        if (pulse == 1) {
            if(id=="FORWARD") {
				_move_dir = 1;
                // m_anim.apply(_character_rig, "character_run");
            }
            if(id=="BACKWARD") {
				_move_dir = -1;
			}
                
                // m_anim.apply(_character_rig, "character_run");

        } else {
            _move_dir = 0;
            // m_anim.apply(_character_rig, "character_idle_01");
        }

        m_phy.set_character_move_dir(obj, _move_dir, _strafe_dir);

        // m_anim.play(_character_rig);
        // m_anim.set_behavior(_character_rig, m_anim.AB_CYCLIC);
    };

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

}

function setup_strafe() {
	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 key_b = m_ctl.create_keyboard_sensor(m_ctl.KEY_B);
	var key_n = m_ctl.create_keyboard_sensor(m_ctl.KEY_N);
	
    var strafe_array = [
        key_a, key_d,
		key_b, key_n,
		];

	var strafe_left_logic = function(s){return (s[0] || s[2])};
	var strafe_right_logic = function(s){return (s[1] || s[3])};

    function strafe_cb(obj, id, pulse) {
        if (pulse == 1) {
            
			if(id=="STRAFE_LEFT") {
				_strafe_dir = 1;
			}
                
                // m_anim.apply(_character_rig, "character_run");
            
			if(id=="STRAFE_RIGHT") {
				_strafe_dir = -1;
			}
                
                // m_anim.apply(_character_rig, "character_run");
        } else {
			_strafe_dir = 0;
            // m_anim.apply(_character_rig, "character_idle_01");
        }

        m_phy.set_character_move_dir(obj, _move_dir, _strafe_dir);

        // m_anim.play(_character_rig);
        // m_anim.set_behavior(_character_rig, m_anim.AB_CYCLIC);
    };

	m_ctl.create_sensor_manifold(_character, "STRAFE_LEFT", m_ctl.CT_CONTINUOUS,
        strafe_array, strafe_left_logic, strafe_cb);
	m_ctl.create_sensor_manifold(_character, "STRAFE_RIGHT", m_ctl.CT_CONTINUOUS,
        strafe_array, strafe_right_logic, strafe_cb);	
}


The two variables _move_dir and _strafe_dir are defined as global variables, I don't know if that is the way they call them… just after the requires.
05 August 2016 00:27
Well, I'm seeing my older post and I have already tried the other approach. Definitely I'l take a look at the way you do it…
05 August 2016 00:19
Yes, it works ok. I'm wondering why you create the keyPresed class to control the posibilities. In my code the value to the variables in set_charachter_move are directly passed but my mistake I think is I have two different methods for walk and strafe and one overwrites the other. I will take a look the way you keep that keyPresed class. It can be helpful later on… Is it strictly necessary for any reason to do the movement right?

By the way, the scene is very good, i'm willing to make a shot.
29 July 2016 19:16
Hello:

I'm trying to implement a first person character movement so far I've been able to make the mouse rotation and the chamera vertical rotation and the movement with the keyboard wasd, but, the movement (w-forward, s-backwards, a-strafe-left and d-strafe-right) has a problem. It doesn't do the diagonal movement aka forward-left or forward-right, you know? The code in the movement function is:

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 key_a     = m_ctl.create_keyboard_sensor(m_ctl.KEY_A);
	var key_d     = m_ctl.create_keyboard_sensor(m_ctl.KEY_D);
    var key_up    = m_ctl.create_keyboard_sensor(m_ctl.KEY_UP);
    var key_down  = m_ctl.create_keyboard_sensor(m_ctl.KEY_DOWN);

    var move_array = [
        key_w, key_up,
        key_s, key_down,
		key_a, key_d
    ];

    var forward_logic  = function(s){return (s[0] || s[1])};
    var backward_logic = function(s){return (s[2] || s[3])};
	var strafe_left_logic = function(s){return (s[4])};
	var strafe_right_logic = function(s){return (s[5])};

    function move_cb(obj, id, pulse) {
        if (pulse == 1) {
            if(id=="FORWARD") {
				var move_dir = 1;
                // m_anim.apply(_character_rig, "character_run");
            }
            if(id=="BACKWARD") {
				var move_dir = -1;
			}
                
                // m_anim.apply(_character_rig, "character_run");
            
			if(id=="STRAFE_LEFT") {
				var strafe_dir = 1;
			}
                
                // m_anim.apply(_character_rig, "character_run");
            
			if(id=="STRAFE_RIGHT") {
				var strafe_dir = -1;
			}
                
                // m_anim.apply(_character_rig, "character_run");
        } else {
            var move_dir = 0;
			var strafe_dir = 0;
            // m_anim.apply(_character_rig, "character_idle_01");
        }

        m_phy.set_character_move_dir(obj, move_dir, strafe_dir);

        // m_anim.play(_character_rig);
        // m_anim.set_behavior(_character_rig, m_anim.AB_CYCLIC);
    };

    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);
	m_ctl.create_sensor_manifold(_character, "STRAFE_LEFT", m_ctl.CT_TRIGGER,
        move_array, strafe_left_logic, move_cb);
	m_ctl.create_sensor_manifold(_character, "STRAFE_RIGHT", m_ctl.CT_TRIGGER,
        move_array, strafe_right_logic, move_cb);
}


Is this a good way to do it? Well, it doesn't cause it doesn't work the way I thought. But could it be a better way to do it? Maybe implementing two different functions for walk and strafe would work?

Any suggestion is welcome
28 July 2016 11:45
Yes, it worked. Thank you very much.
28 July 2016 11:31
Hello,

I'm trying to make a FPS type of movement. The mouse rotation is defined as:

function mouse_rotation_cb(rot_x, rot_y) {
        m_phy.character_rotation_inc(_character, rot_x, 0);
        if (rot_y) {
            m_cam.eye_rotate(camobj, 0, rot_y);

            m_cam.get_camera_angles(camobj, _vec3_tmp);
            offset[2] = -dist * Math.cos(_vec3_tmp[1]);
            offset[1] = -dist * Math.sin(_vec3_tmp[1]);

            m_cons.remove(camobj)
            m_cons.append_semi_stiff_cam(camobj, character, offset);
        }
    }
	
	var canvas_elem = m_container.get_canvas();
	canvas_elem.addEventListener("mousemove", function(e) {
        m_mouse.request_mouse_drag(canvas_elem, null, mouse_rotation_cb);
    }, false);


It works ok if I hold the LMB but, how can I make the rotation without holding it? Should I use other function than request_mouse_drag?
22 July 2016 10:52
Ok, that worked.

Thank you
20 July 2016 21:14
I think I have found an issue in the compilation of the css stylesheets. When you have an image defined as:
background: url(../../deploy/assets/guitar_presentation/background.jpg) 50% 50% no-repeat;

When you compile the project it gets lost and in the debug you find:
GET 
http://localhost:6687/deploy/deploy/assets/guitar_presentation/background.jpg

with the deploy repeated.
It must not be a very difficult problem to address I think. There is also an easy trick: change the css url and setting it without the initial deploy, then the image loads without a problem.
Keep on the good work
19 July 2016 18:23
Well, I tried again and the result is the same:

· In what I understand, it had the javascript contained in the apps_dev/my_app/scripts, which is a simple script to close a div, compiled and renamed to my_app.min.js. If then I open build:my_app.html it crashes at the end of the preloader. The other link in the project manager dev:my_app.html works ok.

· The other thing is the image in the preloader background disappears in the compiled version. It is defined in the css file as a background image of a div. It is stored in the deploy/assets/my_app folder. And like before it worked good in the dev:my_app.html link.

I think I'm going to try another approach and try to include the method to close the div inside the my_app.js file. Maybe I should try to create the eventListener in the javascript instead of passing it through the html link.

Thank you again for your help
19 July 2016 17:50
Thank you, Konstantin:

I'm going to try again, maybe it was my error.