| 1 | /**************************************************************************/ |
| 2 | /* jolt_physics_direct_body_state_3d.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "jolt_physics_direct_body_state_3d.h" |
| 32 | |
| 33 | #include "../spaces/jolt_physics_direct_space_state_3d.h" |
| 34 | #include "../spaces/jolt_space_3d.h" |
| 35 | #include "jolt_body_3d.h" |
| 36 | |
| 37 | JoltPhysicsDirectBodyState3D::JoltPhysicsDirectBodyState3D(JoltBody3D *p_body) : |
| 38 | body(p_body) { |
| 39 | } |
| 40 | |
| 41 | Vector3 JoltPhysicsDirectBodyState3D::get_total_gravity() const { |
| 42 | return body->get_gravity(); |
| 43 | } |
| 44 | |
| 45 | real_t JoltPhysicsDirectBodyState3D::get_total_angular_damp() const { |
| 46 | return (real_t)body->get_total_angular_damp(); |
| 47 | } |
| 48 | |
| 49 | real_t JoltPhysicsDirectBodyState3D::get_total_linear_damp() const { |
| 50 | return (real_t)body->get_total_linear_damp(); |
| 51 | } |
| 52 | |
| 53 | Vector3 JoltPhysicsDirectBodyState3D::get_center_of_mass() const { |
| 54 | return body->get_center_of_mass_relative(); |
| 55 | } |
| 56 | |
| 57 | Vector3 JoltPhysicsDirectBodyState3D::get_center_of_mass_local() const { |
| 58 | return body->get_center_of_mass_local(); |
| 59 | } |
| 60 | |
| 61 | Basis JoltPhysicsDirectBodyState3D::get_principal_inertia_axes() const { |
| 62 | return body->get_principal_inertia_axes(); |
| 63 | } |
| 64 | |
| 65 | real_t JoltPhysicsDirectBodyState3D::get_inverse_mass() const { |
| 66 | return 1.0 / body->get_mass(); |
| 67 | } |
| 68 | |
| 69 | Vector3 JoltPhysicsDirectBodyState3D::get_inverse_inertia() const { |
| 70 | return body->get_inverse_inertia(); |
| 71 | } |
| 72 | |
| 73 | Basis JoltPhysicsDirectBodyState3D::get_inverse_inertia_tensor() const { |
| 74 | return body->get_inverse_inertia_tensor(); |
| 75 | } |
| 76 | |
| 77 | Vector3 JoltPhysicsDirectBodyState3D::get_linear_velocity() const { |
| 78 | return body->get_linear_velocity(); |
| 79 | } |
| 80 | |
| 81 | void JoltPhysicsDirectBodyState3D::set_linear_velocity(const Vector3 &p_velocity) { |
| 82 | return body->set_linear_velocity(p_velocity); |
| 83 | } |
| 84 | |
| 85 | Vector3 JoltPhysicsDirectBodyState3D::get_angular_velocity() const { |
| 86 | return body->get_angular_velocity(); |
| 87 | } |
| 88 | |
| 89 | void JoltPhysicsDirectBodyState3D::set_angular_velocity(const Vector3 &p_velocity) { |
| 90 | return body->set_angular_velocity(p_velocity); |
| 91 | } |
| 92 | |
| 93 | void JoltPhysicsDirectBodyState3D::set_transform(const Transform3D &p_transform) { |
| 94 | return body->set_transform(p_transform); |
| 95 | } |
| 96 | |
| 97 | Transform3D JoltPhysicsDirectBodyState3D::get_transform() const { |
| 98 | return body->get_transform_scaled(); |
| 99 | } |
| 100 | |
| 101 | Vector3 JoltPhysicsDirectBodyState3D::get_velocity_at_local_position(const Vector3 &p_local_position) const { |
| 102 | return body->get_velocity_at_position(body->get_position() + p_local_position); |
| 103 | } |
| 104 | |
| 105 | void JoltPhysicsDirectBodyState3D::apply_central_impulse(const Vector3 &p_impulse) { |
| 106 | return body->apply_central_impulse(p_impulse); |
| 107 | } |
| 108 | |
| 109 | void JoltPhysicsDirectBodyState3D::apply_impulse(const Vector3 &p_impulse, const Vector3 &p_position) { |
| 110 | return body->apply_impulse(p_impulse, p_position); |
| 111 | } |
| 112 | |
| 113 | void JoltPhysicsDirectBodyState3D::apply_torque_impulse(const Vector3 &p_impulse) { |
| 114 | return body->apply_torque_impulse(p_impulse); |
| 115 | } |
| 116 | |
| 117 | void JoltPhysicsDirectBodyState3D::apply_central_force(const Vector3 &p_force) { |
| 118 | return body->apply_central_force(p_force); |
| 119 | } |
| 120 | |
| 121 | void JoltPhysicsDirectBodyState3D::apply_force(const Vector3 &p_force, const Vector3 &p_position) { |
| 122 | return body->apply_force(p_force, p_position); |
| 123 | } |
| 124 | |
| 125 | void JoltPhysicsDirectBodyState3D::apply_torque(const Vector3 &p_torque) { |
| 126 | return body->apply_torque(p_torque); |
| 127 | } |
| 128 | |
| 129 | void JoltPhysicsDirectBodyState3D::add_constant_central_force(const Vector3 &p_force) { |
| 130 | return body->add_constant_central_force(p_force); |
| 131 | } |
| 132 | |
| 133 | void JoltPhysicsDirectBodyState3D::add_constant_force(const Vector3 &p_force, const Vector3 &p_position) { |
| 134 | return body->add_constant_force(p_force, p_position); |
| 135 | } |
| 136 | |
| 137 | void JoltPhysicsDirectBodyState3D::add_constant_torque(const Vector3 &p_torque) { |
| 138 | return body->add_constant_torque(p_torque); |
| 139 | } |
| 140 | |
| 141 | Vector3 JoltPhysicsDirectBodyState3D::get_constant_force() const { |
| 142 | return body->get_constant_force(); |
| 143 | } |
| 144 | |
| 145 | void JoltPhysicsDirectBodyState3D::set_constant_force(const Vector3 &p_force) { |
| 146 | return body->set_constant_force(p_force); |
| 147 | } |
| 148 | |
| 149 | Vector3 JoltPhysicsDirectBodyState3D::get_constant_torque() const { |
| 150 | return body->get_constant_torque(); |
| 151 | } |
| 152 | |
| 153 | void JoltPhysicsDirectBodyState3D::set_constant_torque(const Vector3 &p_torque) { |
| 154 | return body->set_constant_torque(p_torque); |
| 155 | } |
| 156 | |
| 157 | bool JoltPhysicsDirectBodyState3D::is_sleeping() const { |
| 158 | return body->is_sleeping(); |
| 159 | } |
| 160 | |
| 161 | void JoltPhysicsDirectBodyState3D::set_sleep_state(bool p_enabled) { |
| 162 | body->set_is_sleeping(p_enabled); |
| 163 | } |
| 164 | |
| 165 | void JoltPhysicsDirectBodyState3D::set_collision_layer(uint32_t p_layer) { |
| 166 | body->set_collision_layer(p_layer); |
| 167 | } |
| 168 | |
| 169 | uint32_t JoltPhysicsDirectBodyState3D::get_collision_layer() const { |
| 170 | return body->get_collision_layer(); |
| 171 | } |
| 172 | |
| 173 | void JoltPhysicsDirectBodyState3D::set_collision_mask(uint32_t p_mask) { |
| 174 | body->set_collision_mask(p_mask); |
| 175 | } |
| 176 | |
| 177 | uint32_t JoltPhysicsDirectBodyState3D::get_collision_mask() const { |
| 178 | return body->get_collision_mask(); |
| 179 | } |
| 180 | |
| 181 | int JoltPhysicsDirectBodyState3D::get_contact_count() const { |
| 182 | return body->get_contact_count(); |
| 183 | } |
| 184 | |
| 185 | Vector3 JoltPhysicsDirectBodyState3D::get_contact_local_position(int p_contact_idx) const { |
| 186 | ERR_FAIL_INDEX_V(p_contact_idx, (int)body->get_contact_count(), Vector3()); |
| 187 | return body->get_contact(p_contact_idx).position; |
| 188 | } |
| 189 | |
| 190 | Vector3 JoltPhysicsDirectBodyState3D::get_contact_local_normal(int p_contact_idx) const { |
| 191 | ERR_FAIL_INDEX_V(p_contact_idx, (int)body->get_contact_count(), Vector3()); |
| 192 | return body->get_contact(p_contact_idx).normal; |
| 193 | } |
| 194 | |
| 195 | Vector3 JoltPhysicsDirectBodyState3D::get_contact_impulse(int p_contact_idx) const { |
| 196 | ERR_FAIL_INDEX_V(p_contact_idx, (int)body->get_contact_count(), Vector3()); |
| 197 | return body->get_contact(p_contact_idx).impulse; |
| 198 | } |
| 199 | |
| 200 | int JoltPhysicsDirectBodyState3D::get_contact_local_shape(int p_contact_idx) const { |
| 201 | ERR_FAIL_INDEX_V(p_contact_idx, (int)body->get_contact_count(), 0); |
| 202 | return body->get_contact(p_contact_idx).shape_index; |
| 203 | } |
| 204 | |
| 205 | Vector3 JoltPhysicsDirectBodyState3D::get_contact_local_velocity_at_position(int p_contact_idx) const { |
| 206 | ERR_FAIL_INDEX_V(p_contact_idx, (int)body->get_contact_count(), Vector3()); |
| 207 | return body->get_contact(p_contact_idx).velocity; |
| 208 | } |
| 209 | |
| 210 | RID JoltPhysicsDirectBodyState3D::get_contact_collider(int p_contact_idx) const { |
| 211 | ERR_FAIL_INDEX_V(p_contact_idx, (int)body->get_contact_count(), RID()); |
| 212 | return body->get_contact(p_contact_idx).collider_rid; |
| 213 | } |
| 214 | |
| 215 | Vector3 JoltPhysicsDirectBodyState3D::get_contact_collider_position(int p_contact_idx) const { |
| 216 | ERR_FAIL_INDEX_V(p_contact_idx, (int)body->get_contact_count(), Vector3()); |
| 217 | return body->get_contact(p_contact_idx).collider_position; |
| 218 | } |
| 219 | |
| 220 | ObjectID JoltPhysicsDirectBodyState3D::get_contact_collider_id(int p_contact_idx) const { |
| 221 | ERR_FAIL_INDEX_V(p_contact_idx, (int)body->get_contact_count(), ObjectID()); |
| 222 | return body->get_contact(p_contact_idx).collider_id; |
| 223 | } |
| 224 | |
| 225 | Object *JoltPhysicsDirectBodyState3D::get_contact_collider_object(int p_contact_idx) const { |
| 226 | ERR_FAIL_INDEX_V(p_contact_idx, (int)body->get_contact_count(), nullptr); |
| 227 | return ObjectDB::get_instance(body->get_contact(p_contact_idx).collider_id); |
| 228 | } |
| 229 | |
| 230 | int JoltPhysicsDirectBodyState3D::get_contact_collider_shape(int p_contact_idx) const { |
| 231 | ERR_FAIL_INDEX_V(p_contact_idx, (int)body->get_contact_count(), 0); |
| 232 | return body->get_contact(p_contact_idx).collider_shape_index; |
| 233 | } |
| 234 | |
| 235 | Vector3 JoltPhysicsDirectBodyState3D::get_contact_collider_velocity_at_position(int p_contact_idx) const { |
| 236 | ERR_FAIL_INDEX_V(p_contact_idx, (int)body->get_contact_count(), Vector3()); |
| 237 | return body->get_contact(p_contact_idx).collider_velocity; |
| 238 | } |
| 239 | |
| 240 | real_t JoltPhysicsDirectBodyState3D::get_step() const { |
| 241 | return (real_t)body->get_space()->get_last_step(); |
| 242 | } |
| 243 | |
| 244 | void JoltPhysicsDirectBodyState3D::integrate_forces() { |
| 245 | const float step = (float)get_step(); |
| 246 | |
| 247 | Vector3 linear_velocity = get_linear_velocity(); |
| 248 | Vector3 angular_velocity = get_angular_velocity(); |
| 249 | |
| 250 | linear_velocity *= MAX(1.0f - (float)get_total_linear_damp() * step, 0.0f); |
| 251 | angular_velocity *= MAX(1.0f - (float)get_total_angular_damp() * step, 0.0f); |
| 252 | |
| 253 | linear_velocity += get_total_gravity() * step; |
| 254 | |
| 255 | set_linear_velocity(linear_velocity); |
| 256 | set_angular_velocity(angular_velocity); |
| 257 | } |
| 258 | |
| 259 | RequiredResult<PhysicsDirectSpaceState3D> JoltPhysicsDirectBodyState3D::get_space_state() { |
| 260 | return body->get_space()->get_direct_state(); |
| 261 | } |