Forum

line_plane_intersect with ceiling or wall

10 March 2016 08:58
In your Furnishing the room demo, we are trying to place objects by line_plane_intersect on the floor. However, if i want to do the same for the ceiling, How would it be possible?

var point = m_util.line_plane_intersect(FLOOR_PLANE_NORMAL, 0,
                        cam_trans, camera_ray, _vec3_tmp3);


Here the FLOOR_PLANE_NORMAL has the value [0, 1, 0]. If I define another value called CEILING_PLANE_NORMAL with the value [0,-1, 0], it doesnt seem to work with the following call :

m_util.line_plane_intersect(CEILING_PLANE_NORMAL, 1.63189,
                        cam_trans, camera_ray, _vec3_tmp3);


This could be a mistake in my calculation but an explanation would be great
10 March 2016 11:42
it doesnt seem to work with the following call :
Hmm, it works for me like that and it seems correct for 1.63189 altitude:


Do you want to attach them to the ceiling more tightly?
10 March 2016 15:24
Yes, you're right, it worked fine. I had to change the pivot of my object for it to work correctly! Thank you!
17 March 2016 08:48
I have another query with regard to moving objects along the wall. This is the code I have:

 var point_w = m_util.line_plane_intersect(FRONT_WALL_PLANE_NORMAL, 3,
                        cam_trans, camera_ray, _vec3_tmp3);
                    var point_wl = m_util.line_plane_intersect(LEFT_WALL_PLANE_NORMAL, -2.45,
                        cam_trans, camera_ray, _vec3_tmp3);
                    var point_wr = m_util.line_plane_intersect(RIGHT_WALL_PLANE_NORMAL, -1.95,
                        cam_trans, camera_ray, _vec3_tmp3);
                    if (point_w && camera_ray[1] < 0) {
                        var obj_parent = m_obj.get_parent(_selected_obj);
                        if (obj_parent && m_obj.is_armature(obj_parent))
                            // translate the parent (armature) of the animated object
                            m_trans.set_translation_v(obj_parent, point_w);
                        else
                            m_trans.set_translation_v(_selected_obj, point_w);
                        limit_object_position(_selected_obj);
                    }

                    if (point_wl && camera_ray[1] < 0) {
                        m_trans.set_rotation(_selected_obj,0,0.7071,0,0.7071);
                        var obj_parent = m_obj.get_parent(_selected_obj);
                        if (obj_parent && m_obj.is_armature(obj_parent))
                            // translate the parent (armature) of the animated object
                            m_trans.set_translation_v(obj_parent, point_wl);
                        else
                            m_trans.set_translation_v(_selected_obj, point_wl);
                        limit_object_position(_selected_obj);
                    }

                    if (point_wr && camera_ray[1] < 0) {
                        m_trans.set_rotation(_selected_obj,0,0.7071,0,0.7071);
                        var obj_parent = m_obj.get_parent(_selected_obj);
                        if (obj_parent && m_obj.is_armature(obj_parent))
                            // translate the parent (armature) of the animated object
                            m_trans.set_translation_v(obj_parent, point_wr);
                        else
                            m_trans.set_translation_v(_selected_obj, point_wr);
                        limit_object_position(_selected_obj);
                    }


where:

var FRONT_WALL_PLANE_NORMAL = [1, 0, 0];
var LEFT_WALL_PLANE_NORMAL = [0, 0, 1];
var RIGHT_WALL_PLANE_NORMAL = [0, 0, -1];



When I try to move it along a single wall at a time, it works perfectly. But when I put them all together it obviously takes only only one condition because of the way I've structured my if statements.

Is there a better way of doing this movement along the wall? Is there some way I can find the object that the ray from the camera is intersecting? Sorry if this sounds more like a programming question…
17 March 2016 19:55
Is there a better way of doing this movement along the wall? Is there some way I can find the object that the ray from the camera is intersecting?

The easiest way is to calculate intersections with all of the walls, then take the point which is nearest to the camera position and set object translation according to it.

For example:
if (point_w)
	var dist_w = m_vec3.squaredDistance(point_w, cam_trans);
else
	var dist_w = 1e10;

if (point_wl)
	var dist_wl = m_vec3.squaredDistance(point_wl, cam_trans);
else
	var dist_wl = 1e10;

if (point_wr)
	var dist_wr = m_vec3.squaredDistance(point_wr, cam_trans);
else
	var dist_wr = 1e10;

var min_dist = min(dist_w, dist_wl, dist_wr);

if (min_dist == dist_w) {
	// move object to point_w
} else if (min_dist == dist_wl) {
	// move object to point_wl
} else {
	// move object to point_wr
}


But there is also another way.
We can test which wall the ray should intersect. It means that the camera ray lies in a certain sector related to a certain wall:



Knowing a vector we can find its angle in polar coordinates via Math.atan2 function. Then we should test the ray vector against every vector directed from the camera to every corner point (which we obviously know).

Something, like this:
// calculate corner vectors
var cam_to_A = m_vec3.subtract(A, cam_trans, _vec3_tmp);
var cam_to_B = m_vec3.subtract(B, cam_trans, _vec3_tmp2);
var cam_to_C = m_vec3.subtract(C, cam_trans, _vec3_tmp3);
var cam_to_D = m_vec3.subtract(D, cam_trans, _vec3_tmp4);

// calculate angles in polar coordinates
var angle_ray = Math.atan2(camera_ray[0], camera_ray[2]); 
var angle_A = Math.atan2(cam_to_A[0], cam_to_A[2]);
var angle_B = Math.atan2(cam_to_B[0], cam_to_B[2]);
var angle_C = Math.atan2(cam_to_C[0], cam_to_C[2]);
var angle_D = Math.atan2(cam_to_D[0], cam_to_D[2]);

// test 
if (angle_ray >= angle_B && angle_ray <= angle_A) {
    // test front wall only
}
...
 
Please register or log in to leave a reply.