2023-01-10 15:26:54 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* ray_cast.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "ray_cast.h"
|
|
|
|
|
|
|
|
#include "collision_object.h"
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/engine.h"
|
2017-03-21 17:09:04 +01:00
|
|
|
#include "mesh_instance.h"
|
2017-03-05 16:44:50 +01:00
|
|
|
#include "servers/physics_server.h"
|
2017-08-19 01:02:56 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void RayCast::set_cast_to(const Vector3 &p_point) {
|
|
|
|
cast_to = p_point;
|
2021-06-19 06:01:25 +02:00
|
|
|
update_gizmo();
|
|
|
|
|
|
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
|
|
|
if (is_inside_tree()) {
|
|
|
|
_update_debug_shape_vertices();
|
|
|
|
}
|
|
|
|
} else if (debug_shape) {
|
2017-03-21 17:09:04 +01:00
|
|
|
_update_debug_shape();
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector3 RayCast::get_cast_to() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
return cast_to;
|
|
|
|
}
|
|
|
|
|
2017-10-21 22:17:26 +02:00
|
|
|
void RayCast::set_collision_mask(uint32_t p_mask) {
|
|
|
|
collision_mask = p_mask;
|
2016-04-09 20:54:09 +02:00
|
|
|
}
|
|
|
|
|
2017-10-21 22:17:26 +02:00
|
|
|
uint32_t RayCast::get_collision_mask() const {
|
|
|
|
return collision_mask;
|
2016-04-09 20:54:09 +02:00
|
|
|
}
|
|
|
|
|
2017-10-23 14:31:18 +02:00
|
|
|
void RayCast::set_collision_mask_bit(int p_bit, bool p_value) {
|
2021-04-30 17:58:06 +02:00
|
|
|
ERR_FAIL_INDEX_MSG(p_bit, 32, "Collision mask bit must be between 0 and 31 inclusive.");
|
2017-10-23 14:31:18 +02:00
|
|
|
uint32_t mask = get_collision_mask();
|
2021-05-05 12:44:11 +02:00
|
|
|
if (p_value) {
|
2017-10-23 14:31:18 +02:00
|
|
|
mask |= 1 << p_bit;
|
2021-05-05 12:44:11 +02:00
|
|
|
} else {
|
2017-10-23 14:31:18 +02:00
|
|
|
mask &= ~(1 << p_bit);
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
2017-10-23 14:31:18 +02:00
|
|
|
set_collision_mask(mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RayCast::get_collision_mask_bit(int p_bit) const {
|
2021-04-30 17:58:06 +02:00
|
|
|
ERR_FAIL_INDEX_V_MSG(p_bit, 32, false, "Collision mask bit must be between 0 and 31 inclusive.");
|
2017-10-23 14:31:18 +02:00
|
|
|
return get_collision_mask() & (1 << p_bit);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool RayCast::is_colliding() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
return collided;
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
Object *RayCast::get_collider() const {
|
2021-05-05 12:44:11 +02:00
|
|
|
if (against == 0) {
|
2021-05-04 16:00:45 +02:00
|
|
|
return nullptr;
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return ObjectDB::get_instance(against);
|
|
|
|
}
|
|
|
|
|
|
|
|
int RayCast::get_collider_shape() const {
|
|
|
|
return against_shape;
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector3 RayCast::get_collision_point() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
return collision_point;
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector3 RayCast::get_collision_normal() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
return collision_normal;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RayCast::set_enabled(bool p_enabled) {
|
2017-03-05 16:44:50 +01:00
|
|
|
enabled = p_enabled;
|
2019-08-21 15:27:02 +02:00
|
|
|
update_gizmo();
|
|
|
|
|
2021-05-05 12:44:11 +02:00
|
|
|
if (is_inside_tree() && !Engine::get_singleton()->is_editor_hint()) {
|
2018-04-11 09:28:14 +02:00
|
|
|
set_physics_process_internal(p_enabled);
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
|
|
|
if (!p_enabled) {
|
2017-03-05 16:44:50 +01:00
|
|
|
collided = false;
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
2017-03-21 17:09:04 +01:00
|
|
|
|
|
|
|
if (is_inside_tree() && get_tree()->is_debugging_collisions_hint()) {
|
2021-05-05 12:44:11 +02:00
|
|
|
if (p_enabled) {
|
2017-03-21 17:09:04 +01:00
|
|
|
_update_debug_shape();
|
2021-05-05 12:44:11 +02:00
|
|
|
} else {
|
2017-03-21 17:09:04 +01:00
|
|
|
_clear_debug_shape();
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
2017-03-21 17:09:04 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RayCast::is_enabled() const {
|
|
|
|
return enabled;
|
|
|
|
}
|
|
|
|
|
2017-11-30 04:26:13 +01:00
|
|
|
void RayCast::set_exclude_parent_body(bool p_exclude_parent_body) {
|
2021-05-05 12:44:11 +02:00
|
|
|
if (exclude_parent_body == p_exclude_parent_body) {
|
2017-11-30 04:26:13 +01:00
|
|
|
return;
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
2017-11-30 04:26:13 +01:00
|
|
|
|
|
|
|
exclude_parent_body = p_exclude_parent_body;
|
|
|
|
|
2021-05-05 12:44:11 +02:00
|
|
|
if (!is_inside_tree()) {
|
2017-11-30 04:26:13 +01:00
|
|
|
return;
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
2017-11-30 04:26:13 +01:00
|
|
|
|
|
|
|
if (Object::cast_to<CollisionObject>(get_parent())) {
|
2021-05-05 12:44:11 +02:00
|
|
|
if (exclude_parent_body) {
|
2017-11-30 04:26:13 +01:00
|
|
|
exclude.insert(Object::cast_to<CollisionObject>(get_parent())->get_rid());
|
2021-05-05 12:44:11 +02:00
|
|
|
} else {
|
2017-11-30 04:26:13 +01:00
|
|
|
exclude.erase(Object::cast_to<CollisionObject>(get_parent())->get_rid());
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
2017-11-30 04:26:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RayCast::get_exclude_parent_body() const {
|
|
|
|
return exclude_parent_body;
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void RayCast::_notification(int p_what) {
|
2017-03-05 16:44:50 +01:00
|
|
|
switch (p_what) {
|
2014-11-06 01:20:42 +01:00
|
|
|
case NOTIFICATION_ENTER_TREE: {
|
2021-06-19 06:01:25 +02:00
|
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
|
|
|
_update_debug_shape_vertices();
|
|
|
|
}
|
2017-08-19 01:02:56 +02:00
|
|
|
if (enabled && !Engine::get_singleton()->is_editor_hint()) {
|
2018-04-11 09:28:14 +02:00
|
|
|
set_physics_process_internal(true);
|
2021-01-14 23:51:22 +01:00
|
|
|
} else {
|
2018-04-11 09:28:14 +02:00
|
|
|
set_physics_process_internal(false);
|
2021-01-14 23:51:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (get_tree()->is_debugging_collisions_hint()) {
|
|
|
|
_update_debug_shape();
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-11-30 04:26:13 +01:00
|
|
|
if (Object::cast_to<CollisionObject>(get_parent())) {
|
2021-05-05 12:44:11 +02:00
|
|
|
if (exclude_parent_body) {
|
2017-11-30 04:26:13 +01:00
|
|
|
exclude.insert(Object::cast_to<CollisionObject>(get_parent())->get_rid());
|
2021-05-05 12:44:11 +02:00
|
|
|
} else {
|
2017-11-30 04:26:13 +01:00
|
|
|
exclude.erase(Object::cast_to<CollisionObject>(get_parent())->get_rid());
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
2017-11-30 04:26:13 +01:00
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
} break;
|
2014-11-06 01:20:42 +01:00
|
|
|
case NOTIFICATION_EXIT_TREE: {
|
2014-02-10 02:10:30 +01:00
|
|
|
if (enabled) {
|
2018-04-11 09:28:14 +02:00
|
|
|
set_physics_process_internal(false);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2021-05-05 12:44:11 +02:00
|
|
|
if (debug_shape) {
|
2017-03-21 17:09:04 +01:00
|
|
|
_clear_debug_shape();
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
2017-03-21 17:09:04 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
} break;
|
2018-04-11 09:28:14 +02:00
|
|
|
case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
|
2021-05-05 12:44:11 +02:00
|
|
|
if (!enabled) {
|
2014-02-10 02:10:30 +01:00
|
|
|
break;
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-21 17:09:04 +01:00
|
|
|
bool prev_collision_state = collided;
|
2016-08-09 19:52:15 +02:00
|
|
|
_update_raycast_state();
|
2017-03-21 17:09:04 +01:00
|
|
|
if (prev_collision_state != collided && get_tree()->is_debugging_collisions_hint()) {
|
2021-06-19 06:01:25 +02:00
|
|
|
_update_debug_shape_material(true);
|
2017-03-21 17:09:04 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-08-09 19:52:15 +02:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void RayCast::_update_raycast_state() {
|
2016-08-09 19:52:15 +02:00
|
|
|
Ref<World> w3d = get_world();
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND(w3d.is_null());
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-08-09 19:52:15 +02:00
|
|
|
PhysicsDirectSpaceState *dss = PhysicsServer::get_singleton()->space_get_direct_state(w3d->get_space());
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND(!dss);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-08-09 19:52:15 +02:00
|
|
|
Transform gt = get_global_transform();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-08-09 19:52:15 +02:00
|
|
|
Vector3 to = cast_to;
|
2021-05-05 12:44:11 +02:00
|
|
|
if (to == Vector3()) {
|
2017-03-05 16:44:50 +01:00
|
|
|
to = Vector3(0, 0.01, 0);
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-08-09 19:52:15 +02:00
|
|
|
PhysicsDirectSpaceState::RayResult rr;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2018-08-21 17:37:51 +02:00
|
|
|
if (dss->intersect_ray(gt.get_origin(), gt.xform(to), rr, exclude, collision_mask, collide_with_bodies, collide_with_areas)) {
|
2017-03-05 16:44:50 +01:00
|
|
|
collided = true;
|
|
|
|
against = rr.collider_id;
|
|
|
|
collision_point = rr.position;
|
|
|
|
collision_normal = rr.normal;
|
|
|
|
against_shape = rr.shape;
|
2016-08-09 19:52:15 +02:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
collided = false;
|
2018-01-12 12:31:02 +01:00
|
|
|
against = 0;
|
|
|
|
against_shape = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-09 19:52:15 +02:00
|
|
|
void RayCast::force_raycast_update() {
|
|
|
|
_update_raycast_state();
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void RayCast::add_exception_rid(const RID &p_rid) {
|
2015-01-03 20:52:37 +01:00
|
|
|
exclude.insert(p_rid);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void RayCast::add_exception(const Object *p_object) {
|
2015-01-03 20:52:37 +01:00
|
|
|
ERR_FAIL_NULL(p_object);
|
2017-08-24 22:58:51 +02:00
|
|
|
const CollisionObject *co = Object::cast_to<CollisionObject>(p_object);
|
2023-02-24 00:36:30 +01:00
|
|
|
ERR_FAIL_COND_MSG(!co, "The passed Node must be an instance of CollisionObject.");
|
2015-01-03 20:52:37 +01:00
|
|
|
add_exception_rid(co->get_rid());
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void RayCast::remove_exception_rid(const RID &p_rid) {
|
2015-01-03 20:52:37 +01:00
|
|
|
exclude.erase(p_rid);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void RayCast::remove_exception(const Object *p_object) {
|
2015-01-03 20:52:37 +01:00
|
|
|
ERR_FAIL_NULL(p_object);
|
2017-08-24 22:58:51 +02:00
|
|
|
const CollisionObject *co = Object::cast_to<CollisionObject>(p_object);
|
2023-02-24 00:36:30 +01:00
|
|
|
ERR_FAIL_COND_MSG(!co, "The passed Node must be an instance of CollisionObject.");
|
2015-01-03 20:52:37 +01:00
|
|
|
remove_exception_rid(co->get_rid());
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void RayCast::clear_exceptions() {
|
2015-01-03 20:52:37 +01:00
|
|
|
exclude.clear();
|
2022-02-08 09:22:10 +01:00
|
|
|
|
|
|
|
if (exclude_parent_body && is_inside_tree()) {
|
|
|
|
CollisionObject *parent = Object::cast_to<CollisionObject>(get_parent());
|
|
|
|
if (parent) {
|
|
|
|
exclude.insert(parent->get_rid());
|
|
|
|
}
|
|
|
|
}
|
2015-01-03 20:52:37 +01:00
|
|
|
}
|
|
|
|
|
2018-08-21 20:30:41 +02:00
|
|
|
void RayCast::set_collide_with_areas(bool p_clip) {
|
|
|
|
collide_with_areas = p_clip;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RayCast::is_collide_with_areas_enabled() const {
|
|
|
|
return collide_with_areas;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RayCast::set_collide_with_bodies(bool p_clip) {
|
|
|
|
collide_with_bodies = p_clip;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RayCast::is_collide_with_bodies_enabled() const {
|
|
|
|
return collide_with_bodies;
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void RayCast::_bind_methods() {
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &RayCast::set_enabled);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_enabled"), &RayCast::is_enabled);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_cast_to", "local_point"), &RayCast::set_cast_to);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_cast_to"), &RayCast::get_cast_to);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("is_colliding"), &RayCast::is_colliding);
|
|
|
|
ClassDB::bind_method(D_METHOD("force_raycast_update"), &RayCast::force_raycast_update);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_collider"), &RayCast::get_collider);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_collider_shape"), &RayCast::get_collider_shape);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_collision_point"), &RayCast::get_collision_point);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_collision_normal"), &RayCast::get_collision_normal);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("add_exception_rid", "rid"), &RayCast::add_exception_rid);
|
|
|
|
ClassDB::bind_method(D_METHOD("add_exception", "node"), &RayCast::add_exception);
|
2015-01-03 20:52:37 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("remove_exception_rid", "rid"), &RayCast::remove_exception_rid);
|
|
|
|
ClassDB::bind_method(D_METHOD("remove_exception", "node"), &RayCast::remove_exception);
|
2015-01-03 20:52:37 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("clear_exceptions"), &RayCast::clear_exceptions);
|
2015-01-03 20:52:37 +01:00
|
|
|
|
2017-10-21 22:17:26 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &RayCast::set_collision_mask);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_collision_mask"), &RayCast::get_collision_mask);
|
2016-04-09 20:54:09 +02:00
|
|
|
|
2017-10-23 14:31:18 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_collision_mask_bit", "bit", "value"), &RayCast::set_collision_mask_bit);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_collision_mask_bit", "bit"), &RayCast::get_collision_mask_bit);
|
|
|
|
|
2017-11-30 04:26:13 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_exclude_parent_body", "mask"), &RayCast::set_exclude_parent_body);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_exclude_parent_body"), &RayCast::get_exclude_parent_body);
|
|
|
|
|
2018-08-21 20:30:41 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_collide_with_areas", "enable"), &RayCast::set_collide_with_areas);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_collide_with_areas_enabled"), &RayCast::is_collide_with_areas_enabled);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("set_collide_with_bodies", "enable"), &RayCast::set_collide_with_bodies);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_collide_with_bodies_enabled"), &RayCast::is_collide_with_bodies_enabled);
|
|
|
|
|
2021-06-19 06:01:25 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_debug_shape_custom_color", "debug_shape_custom_color"), &RayCast::set_debug_shape_custom_color);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_debug_shape_custom_color"), &RayCast::get_debug_shape_custom_color);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("set_debug_shape_thickness", "debug_shape_thickness"), &RayCast::set_debug_shape_thickness);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_debug_shape_thickness"), &RayCast::get_debug_shape_thickness);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled");
|
2017-11-30 04:26:13 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "exclude_parent"), "set_exclude_parent_body", "get_exclude_parent_body");
|
2017-03-05 16:44:50 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "cast_to"), "set_cast_to", "get_cast_to");
|
2017-10-21 22:17:26 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask");
|
2018-08-21 20:30:41 +02:00
|
|
|
|
|
|
|
ADD_GROUP("Collide With", "collide_with");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_areas", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collide_with_areas", "is_collide_with_areas_enabled");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_bodies", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collide_with_bodies", "is_collide_with_bodies_enabled");
|
2021-06-19 06:01:25 +02:00
|
|
|
|
|
|
|
ADD_GROUP("Debug Shape", "debug_shape");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "debug_shape_custom_color"), "set_debug_shape_custom_color", "get_debug_shape_custom_color");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "debug_shape_thickness", PROPERTY_HINT_RANGE, "1,5"), "set_debug_shape_thickness", "get_debug_shape_thickness");
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2022-01-11 05:17:42 +01:00
|
|
|
int RayCast::get_debug_shape_thickness() const {
|
2021-06-19 06:01:25 +02:00
|
|
|
return debug_shape_thickness;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RayCast::_update_debug_shape_vertices() {
|
|
|
|
debug_shape_vertices.clear();
|
|
|
|
debug_line_vertices.clear();
|
|
|
|
|
|
|
|
if (cast_to == Vector3()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug_line_vertices.push_back(Vector3());
|
|
|
|
debug_line_vertices.push_back(cast_to);
|
|
|
|
|
|
|
|
if (debug_shape_thickness > 1) {
|
|
|
|
float scale_factor = 100.0;
|
|
|
|
Vector3 dir = Vector3(cast_to).normalized();
|
|
|
|
// Draw truncated pyramid
|
|
|
|
Vector3 normal = (fabs(dir.x) + fabs(dir.y) > CMP_EPSILON) ? Vector3(-dir.y, dir.x, 0).normalized() : Vector3(0, -dir.z, dir.y).normalized();
|
|
|
|
normal *= debug_shape_thickness / scale_factor;
|
|
|
|
int vertices_strip_order[14] = { 4, 5, 0, 1, 2, 5, 6, 4, 7, 0, 3, 2, 7, 6 };
|
|
|
|
for (int v = 0; v < 14; v++) {
|
|
|
|
Vector3 vertex = vertices_strip_order[v] < 4 ? normal : normal / 3.0 + cast_to;
|
|
|
|
debug_shape_vertices.push_back(vertex.rotated(dir, Math_PI * (0.5 * (vertices_strip_order[v] % 4) + 0.25)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-11 05:17:42 +01:00
|
|
|
void RayCast::set_debug_shape_thickness(const int p_debug_shape_thickness) {
|
2021-06-19 06:01:25 +02:00
|
|
|
debug_shape_thickness = p_debug_shape_thickness;
|
|
|
|
update_gizmo();
|
|
|
|
|
|
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
|
|
|
if (is_inside_tree()) {
|
|
|
|
_update_debug_shape_vertices();
|
|
|
|
}
|
|
|
|
} else if (debug_shape) {
|
|
|
|
_update_debug_shape();
|
|
|
|
}
|
|
|
|
}
|
2017-03-21 17:09:04 +01:00
|
|
|
|
2021-06-19 06:01:25 +02:00
|
|
|
const Vector<Vector3> &RayCast::get_debug_shape_vertices() const {
|
|
|
|
return debug_shape_vertices;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Vector<Vector3> &RayCast::get_debug_line_vertices() const {
|
|
|
|
return debug_line_vertices;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RayCast::set_debug_shape_custom_color(const Color &p_color) {
|
|
|
|
debug_shape_custom_color = p_color;
|
|
|
|
if (debug_material.is_valid()) {
|
|
|
|
_update_debug_shape_material();
|
2017-03-21 17:09:04 +01:00
|
|
|
}
|
2021-06-19 06:01:25 +02:00
|
|
|
}
|
|
|
|
|
2024-02-08 20:10:26 +01:00
|
|
|
Ref<Material3D> RayCast::get_debug_material() {
|
2021-06-19 06:01:25 +02:00
|
|
|
_update_debug_shape_material();
|
|
|
|
return debug_material;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Color &RayCast::get_debug_shape_custom_color() const {
|
|
|
|
return debug_shape_custom_color;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RayCast::_create_debug_shape() {
|
|
|
|
_update_debug_shape_material();
|
2017-03-21 17:09:04 +01:00
|
|
|
|
2017-06-07 23:18:55 +02:00
|
|
|
Ref<ArrayMesh> mesh = memnew(ArrayMesh);
|
2017-03-21 17:09:04 +01:00
|
|
|
|
|
|
|
MeshInstance *mi = memnew(MeshInstance);
|
2022-09-12 07:46:48 +02:00
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
// This enables the debug helper to show up in editor runs.
|
|
|
|
// However it should not show up during export, because global mode
|
|
|
|
// can slow the portal system, and this should only be used for debugging.
|
|
|
|
mi->set_portal_mode(CullInstance::PORTAL_MODE_GLOBAL);
|
|
|
|
#endif
|
2017-03-21 17:09:04 +01:00
|
|
|
mi->set_mesh(mesh);
|
|
|
|
add_child(mi);
|
2021-06-19 06:01:25 +02:00
|
|
|
|
2017-03-21 17:09:04 +01:00
|
|
|
debug_shape = mi;
|
|
|
|
}
|
|
|
|
|
2021-06-19 06:01:25 +02:00
|
|
|
void RayCast::_update_debug_shape_material(bool p_check_collision) {
|
|
|
|
if (!debug_material.is_valid()) {
|
2024-02-13 15:44:23 +01:00
|
|
|
Ref<SpatialMaterial> material = memnew(SpatialMaterial);
|
2021-06-19 06:01:25 +02:00
|
|
|
debug_material = material;
|
|
|
|
|
2024-02-08 20:10:26 +01:00
|
|
|
material->set_flag(Material3D::FLAG_UNSHADED, true);
|
|
|
|
material->set_feature(Material3D::FEATURE_TRANSPARENT, true);
|
2021-06-19 09:12:58 +02:00
|
|
|
// Use double-sided rendering so that the RayCast can be seen if the camera is inside.
|
2024-02-08 20:10:26 +01:00
|
|
|
material->set_cull_mode(Material3D::CULL_DISABLED);
|
2021-06-19 06:01:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Color color = debug_shape_custom_color;
|
|
|
|
if (color == Color(0.0, 0.0, 0.0)) {
|
|
|
|
// Use the default debug shape color defined in the Project Settings.
|
|
|
|
color = get_tree()->get_debug_collisions_color();
|
|
|
|
}
|
|
|
|
|
2021-08-24 11:24:02 +02:00
|
|
|
if (p_check_collision && collided) {
|
2021-06-19 06:01:25 +02:00
|
|
|
if ((color.get_h() < 0.055 || color.get_h() > 0.945) && color.get_s() > 0.5 && color.get_v() > 0.5) {
|
2022-01-06 23:18:25 +01:00
|
|
|
// If base color is already quite reddish, highlight collision with green color
|
2021-06-19 06:01:25 +02:00
|
|
|
color = Color(0.0, 1.0, 0.0, color.a);
|
|
|
|
} else {
|
2022-01-06 23:18:25 +01:00
|
|
|
// Else, highlight collision with red color
|
2021-06-19 06:01:25 +02:00
|
|
|
color = Color(1.0, 0, 0, color.a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-08 20:10:26 +01:00
|
|
|
Ref<Material3D> material = static_cast<Ref<Material3D>>(debug_material);
|
2021-06-19 06:01:25 +02:00
|
|
|
material->set_albedo(color);
|
|
|
|
}
|
|
|
|
|
2017-03-21 17:09:04 +01:00
|
|
|
void RayCast::_update_debug_shape() {
|
2021-05-05 12:44:11 +02:00
|
|
|
if (!enabled) {
|
2017-03-21 17:09:04 +01:00
|
|
|
return;
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
2017-03-21 17:09:04 +01:00
|
|
|
|
2021-05-05 12:44:11 +02:00
|
|
|
if (!debug_shape) {
|
2017-03-21 17:09:04 +01:00
|
|
|
_create_debug_shape();
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
2017-03-21 17:09:04 +01:00
|
|
|
|
|
|
|
MeshInstance *mi = static_cast<MeshInstance *>(debug_shape);
|
2017-06-07 23:18:55 +02:00
|
|
|
Ref<ArrayMesh> mesh = mi->get_mesh();
|
2021-05-05 12:44:11 +02:00
|
|
|
if (!mesh.is_valid()) {
|
2021-01-14 23:51:22 +01:00
|
|
|
return;
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
2017-03-21 17:09:04 +01:00
|
|
|
|
2021-06-19 06:01:25 +02:00
|
|
|
_update_debug_shape_vertices();
|
2017-03-21 17:09:04 +01:00
|
|
|
|
2021-06-19 06:01:25 +02:00
|
|
|
mesh->clear_surfaces();
|
2021-01-14 23:51:22 +01:00
|
|
|
|
2021-06-19 06:01:25 +02:00
|
|
|
Array a;
|
|
|
|
a.resize(Mesh::ARRAY_MAX);
|
2021-01-14 23:51:22 +01:00
|
|
|
|
2021-06-19 06:01:25 +02:00
|
|
|
uint32_t flags = 0;
|
|
|
|
int surface_count = 0;
|
|
|
|
|
|
|
|
if (!debug_line_vertices.empty()) {
|
|
|
|
a[Mesh::ARRAY_VERTEX] = debug_line_vertices;
|
2021-01-14 23:51:22 +01:00
|
|
|
mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, a, Array(), flags);
|
2021-06-19 06:01:25 +02:00
|
|
|
mesh->surface_set_material(surface_count, debug_material);
|
|
|
|
++surface_count;
|
|
|
|
}
|
2021-01-14 23:51:22 +01:00
|
|
|
|
2021-06-19 06:01:25 +02:00
|
|
|
if (!debug_shape_vertices.empty()) {
|
|
|
|
a[Mesh::ARRAY_VERTEX] = debug_shape_vertices;
|
|
|
|
mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLE_STRIP, a, Array(), flags);
|
|
|
|
mesh->surface_set_material(surface_count, debug_material);
|
|
|
|
++surface_count;
|
2021-01-14 23:51:22 +01:00
|
|
|
}
|
2017-03-21 17:09:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void RayCast::_clear_debug_shape() {
|
2021-05-05 12:44:11 +02:00
|
|
|
if (!debug_shape) {
|
2017-03-21 17:09:04 +01:00
|
|
|
return;
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
2017-03-21 17:09:04 +01:00
|
|
|
|
|
|
|
MeshInstance *mi = static_cast<MeshInstance *>(debug_shape);
|
2021-05-05 12:44:11 +02:00
|
|
|
if (mi->is_inside_tree()) {
|
2017-03-21 17:09:04 +01:00
|
|
|
mi->queue_delete();
|
2021-05-05 12:44:11 +02:00
|
|
|
} else {
|
2017-03-21 17:09:04 +01:00
|
|
|
memdelete(mi);
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
2017-03-21 17:09:04 +01:00
|
|
|
|
2021-05-04 16:00:45 +02:00
|
|
|
debug_shape = nullptr;
|
2017-03-21 17:09:04 +01:00
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
RayCast::RayCast() {
|
2017-03-05 16:44:50 +01:00
|
|
|
enabled = false;
|
|
|
|
against = 0;
|
|
|
|
collided = false;
|
|
|
|
against_shape = 0;
|
2017-10-21 22:17:26 +02:00
|
|
|
collision_mask = 1;
|
2017-03-05 16:44:50 +01:00
|
|
|
cast_to = Vector3(0, -1, 0);
|
2021-05-04 16:00:45 +02:00
|
|
|
debug_shape = nullptr;
|
2017-11-30 04:26:13 +01:00
|
|
|
exclude_parent_body = true;
|
2018-08-21 20:30:41 +02:00
|
|
|
collide_with_areas = false;
|
|
|
|
collide_with_bodies = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|