| 1 | /**************************************************************************/ | 
|---|
| 2 | /*  godot_body_direct_state_2d.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 "godot_body_direct_state_2d.h" | 
|---|
| 32 |  | 
|---|
| 33 | #include "godot_body_2d.h" | 
|---|
| 34 | #include "godot_physics_server_2d.h" | 
|---|
| 35 | #include "godot_space_2d.h" | 
|---|
| 36 |  | 
|---|
| 37 | Vector2 GodotPhysicsDirectBodyState2D::get_total_gravity() const { | 
|---|
| 38 | return body->gravity; | 
|---|
| 39 | } | 
|---|
| 40 |  | 
|---|
| 41 | real_t GodotPhysicsDirectBodyState2D::get_total_angular_damp() const { | 
|---|
| 42 | return body->total_angular_damp; | 
|---|
| 43 | } | 
|---|
| 44 |  | 
|---|
| 45 | real_t GodotPhysicsDirectBodyState2D::get_total_linear_damp() const { | 
|---|
| 46 | return body->total_linear_damp; | 
|---|
| 47 | } | 
|---|
| 48 |  | 
|---|
| 49 | Vector2 GodotPhysicsDirectBodyState2D::get_center_of_mass() const { | 
|---|
| 50 | return body->get_center_of_mass(); | 
|---|
| 51 | } | 
|---|
| 52 |  | 
|---|
| 53 | Vector2 GodotPhysicsDirectBodyState2D::get_center_of_mass_local() const { | 
|---|
| 54 | return body->get_center_of_mass_local(); | 
|---|
| 55 | } | 
|---|
| 56 |  | 
|---|
| 57 | real_t GodotPhysicsDirectBodyState2D::get_inverse_mass() const { | 
|---|
| 58 | return body->get_inv_mass(); | 
|---|
| 59 | } | 
|---|
| 60 |  | 
|---|
| 61 | real_t GodotPhysicsDirectBodyState2D::get_inverse_inertia() const { | 
|---|
| 62 | return body->get_inv_inertia(); | 
|---|
| 63 | } | 
|---|
| 64 |  | 
|---|
| 65 | void GodotPhysicsDirectBodyState2D::set_linear_velocity(const Vector2 &p_velocity) { | 
|---|
| 66 | body->wakeup(); | 
|---|
| 67 | body->set_linear_velocity(p_velocity); | 
|---|
| 68 | } | 
|---|
| 69 |  | 
|---|
| 70 | Vector2 GodotPhysicsDirectBodyState2D::get_linear_velocity() const { | 
|---|
| 71 | return body->get_linear_velocity(); | 
|---|
| 72 | } | 
|---|
| 73 |  | 
|---|
| 74 | void GodotPhysicsDirectBodyState2D::set_angular_velocity(real_t p_velocity) { | 
|---|
| 75 | body->wakeup(); | 
|---|
| 76 | body->set_angular_velocity(p_velocity); | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | real_t GodotPhysicsDirectBodyState2D::get_angular_velocity() const { | 
|---|
| 80 | return body->get_angular_velocity(); | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 | void GodotPhysicsDirectBodyState2D::set_transform(const Transform2D &p_transform) { | 
|---|
| 84 | body->set_state(PhysicsServer2D::BODY_STATE_TRANSFORM, p_transform); | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 | Transform2D GodotPhysicsDirectBodyState2D::get_transform() const { | 
|---|
| 88 | return body->get_transform(); | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | Vector2 GodotPhysicsDirectBodyState2D::get_velocity_at_local_position(const Vector2 &p_position) const { | 
|---|
| 92 | return body->get_velocity_in_local_point(p_position); | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | void GodotPhysicsDirectBodyState2D::apply_central_impulse(const Vector2 &p_impulse) { | 
|---|
| 96 | body->wakeup(); | 
|---|
| 97 | body->apply_central_impulse(p_impulse); | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | void GodotPhysicsDirectBodyState2D::apply_impulse(const Vector2 &p_impulse, const Vector2 &p_position) { | 
|---|
| 101 | body->wakeup(); | 
|---|
| 102 | body->apply_impulse(p_impulse, p_position); | 
|---|
| 103 | } | 
|---|
| 104 |  | 
|---|
| 105 | void GodotPhysicsDirectBodyState2D::apply_torque_impulse(real_t p_torque) { | 
|---|
| 106 | body->wakeup(); | 
|---|
| 107 | body->apply_torque_impulse(p_torque); | 
|---|
| 108 | } | 
|---|
| 109 |  | 
|---|
| 110 | void GodotPhysicsDirectBodyState2D::apply_central_force(const Vector2 &p_force) { | 
|---|
| 111 | body->wakeup(); | 
|---|
| 112 | body->apply_central_force(p_force); | 
|---|
| 113 | } | 
|---|
| 114 |  | 
|---|
| 115 | void GodotPhysicsDirectBodyState2D::apply_force(const Vector2 &p_force, const Vector2 &p_position) { | 
|---|
| 116 | body->wakeup(); | 
|---|
| 117 | body->apply_force(p_force, p_position); | 
|---|
| 118 | } | 
|---|
| 119 |  | 
|---|
| 120 | void GodotPhysicsDirectBodyState2D::apply_torque(real_t p_torque) { | 
|---|
| 121 | body->wakeup(); | 
|---|
| 122 | body->apply_torque(p_torque); | 
|---|
| 123 | } | 
|---|
| 124 |  | 
|---|
| 125 | void GodotPhysicsDirectBodyState2D::add_constant_central_force(const Vector2 &p_force) { | 
|---|
| 126 | body->wakeup(); | 
|---|
| 127 | body->add_constant_central_force(p_force); | 
|---|
| 128 | } | 
|---|
| 129 |  | 
|---|
| 130 | void GodotPhysicsDirectBodyState2D::add_constant_force(const Vector2 &p_force, const Vector2 &p_position) { | 
|---|
| 131 | body->wakeup(); | 
|---|
| 132 | body->add_constant_force(p_force, p_position); | 
|---|
| 133 | } | 
|---|
| 134 |  | 
|---|
| 135 | void GodotPhysicsDirectBodyState2D::add_constant_torque(real_t p_torque) { | 
|---|
| 136 | body->wakeup(); | 
|---|
| 137 | body->add_constant_torque(p_torque); | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|
| 140 | void GodotPhysicsDirectBodyState2D::set_constant_force(const Vector2 &p_force) { | 
|---|
| 141 | if (!p_force.is_zero_approx()) { | 
|---|
| 142 | body->wakeup(); | 
|---|
| 143 | } | 
|---|
| 144 | body->set_constant_force(p_force); | 
|---|
| 145 | } | 
|---|
| 146 |  | 
|---|
| 147 | Vector2 GodotPhysicsDirectBodyState2D::get_constant_force() const { | 
|---|
| 148 | return body->get_constant_force(); | 
|---|
| 149 | } | 
|---|
| 150 |  | 
|---|
| 151 | void GodotPhysicsDirectBodyState2D::set_constant_torque(real_t p_torque) { | 
|---|
| 152 | if (!Math::is_zero_approx(p_torque)) { | 
|---|
| 153 | body->wakeup(); | 
|---|
| 154 | } | 
|---|
| 155 | body->set_constant_torque(p_torque); | 
|---|
| 156 | } | 
|---|
| 157 |  | 
|---|
| 158 | real_t GodotPhysicsDirectBodyState2D::get_constant_torque() const { | 
|---|
| 159 | return body->get_constant_torque(); | 
|---|
| 160 | } | 
|---|
| 161 |  | 
|---|
| 162 | void GodotPhysicsDirectBodyState2D::set_sleep_state(bool p_enable) { | 
|---|
| 163 | body->set_active(!p_enable); | 
|---|
| 164 | } | 
|---|
| 165 |  | 
|---|
| 166 | bool GodotPhysicsDirectBodyState2D::is_sleeping() const { | 
|---|
| 167 | return !body->is_active(); | 
|---|
| 168 | } | 
|---|
| 169 |  | 
|---|
| 170 | int GodotPhysicsDirectBodyState2D::get_contact_count() const { | 
|---|
| 171 | return body->contact_count; | 
|---|
| 172 | } | 
|---|
| 173 |  | 
|---|
| 174 | Vector2 GodotPhysicsDirectBodyState2D::get_contact_local_position(int p_contact_idx) const { | 
|---|
| 175 | ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, Vector2()); | 
|---|
| 176 | return body->contacts[p_contact_idx].local_pos; | 
|---|
| 177 | } | 
|---|
| 178 |  | 
|---|
| 179 | Vector2 GodotPhysicsDirectBodyState2D::get_contact_local_normal(int p_contact_idx) const { | 
|---|
| 180 | ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, Vector2()); | 
|---|
| 181 | return body->contacts[p_contact_idx].local_normal; | 
|---|
| 182 | } | 
|---|
| 183 |  | 
|---|
| 184 | int GodotPhysicsDirectBodyState2D::get_contact_local_shape(int p_contact_idx) const { | 
|---|
| 185 | ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, -1); | 
|---|
| 186 | return body->contacts[p_contact_idx].local_shape; | 
|---|
| 187 | } | 
|---|
| 188 |  | 
|---|
| 189 | Vector2 GodotPhysicsDirectBodyState2D::get_contact_local_velocity_at_position(int p_contact_idx) const { | 
|---|
| 190 | ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, Vector2()); | 
|---|
| 191 | return body->contacts[p_contact_idx].local_velocity_at_pos; | 
|---|
| 192 | } | 
|---|
| 193 |  | 
|---|
| 194 | RID GodotPhysicsDirectBodyState2D::get_contact_collider(int p_contact_idx) const { | 
|---|
| 195 | ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, RID()); | 
|---|
| 196 | return body->contacts[p_contact_idx].collider; | 
|---|
| 197 | } | 
|---|
| 198 | Vector2 GodotPhysicsDirectBodyState2D::get_contact_collider_position(int p_contact_idx) const { | 
|---|
| 199 | ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, Vector2()); | 
|---|
| 200 | return body->contacts[p_contact_idx].collider_pos; | 
|---|
| 201 | } | 
|---|
| 202 |  | 
|---|
| 203 | ObjectID GodotPhysicsDirectBodyState2D::get_contact_collider_id(int p_contact_idx) const { | 
|---|
| 204 | ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, ObjectID()); | 
|---|
| 205 | return body->contacts[p_contact_idx].collider_instance_id; | 
|---|
| 206 | } | 
|---|
| 207 |  | 
|---|
| 208 | int GodotPhysicsDirectBodyState2D::get_contact_collider_shape(int p_contact_idx) const { | 
|---|
| 209 | ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, 0); | 
|---|
| 210 | return body->contacts[p_contact_idx].collider_shape; | 
|---|
| 211 | } | 
|---|
| 212 |  | 
|---|
| 213 | Vector2 GodotPhysicsDirectBodyState2D::get_contact_collider_velocity_at_position(int p_contact_idx) const { | 
|---|
| 214 | ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, Vector2()); | 
|---|
| 215 | return body->contacts[p_contact_idx].collider_velocity_at_pos; | 
|---|
| 216 | } | 
|---|
| 217 |  | 
|---|
| 218 | Vector2 GodotPhysicsDirectBodyState2D::get_contact_impulse(int p_contact_idx) const { | 
|---|
| 219 | ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, Vector2()); | 
|---|
| 220 | return body->contacts[p_contact_idx].impulse; | 
|---|
| 221 | } | 
|---|
| 222 |  | 
|---|
| 223 | PhysicsDirectSpaceState2D *GodotPhysicsDirectBodyState2D::get_space_state() { | 
|---|
| 224 | return body->get_space()->get_direct_state(); | 
|---|
| 225 | } | 
|---|
| 226 |  | 
|---|
| 227 | real_t GodotPhysicsDirectBodyState2D::get_step() const { | 
|---|
| 228 | return body->get_space()->get_last_step(); | 
|---|
| 229 | } | 
|---|
| 230 |  | 
|---|