From 285b816f1fc2a52eae8d9e27e70d499fb26428c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steve=20Szil=C3=A1gyi?= Date: Wed, 8 Dec 2021 12:10:56 +0100 Subject: [PATCH] VehicleWheel can now return the surface it's colliding with. Fixed PR issues. Update vehicle_body_3d.cpp Apply suggestions from code review Co-authored-by: Camille Mohr-Daurat (cherry picked from commit 0c352407d8e7d3c8eb3108a734b5a9746ea14ad7) --- doc/classes/VehicleWheel.xml | 7 +++++++ scene/3d/vehicle_body.cpp | 9 +++++++-- scene/3d/vehicle_body.h | 2 ++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/doc/classes/VehicleWheel.xml b/doc/classes/VehicleWheel.xml index ef3418b419c..f9ee35d61e3 100644 --- a/doc/classes/VehicleWheel.xml +++ b/doc/classes/VehicleWheel.xml @@ -11,6 +11,13 @@ https://godotengine.org/asset-library/asset/524 + + + + Returns the contacting body node if valid in the tree, as [Spatial]. At the moment, [GridMap] is not supported so the node will be always of type [PhysicsBody]. + Returns [code]null[/code] if the wheel is not in contact with a surface, or the contact body is not a [PhysicsBody]. + + diff --git a/scene/3d/vehicle_body.cpp b/scene/3d/vehicle_body.cpp index 7d814f74526..4f55783ded8 100644 --- a/scene/3d/vehicle_body.cpp +++ b/scene/3d/vehicle_body.cpp @@ -217,6 +217,10 @@ bool VehicleWheel::is_in_contact() const { return m_raycastInfo.m_isInContact; } +Spatial *VehicleWheel::get_contact_body() const { + return m_raycastInfo.m_groundObject; +} + void VehicleWheel::_bind_methods() { ClassDB::bind_method(D_METHOD("set_radius", "length"), &VehicleWheel::set_radius); ClassDB::bind_method(D_METHOD("get_radius"), &VehicleWheel::get_radius); @@ -249,6 +253,7 @@ void VehicleWheel::_bind_methods() { ClassDB::bind_method(D_METHOD("get_friction_slip"), &VehicleWheel::get_friction_slip); ClassDB::bind_method(D_METHOD("is_in_contact"), &VehicleWheel::is_in_contact); + ClassDB::bind_method(D_METHOD("get_contact_body"), &VehicleWheel::get_contact_body); ClassDB::bind_method(D_METHOD("set_roll_influence", "roll_influence"), &VehicleWheel::set_roll_influence); ClassDB::bind_method(D_METHOD("get_roll_influence"), &VehicleWheel::get_roll_influence); @@ -357,6 +362,7 @@ VehicleWheel::VehicleWheel() { m_suspensionRelativeVelocity = 0; m_clippedInvContactDotSuspension = 1.0; m_raycastInfo.m_isInContact = false; + m_raycastInfo.m_groundObject = nullptr; m_raycastInfo.m_suspensionLength = 0.0; body = nullptr; @@ -423,9 +429,8 @@ real_t VehicleBody::_ray_cast(int p_idx, PhysicsDirectBodyState *s) { PhysicsDirectSpaceState *ss = s->get_space_state(); - bool col = ss->intersect_ray(source, target, rr, exclude, get_collision_mask()); - wheel.m_raycastInfo.m_groundObject = nullptr; + bool col = ss->intersect_ray(source, target, rr, exclude, get_collision_mask()); if (col) { param = source.distance_to(rr.position) / source.distance_to(target); diff --git a/scene/3d/vehicle_body.h b/scene/3d/vehicle_body.h index f8de693856c..b3eed665113 100644 --- a/scene/3d/vehicle_body.h +++ b/scene/3d/vehicle_body.h @@ -129,6 +129,8 @@ public: bool is_in_contact() const; + Spatial *get_contact_body() const; + void set_roll_influence(float p_value); float get_roll_influence() const;