Skip to content
0.2.0-dev Download ZIP
278 lines ·10.7 KB Raw
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#include "core/object/class_db.h"
38
39void JoltPhysicsDirectBodyState3D::_bind_methods() {
40 ClassDB::bind_method(D_METHOD("get_contact_local_face", "contact_idx"), &JoltPhysicsDirectBodyState3D::get_contact_local_face);
41 ClassDB::bind_method(D_METHOD("get_contact_collider_face", "contact_idx"), &JoltPhysicsDirectBodyState3D::get_contact_collider_face);
42}
43
44JoltPhysicsDirectBodyState3D::JoltPhysicsDirectBodyState3D(JoltBody3D *p_body) :
45 body(p_body) {
46}
47
48Vector3 JoltPhysicsDirectBodyState3D::get_total_gravity() const {
49 return body->get_total_gravity();
50}
51
52real_t JoltPhysicsDirectBodyState3D::get_total_angular_damp() const {
53 return (real_t)body->get_total_angular_damp();
54}
55
56real_t JoltPhysicsDirectBodyState3D::get_total_linear_damp() const {
57 return (real_t)body->get_total_linear_damp();
58}
59
60Vector3 JoltPhysicsDirectBodyState3D::get_center_of_mass() const {
61 return body->get_center_of_mass_relative();
62}
63
64Vector3 JoltPhysicsDirectBodyState3D::get_center_of_mass_local() const {
65 return body->get_center_of_mass_local();
66}
67
68Basis JoltPhysicsDirectBodyState3D::get_principal_inertia_axes() const {
69 return body->get_principal_inertia_axes();
70}
71
72real_t JoltPhysicsDirectBodyState3D::get_inverse_mass() const {
73 return 1.0 / body->get_mass();
74}
75
76Vector3 JoltPhysicsDirectBodyState3D::get_inverse_inertia() const {
77 return body->get_inverse_inertia();
78}
79
80Basis JoltPhysicsDirectBodyState3D::get_inverse_inertia_tensor() const {
81 return body->get_inverse_inertia_tensor();
82}
83
84Vector3 JoltPhysicsDirectBodyState3D::get_linear_velocity() const {
85 return body->get_linear_velocity();
86}
87
88void JoltPhysicsDirectBodyState3D::set_linear_velocity(const Vector3 &p_velocity) {
89 return body->set_linear_velocity(p_velocity);
90}
91
92Vector3 JoltPhysicsDirectBodyState3D::get_angular_velocity() const {
93 return body->get_angular_velocity();
94}
95
96void JoltPhysicsDirectBodyState3D::set_angular_velocity(const Vector3 &p_velocity) {
97 return body->set_angular_velocity(p_velocity);
98}
99
100void JoltPhysicsDirectBodyState3D::set_transform(const Transform3D &p_transform) {
101 return body->set_transform(p_transform);
102}
103
104Transform3D JoltPhysicsDirectBodyState3D::get_transform() const {
105 return body->get_transform_scaled();
106}
107
108Vector3 JoltPhysicsDirectBodyState3D::get_velocity_at_local_position(const Vector3 &p_local_position) const {
109 return body->get_velocity_at_position(body->get_position() + p_local_position);
110}
111
112void JoltPhysicsDirectBodyState3D::apply_central_impulse(const Vector3 &p_impulse) {
113 return body->apply_central_impulse(p_impulse);
114}
115
116void JoltPhysicsDirectBodyState3D::apply_impulse(const Vector3 &p_impulse, const Vector3 &p_position) {
117 return body->apply_impulse(p_impulse, p_position);
118}
119
120void JoltPhysicsDirectBodyState3D::apply_torque_impulse(const Vector3 &p_impulse) {
121 return body->apply_torque_impulse(p_impulse);
122}
123
124void JoltPhysicsDirectBodyState3D::apply_central_force(const Vector3 &p_force) {
125 return body->apply_central_force(p_force);
126}
127
128void JoltPhysicsDirectBodyState3D::apply_force(const Vector3 &p_force, const Vector3 &p_position) {
129 return body->apply_force(p_force, p_position);
130}
131
132void JoltPhysicsDirectBodyState3D::apply_torque(const Vector3 &p_torque) {
133 return body->apply_torque(p_torque);
134}
135
136void JoltPhysicsDirectBodyState3D::add_constant_central_force(const Vector3 &p_force) {
137 return body->add_constant_central_force(p_force);
138}
139
140void JoltPhysicsDirectBodyState3D::add_constant_force(const Vector3 &p_force, const Vector3 &p_position) {
141 return body->add_constant_force(p_force, p_position);
142}
143
144void JoltPhysicsDirectBodyState3D::add_constant_torque(const Vector3 &p_torque) {
145 return body->add_constant_torque(p_torque);
146}
147
148Vector3 JoltPhysicsDirectBodyState3D::get_constant_force() const {
149 return body->get_constant_force();
150}
151
152void JoltPhysicsDirectBodyState3D::set_constant_force(const Vector3 &p_force) {
153 return body->set_constant_force(p_force);
154}
155
156Vector3 JoltPhysicsDirectBodyState3D::get_constant_torque() const {
157 return body->get_constant_torque();
158}
159
160void JoltPhysicsDirectBodyState3D::set_constant_torque(const Vector3 &p_torque) {
161 return body->set_constant_torque(p_torque);
162}
163
164bool JoltPhysicsDirectBodyState3D::is_sleeping() const {
165 return body->is_sleeping();
166}
167
168void JoltPhysicsDirectBodyState3D::set_sleep_state(bool p_enabled) {
169 body->set_is_sleeping(p_enabled);
170}
171
172void JoltPhysicsDirectBodyState3D::set_collision_layer(uint32_t p_layer) {
173 body->set_collision_layer(p_layer);
174}
175
176uint32_t JoltPhysicsDirectBodyState3D::get_collision_layer() const {
177 return body->get_collision_layer();
178}
179
180void JoltPhysicsDirectBodyState3D::set_collision_mask(uint32_t p_mask) {
181 body->set_collision_mask(p_mask);
182}
183
184uint32_t JoltPhysicsDirectBodyState3D::get_collision_mask() const {
185 return body->get_collision_mask();
186}
187
188int JoltPhysicsDirectBodyState3D::get_contact_count() const {
189 return body->get_contact_count();
190}
191
192Vector3 JoltPhysicsDirectBodyState3D::get_contact_local_position(int p_contact_idx) const {
193 ERR_FAIL_INDEX_V(p_contact_idx, (int)body->get_contact_count(), Vector3());
194 return body->get_contact(p_contact_idx).position;
195}
196
197Vector3 JoltPhysicsDirectBodyState3D::get_contact_local_normal(int p_contact_idx) const {
198 ERR_FAIL_INDEX_V(p_contact_idx, (int)body->get_contact_count(), Vector3());
199 return body->get_contact(p_contact_idx).normal;
200}
201
202Vector3 JoltPhysicsDirectBodyState3D::get_contact_impulse(int p_contact_idx) const {
203 ERR_FAIL_INDEX_V(p_contact_idx, (int)body->get_contact_count(), Vector3());
204 return body->get_contact(p_contact_idx).impulse;
205}
206
207int JoltPhysicsDirectBodyState3D::get_contact_local_shape(int p_contact_idx) const {
208 ERR_FAIL_INDEX_V(p_contact_idx, (int)body->get_contact_count(), 0);
209 return body->get_contact(p_contact_idx).shape_index;
210}
211
212Vector3 JoltPhysicsDirectBodyState3D::get_contact_local_velocity_at_position(int p_contact_idx) const {
213 ERR_FAIL_INDEX_V(p_contact_idx, (int)body->get_contact_count(), Vector3());
214 return body->get_contact(p_contact_idx).velocity;
215}
216
217RID JoltPhysicsDirectBodyState3D::get_contact_collider(int p_contact_idx) const {
218 ERR_FAIL_INDEX_V(p_contact_idx, (int)body->get_contact_count(), RID());
219 return body->get_contact(p_contact_idx).collider_rid;
220}
221
222Vector3 JoltPhysicsDirectBodyState3D::get_contact_collider_position(int p_contact_idx) const {
223 ERR_FAIL_INDEX_V(p_contact_idx, (int)body->get_contact_count(), Vector3());
224 return body->get_contact(p_contact_idx).collider_position;
225}
226
227ObjectID JoltPhysicsDirectBodyState3D::get_contact_collider_id(int p_contact_idx) const {
228 ERR_FAIL_INDEX_V(p_contact_idx, (int)body->get_contact_count(), ObjectID());
229 return body->get_contact(p_contact_idx).collider_id;
230}
231
232Object *JoltPhysicsDirectBodyState3D::get_contact_collider_object(int p_contact_idx) const {
233 ERR_FAIL_INDEX_V(p_contact_idx, (int)body->get_contact_count(), nullptr);
234 return ObjectDB::get_instance(body->get_contact(p_contact_idx).collider_id);
235}
236
237int JoltPhysicsDirectBodyState3D::get_contact_collider_shape(int p_contact_idx) const {
238 ERR_FAIL_INDEX_V(p_contact_idx, (int)body->get_contact_count(), 0);
239 return body->get_contact(p_contact_idx).collider_shape_index;
240}
241
242int JoltPhysicsDirectBodyState3D::get_contact_local_face(int p_contact_idx) const {
243 ERR_FAIL_INDEX_V(p_contact_idx, (int)body->get_contact_count(), -1);
244 return body->get_contact(p_contact_idx).face_index;
245}
246
247int JoltPhysicsDirectBodyState3D::get_contact_collider_face(int p_contact_idx) const {
248 ERR_FAIL_INDEX_V(p_contact_idx, (int)body->get_contact_count(), -1);
249 return body->get_contact(p_contact_idx).collider_face_index;
250}
251
252Vector3 JoltPhysicsDirectBodyState3D::get_contact_collider_velocity_at_position(int p_contact_idx) const {
253 ERR_FAIL_INDEX_V(p_contact_idx, (int)body->get_contact_count(), Vector3());
254 return body->get_contact(p_contact_idx).collider_velocity;
255}
256
257real_t JoltPhysicsDirectBodyState3D::get_step() const {
258 return (real_t)body->get_space()->get_last_step();
259}
260
261void JoltPhysicsDirectBodyState3D::integrate_forces() {
262 const float step = (float)get_step();
263
264 Vector3 linear_velocity = get_linear_velocity();
265 Vector3 angular_velocity = get_angular_velocity();
266
267 linear_velocity *= MAX(1.0f - (float)get_total_linear_damp() * step, 0.0f);
268 angular_velocity *= MAX(1.0f - (float)get_total_angular_damp() * step, 0.0f);
269
270 linear_velocity += get_total_gravity() * step;
271
272 set_linear_velocity(linear_velocity);
273 set_angular_velocity(angular_velocity);
274}
275
276RequiredResult<PhysicsDirectSpaceState3D> JoltPhysicsDirectBodyState3D::get_space_state() {
277 return body->get_space()->get_direct_state();
278}