Forum

Stopping on collision

18 July 2015 11:01 #3090
Since I've been playing with the furniture scene, my latest adventure is trying to understand collisions. I plan to go through the Jungle Outpost tutorial when my brain has forgiven me, but in the meantime I will ask my question, since maybe the answer is fast and easy:

How do I make a moveable object stop on collision? For example, if I wanted to put a structure in the "Cartoon Interior" scene that no furniture could move through, how would that be added to the existing demo?
18 July 2015 21:57 #3094
Hello, It's actually a bit tricky to do so. One of the possible solution is using STATIC collision and the following code:

var collision = m_ctl.create_collision_sensor(obj, null, true);

var collision_cb = function(obj, id, pulse) {
    if (pulse == 1) {
        var coll_dist = m_ctl.get_sensor_payload(obj, id, 0).coll_dist;
        if (coll_dist < 0) {
            var coll_norm = m_ctl.get_sensor_payload(obj, id, 0).coll_norm;
            var recover_offset = _vec3_tmp;
            m_vec3.scale(coll_norm, -0.25 * coll_dist, recover_offset);
            var trans = m_trans.get_translation(obj, _vec3_tmp2);
            m_vec3.add(trans, recover_offset, trans);
            m_trans.set_translation_v(obj, trans);
        }
    }
}
m_ctl.create_sensor_manifold(obj, "COLLISION", m_ctl.CT_CONTINUOUS,
        [collision], null, collision_cb);


This code moves one object out of another along the normal which is calculated in collision point.
Blend4Web Team
https://twitter.com/AlexKowel
21 July 2015 04:42 #3131
Thank you, not just for the example but also the chance to boost my own confidence with a bit of debugging - not only did I figure out that "trans" and "recover_offset" needed to be defined as Float32 arrays, but I also had a problem of multiple objects trying to move out of each others way that I partly fixed by testing for obj=selected_obj.
21 July 2015 14:23 #3136
By partly, I mean that if an object collides with two objects it will move from only one, so it can get inside the other. Interesting puzzle.
22 July 2015 10:54 #3142
Ответ на сообщение пользователя Rollo-s_Son
By partly, I mean that if an object collides with two objects it will move from only one, so it can get inside the other. Interesting puzzle.
You can get all needed collision vectors on one frame, put them into some container, and on the next frame, you will have an info about all colliding objects and will be able to calculate the required offset vector.
There will be a little one-frame lag with such technique but with decent fps it will be almost invisible.
Blend4Web developer
 
Please register or log in to leave a reply.