Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.x] Make intersect_ray() return true, if ray orginates inside of a shape. #41348

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions doc/classes/Physics2DDirectSpaceState.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@
<argument index="5" name="collide_with_areas" type="bool" default="false">
</argument>
<description>
Intersects a ray in a given space. The returned object is a dictionary with the following fields:
Intersects a ray in a given space. If the ray originates inside one or more objects, the first detected object will be selected. The returned object is a dictionary with the following fields:
[code]collider[/code]: The colliding object.
[code]collider_id[/code]: The colliding object's ID.
[code]metadata[/code]: The intersecting shape's metadata. This metadata is different from [method Object.get_meta], and is set with [method Physics2DServer.shape_set_data].
[code]normal[/code]: The object's surface normal at the intersection point.
[code]position[/code]: The intersection point.
[code]normal[/code]: The object's surface normal at the intersection point. If the ray originates inside the object, this will be a zero length [Vector2].
[code]position[/code]: The intersection point. If the ray originates inside the object, this will be the ray's origin.
[code]rid[/code]: The intersecting object's [RID].
[code]shape[/code]: The shape index of the colliding shape.
If the ray did not intersect anything, then an empty dictionary is returned instead.
Expand Down
6 changes: 3 additions & 3 deletions doc/classes/PhysicsDirectSpaceState.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@
<argument index="5" name="collide_with_areas" type="bool" default="false">
</argument>
<description>
Intersects a ray in a given space. The returned object is a dictionary with the following fields:
Intersects a ray in a given space. In Godot physics, if the ray originates inside one or more objects, the first detected object will be selected. In Bullet physics, these objects are ignored. The returned object is a dictionary with the following fields:
[code]collider[/code]: The colliding object.
[code]collider_id[/code]: The colliding object's ID.
[code]normal[/code]: The object's surface normal at the intersection point.
[code]position[/code]: The intersection point.
[code]normal[/code]: The object's surface normal at the intersection point. In Godot physics, if the ray originates inside one or more objects, this will be a zero length [Vector3].
[code]position[/code]: The intersection point. In Godot physics, if the ray originates inside the object, this will be the ray's origin.
[code]rid[/code]: The intersecting object's [RID].
[code]shape[/code]: The shape index of the colliding shape.
If the ray did not intersect anything, then an empty dictionary is returned instead.
Expand Down
6 changes: 5 additions & 1 deletion doc/classes/RayCast.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Query the closest object intersecting a ray.
</brief_description>
<description>
A RayCast represents a line from its origin to its destination position, [code]cast_to[/code]. It is used to query the 3D space in order to find the closest object along the path of the ray.
A RayCast represents a line from its origin to its destination position, [code]cast_to[/code]. It is used to query the 3D space in order to find the closest object along the path of the ray. In Godot physics, if the ray originates inside one or more objects, the first detected object will be selected. In Bullet physics, these objects are ignored.
RayCast can ignore some objects by adding them to the exception list via [code]add_exception[/code] or by setting proper filtering with collision layers and masks.
RayCast can be configured to report collisions with [Area]s ([member collide_with_areas]) and/or [PhysicsBody]s ([member collide_with_bodies]).
Only enabled raycasts will be able to query the space and report collisions.
Expand Down Expand Up @@ -54,6 +54,7 @@
</return>
<description>
Returns the first object that the ray intersects, or [code]null[/code] if no object is intersecting the ray (i.e. [method is_colliding] returns [code]false[/code]).
In Godot physics, if the ray originates inside one or more objects, the first detected object will be returned. In Bullet physics, these objects are ignored.
</description>
</method>
<method name="get_collider_shape" qualifiers="const">
Expand All @@ -78,13 +79,15 @@
</return>
<description>
Returns the normal of the intersecting object's shape at the collision point.
In Godot physics, if the ray originates inside one or more objects, a zero length [Vector3] will be returned. In Bullet physics, these objects are ignored.
</description>
</method>
<method name="get_collision_point" qualifiers="const">
<return type="Vector3">
</return>
<description>
Returns the collision point at which the ray intersects the closest object.
In Godot physics, if the ray originates inside one or more objects, the ray origin will be returned. In Bullet physics, these objects are ignored.
[b]Note:[/b] This point is in the [b]global[/b] coordinate system.
</description>
</method>
Expand All @@ -93,6 +96,7 @@
</return>
<description>
Returns whether any object is intersecting with the ray's vector (considering the vector length).
[b]Note:[/b] In Godot physics, if the ray originates inside one or more objects, [method is_colliding] will return [code]true[/code]. In Bullet physics, these objects are ignored.
</description>
</method>
<method name="remove_exception">
Expand Down
6 changes: 3 additions & 3 deletions doc/classes/RayCast2D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Query the closest object intersecting a ray.
</brief_description>
<description>
A RayCast represents a line from its origin to its destination position, [code]cast_to[/code]. It is used to query the 2D space in order to find the closest object along the path of the ray.
A RayCast represents a line from its origin to its destination position, [code]cast_to[/code]. It is used to query the 2D space in order to find the closest object along the path of the ray. If the ray originates inside one or more objects, the first detected object will be selected.
RayCast2D can ignore some objects by adding them to the exception list via [code]add_exception[/code], by setting proper filtering with collision layers, or by filtering object types with type masks.
RayCast2D can be configured to report collisions with [Area2D]s ([member collide_with_areas]) and/or [PhysicsBody2D]s ([member collide_with_bodies]).
Only enabled raycasts will be able to query the space and report collisions.
Expand Down Expand Up @@ -74,14 +74,14 @@
<return type="Vector2">
</return>
<description>
Returns the normal of the intersecting object's shape at the collision point.
Returns the normal of the intersecting object's shape at the collision point. If the ray originates inside the object, a zero length [Vector2] will be returned.
</description>
</method>
<method name="get_collision_point" qualifiers="const">
<return type="Vector2">
</return>
<description>
Returns the collision point at which the ray intersects the closest object.
Returns the collision point at which the ray intersects the closest object. If the ray originates inside the object, the ray's origin will be returned.
[b]Note:[/b] this point is in the [b]global[/b] coordinate system.
</description>
</method>
Expand Down
11 changes: 9 additions & 2 deletions servers/physics/space_sw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,15 @@ bool PhysicsDirectSpaceStateSW::intersect_ray(const Vector3 &p_from, const Vecto

Vector3 shape_point, shape_normal;

if (shape->intersect_segment(local_from, local_to, shape_point, shape_normal)) {

if (shape->get_closest_point_to(local_from) == local_from) {
min_d = 0;
res_point = local_from;
res_normal = Vector3();
res_shape = shape_idx;
res_obj = col_obj;
collided = true;
break;
} else if (shape->intersect_segment(local_from, local_to, shape_point, shape_normal)) {
Transform xform = col_obj->get_transform() * col_obj->get_shape_transform(shape_idx);
shape_point = xform.xform(shape_point);

Expand Down
11 changes: 9 additions & 2 deletions servers/physics_2d/space_2d_sw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,15 @@ bool Physics2DDirectSpaceStateSW::intersect_ray(const Vector2 &p_from, const Vec

Vector2 shape_point, shape_normal;

if (shape->intersect_segment(local_from, local_to, shape_point, shape_normal)) {

if (shape->contains_point(local_from)) {
min_d = 0;
res_point = local_from;
res_normal = Vector2();
res_shape = shape_idx;
res_obj = col_obj;
collided = true;
break;
} else if (shape->intersect_segment(local_from, local_to, shape_point, shape_normal)) {
Transform2D xform = col_obj->get_transform() * col_obj->get_shape_transform(shape_idx);
shape_point = xform.xform(shape_point);

Expand Down