Forum

Character movement walk/strafe

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
02 August 2016 22:40
Hi. Say, in this demo control character is working properly?
Source
http://naviris.ru/

I'm sorry for my bad English…
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.
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: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 07:46
I tried your source code. As long as the button is pressed "D" button "A" does not react.

I also encountered this problem. Therefore I invented a class "keyPresed "

P.S. If you want to shoot, try this demo
http://naviris.ru/

I'm sorry for my bad English…
05 August 2016 12:01
Hi, Kirill. I've been trying your scene, and it is quite nice. You have several tricks that I want to implement: reflection on transparent objects is something I haven't quite get, and it's cool, and the dynamic grass is awesome too. I still haven't tried the climbing ramps neither, I think it should work ok with the character movement. Nice work. You have the logic for the FPS almost done. One little thing you should fix I think is jumping, if you jump and press a movement key it responds so it is not so realisticI haven't done it yet in blend4web but in blender it's done with the old collision trick.One other thing is the menu, I don't have a clue what it says as I'm from Spain and Russian is like Chinese to me.Very nice anyway.

On other side I have tested my movement and it does work pressing one key while keeping the other pressed. No problem in that. It's a bit strange anyway. The first time I tried with the walk and strafe keys controlled from the same method it didn't work but with the walk and strafe methods side by side it does.

Thank you for the feedback, keep on with the work, it's very cool. I hope I see your progress.
05 August 2016 13:36
Thank you for your feedback. I agree that the movement in the air does not work correctly. I will fix it.

Now I'm working on the server part of the game.
http://naviris.ru/

I'm sorry for my bad English…
05 August 2016 14:06
All right, it's very interesting that part too.
Cheers
 
Please register or log in to leave a reply.