2018-05-13 19:27:56 +00:00
|
|
|
/*************************************************************************/
|
2020-03-26 21:49:16 +00:00
|
|
|
/* spring_arm_3d.cpp */
|
2018-05-13 19:27:56 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
2022-01-03 20:27:34 +00:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2018-05-13 19:27:56 +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. */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
2020-03-26 21:49:16 +00:00
|
|
|
#include "spring_arm_3d.h"
|
2022-02-15 17:06:48 +00:00
|
|
|
|
2021-10-03 11:51:25 +00:00
|
|
|
#include "scene/3d/camera_3d.h"
|
2020-05-12 15:01:17 +00:00
|
|
|
|
2020-03-26 21:49:16 +00:00
|
|
|
void SpringArm3D::_notification(int p_what) {
|
2018-05-13 19:27:56 +00:00
|
|
|
switch (p_what) {
|
2022-02-15 17:06:48 +00:00
|
|
|
case NOTIFICATION_ENTER_TREE: {
|
2018-05-13 19:27:56 +00:00
|
|
|
if (!Engine::get_singleton()->is_editor_hint()) {
|
2020-04-17 14:22:24 +00:00
|
|
|
set_physics_process_internal(true);
|
2018-05-13 19:27:56 +00:00
|
|
|
}
|
2022-02-15 17:06:48 +00:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case NOTIFICATION_EXIT_TREE: {
|
2018-05-13 19:27:56 +00:00
|
|
|
if (!Engine::get_singleton()->is_editor_hint()) {
|
2020-04-17 14:22:24 +00:00
|
|
|
set_physics_process_internal(false);
|
2018-05-13 19:27:56 +00:00
|
|
|
}
|
2022-02-15 17:06:48 +00:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
|
2018-05-13 19:27:56 +00:00
|
|
|
process_spring();
|
2022-02-15 17:06:48 +00:00
|
|
|
} break;
|
2018-05-13 19:27:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-26 21:49:16 +00:00
|
|
|
void SpringArm3D::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("get_hit_length"), &SpringArm3D::get_hit_length);
|
2018-05-13 19:27:56 +00:00
|
|
|
|
2020-03-26 21:49:16 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_length", "length"), &SpringArm3D::set_length);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_length"), &SpringArm3D::get_length);
|
2018-05-13 19:27:56 +00:00
|
|
|
|
2020-03-26 21:49:16 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_shape", "shape"), &SpringArm3D::set_shape);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_shape"), &SpringArm3D::get_shape);
|
2018-05-13 19:27:56 +00:00
|
|
|
|
2020-03-26 21:49:16 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("add_excluded_object", "RID"), &SpringArm3D::add_excluded_object);
|
|
|
|
ClassDB::bind_method(D_METHOD("remove_excluded_object", "RID"), &SpringArm3D::remove_excluded_object);
|
|
|
|
ClassDB::bind_method(D_METHOD("clear_excluded_objects"), &SpringArm3D::clear_excluded_objects);
|
2018-05-13 19:27:56 +00:00
|
|
|
|
2020-03-26 21:49:16 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &SpringArm3D::set_mask);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_collision_mask"), &SpringArm3D::get_mask);
|
2018-05-13 19:27:56 +00:00
|
|
|
|
2020-03-26 21:49:16 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_margin", "margin"), &SpringArm3D::set_margin);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_margin"), &SpringArm3D::get_margin);
|
2018-05-13 19:27:56 +00:00
|
|
|
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask");
|
2020-03-26 21:49:16 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shape", PROPERTY_HINT_RESOURCE_TYPE, "Shape3D"), "set_shape", "get_shape");
|
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 18:20:53 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "spring_length"), "set_length", "get_length");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "margin"), "set_margin", "get_margin");
|
2018-05-13 19:27:56 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 23:22:12 +00:00
|
|
|
real_t SpringArm3D::get_length() const {
|
2018-05-13 19:27:56 +00:00
|
|
|
return spring_length;
|
|
|
|
}
|
|
|
|
|
2021-01-29 23:22:12 +00:00
|
|
|
void SpringArm3D::set_length(real_t p_length) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (is_inside_tree() && (Engine::get_singleton()->is_editor_hint() || get_tree()->is_debugging_collisions_hint())) {
|
2021-06-23 14:49:50 +00:00
|
|
|
update_gizmos();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-05-13 19:27:56 +00:00
|
|
|
|
|
|
|
spring_length = p_length;
|
|
|
|
}
|
|
|
|
|
2020-03-26 21:49:16 +00:00
|
|
|
void SpringArm3D::set_shape(Ref<Shape3D> p_shape) {
|
2018-05-13 19:27:56 +00:00
|
|
|
shape = p_shape;
|
|
|
|
}
|
|
|
|
|
2020-03-26 21:49:16 +00:00
|
|
|
Ref<Shape3D> SpringArm3D::get_shape() const {
|
2018-05-13 19:27:56 +00:00
|
|
|
return shape;
|
|
|
|
}
|
|
|
|
|
2020-03-26 21:49:16 +00:00
|
|
|
void SpringArm3D::set_mask(uint32_t p_mask) {
|
2018-05-13 19:27:56 +00:00
|
|
|
mask = p_mask;
|
|
|
|
}
|
|
|
|
|
2020-03-26 21:49:16 +00:00
|
|
|
uint32_t SpringArm3D::get_mask() {
|
2018-05-13 19:27:56 +00:00
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
2021-01-29 23:22:12 +00:00
|
|
|
real_t SpringArm3D::get_margin() {
|
2018-05-13 19:27:56 +00:00
|
|
|
return margin;
|
|
|
|
}
|
|
|
|
|
2021-01-29 23:22:12 +00:00
|
|
|
void SpringArm3D::set_margin(real_t p_margin) {
|
2018-05-13 19:27:56 +00:00
|
|
|
margin = p_margin;
|
|
|
|
}
|
|
|
|
|
2020-03-26 21:49:16 +00:00
|
|
|
void SpringArm3D::add_excluded_object(RID p_rid) {
|
2018-05-13 19:27:56 +00:00
|
|
|
excluded_objects.insert(p_rid);
|
|
|
|
}
|
|
|
|
|
2020-03-26 21:49:16 +00:00
|
|
|
bool SpringArm3D::remove_excluded_object(RID p_rid) {
|
2018-05-13 19:27:56 +00:00
|
|
|
return excluded_objects.erase(p_rid);
|
|
|
|
}
|
|
|
|
|
2020-03-26 21:49:16 +00:00
|
|
|
void SpringArm3D::clear_excluded_objects() {
|
2018-05-13 19:27:56 +00:00
|
|
|
excluded_objects.clear();
|
|
|
|
}
|
|
|
|
|
2021-01-29 23:22:12 +00:00
|
|
|
real_t SpringArm3D::get_hit_length() {
|
2018-05-13 19:27:56 +00:00
|
|
|
return current_spring_length;
|
|
|
|
}
|
|
|
|
|
2020-03-26 21:49:16 +00:00
|
|
|
void SpringArm3D::process_spring() {
|
2018-05-13 19:27:56 +00:00
|
|
|
// From
|
|
|
|
real_t motion_delta(1);
|
|
|
|
real_t motion_delta_unsafe(1);
|
|
|
|
|
|
|
|
Vector3 motion;
|
|
|
|
const Vector3 cast_direction(get_global_transform().basis.xform(Vector3(0, 0, 1)));
|
|
|
|
|
2021-10-03 11:51:25 +00:00
|
|
|
motion = Vector3(cast_direction * (spring_length));
|
|
|
|
|
2018-05-13 19:27:56 +00:00
|
|
|
if (shape.is_null()) {
|
2021-10-03 11:51:25 +00:00
|
|
|
Camera3D *camera = nullptr;
|
|
|
|
for (int i = get_child_count() - 1; 0 <= i; --i) {
|
|
|
|
camera = Object::cast_to<Camera3D>(get_child(i));
|
|
|
|
if (camera) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (camera != nullptr) {
|
|
|
|
//use camera rotation, but spring arm position
|
|
|
|
Transform3D base_transform = camera->get_global_transform();
|
|
|
|
base_transform.origin = get_global_transform().origin;
|
2021-11-02 01:00:58 +00:00
|
|
|
|
|
|
|
PhysicsDirectSpaceState3D::ShapeParameters shape_params;
|
|
|
|
shape_params.shape_rid = camera->get_pyramid_shape_rid();
|
|
|
|
shape_params.transform = base_transform;
|
|
|
|
shape_params.motion = motion;
|
|
|
|
shape_params.exclude = excluded_objects;
|
|
|
|
shape_params.collision_mask = mask;
|
|
|
|
|
|
|
|
get_world_3d()->get_direct_space_state()->cast_motion(shape_params, motion_delta, motion_delta_unsafe);
|
2021-10-03 11:51:25 +00:00
|
|
|
} else {
|
2021-11-02 01:00:58 +00:00
|
|
|
PhysicsDirectSpaceState3D::RayParameters ray_params;
|
|
|
|
ray_params.from = get_global_transform().origin;
|
|
|
|
ray_params.to = get_global_transform().origin + motion;
|
|
|
|
ray_params.exclude = excluded_objects;
|
|
|
|
ray_params.collision_mask = mask;
|
|
|
|
|
2021-10-03 11:51:25 +00:00
|
|
|
PhysicsDirectSpaceState3D::RayResult r;
|
2021-11-02 01:00:58 +00:00
|
|
|
bool intersected = get_world_3d()->get_direct_space_state()->intersect_ray(ray_params, r);
|
2021-10-03 11:51:25 +00:00
|
|
|
if (intersected) {
|
|
|
|
real_t dist = get_global_transform().origin.distance_to(r.position);
|
|
|
|
dist -= margin;
|
|
|
|
motion_delta = dist / (spring_length);
|
|
|
|
}
|
2018-05-13 19:27:56 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-11-02 01:00:58 +00:00
|
|
|
PhysicsDirectSpaceState3D::ShapeParameters shape_params;
|
|
|
|
shape_params.shape_rid = shape->get_rid();
|
|
|
|
shape_params.transform = get_global_transform();
|
|
|
|
shape_params.motion = motion;
|
|
|
|
shape_params.exclude = excluded_objects;
|
|
|
|
shape_params.collision_mask = mask;
|
|
|
|
|
|
|
|
get_world_3d()->get_direct_space_state()->cast_motion(shape_params, motion_delta, motion_delta_unsafe);
|
2018-05-13 19:27:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
current_spring_length = spring_length * motion_delta;
|
2022-02-10 11:00:11 +00:00
|
|
|
Transform3D child_transform;
|
|
|
|
child_transform.origin = get_global_transform().origin + cast_direction * (spring_length * motion_delta);
|
2018-05-13 19:27:56 +00:00
|
|
|
|
|
|
|
for (int i = get_child_count() - 1; 0 <= i; --i) {
|
2020-03-26 21:49:16 +00:00
|
|
|
Node3D *child = Object::cast_to<Node3D>(get_child(i));
|
2018-05-13 19:27:56 +00:00
|
|
|
if (child) {
|
2022-02-10 11:00:11 +00:00
|
|
|
child_transform.basis = child->get_global_transform().basis;
|
|
|
|
child->set_global_transform(child_transform);
|
2018-05-13 19:27:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|