Calculate line/object intersection

14 September 2016 16:04 #11795
Hi and thanks for your helping.

So far, I managed to calculate an intersection between a 3D Point and the screen
–> use m_cam.project_point()… , it's OK.

I managed to calculate an intersection between a 2D click with a plan in the 3D space
–> use parametric line, m_cam.calc_ray, m_math.line_pline_intersect …, it's OK.

Now, I want to calculate an intersection between a parametric line and an object but I dont't know how it works in Blend4Web. It is possible ?
I say it's works on Unity with : Physics.Raycast( transform.position, -Vector3.up, out hit, 50, LayerMask.GetMask("myFloor") )

To illustrate :
I want, every time I move my player, calculate the distance between my player and the fake floor not-rendering. If this distance is higher or lower than my constant (1.7m), I modify the Y value of my player's transform to my constant. Like this, the player is all the time "standing".
Create a fake floor like this allows me to create an other floor to smooth the steps of a staircase for example.

My problem :
In this example, I don't use the pline/plane intersection method because my fake floor is not a plane.

An other possibilty ?
To overcome my problem, I thought to use physics with my player and my floor but I'm not really convinced to use physics. if it's possible, I would rather do without.
15 September 2016 14:50 #11818

We got a ray casting code snippet here, may be it'll help?
Development Lead | Twitter
16 September 2016 10:56 #11844
Like always, thanks Yuri !

I did not remember this app exists while I had just found great few weeks ago.
After some tests, I managed to do what I wanted.

For those who are interested. To create a raycast with a 3D object, it's necessary :

In Blend file :
  • Active "enabled physics".
  • In the 3D object who cast the ray, active "Object Physics".
  • In the 3D object who receive the ray, add a material and enable "Special: Collision".
  • (The collision ID allows to target only certain objects)

In javascript :
function my_function() {
        var obj_src = m_scenes.get_active_camera(); //the ray begin at camera's position
        var begin_ray = m_vec3.fromValues(0, 5, 0); //the ray begin 5m above obj_src...
        var end_ray = m_vec3.fromValues(0, -5, 0); //... and ends 5m below obj_src

        var ray_test_cb = function (id, hit_fract, obj_hit, hit_time, hit_pos, hit_norm) {

            //my calculations
            var pos_player = m_trans.get_translation(_player);
            m_trans.set_translation(_player, pos_player[0], hit_pos[1] + _PLAYER_HEIGHT, pos_player[2]);
        var id = m_phy.append_ray_test_ext(obj_src, begin_ray , end_ray, "Floor",
                    ray_test_cb, true, false, true, true);

To understand how "m_phy.append_ray_test_ext" and "ray_test_cb" works, see
append_ray_test_ext and
16 September 2016 11:21 #11847
Always happy to help! Thanks for posting the solution here.
Development Lead | Twitter
Please register or log in to leave a reply.