2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* ray_cast.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2021-01-01 19:13:46 +00:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* 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-04 23:50:27 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "ray_cast.h"
|
|
|
|
|
|
|
|
#include "collision_object.h"
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/engine.h"
|
2017-03-21 16:09:04 +00:00
|
|
|
#include "mesh_instance.h"
|
2017-03-05 15:44:50 +00:00
|
|
|
#include "servers/physics_server.h"
|
2017-08-18 23:02:56 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void RayCast::set_cast_to(const Vector3 &p_point) {
|
|
|
|
cast_to = p_point;
|
2021-06-19 04:01:25 +00: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 16:09:04 +00:00
|
|
|
_update_debug_shape();
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector3 RayCast::get_cast_to() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
return cast_to;
|
|
|
|
}
|
|
|
|
|
2017-10-21 20:17:26 +00:00
|
|
|
void RayCast::set_collision_mask(uint32_t p_mask) {
|
|
|
|
collision_mask = p_mask;
|
2016-04-09 18:54:09 +00:00
|
|
|
}
|
|
|
|
|
2017-10-21 20:17:26 +00:00
|
|
|
uint32_t RayCast::get_collision_mask() const {
|
|
|
|
return collision_mask;
|
2016-04-09 18:54:09 +00:00
|
|
|
}
|
|
|
|
|
2017-10-23 12:31:18 +00:00
|
|
|
void RayCast::set_collision_mask_bit(int p_bit, bool p_value) {
|
2021-04-30 15:58:06 +00:00
|
|
|
ERR_FAIL_INDEX_MSG(p_bit, 32, "Collision mask bit must be between 0 and 31 inclusive.");
|
2017-10-23 12:31:18 +00:00
|
|
|
uint32_t mask = get_collision_mask();
|
2021-05-05 10:44:11 +00:00
|
|
|
if (p_value) {
|
2017-10-23 12:31:18 +00:00
|
|
|
mask |= 1 << p_bit;
|
2021-05-05 10:44:11 +00:00
|
|
|
} else {
|
2017-10-23 12:31:18 +00:00
|
|
|
mask &= ~(1 << p_bit);
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-10-23 12:31:18 +00:00
|
|
|
set_collision_mask(mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RayCast::get_collision_mask_bit(int p_bit) const {
|
2021-04-30 15:58:06 +00:00
|
|
|
ERR_FAIL_INDEX_V_MSG(p_bit, 32, false, "Collision mask bit must be between 0 and 31 inclusive.");
|
2017-10-23 12:31:18 +00:00
|
|
|
return get_collision_mask() & (1 << p_bit);
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool RayCast::is_colliding() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
return collided;
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
Object *RayCast::get_collider() const {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (against == 0) {
|
2021-05-04 14:00:45 +00:00
|
|
|
return nullptr;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return ObjectDB::get_instance(against);
|
|
|
|
}
|
|
|
|
|
|
|
|
int RayCast::get_collider_shape() const {
|
|
|
|
return against_shape;
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector3 RayCast::get_collision_point() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
return collision_point;
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector3 RayCast::get_collision_normal() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
return collision_normal;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RayCast::set_enabled(bool p_enabled) {
|
2017-03-05 15:44:50 +00:00
|
|
|
enabled = p_enabled;
|
2019-08-21 13:27:02 +00:00
|
|
|
update_gizmo();
|
|
|
|
|
2021-05-05 10:44:11 +00:00
|
|
|
if (is_inside_tree() && !Engine::get_singleton()->is_editor_hint()) {
|
2018-04-11 07:28:14 +00:00
|
|
|
set_physics_process_internal(p_enabled);
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
|
|
|
if (!p_enabled) {
|
2017-03-05 15:44:50 +00:00
|
|
|
collided = false;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-03-21 16:09:04 +00:00
|
|
|
|
|
|
|
if (is_inside_tree() && get_tree()->is_debugging_collisions_hint()) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (p_enabled) {
|
2017-03-21 16:09:04 +00:00
|
|
|
_update_debug_shape();
|
2021-05-05 10:44:11 +00:00
|
|
|
} else {
|
2017-03-21 16:09:04 +00:00
|
|
|
_clear_debug_shape();
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-03-21 16:09:04 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RayCast::is_enabled() const {
|
|
|
|
return enabled;
|
|
|
|
}
|
|
|
|
|
2017-11-30 03:26:13 +00:00
|
|
|
void RayCast::set_exclude_parent_body(bool p_exclude_parent_body) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (exclude_parent_body == p_exclude_parent_body) {
|
2017-11-30 03:26:13 +00:00
|
|
|
return;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-11-30 03:26:13 +00:00
|
|
|
|
|
|
|
exclude_parent_body = p_exclude_parent_body;
|
|
|
|
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!is_inside_tree()) {
|
2017-11-30 03:26:13 +00:00
|
|
|
return;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-11-30 03:26:13 +00:00
|
|
|
|
|
|
|
if (Object::cast_to<CollisionObject>(get_parent())) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (exclude_parent_body) {
|
2017-11-30 03:26:13 +00:00
|
|
|
exclude.insert(Object::cast_to<CollisionObject>(get_parent())->get_rid());
|
2021-05-05 10:44:11 +00:00
|
|
|
} else {
|
2017-11-30 03:26:13 +00:00
|
|
|
exclude.erase(Object::cast_to<CollisionObject>(get_parent())->get_rid());
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-11-30 03:26:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RayCast::get_exclude_parent_body() const {
|
|
|
|
return exclude_parent_body;
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void RayCast::_notification(int p_what) {
|
2017-03-05 15:44:50 +00:00
|
|
|
switch (p_what) {
|
2014-11-06 00:20:42 +00:00
|
|
|
case NOTIFICATION_ENTER_TREE: {
|
2021-06-19 04:01:25 +00:00
|
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
|
|
|
_update_debug_shape_vertices();
|
|
|
|
}
|
2017-08-18 23:02:56 +00:00
|
|
|
if (enabled && !Engine::get_singleton()->is_editor_hint()) {
|
2018-04-11 07:28:14 +00:00
|
|
|
set_physics_process_internal(true);
|
2021-01-14 22:51:22 +00:00
|
|
|
} else {
|
2018-04-11 07:28:14 +00:00
|
|
|
set_physics_process_internal(false);
|
2021-01-14 22:51:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (get_tree()->is_debugging_collisions_hint()) {
|
|
|
|
_update_debug_shape();
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-11-30 03:26:13 +00:00
|
|
|
if (Object::cast_to<CollisionObject>(get_parent())) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (exclude_parent_body) {
|
2017-11-30 03:26:13 +00:00
|
|
|
exclude.insert(Object::cast_to<CollisionObject>(get_parent())->get_rid());
|
2021-05-05 10:44:11 +00:00
|
|
|
} else {
|
2017-11-30 03:26:13 +00:00
|
|
|
exclude.erase(Object::cast_to<CollisionObject>(get_parent())->get_rid());
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-11-30 03:26:13 +00:00
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
} break;
|
2014-11-06 00:20:42 +00:00
|
|
|
case NOTIFICATION_EXIT_TREE: {
|
2014-02-10 01:10:30 +00:00
|
|
|
if (enabled) {
|
2018-04-11 07:28:14 +00:00
|
|
|
set_physics_process_internal(false);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2021-05-05 10:44:11 +00:00
|
|
|
if (debug_shape) {
|
2017-03-21 16:09:04 +00:00
|
|
|
_clear_debug_shape();
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-03-21 16:09:04 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
} break;
|
2018-04-11 07:28:14 +00:00
|
|
|
case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!enabled) {
|
2014-02-10 01:10:30 +00:00
|
|
|
break;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-21 16:09:04 +00:00
|
|
|
bool prev_collision_state = collided;
|
2016-08-09 17:52:15 +00:00
|
|
|
_update_raycast_state();
|
2017-03-21 16:09:04 +00:00
|
|
|
if (prev_collision_state != collided && get_tree()->is_debugging_collisions_hint()) {
|
2021-06-19 04:01:25 +00:00
|
|
|
_update_debug_shape_material(true);
|
2017-03-21 16:09:04 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2016-08-09 17:52:15 +00:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void RayCast::_update_raycast_state() {
|
2016-08-09 17:52:15 +00:00
|
|
|
Ref<World> w3d = get_world();
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND(w3d.is_null());
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2016-08-09 17:52:15 +00:00
|
|
|
PhysicsDirectSpaceState *dss = PhysicsServer::get_singleton()->space_get_direct_state(w3d->get_space());
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND(!dss);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2016-08-09 17:52:15 +00:00
|
|
|
Transform gt = get_global_transform();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2016-08-09 17:52:15 +00:00
|
|
|
Vector3 to = cast_to;
|
2021-05-05 10:44:11 +00:00
|
|
|
if (to == Vector3()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
to = Vector3(0, 0.01, 0);
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2016-08-09 17:52:15 +00:00
|
|
|
PhysicsDirectSpaceState::RayResult rr;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-08-21 15:37:51 +00:00
|
|
|
if (dss->intersect_ray(gt.get_origin(), gt.xform(to), rr, exclude, collision_mask, collide_with_bodies, collide_with_areas)) {
|
2017-03-05 15:44:50 +00:00
|
|
|
collided = true;
|
|
|
|
against = rr.collider_id;
|
|
|
|
collision_point = rr.position;
|
|
|
|
collision_normal = rr.normal;
|
|
|
|
against_shape = rr.shape;
|
2016-08-09 17:52:15 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
collided = false;
|
2018-01-12 11:31:02 +00:00
|
|
|
against = 0;
|
|
|
|
against_shape = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-09 17:52:15 +00:00
|
|
|
void RayCast::force_raycast_update() {
|
|
|
|
_update_raycast_state();
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void RayCast::add_exception_rid(const RID &p_rid) {
|
2015-01-03 19:52:37 +00:00
|
|
|
exclude.insert(p_rid);
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void RayCast::add_exception(const Object *p_object) {
|
2015-01-03 19:52:37 +00:00
|
|
|
ERR_FAIL_NULL(p_object);
|
2017-08-24 20:58:51 +00:00
|
|
|
const CollisionObject *co = Object::cast_to<CollisionObject>(p_object);
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!co) {
|
2015-01-03 19:52:37 +00:00
|
|
|
return;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2015-01-03 19:52:37 +00:00
|
|
|
add_exception_rid(co->get_rid());
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void RayCast::remove_exception_rid(const RID &p_rid) {
|
2015-01-03 19:52:37 +00:00
|
|
|
exclude.erase(p_rid);
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void RayCast::remove_exception(const Object *p_object) {
|
2015-01-03 19:52:37 +00:00
|
|
|
ERR_FAIL_NULL(p_object);
|
2017-08-24 20:58:51 +00:00
|
|
|
const CollisionObject *co = Object::cast_to<CollisionObject>(p_object);
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!co) {
|
2015-01-03 19:52:37 +00:00
|
|
|
return;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2015-01-03 19:52:37 +00:00
|
|
|
remove_exception_rid(co->get_rid());
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void RayCast::clear_exceptions() {
|
2015-01-03 19:52:37 +00:00
|
|
|
exclude.clear();
|
|
|
|
}
|
|
|
|
|
2018-08-21 18:30:41 +00: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 01:10:30 +00:00
|
|
|
void RayCast::_bind_methods() {
|
2017-03-05 15:44:50 +00: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 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00: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 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00: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 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00: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 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00: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 19:52:37 +00:00
|
|
|
|
2017-03-05 15:44:50 +00: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 19:52:37 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("clear_exceptions"), &RayCast::clear_exceptions);
|
2015-01-03 19:52:37 +00:00
|
|
|
|
2017-10-21 20:17:26 +00: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 18:54:09 +00:00
|
|
|
|
2017-10-23 12:31:18 +00: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 03:26:13 +00: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 18:30:41 +00: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 04:01:25 +00: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 15:44:50 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled");
|
2017-11-30 03:26:13 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "exclude_parent"), "set_exclude_parent_body", "get_exclude_parent_body");
|
2017-03-05 15:44:50 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "cast_to"), "set_cast_to", "get_cast_to");
|
2017-10-21 20:17:26 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask");
|
2018-08-21 18:30:41 +00: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 04:01:25 +00: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 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2021-06-19 04:01:25 +00:00
|
|
|
float RayCast::get_debug_shape_thickness() const {
|
|
|
|
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)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RayCast::set_debug_shape_thickness(const float p_debug_shape_thickness) {
|
|
|
|
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 16:09:04 +00:00
|
|
|
|
2021-06-19 04:01:25 +00: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 16:09:04 +00:00
|
|
|
}
|
2021-06-19 04:01:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ref<SpatialMaterial> RayCast::get_debug_material() {
|
|
|
|
_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 16:09:04 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
Ref<ArrayMesh> mesh = memnew(ArrayMesh);
|
2017-03-21 16:09:04 +00:00
|
|
|
|
|
|
|
MeshInstance *mi = memnew(MeshInstance);
|
|
|
|
mi->set_mesh(mesh);
|
|
|
|
add_child(mi);
|
2021-06-19 04:01:25 +00:00
|
|
|
|
2017-03-21 16:09:04 +00:00
|
|
|
debug_shape = mi;
|
|
|
|
}
|
|
|
|
|
2021-06-19 04:01:25 +00:00
|
|
|
void RayCast::_update_debug_shape_material(bool p_check_collision) {
|
|
|
|
if (!debug_material.is_valid()) {
|
|
|
|
Ref<SpatialMaterial> material = memnew(SpatialMaterial);
|
|
|
|
debug_material = material;
|
|
|
|
|
|
|
|
material->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
|
|
|
|
material->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
|
2021-06-19 07:12:58 +00:00
|
|
|
// Use double-sided rendering so that the RayCast can be seen if the camera is inside.
|
|
|
|
material->set_cull_mode(SpatialMaterial::CULL_DISABLED);
|
2021-06-19 04:01:25 +00: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();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p_check_collision) {
|
|
|
|
if ((color.get_h() < 0.055 || color.get_h() > 0.945) && color.get_s() > 0.5 && color.get_v() > 0.5) {
|
|
|
|
// If base color is already quite reddish, hightlight collision with green color
|
|
|
|
color = Color(0.0, 1.0, 0.0, color.a);
|
|
|
|
} else {
|
|
|
|
// Else, hightlight collision with red color
|
|
|
|
color = Color(1.0, 0, 0, color.a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<SpatialMaterial> material = static_cast<Ref<SpatialMaterial>>(debug_material);
|
|
|
|
material->set_albedo(color);
|
|
|
|
}
|
|
|
|
|
2017-03-21 16:09:04 +00:00
|
|
|
void RayCast::_update_debug_shape() {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!enabled) {
|
2017-03-21 16:09:04 +00:00
|
|
|
return;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-03-21 16:09:04 +00:00
|
|
|
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!debug_shape) {
|
2017-03-21 16:09:04 +00:00
|
|
|
_create_debug_shape();
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-03-21 16:09:04 +00:00
|
|
|
|
|
|
|
MeshInstance *mi = static_cast<MeshInstance *>(debug_shape);
|
2017-06-07 21:18:55 +00:00
|
|
|
Ref<ArrayMesh> mesh = mi->get_mesh();
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!mesh.is_valid()) {
|
2021-01-14 22:51:22 +00:00
|
|
|
return;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-03-21 16:09:04 +00:00
|
|
|
|
2021-06-19 04:01:25 +00:00
|
|
|
_update_debug_shape_vertices();
|
2017-03-21 16:09:04 +00:00
|
|
|
|
2021-06-19 04:01:25 +00:00
|
|
|
mesh->clear_surfaces();
|
2021-01-14 22:51:22 +00:00
|
|
|
|
2021-06-19 04:01:25 +00:00
|
|
|
Array a;
|
|
|
|
a.resize(Mesh::ARRAY_MAX);
|
2021-01-14 22:51:22 +00:00
|
|
|
|
2021-06-19 04:01:25 +00:00
|
|
|
uint32_t flags = 0;
|
|
|
|
int surface_count = 0;
|
|
|
|
|
|
|
|
if (!debug_line_vertices.empty()) {
|
|
|
|
a[Mesh::ARRAY_VERTEX] = debug_line_vertices;
|
2021-01-14 22:51:22 +00:00
|
|
|
mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, a, Array(), flags);
|
2021-06-19 04:01:25 +00:00
|
|
|
mesh->surface_set_material(surface_count, debug_material);
|
|
|
|
++surface_count;
|
|
|
|
}
|
2021-01-14 22:51:22 +00:00
|
|
|
|
2021-06-19 04:01:25 +00: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 22:51:22 +00:00
|
|
|
}
|
2017-03-21 16:09:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RayCast::_clear_debug_shape() {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!debug_shape) {
|
2017-03-21 16:09:04 +00:00
|
|
|
return;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-03-21 16:09:04 +00:00
|
|
|
|
|
|
|
MeshInstance *mi = static_cast<MeshInstance *>(debug_shape);
|
2021-05-05 10:44:11 +00:00
|
|
|
if (mi->is_inside_tree()) {
|
2017-03-21 16:09:04 +00:00
|
|
|
mi->queue_delete();
|
2021-05-05 10:44:11 +00:00
|
|
|
} else {
|
2017-03-21 16:09:04 +00:00
|
|
|
memdelete(mi);
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-03-21 16:09:04 +00:00
|
|
|
|
2021-05-04 14:00:45 +00:00
|
|
|
debug_shape = nullptr;
|
2017-03-21 16:09:04 +00:00
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
RayCast::RayCast() {
|
2017-03-05 15:44:50 +00:00
|
|
|
enabled = false;
|
|
|
|
against = 0;
|
|
|
|
collided = false;
|
|
|
|
against_shape = 0;
|
2017-10-21 20:17:26 +00:00
|
|
|
collision_mask = 1;
|
2017-03-05 15:44:50 +00:00
|
|
|
cast_to = Vector3(0, -1, 0);
|
2021-05-04 14:00:45 +00:00
|
|
|
debug_shape = nullptr;
|
2017-11-30 03:26:13 +00:00
|
|
|
exclude_parent_body = true;
|
2018-08-21 18:30:41 +00:00
|
|
|
collide_with_areas = false;
|
|
|
|
collide_with_bodies = true;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|