Page Menu
Home
Search
Configure Global Search
Log In
Files
F19868
raycast_worldspace.patch
Public
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Authored By
Andrew Hale (trumanblending)
Nov 13 2013, 4:20 PM
Size
2 KB
Subscribers
None
raycast_worldspace.patch
View Options
Index: rna_object_api.c
===================================================================
--- rna_object_api.c (revision 42488)
+++ rna_object_api.c (working copy)
@@ -386,7 +386,7 @@
}
*/
-void rna_Object_ray_cast(Object *ob, ReportList *reports, float ray_start[3], float ray_end[3], float r_location[3], float r_normal[3], int *index)
+void rna_Object_ray_cast(Object *ob, ReportList *reports, float ray_start[3], float ray_end[3], int world_space, float r_location[3], float r_normal[3], int *index)
{
BVHTreeFromMesh treeData= {NULL};
@@ -404,16 +404,44 @@
}
else {
BVHTreeRayHit hit;
- float ray_nor[3], dist;
- sub_v3_v3v3(ray_nor, ray_end, ray_start);
+ float ray_nor[3], mat[3][3], os_start[3], os_end[3], dist;
+ if (world_space) {
+ /* If user gives world space coords, we need to convert to object spacd */
+ copy_m3_m4(mat, ob->obmat);
+
+ /* Create the matrix to convert the surface normal back to worldspace */
+ invert_m3(mat);
+ transpose_m3(mat);
+
+ /* Transform WS coords to OS and compute the ray direction */
+ mul_v3_m4v3(os_end, ob->imat, ray_end);
+ mul_v3_m4v3(os_start, ob->imat, ray_start);
+ sub_v3_v3v3(ray_nor, os_end, os_start);
+ }
+ else {
+ /* If the user gives coords in object space, no need for conversion.*/
+ copy_v3_v3(os_start, ray_start);
+ sub_v3_v3v3(ray_nor, ray_end, ray_start);
+ }
+
dist= hit.dist = normalize_v3(ray_nor);
hit.index = -1;
- if(BLI_bvhtree_ray_cast(treeData.tree, ray_start, ray_nor, 0.0f, &hit, treeData.raycast_callback, &treeData) != -1) {
+ if(BLI_bvhtree_ray_cast(treeData.tree, os_start, ray_nor, 0.0f, &hit, treeData.raycast_callback, &treeData) != -1) {
if(hit.dist<=dist) {
copy_v3_v3(r_location, hit.co);
copy_v3_v3(r_normal, hit.no);
+
+ if (world_space) {
+ /* If we need world space then convert hit location and normal back to WS.*/
+ mul_m4_v3(ob->obmat, r_location);
+ mul_m3_v3(mat, r_normal);
+
+ /* Because of the scale component of the matrix we need to renormalize. */
+ normalize_v3(r_normal);
+ }
+
*index= hit.index;
return;
}
@@ -536,6 +564,7 @@
RNA_def_property_flag(parm, PROP_REQUIRED);
parm= RNA_def_float_vector(func, "end", 3, NULL, -FLT_MAX, FLT_MAX, "", "", -1e4, 1e4);
RNA_def_property_flag(parm, PROP_REQUIRED);
+ parm= RNA_def_boolean(func, "world_space", 0, "", "");
/* return location and normal */
parm= RNA_def_float_vector(func, "location", 3, NULL, -FLT_MAX, FLT_MAX, "Location", "The hit location of this ray cast", -1e4, 1e4);
File Metadata
Details
Mime Type
text/x-diff
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
2f/97/c3bf2ae57f05921ffd315781f4e7
Event Timeline
Log In to Comment