| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2018-01-05 00:50:27 +01:00
										 |  |  | /*  rigid_body_bullet.cpp                                                */ | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*                       This file is part of:                           */ | 
					
						
							|  |  |  | /*                           GODOT ENGINE                                */ | 
					
						
							| 
									
										
										
										
											2018-01-05 00:50:27 +01:00
										 |  |  | /*                      https://godotengine.org                          */ | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2020-01-01 11:16:22 +01:00
										 |  |  | /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */ | 
					
						
							|  |  |  | /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */ | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01: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.                */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "rigid_body_bullet.h"
 | 
					
						
							| 
									
										
										
										
											2018-01-05 00:50:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | #include "btRayShape.h"
 | 
					
						
							|  |  |  | #include "bullet_physics_server.h"
 | 
					
						
							|  |  |  | #include "bullet_types_converter.h"
 | 
					
						
							|  |  |  | #include "bullet_utilities.h"
 | 
					
						
							|  |  |  | #include "godot_motion_state.h"
 | 
					
						
							|  |  |  | #include "joint_bullet.h"
 | 
					
						
							| 
									
										
										
										
											2018-01-05 00:50:27 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <BulletCollision/CollisionDispatch/btGhostObject.h>
 | 
					
						
							|  |  |  | #include <BulletCollision/CollisionShapes/btConvexPointCloudShape.h>
 | 
					
						
							|  |  |  | #include <BulletDynamics/Dynamics/btRigidBody.h>
 | 
					
						
							|  |  |  | #include <btBulletCollisionCommon.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | #include <assert.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-05 00:50:27 +01:00
										 |  |  | /**
 | 
					
						
							|  |  |  | 	@author AndreaCatania | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | BulletPhysicsDirectBodyState3D *BulletPhysicsDirectBodyState3D::singleton = nullptr; | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 18:22:57 +02:00
										 |  |  | Vector3 BulletPhysicsDirectBodyState3D::get_total_gravity() const { | 
					
						
							| 
									
										
										
										
											2020-07-10 09:21:12 +02:00
										 |  |  | 	return body->total_gravity; | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 18:22:57 +02:00
										 |  |  | float BulletPhysicsDirectBodyState3D::get_total_angular_damp() const { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	return body->btBody->getAngularDamping(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 18:22:57 +02:00
										 |  |  | float BulletPhysicsDirectBodyState3D::get_total_linear_damp() const { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	return body->btBody->getLinearDamping(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 18:22:57 +02:00
										 |  |  | Vector3 BulletPhysicsDirectBodyState3D::get_center_of_mass() const { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	Vector3 gVec; | 
					
						
							|  |  |  | 	B_TO_G(body->btBody->getCenterOfMassPosition(), gVec); | 
					
						
							|  |  |  | 	return gVec; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 18:22:57 +02:00
										 |  |  | Basis BulletPhysicsDirectBodyState3D::get_principal_inertia_axes() const { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	return Basis(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 18:22:57 +02:00
										 |  |  | float BulletPhysicsDirectBodyState3D::get_inverse_mass() const { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	return body->btBody->getInvMass(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 18:22:57 +02:00
										 |  |  | Vector3 BulletPhysicsDirectBodyState3D::get_inverse_inertia() const { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	Vector3 gVec; | 
					
						
							|  |  |  | 	B_TO_G(body->btBody->getInvInertiaDiagLocal(), gVec); | 
					
						
							|  |  |  | 	return gVec; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 18:22:57 +02:00
										 |  |  | Basis BulletPhysicsDirectBodyState3D::get_inverse_inertia_tensor() const { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	Basis gInertia; | 
					
						
							|  |  |  | 	B_TO_G(body->btBody->getInvInertiaTensorWorld(), gInertia); | 
					
						
							|  |  |  | 	return gInertia; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 18:22:57 +02:00
										 |  |  | void BulletPhysicsDirectBodyState3D::set_linear_velocity(const Vector3 &p_velocity) { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	body->set_linear_velocity(p_velocity); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 18:22:57 +02:00
										 |  |  | Vector3 BulletPhysicsDirectBodyState3D::get_linear_velocity() const { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	return body->get_linear_velocity(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 18:22:57 +02:00
										 |  |  | void BulletPhysicsDirectBodyState3D::set_angular_velocity(const Vector3 &p_velocity) { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	body->set_angular_velocity(p_velocity); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 18:22:57 +02:00
										 |  |  | Vector3 BulletPhysicsDirectBodyState3D::get_angular_velocity() const { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	return body->get_angular_velocity(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 18:22:57 +02:00
										 |  |  | void BulletPhysicsDirectBodyState3D::set_transform(const Transform &p_transform) { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	body->set_transform(p_transform); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 18:22:57 +02:00
										 |  |  | Transform BulletPhysicsDirectBodyState3D::get_transform() const { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	return body->get_transform(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 18:22:57 +02:00
										 |  |  | void BulletPhysicsDirectBodyState3D::add_central_force(const Vector3 &p_force) { | 
					
						
							| 
									
										
										
										
											2018-02-16 17:48:07 +01:00
										 |  |  | 	body->apply_central_force(p_force); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 00:23:34 -04:00
										 |  |  | void BulletPhysicsDirectBodyState3D::add_force(const Vector3 &p_force, const Vector3 &p_position) { | 
					
						
							|  |  |  | 	body->apply_force(p_force, p_position); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 18:22:57 +02:00
										 |  |  | void BulletPhysicsDirectBodyState3D::add_torque(const Vector3 &p_torque) { | 
					
						
							| 
									
										
										
										
											2018-02-16 17:48:07 +01:00
										 |  |  | 	body->apply_torque(p_torque); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 18:22:57 +02:00
										 |  |  | void BulletPhysicsDirectBodyState3D::apply_central_impulse(const Vector3 &p_impulse) { | 
					
						
							| 
									
										
										
										
											2019-10-28 08:07:29 +01:00
										 |  |  | 	body->apply_central_impulse(p_impulse); | 
					
						
							| 
									
										
										
										
											2018-07-24 03:49:12 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 00:23:34 -04:00
										 |  |  | void BulletPhysicsDirectBodyState3D::apply_impulse(const Vector3 &p_impulse, const Vector3 &p_position) { | 
					
						
							|  |  |  | 	body->apply_impulse(p_impulse, p_position); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 18:22:57 +02:00
										 |  |  | void BulletPhysicsDirectBodyState3D::apply_torque_impulse(const Vector3 &p_impulse) { | 
					
						
							| 
									
										
										
										
											2019-10-28 08:07:29 +01:00
										 |  |  | 	body->apply_torque_impulse(p_impulse); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-12 14:39:49 +02:00
										 |  |  | void BulletPhysicsDirectBodyState3D::set_sleep_state(bool p_sleep) { | 
					
						
							|  |  |  | 	body->set_activation_state(!p_sleep); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 18:22:57 +02:00
										 |  |  | bool BulletPhysicsDirectBodyState3D::is_sleeping() const { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	return !body->is_active(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 18:22:57 +02:00
										 |  |  | int BulletPhysicsDirectBodyState3D::get_contact_count() const { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	return body->collisionsCount; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 18:22:57 +02:00
										 |  |  | Vector3 BulletPhysicsDirectBodyState3D::get_contact_local_position(int p_contact_idx) const { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	return body->collisions[p_contact_idx].hitLocalLocation; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 18:22:57 +02:00
										 |  |  | Vector3 BulletPhysicsDirectBodyState3D::get_contact_local_normal(int p_contact_idx) const { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	return body->collisions[p_contact_idx].hitNormal; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 18:22:57 +02:00
										 |  |  | float BulletPhysicsDirectBodyState3D::get_contact_impulse(int p_contact_idx) const { | 
					
						
							| 
									
										
										
										
											2018-01-12 15:27:45 -06:00
										 |  |  | 	return body->collisions[p_contact_idx].appliedImpulse; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 18:22:57 +02:00
										 |  |  | int BulletPhysicsDirectBodyState3D::get_contact_local_shape(int p_contact_idx) const { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	return body->collisions[p_contact_idx].local_shape; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 18:22:57 +02:00
										 |  |  | RID BulletPhysicsDirectBodyState3D::get_contact_collider(int p_contact_idx) const { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	return body->collisions[p_contact_idx].otherObject->get_self(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 18:22:57 +02:00
										 |  |  | Vector3 BulletPhysicsDirectBodyState3D::get_contact_collider_position(int p_contact_idx) const { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	return body->collisions[p_contact_idx].hitWorldLocation; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 18:22:57 +02:00
										 |  |  | ObjectID BulletPhysicsDirectBodyState3D::get_contact_collider_id(int p_contact_idx) const { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	return body->collisions[p_contact_idx].otherObject->get_instance_id(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 18:22:57 +02:00
										 |  |  | int BulletPhysicsDirectBodyState3D::get_contact_collider_shape(int p_contact_idx) const { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	return body->collisions[p_contact_idx].other_object_shape; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 18:22:57 +02:00
										 |  |  | Vector3 BulletPhysicsDirectBodyState3D::get_contact_collider_velocity_at_position(int p_contact_idx) const { | 
					
						
							| 
									
										
										
										
											2020-07-10 09:21:12 +02:00
										 |  |  | 	RigidBodyBullet::CollisionData &colDat = body->collisions[p_contact_idx]; | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	btVector3 hitLocation; | 
					
						
							|  |  |  | 	G_TO_B(colDat.hitLocalLocation, hitLocation); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Vector3 velocityAtPoint; | 
					
						
							|  |  |  | 	B_TO_G(colDat.otherObject->get_bt_rigid_body()->getVelocityInLocalPoint(hitLocation), velocityAtPoint); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return velocityAtPoint; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 18:22:57 +02:00
										 |  |  | PhysicsDirectSpaceState3D *BulletPhysicsDirectBodyState3D::get_space_state() { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	return body->get_space()->get_direct_state(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-06 21:36:34 +01:00
										 |  |  | RigidBodyBullet::KinematicUtilities::KinematicUtilities(RigidBodyBullet *p_owner) : | 
					
						
							|  |  |  | 		owner(p_owner), | 
					
						
							|  |  |  | 		safe_margin(0.001) { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | RigidBodyBullet::KinematicUtilities::~KinematicUtilities() { | 
					
						
							| 
									
										
										
										
											2017-11-07 15:22:09 +01:00
										 |  |  | 	just_delete_shapes(shapes.size()); // don't need to resize
 | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-07 15:22:09 +01:00
										 |  |  | void RigidBodyBullet::KinematicUtilities::setSafeMargin(btScalar p_margin) { | 
					
						
							|  |  |  | 	safe_margin = p_margin; | 
					
						
							|  |  |  | 	copyAllOwnerShapes(); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void RigidBodyBullet::KinematicUtilities::copyAllOwnerShapes() { | 
					
						
							| 
									
										
										
										
											2020-07-10 09:21:12 +02:00
										 |  |  | 	const LocalVector<CollisionObjectBullet::ShapeWrapper> &shapes_wrappers(owner->get_shapes_wrappers()); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	const int shapes_count = shapes_wrappers.size(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	just_delete_shapes(shapes_count); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	const CollisionObjectBullet::ShapeWrapper *shape_wrapper; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-08 01:22:54 +01:00
										 |  |  | 	btVector3 owner_scale(owner->get_bt_body_scale()); | 
					
						
							| 
									
										
										
										
											2017-12-02 18:10:36 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	for (int i = shapes_count - 1; 0 <= i; --i) { | 
					
						
							|  |  |  | 		shape_wrapper = &shapes_wrappers[i]; | 
					
						
							|  |  |  | 		if (!shape_wrapper->active) { | 
					
						
							|  |  |  | 			continue; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-10 09:21:12 +02:00
										 |  |  | 		shapes[i].transform = shape_wrapper->transform; | 
					
						
							|  |  |  | 		shapes[i].transform.getOrigin() *= owner_scale; | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 		switch (shape_wrapper->shape->get_type()) { | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 			case PhysicsServer3D::SHAPE_SPHERE: | 
					
						
							|  |  |  | 			case PhysicsServer3D::SHAPE_BOX: | 
					
						
							|  |  |  | 			case PhysicsServer3D::SHAPE_CAPSULE: | 
					
						
							|  |  |  | 			case PhysicsServer3D::SHAPE_CYLINDER: | 
					
						
							|  |  |  | 			case PhysicsServer3D::SHAPE_CONVEX_POLYGON: | 
					
						
							|  |  |  | 			case PhysicsServer3D::SHAPE_RAY: { | 
					
						
							| 
									
										
										
										
											2020-07-10 09:21:12 +02:00
										 |  |  | 				shapes[i].shape = static_cast<btConvexShape *>(shape_wrapper->shape->internal_create_bt_shape(owner_scale * shape_wrapper->scale, safe_margin)); | 
					
						
							| 
									
										
										
										
											2017-12-23 18:23:12 +01:00
										 |  |  | 			} break; | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			default: | 
					
						
							| 
									
										
										
										
											2020-03-09 13:15:20 +01:00
										 |  |  | 				WARN_PRINT("This shape is not supported for kinematic collision."); | 
					
						
							| 
									
										
										
										
											2020-07-10 09:21:12 +02:00
										 |  |  | 				shapes[i].shape = nullptr; | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void RigidBodyBullet::KinematicUtilities::just_delete_shapes(int new_size) { | 
					
						
							| 
									
										
										
										
											2017-11-07 15:22:09 +01:00
										 |  |  | 	for (int i = shapes.size() - 1; 0 <= i; --i) { | 
					
						
							|  |  |  | 		if (shapes[i].shape) { | 
					
						
							| 
									
										
										
										
											2020-07-10 09:21:12 +02:00
										 |  |  | 			bulletdelete(shapes[i].shape); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-11-07 15:22:09 +01:00
										 |  |  | 	shapes.resize(new_size); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-06 21:36:34 +01:00
										 |  |  | RigidBodyBullet::RigidBodyBullet() : | 
					
						
							| 
									
										
										
										
											2020-05-12 17:01:17 +02:00
										 |  |  | 		RigidCollisionObjectBullet(CollisionObjectBullet::TYPE_RIGID_BODY) { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	godotMotionState = bulletnew(GodotMotionState(this)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Initial properties
 | 
					
						
							|  |  |  | 	const btVector3 localInertia(0, 0, 0); | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 	btRigidBody::btRigidBodyConstructionInfo cInfo(mass, godotMotionState, nullptr, localInertia); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	btBody = bulletnew(btRigidBody(cInfo)); | 
					
						
							| 
									
										
										
										
											2018-10-06 16:50:10 +02:00
										 |  |  | 	reload_shapes(); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	setupBulletCollisionObject(btBody); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 	set_mode(PhysicsServer3D::BODY_MODE_RIGID); | 
					
						
							| 
									
										
										
										
											2017-12-10 17:21:14 +01:00
										 |  |  | 	reload_axis_lock(); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	areasWhereIam.resize(maxAreasWhereIam); | 
					
						
							| 
									
										
										
										
											2020-07-10 09:21:12 +02:00
										 |  |  | 	for (uint32_t i = 0; i < areasWhereIam.size(); i += 1) { | 
					
						
							|  |  |  | 		areasWhereIam[i] = nullptr; | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-12-23 05:13:39 +01:00
										 |  |  | 	btBody->setSleepingThresholds(0.2, 0.2); | 
					
						
							| 
									
										
										
										
											2018-11-22 09:03:21 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	prev_collision_traces = &collision_traces_1; | 
					
						
							|  |  |  | 	curr_collision_traces = &collision_traces_2; | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | RigidBodyBullet::~RigidBodyBullet() { | 
					
						
							|  |  |  | 	bulletdelete(godotMotionState); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (force_integration_callback) { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 		memdelete(force_integration_callback); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	destroy_kinematic_utilities(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void RigidBodyBullet::init_kinematic_utilities() { | 
					
						
							|  |  |  | 	kinematic_utilities = memnew(KinematicUtilities(this)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void RigidBodyBullet::destroy_kinematic_utilities() { | 
					
						
							|  |  |  | 	if (kinematic_utilities) { | 
					
						
							|  |  |  | 		memdelete(kinematic_utilities); | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 		kinematic_utilities = nullptr; | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-06 16:50:10 +02:00
										 |  |  | void RigidBodyBullet::main_shape_changed() { | 
					
						
							| 
									
										
										
										
											2019-11-07 10:37:44 +01:00
										 |  |  | 	CRASH_COND(!get_main_shape()); | 
					
						
							| 
									
										
										
										
											2018-10-06 16:50:10 +02:00
										 |  |  | 	btBody->setCollisionShape(get_main_shape()); | 
					
						
							| 
									
										
										
										
											2018-09-07 20:38:30 +02:00
										 |  |  | 	set_continuous_collision_detection(is_continuous_collision_detection_enabled()); // Reset
 | 
					
						
							| 
									
										
										
										
											2018-09-06 18:19:05 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-21 12:48:40 +02:00
										 |  |  | void RigidBodyBullet::do_reload_body() { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	if (space) { | 
					
						
							|  |  |  | 		space->remove_rigid_body(this); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 		if (get_main_shape()) { | 
					
						
							| 
									
										
										
										
											2018-08-31 09:40:50 +02:00
										 |  |  | 			space->add_rigid_body(this); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void RigidBodyBullet::set_space(SpaceBullet *p_space) { | 
					
						
							|  |  |  | 	// Clear the old space if there is one
 | 
					
						
							|  |  |  | 	if (space) { | 
					
						
							| 
									
										
										
										
											2018-10-07 07:14:38 +02:00
										 |  |  | 		can_integrate_forces = false; | 
					
						
							| 
									
										
										
										
											2020-07-12 07:46:14 +01:00
										 |  |  | 		isScratchedSpaceOverrideModificator = false; | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// Remove all eventual constraints
 | 
					
						
							|  |  |  | 		assert_no_constraints(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Remove this object form the physics world
 | 
					
						
							| 
									
										
										
										
											2020-06-21 12:48:40 +02:00
										 |  |  | 		space->unregister_collision_object(this); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 		space->remove_rigid_body(this); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	space = p_space; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (space) { | 
					
						
							| 
									
										
										
										
											2020-06-21 12:48:40 +02:00
										 |  |  | 		space->register_collision_object(this); | 
					
						
							|  |  |  | 		reload_body(); | 
					
						
							| 
									
										
										
										
											2020-07-10 09:21:12 +02:00
										 |  |  | 		space->add_to_flush_queue(this); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void RigidBodyBullet::dispatch_callbacks() { | 
					
						
							| 
									
										
										
										
											2020-07-10 09:21:12 +02:00
										 |  |  | 	RigidCollisionObjectBullet::dispatch_callbacks(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-07 07:14:38 +02:00
										 |  |  | 	/// The check isFirstTransformChanged is necessary in order to call integrated forces only when the first transform is sent
 | 
					
						
							|  |  |  | 	if ((btBody->isKinematicObject() || btBody->isActive() || previousActiveState != btBody->isActive()) && force_integration_callback && can_integrate_forces) { | 
					
						
							| 
									
										
										
										
											2020-03-30 18:22:57 +02:00
										 |  |  | 		BulletPhysicsDirectBodyState3D *bodyDirect = BulletPhysicsDirectBodyState3D::get_singleton(this); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		Variant variantBodyDirect = bodyDirect; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		Object *obj = ObjectDB::get_instance(force_integration_callback->id); | 
					
						
							|  |  |  | 		if (!obj) { | 
					
						
							|  |  |  | 			// Remove integration callback
 | 
					
						
							| 
									
										
										
										
											2020-02-12 14:24:06 -03:00
										 |  |  | 			set_force_integration_callback(ObjectID(), StringName()); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 		} else { | 
					
						
							|  |  |  | 			const Variant *vp[2] = { &variantBodyDirect, &force_integration_callback->udata }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-19 16:27:19 -03:00
										 |  |  | 			Callable::CallError responseCallError; | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			int argc = (force_integration_callback->udata.get_type() == Variant::NIL) ? 1 : 2; | 
					
						
							|  |  |  | 			obj->call(force_integration_callback->method, vp, argc, responseCallError); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-10 09:21:12 +02:00
										 |  |  | 	previousActiveState = btBody->isActive(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void RigidBodyBullet::pre_process() { | 
					
						
							|  |  |  | 	RigidCollisionObjectBullet::pre_process(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	if (isScratchedSpaceOverrideModificator || 0 < countGravityPointSpaces) { | 
					
						
							|  |  |  | 		isScratchedSpaceOverrideModificator = false; | 
					
						
							|  |  |  | 		reload_space_override_modificator(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-10 09:21:12 +02:00
										 |  |  | 	if (is_active()) { | 
					
						
							|  |  |  | 		/// Lock axis
 | 
					
						
							|  |  |  | 		btBody->setLinearVelocity(btBody->getLinearVelocity() * btBody->getLinearFactor()); | 
					
						
							|  |  |  | 		btBody->setAngularVelocity(btBody->getAngularVelocity() * btBody->getAngularFactor()); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void RigidBodyBullet::set_force_integration_callback(ObjectID p_id, const StringName &p_method, const Variant &p_udata) { | 
					
						
							|  |  |  | 	if (force_integration_callback) { | 
					
						
							|  |  |  | 		memdelete(force_integration_callback); | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 		force_integration_callback = nullptr; | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-12 14:24:06 -03:00
										 |  |  | 	if (p_id.is_valid()) { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 		force_integration_callback = memnew(ForceIntegrationCallback); | 
					
						
							|  |  |  | 		force_integration_callback->id = p_id; | 
					
						
							|  |  |  | 		force_integration_callback->method = p_method; | 
					
						
							|  |  |  | 		force_integration_callback->udata = p_udata; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void RigidBodyBullet::scratch_space_override_modificator() { | 
					
						
							|  |  |  | 	isScratchedSpaceOverrideModificator = true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-10 09:21:12 +02:00
										 |  |  | void RigidBodyBullet::do_reload_collision_filters() { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	if (space) { | 
					
						
							|  |  |  | 		space->reload_collision_filters(this); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-10-08 11:35:35 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	set_activation_state(true); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void RigidBodyBullet::on_collision_checker_start() { | 
					
						
							| 
									
										
										
										
											2018-11-22 09:03:21 +01:00
										 |  |  | 	prev_collision_count = collisionsCount; | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	collisionsCount = 0; | 
					
						
							| 
									
										
										
										
											2018-11-22 09:03:21 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Swap array
 | 
					
						
							| 
									
										
										
										
											2020-07-10 09:21:12 +02:00
										 |  |  | 	SWAP(prev_collision_traces, curr_collision_traces); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-07 07:14:38 +02:00
										 |  |  | void RigidBodyBullet::on_collision_checker_end() { | 
					
						
							|  |  |  | 	// Always true if active and not a static or kinematic body
 | 
					
						
							|  |  |  | 	isTransformChanged = btBody->isActive() && !btBody->isStaticOrKinematicObject(); | 
					
						
							| 
									
										
										
										
											2020-07-10 09:21:12 +02:00
										 |  |  | 	if (isTransformChanged && space != nullptr) { | 
					
						
							|  |  |  | 		space->add_to_flush_queue(this); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-10-07 07:14:38 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-12 15:27:45 -06:00
										 |  |  | bool RigidBodyBullet::add_collision_object(RigidBodyBullet *p_otherObject, const Vector3 &p_hitWorldLocation, const Vector3 &p_hitLocalLocation, const Vector3 &p_hitNormal, const float &p_appliedImpulse, int p_other_shape_index, int p_local_shape_index) { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	if (collisionsCount >= maxCollisionsDetection) { | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-10 09:21:12 +02:00
										 |  |  | 	CollisionData &cd = collisions[collisionsCount]; | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	cd.hitLocalLocation = p_hitLocalLocation; | 
					
						
							|  |  |  | 	cd.otherObject = p_otherObject; | 
					
						
							|  |  |  | 	cd.hitWorldLocation = p_hitWorldLocation; | 
					
						
							|  |  |  | 	cd.hitNormal = p_hitNormal; | 
					
						
							| 
									
										
										
										
											2018-01-12 15:27:45 -06:00
										 |  |  | 	cd.appliedImpulse = p_appliedImpulse; | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	cd.other_object_shape = p_other_shape_index; | 
					
						
							|  |  |  | 	cd.local_shape = p_local_shape_index; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-10 09:21:12 +02:00
										 |  |  | 	(*curr_collision_traces)[collisionsCount] = p_otherObject; | 
					
						
							| 
									
										
										
										
											2018-11-22 09:03:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	++collisionsCount; | 
					
						
							|  |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-22 09:03:21 +01:00
										 |  |  | bool RigidBodyBullet::was_colliding(RigidBodyBullet *p_other_object) { | 
					
						
							|  |  |  | 	for (int i = prev_collision_count - 1; 0 <= i; --i) { | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 		if ((*prev_collision_traces)[i] == p_other_object) { | 
					
						
							| 
									
										
										
										
											2018-11-22 09:03:21 +01:00
										 |  |  | 			return true; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-11-22 09:03:21 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | void RigidBodyBullet::assert_no_constraints() { | 
					
						
							|  |  |  | 	if (btBody->getNumConstraintRefs()) { | 
					
						
							|  |  |  | 		WARN_PRINT("A body with a joints is destroyed. Please check the implementation in order to destroy the joint before the body."); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void RigidBodyBullet::set_activation_state(bool p_active) { | 
					
						
							|  |  |  | 	if (p_active) { | 
					
						
							| 
									
										
										
										
											2019-10-14 10:03:10 +02:00
										 |  |  | 		btBody->activate(); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		btBody->setActivationState(WANTS_DEACTIVATION); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool RigidBodyBullet::is_active() const { | 
					
						
							|  |  |  | 	return btBody->isActive(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-27 08:41:17 +01:00
										 |  |  | void RigidBodyBullet::set_omit_forces_integration(bool p_omit) { | 
					
						
							|  |  |  | 	omit_forces_integration = p_omit; | 
					
						
							| 
									
										
										
										
											2020-07-10 09:21:12 +02:00
										 |  |  | 	scratch_space_override_modificator(); | 
					
						
							| 
									
										
										
										
											2018-02-27 08:41:17 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | void RigidBodyBullet::set_param(PhysicsServer3D::BodyParameter p_param, real_t p_value) { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	switch (p_param) { | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		case PhysicsServer3D::BODY_PARAM_BOUNCE: | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			btBody->setRestitution(p_value); | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		case PhysicsServer3D::BODY_PARAM_FRICTION: | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			btBody->setFriction(p_value); | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		case PhysicsServer3D::BODY_PARAM_MASS: { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			ERR_FAIL_COND(p_value < 0); | 
					
						
							|  |  |  | 			mass = p_value; | 
					
						
							|  |  |  | 			_internal_set_mass(p_value); | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		case PhysicsServer3D::BODY_PARAM_LINEAR_DAMP: | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			linearDamp = p_value; | 
					
						
							| 
									
										
										
										
											2020-03-26 12:32:20 +01:00
										 |  |  | 			// Mark for updating total linear damping.
 | 
					
						
							|  |  |  | 			scratch_space_override_modificator(); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		case PhysicsServer3D::BODY_PARAM_ANGULAR_DAMP: | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			angularDamp = p_value; | 
					
						
							| 
									
										
										
										
											2020-03-26 12:32:20 +01:00
										 |  |  | 			// Mark for updating total angular damping.
 | 
					
						
							|  |  |  | 			scratch_space_override_modificator(); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		case PhysicsServer3D::BODY_PARAM_GRAVITY_SCALE: | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			gravity_scale = p_value; | 
					
						
							| 
									
										
										
										
											2020-03-26 12:32:20 +01:00
										 |  |  | 			// The Bullet gravity will be is set by reload_space_override_modificator.
 | 
					
						
							|  |  |  | 			// Mark for updating total gravity scale.
 | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			scratch_space_override_modificator(); | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		default: | 
					
						
							| 
									
										
										
										
											2019-11-07 09:44:15 +01:00
										 |  |  | 			WARN_PRINT("Parameter " + itos(p_param) + " not supported by bullet. Value: " + itos(p_value)); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | real_t RigidBodyBullet::get_param(PhysicsServer3D::BodyParameter p_param) const { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	switch (p_param) { | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		case PhysicsServer3D::BODY_PARAM_BOUNCE: | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			return btBody->getRestitution(); | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		case PhysicsServer3D::BODY_PARAM_FRICTION: | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			return btBody->getFriction(); | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		case PhysicsServer3D::BODY_PARAM_MASS: { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			const btScalar invMass = btBody->getInvMass(); | 
					
						
							|  |  |  | 			return 0 == invMass ? 0 : 1 / invMass; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		case PhysicsServer3D::BODY_PARAM_LINEAR_DAMP: | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			return linearDamp; | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		case PhysicsServer3D::BODY_PARAM_ANGULAR_DAMP: | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			return angularDamp; | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		case PhysicsServer3D::BODY_PARAM_GRAVITY_SCALE: | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			return gravity_scale; | 
					
						
							|  |  |  | 		default: | 
					
						
							| 
									
										
										
										
											2019-11-07 09:44:15 +01:00
										 |  |  | 			WARN_PRINT("Parameter " + itos(p_param) + " not supported by bullet"); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | void RigidBodyBullet::set_mode(PhysicsServer3D::BodyMode p_mode) { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	// This is necessary to block force_integration untile next move
 | 
					
						
							| 
									
										
										
										
											2018-10-07 07:14:38 +02:00
										 |  |  | 	can_integrate_forces = false; | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	destroy_kinematic_utilities(); | 
					
						
							|  |  |  | 	// The mode change is relevant to its mass
 | 
					
						
							|  |  |  | 	switch (p_mode) { | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		case PhysicsServer3D::BODY_MODE_KINEMATIC: | 
					
						
							|  |  |  | 			mode = PhysicsServer3D::BODY_MODE_KINEMATIC; | 
					
						
							| 
									
										
										
										
											2017-12-10 17:21:14 +01:00
										 |  |  | 			reload_axis_lock(); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			_internal_set_mass(0); | 
					
						
							|  |  |  | 			init_kinematic_utilities(); | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		case PhysicsServer3D::BODY_MODE_STATIC: | 
					
						
							|  |  |  | 			mode = PhysicsServer3D::BODY_MODE_STATIC; | 
					
						
							| 
									
										
										
										
											2017-12-10 17:21:14 +01:00
										 |  |  | 			reload_axis_lock(); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			_internal_set_mass(0); | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		case PhysicsServer3D::BODY_MODE_RIGID: | 
					
						
							|  |  |  | 			mode = PhysicsServer3D::BODY_MODE_RIGID; | 
					
						
							| 
									
										
										
										
											2017-12-10 17:21:14 +01:00
										 |  |  | 			reload_axis_lock(); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			_internal_set_mass(0 == mass ? 1 : mass); | 
					
						
							| 
									
										
										
										
											2017-11-21 18:31:43 +01:00
										 |  |  | 			scratch_space_override_modificator(); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		case PhysicsServer3D::BODY_MODE_CHARACTER: | 
					
						
							|  |  |  | 			mode = PhysicsServer3D::BODY_MODE_CHARACTER; | 
					
						
							| 
									
										
										
										
											2017-12-10 17:21:14 +01:00
										 |  |  | 			reload_axis_lock(); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			_internal_set_mass(0 == mass ? 1 : mass); | 
					
						
							| 
									
										
										
										
											2017-11-21 18:31:43 +01:00
										 |  |  | 			scratch_space_override_modificator(); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			break; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	btBody->setAngularVelocity(btVector3(0, 0, 0)); | 
					
						
							|  |  |  | 	btBody->setLinearVelocity(btVector3(0, 0, 0)); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-14 14:29:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | PhysicsServer3D::BodyMode RigidBodyBullet::get_mode() const { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	return mode; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | void RigidBodyBullet::set_state(PhysicsServer3D::BodyState p_state, const Variant &p_variant) { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	switch (p_state) { | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		case PhysicsServer3D::BODY_STATE_TRANSFORM: | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			set_transform(p_variant); | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		case PhysicsServer3D::BODY_STATE_LINEAR_VELOCITY: | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			set_linear_velocity(p_variant); | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		case PhysicsServer3D::BODY_STATE_ANGULAR_VELOCITY: | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			set_angular_velocity(p_variant); | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		case PhysicsServer3D::BODY_STATE_SLEEPING: | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			set_activation_state(!bool(p_variant)); | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		case PhysicsServer3D::BODY_STATE_CAN_SLEEP: | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			can_sleep = bool(p_variant); | 
					
						
							|  |  |  | 			if (!can_sleep) { | 
					
						
							|  |  |  | 				// Can't sleep
 | 
					
						
							|  |  |  | 				btBody->forceActivationState(DISABLE_DEACTIVATION); | 
					
						
							| 
									
										
										
										
											2019-04-06 17:11:36 +02:00
										 |  |  | 			} else { | 
					
						
							|  |  |  | 				btBody->forceActivationState(ACTIVE_TAG); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | Variant RigidBodyBullet::get_state(PhysicsServer3D::BodyState p_state) const { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	switch (p_state) { | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		case PhysicsServer3D::BODY_STATE_TRANSFORM: | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			return get_transform(); | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		case PhysicsServer3D::BODY_STATE_LINEAR_VELOCITY: | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			return get_linear_velocity(); | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		case PhysicsServer3D::BODY_STATE_ANGULAR_VELOCITY: | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			return get_angular_velocity(); | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		case PhysicsServer3D::BODY_STATE_SLEEPING: | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			return !is_active(); | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		case PhysicsServer3D::BODY_STATE_CAN_SLEEP: | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			return can_sleep; | 
					
						
							|  |  |  | 		default: | 
					
						
							| 
									
										
										
										
											2019-11-07 09:44:15 +01:00
										 |  |  | 			WARN_PRINT("This state " + itos(p_state) + " is not supported by Bullet"); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			return Variant(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void RigidBodyBullet::apply_central_impulse(const Vector3 &p_impulse) { | 
					
						
							| 
									
										
										
										
											2020-03-26 00:23:34 -04:00
										 |  |  | 	btVector3 btImpulse; | 
					
						
							|  |  |  | 	G_TO_B(p_impulse, btImpulse); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (Vector3() != p_impulse) { | 
					
						
							| 
									
										
										
										
											2017-12-23 05:13:39 +01:00
										 |  |  | 		btBody->activate(); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-03-26 00:23:34 -04:00
										 |  |  | 	btBody->applyCentralImpulse(btImpulse); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 00:23:34 -04:00
										 |  |  | void RigidBodyBullet::apply_impulse(const Vector3 &p_impulse, const Vector3 &p_position) { | 
					
						
							|  |  |  | 	btVector3 btImpulse; | 
					
						
							|  |  |  | 	btVector3 btPosition; | 
					
						
							|  |  |  | 	G_TO_B(p_impulse, btImpulse); | 
					
						
							|  |  |  | 	G_TO_B(p_position, btPosition); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (Vector3() != p_impulse) { | 
					
						
							| 
									
										
										
										
											2017-12-23 05:13:39 +01:00
										 |  |  | 		btBody->activate(); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-03-26 00:23:34 -04:00
										 |  |  | 	btBody->applyImpulse(btImpulse, btPosition); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void RigidBodyBullet::apply_torque_impulse(const Vector3 &p_impulse) { | 
					
						
							|  |  |  | 	btVector3 btImp; | 
					
						
							|  |  |  | 	G_TO_B(p_impulse, btImp); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (Vector3() != p_impulse) { | 
					
						
							| 
									
										
										
										
											2017-12-23 05:13:39 +01:00
										 |  |  | 		btBody->activate(); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	btBody->applyTorqueImpulse(btImp); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 00:23:34 -04:00
										 |  |  | void RigidBodyBullet::apply_force(const Vector3 &p_force, const Vector3 &p_position) { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	btVector3 btForce; | 
					
						
							| 
									
										
										
										
											2020-03-26 00:23:34 -04:00
										 |  |  | 	btVector3 btPosition; | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	G_TO_B(p_force, btForce); | 
					
						
							| 
									
										
										
										
											2020-03-26 00:23:34 -04:00
										 |  |  | 	G_TO_B(p_position, btPosition); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (Vector3() != p_force) { | 
					
						
							| 
									
										
										
										
											2017-12-23 05:13:39 +01:00
										 |  |  | 		btBody->activate(); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-03-26 00:23:34 -04:00
										 |  |  | 	btBody->applyForce(btForce, btPosition); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void RigidBodyBullet::apply_central_force(const Vector3 &p_force) { | 
					
						
							|  |  |  | 	btVector3 btForce; | 
					
						
							|  |  |  | 	G_TO_B(p_force, btForce); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (Vector3() != p_force) { | 
					
						
							| 
									
										
										
										
											2017-12-23 05:13:39 +01:00
										 |  |  | 		btBody->activate(); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	btBody->applyCentralForce(btForce); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void RigidBodyBullet::apply_torque(const Vector3 &p_torque) { | 
					
						
							|  |  |  | 	btVector3 btTorq; | 
					
						
							|  |  |  | 	G_TO_B(p_torque, btTorq); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (Vector3() != p_torque) { | 
					
						
							| 
									
										
										
										
											2017-12-23 05:13:39 +01:00
										 |  |  | 		btBody->activate(); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	btBody->applyTorque(btTorq); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void RigidBodyBullet::set_applied_force(const Vector3 &p_force) { | 
					
						
							|  |  |  | 	btVector3 btVec = btBody->getTotalTorque(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (Vector3() != p_force) { | 
					
						
							| 
									
										
										
										
											2017-12-23 05:13:39 +01:00
										 |  |  | 		btBody->activate(); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	btBody->clearForces(); | 
					
						
							|  |  |  | 	btBody->applyTorque(btVec); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	G_TO_B(p_force, btVec); | 
					
						
							|  |  |  | 	btBody->applyCentralForce(btVec); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Vector3 RigidBodyBullet::get_applied_force() const { | 
					
						
							|  |  |  | 	Vector3 gTotForc; | 
					
						
							|  |  |  | 	B_TO_G(btBody->getTotalForce(), gTotForc); | 
					
						
							|  |  |  | 	return gTotForc; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void RigidBodyBullet::set_applied_torque(const Vector3 &p_torque) { | 
					
						
							|  |  |  | 	btVector3 btVec = btBody->getTotalForce(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (Vector3() != p_torque) { | 
					
						
							| 
									
										
										
										
											2017-12-23 05:13:39 +01:00
										 |  |  | 		btBody->activate(); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	btBody->clearForces(); | 
					
						
							|  |  |  | 	btBody->applyCentralForce(btVec); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	G_TO_B(p_torque, btVec); | 
					
						
							|  |  |  | 	btBody->applyTorque(btVec); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Vector3 RigidBodyBullet::get_applied_torque() const { | 
					
						
							|  |  |  | 	Vector3 gTotTorq; | 
					
						
							|  |  |  | 	B_TO_G(btBody->getTotalTorque(), gTotTorq); | 
					
						
							|  |  |  | 	return gTotTorq; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | void RigidBodyBullet::set_axis_lock(PhysicsServer3D::BodyAxis p_axis, bool lock) { | 
					
						
							| 
									
										
										
										
											2017-12-10 17:21:14 +01:00
										 |  |  | 	if (lock) { | 
					
						
							|  |  |  | 		locked_axis |= p_axis; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		locked_axis &= ~p_axis; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-10 17:21:14 +01:00
										 |  |  | 	reload_axis_lock(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | bool RigidBodyBullet::is_axis_locked(PhysicsServer3D::BodyAxis p_axis) const { | 
					
						
							| 
									
										
										
										
											2017-12-10 17:21:14 +01:00
										 |  |  | 	return locked_axis & p_axis; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-10 17:21:14 +01:00
										 |  |  | void RigidBodyBullet::reload_axis_lock() { | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 	btBody->setLinearFactor(btVector3(float(!is_axis_locked(PhysicsServer3D::BODY_AXIS_LINEAR_X)), float(!is_axis_locked(PhysicsServer3D::BODY_AXIS_LINEAR_Y)), float(!is_axis_locked(PhysicsServer3D::BODY_AXIS_LINEAR_Z)))); | 
					
						
							|  |  |  | 	if (PhysicsServer3D::BODY_MODE_CHARACTER == mode) { | 
					
						
							| 
									
										
										
										
											2017-12-10 17:21:14 +01:00
										 |  |  | 		/// When character angular is always locked
 | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 		btBody->setAngularFactor(btVector3(0., 0., 0.)); | 
					
						
							| 
									
										
										
										
											2017-12-10 17:21:14 +01:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		btBody->setAngularFactor(btVector3(float(!is_axis_locked(PhysicsServer3D::BODY_AXIS_ANGULAR_X)), float(!is_axis_locked(PhysicsServer3D::BODY_AXIS_ANGULAR_Y)), float(!is_axis_locked(PhysicsServer3D::BODY_AXIS_ANGULAR_Z)))); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void RigidBodyBullet::set_continuous_collision_detection(bool p_enable) { | 
					
						
							|  |  |  | 	if (p_enable) { | 
					
						
							|  |  |  | 		// This threshold enable CCD if the object moves more than
 | 
					
						
							|  |  |  | 		// 1 meter in one simulation frame
 | 
					
						
							| 
									
										
										
										
											2019-05-16 18:56:37 +02:00
										 |  |  | 		btBody->setCcdMotionThreshold(1e-7); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		/// Calculate using the rule writte below the CCD swept sphere radius
 | 
					
						
							|  |  |  | 		///     CCD works on an embedded sphere of radius, make sure this radius
 | 
					
						
							|  |  |  | 		///     is embedded inside the convex objects, preferably smaller:
 | 
					
						
							| 
									
										
										
										
											2018-01-18 21:37:17 +01:00
										 |  |  | 		///     for an object of dimensions 1 meter, try 0.2
 | 
					
						
							| 
									
										
										
										
											2019-05-16 18:56:37 +02:00
										 |  |  | 		btScalar radius(1.0); | 
					
						
							| 
									
										
										
										
											2018-09-07 20:38:30 +02:00
										 |  |  | 		if (btBody->getCollisionShape()) { | 
					
						
							|  |  |  | 			btVector3 center; | 
					
						
							|  |  |  | 			btBody->getCollisionShape()->getBoundingSphere(center, radius); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 		btBody->setCcdSweptSphereRadius(radius * 0.2); | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2019-05-16 18:56:37 +02:00
										 |  |  | 		btBody->setCcdMotionThreshold(10000.0); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 		btBody->setCcdSweptSphereRadius(0.); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool RigidBodyBullet::is_continuous_collision_detection_enabled() const { | 
					
						
							| 
									
										
										
										
											2018-09-07 20:38:30 +02:00
										 |  |  | 	return 0. < btBody->getCcdMotionThreshold(); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void RigidBodyBullet::set_linear_velocity(const Vector3 &p_velocity) { | 
					
						
							|  |  |  | 	btVector3 btVec; | 
					
						
							|  |  |  | 	G_TO_B(p_velocity, btVec); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (Vector3() != p_velocity) { | 
					
						
							| 
									
										
										
										
											2017-12-23 05:13:39 +01:00
										 |  |  | 		btBody->activate(); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	btBody->setLinearVelocity(btVec); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Vector3 RigidBodyBullet::get_linear_velocity() const { | 
					
						
							|  |  |  | 	Vector3 gVec; | 
					
						
							|  |  |  | 	B_TO_G(btBody->getLinearVelocity(), gVec); | 
					
						
							|  |  |  | 	return gVec; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void RigidBodyBullet::set_angular_velocity(const Vector3 &p_velocity) { | 
					
						
							|  |  |  | 	btVector3 btVec; | 
					
						
							|  |  |  | 	G_TO_B(p_velocity, btVec); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (Vector3() != p_velocity) { | 
					
						
							| 
									
										
										
										
											2017-12-23 05:13:39 +01:00
										 |  |  | 		btBody->activate(); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	btBody->setAngularVelocity(btVec); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Vector3 RigidBodyBullet::get_angular_velocity() const { | 
					
						
							|  |  |  | 	Vector3 gVec; | 
					
						
							|  |  |  | 	B_TO_G(btBody->getAngularVelocity(), gVec); | 
					
						
							|  |  |  | 	return gVec; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void RigidBodyBullet::set_transform__bullet(const btTransform &p_global_transform) { | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 	if (mode == PhysicsServer3D::BODY_MODE_KINEMATIC) { | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 		if (space && space->get_delta_time() != 0) { | 
					
						
							| 
									
										
										
										
											2018-09-19 19:46:07 +02:00
										 |  |  | 			btBody->setLinearVelocity((p_global_transform.getOrigin() - btBody->getWorldTransform().getOrigin()) / space->get_delta_time()); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 		// The kinematic use MotionState class
 | 
					
						
							|  |  |  | 		godotMotionState->moveBody(p_global_transform); | 
					
						
							| 
									
										
										
										
											2019-01-28 08:30:26 +01:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2020-01-21 00:20:57 +01:00
										 |  |  | 		// Is necessary to avoid wrong location on the rendering side on the next frame
 | 
					
						
							| 
									
										
										
										
											2019-01-28 08:30:26 +01:00
										 |  |  | 		godotMotionState->setWorldTransform(p_global_transform); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-10-07 07:14:38 +02:00
										 |  |  | 	CollisionObjectBullet::set_transform__bullet(p_global_transform); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const btTransform &RigidBodyBullet::get_transform__bullet() const { | 
					
						
							|  |  |  | 	if (is_static()) { | 
					
						
							|  |  |  | 		return RigidCollisionObjectBullet::get_transform__bullet(); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		return godotMotionState->getCurrentWorldTransform(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-21 12:48:40 +02:00
										 |  |  | void RigidBodyBullet::do_reload_shapes() { | 
					
						
							|  |  |  | 	RigidCollisionObjectBullet::do_reload_shapes(); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	const btScalar invMass = btBody->getInvMass(); | 
					
						
							|  |  |  | 	const btScalar mass = invMass == 0 ? 0 : 1 / invMass; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-06 18:19:05 +02:00
										 |  |  | 	if (mainShape) { | 
					
						
							| 
									
										
										
										
											2018-11-16 14:20:09 +00:00
										 |  |  | 		// inertia initialised zero here because some of bullet's collision
 | 
					
						
							|  |  |  | 		// shapes incorrectly do not set the vector in calculateLocalIntertia.
 | 
					
						
							|  |  |  | 		// Arbitrary zero is preferable to undefined behaviour.
 | 
					
						
							|  |  |  | 		btVector3 inertia(0, 0, 0); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 		if (EMPTY_SHAPE_PROXYTYPE != mainShape->getShapeType()) { // Necessary to avoid assertion of the empty shape
 | 
					
						
							| 
									
										
										
										
											2019-01-30 10:33:33 +01:00
										 |  |  | 			mainShape->calculateLocalInertia(mass, inertia); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-09-06 18:19:05 +02:00
										 |  |  | 		btBody->setMassProps(mass, inertia); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	btBody->updateInertiaTensor(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	reload_kinematic_shapes(); | 
					
						
							| 
									
										
										
										
											2019-05-16 18:56:37 +02:00
										 |  |  | 	set_continuous_collision_detection(btBody->getCcdMotionThreshold() < 9998.0); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	reload_body(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void RigidBodyBullet::on_enter_area(AreaBullet *p_area) { | 
					
						
							|  |  |  | 	/// Add this area to the array in an ordered way
 | 
					
						
							|  |  |  | 	++areaWhereIamCount; | 
					
						
							|  |  |  | 	if (areaWhereIamCount >= maxAreasWhereIam) { | 
					
						
							|  |  |  | 		--areaWhereIamCount; | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for (int i = 0; i < areaWhereIamCount; ++i) { | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 		if (nullptr == areasWhereIam[i]) { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			// This area has the highest priority
 | 
					
						
							| 
									
										
										
										
											2020-07-10 09:21:12 +02:00
										 |  |  | 			areasWhereIam[i] = p_area; | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			break; | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			if (areasWhereIam[i]->get_spOv_priority() > p_area->get_spOv_priority()) { | 
					
						
							|  |  |  | 				// The position was found, just shift all elements
 | 
					
						
							|  |  |  | 				for (int j = i; j < areaWhereIamCount; ++j) { | 
					
						
							| 
									
										
										
										
											2020-07-10 09:21:12 +02:00
										 |  |  | 					areasWhereIam[j + 1] = areasWhereIam[j]; | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2020-07-10 09:21:12 +02:00
										 |  |  | 				areasWhereIam[i] = p_area; | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 				break; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 	if (PhysicsServer3D::AREA_SPACE_OVERRIDE_DISABLED != p_area->get_spOv_mode()) { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 		scratch_space_override_modificator(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (p_area->is_spOv_gravityPoint()) { | 
					
						
							|  |  |  | 		++countGravityPointSpaces; | 
					
						
							| 
									
										
										
										
											2019-06-22 15:52:51 +03:00
										 |  |  | 		ERR_FAIL_COND(countGravityPointSpaces <= 0); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void RigidBodyBullet::on_exit_area(AreaBullet *p_area) { | 
					
						
							|  |  |  | 	RigidCollisionObjectBullet::on_exit_area(p_area); | 
					
						
							|  |  |  | 	/// Remove this area and keep the order
 | 
					
						
							|  |  |  | 	/// N.B. Since I don't want resize the array I can't use the "erase" function
 | 
					
						
							|  |  |  | 	bool wasTheAreaFound = false; | 
					
						
							|  |  |  | 	for (int i = 0; i < areaWhereIamCount; ++i) { | 
					
						
							|  |  |  | 		if (p_area == areasWhereIam[i]) { | 
					
						
							| 
									
										
										
										
											2018-09-12 21:38:39 -04:00
										 |  |  | 			// The area was found, just shift down all elements
 | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			for (int j = i; j < areaWhereIamCount; ++j) { | 
					
						
							| 
									
										
										
										
											2020-07-10 09:21:12 +02:00
										 |  |  | 				areasWhereIam[j] = areasWhereIam[j + 1]; | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			wasTheAreaFound = true; | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (wasTheAreaFound) { | 
					
						
							|  |  |  | 		if (p_area->is_spOv_gravityPoint()) { | 
					
						
							|  |  |  | 			--countGravityPointSpaces; | 
					
						
							| 
									
										
										
										
											2019-06-22 15:52:51 +03:00
										 |  |  | 			ERR_FAIL_COND(countGravityPointSpaces < 0); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		--areaWhereIamCount; | 
					
						
							| 
									
										
										
										
											2020-07-10 09:21:12 +02:00
										 |  |  | 		areasWhereIam[areaWhereIamCount] = nullptr; // Even if this is not required, I clear the last element to be safe
 | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		if (PhysicsServer3D::AREA_SPACE_OVERRIDE_DISABLED != p_area->get_spOv_mode()) { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			scratch_space_override_modificator(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void RigidBodyBullet::reload_space_override_modificator() { | 
					
						
							| 
									
										
										
										
											2020-08-03 11:26:44 +01:00
										 |  |  | 	if (mode == PhysicsServer3D::BODY_MODE_STATIC) { | 
					
						
							| 
									
										
										
										
											2017-12-23 05:13:39 +01:00
										 |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-12-23 05:13:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-10 09:21:12 +02:00
										 |  |  | 	Vector3 newGravity; | 
					
						
							| 
									
										
										
										
											2020-03-26 12:32:20 +01:00
										 |  |  | 	real_t newLinearDamp = MAX(0.0, linearDamp); | 
					
						
							|  |  |  | 	real_t newAngularDamp = MAX(0.0, angularDamp); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 12:32:20 +01:00
										 |  |  | 	bool stopped = false; | 
					
						
							| 
									
										
										
										
											2020-07-10 09:21:12 +02:00
										 |  |  | 	for (int i = 0; i < areaWhereIamCount && !stopped; i += 1) { | 
					
						
							|  |  |  | 		AreaBullet *currentArea = areasWhereIam[i]; | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		if (!currentArea || PhysicsServer3D::AREA_SPACE_OVERRIDE_DISABLED == currentArea->get_spOv_mode()) { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			continue; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-10 09:21:12 +02:00
										 |  |  | 		Vector3 support_gravity; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 		/// Here is calculated the gravity
 | 
					
						
							|  |  |  | 		if (currentArea->is_spOv_gravityPoint()) { | 
					
						
							|  |  |  | 			/// It calculates the direction of new gravity
 | 
					
						
							|  |  |  | 			support_gravity = currentArea->get_transform().xform(currentArea->get_spOv_gravityVec()) - get_transform().get_origin(); | 
					
						
							| 
									
										
										
										
											2020-07-10 09:21:12 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			const real_t distanceMag = support_gravity.length(); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			// Normalized in this way to avoid the double call of function "length()"
 | 
					
						
							|  |  |  | 			if (distanceMag == 0) { | 
					
						
							| 
									
										
										
										
											2020-07-10 09:21:12 +02:00
										 |  |  | 				support_gravity = Vector3(); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2020-07-10 09:21:12 +02:00
										 |  |  | 				support_gravity /= distanceMag; | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			/// Here is calculated the final gravity
 | 
					
						
							|  |  |  | 			if (currentArea->get_spOv_gravityPointDistanceScale() > 0) { | 
					
						
							|  |  |  | 				// Scaled gravity by distance
 | 
					
						
							|  |  |  | 				support_gravity *= currentArea->get_spOv_gravityMag() / Math::pow(distanceMag * currentArea->get_spOv_gravityPointDistanceScale() + 1, 2); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				// Unscaled gravity
 | 
					
						
							|  |  |  | 				support_gravity *= currentArea->get_spOv_gravityMag(); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			support_gravity = currentArea->get_spOv_gravityVec() * currentArea->get_spOv_gravityMag(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		switch (currentArea->get_spOv_mode()) { | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 			case PhysicsServer3D::AREA_SPACE_OVERRIDE_DISABLED: | 
					
						
							| 
									
										
										
										
											2018-09-26 13:13:56 +02:00
										 |  |  | 				/// This area does not affect gravity/damp. These are generally areas
 | 
					
						
							|  |  |  | 				/// that exist only to detect collisions, and objects entering or exiting them.
 | 
					
						
							|  |  |  | 				break; | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 			case PhysicsServer3D::AREA_SPACE_OVERRIDE_COMBINE: | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 				/// This area adds its gravity/damp values to whatever has been
 | 
					
						
							|  |  |  | 				/// calculated so far. This way, many overlapping areas can combine
 | 
					
						
							|  |  |  | 				/// their physics to make interesting
 | 
					
						
							|  |  |  | 				newGravity += support_gravity; | 
					
						
							|  |  |  | 				newLinearDamp += currentArea->get_spOv_linearDamp(); | 
					
						
							|  |  |  | 				newAngularDamp += currentArea->get_spOv_angularDamp(); | 
					
						
							|  |  |  | 				break; | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 			case PhysicsServer3D::AREA_SPACE_OVERRIDE_COMBINE_REPLACE: | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 				/// This area adds its gravity/damp values to whatever has been calculated
 | 
					
						
							|  |  |  | 				/// so far. Then stops taking into account the rest of the areas, even the
 | 
					
						
							|  |  |  | 				/// default one.
 | 
					
						
							|  |  |  | 				newGravity += support_gravity; | 
					
						
							|  |  |  | 				newLinearDamp += currentArea->get_spOv_linearDamp(); | 
					
						
							|  |  |  | 				newAngularDamp += currentArea->get_spOv_angularDamp(); | 
					
						
							| 
									
										
										
										
											2020-03-26 12:32:20 +01:00
										 |  |  | 				stopped = true; | 
					
						
							|  |  |  | 				break; | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 			case PhysicsServer3D::AREA_SPACE_OVERRIDE_REPLACE: | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 				/// This area replaces any gravity/damp, even the default one, and
 | 
					
						
							|  |  |  | 				/// stops taking into account the rest of the areas.
 | 
					
						
							|  |  |  | 				newGravity = support_gravity; | 
					
						
							|  |  |  | 				newLinearDamp = currentArea->get_spOv_linearDamp(); | 
					
						
							|  |  |  | 				newAngularDamp = currentArea->get_spOv_angularDamp(); | 
					
						
							| 
									
										
										
										
											2020-03-26 12:32:20 +01:00
										 |  |  | 				stopped = true; | 
					
						
							|  |  |  | 				break; | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 			case PhysicsServer3D::AREA_SPACE_OVERRIDE_REPLACE_COMBINE: | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 				/// This area replaces any gravity/damp calculated so far, but keeps
 | 
					
						
							|  |  |  | 				/// calculating the rest of the areas, down to the default one.
 | 
					
						
							|  |  |  | 				newGravity = support_gravity; | 
					
						
							|  |  |  | 				newLinearDamp = currentArea->get_spOv_linearDamp(); | 
					
						
							|  |  |  | 				newAngularDamp = currentArea->get_spOv_angularDamp(); | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 12:32:20 +01:00
										 |  |  | 	// Add default gravity and damping from space.
 | 
					
						
							|  |  |  | 	if (!stopped) { | 
					
						
							|  |  |  | 		newGravity += space->get_gravity_direction() * space->get_gravity_magnitude(); | 
					
						
							|  |  |  | 		newLinearDamp += space->get_linear_damp(); | 
					
						
							|  |  |  | 		newAngularDamp += space->get_angular_damp(); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-10 09:21:12 +02:00
										 |  |  | 	total_gravity = newGravity; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (omit_forces_integration) { | 
					
						
							|  |  |  | 		// Custom behaviour.
 | 
					
						
							|  |  |  | 		btBody->setGravity(btVector3(0, 0, 0)); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		btVector3 newBtGravity; | 
					
						
							|  |  |  | 		G_TO_B(newGravity * gravity_scale, newBtGravity); | 
					
						
							|  |  |  | 		btBody->setGravity(newBtGravity); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	btBody->setDamping(newLinearDamp, newAngularDamp); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void RigidBodyBullet::reload_kinematic_shapes() { | 
					
						
							|  |  |  | 	if (!kinematic_utilities) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	kinematic_utilities->copyAllOwnerShapes(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-07 07:14:38 +02:00
										 |  |  | void RigidBodyBullet::notify_transform_changed() { | 
					
						
							|  |  |  | 	RigidCollisionObjectBullet::notify_transform_changed(); | 
					
						
							|  |  |  | 	can_integrate_forces = true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | void RigidBodyBullet::_internal_set_mass(real_t p_mass) { | 
					
						
							|  |  |  | 	btVector3 localInertia(0, 0, 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	int clearedCurrentFlags = btBody->getCollisionFlags(); | 
					
						
							|  |  |  | 	clearedCurrentFlags &= ~(btCollisionObject::CF_KINEMATIC_OBJECT | btCollisionObject::CF_STATIC_OBJECT | btCollisionObject::CF_CHARACTER_OBJECT); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Rigidbody is dynamic if and only if mass is non Zero, otherwise static
 | 
					
						
							|  |  |  | 	const bool isDynamic = p_mass != 0.f; | 
					
						
							|  |  |  | 	if (isDynamic) { | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 		if (PhysicsServer3D::BODY_MODE_RIGID != mode && PhysicsServer3D::BODY_MODE_CHARACTER != mode) { | 
					
						
							| 
									
										
										
										
											2018-01-21 14:23:30 -03:00
										 |  |  | 			return; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		m_isStatic = false; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 		if (mainShape) { | 
					
						
							| 
									
										
										
										
											2018-09-06 18:19:05 +02:00
										 |  |  | 			mainShape->calculateLocalInertia(p_mass, localInertia); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		if (PhysicsServer3D::BODY_MODE_RIGID == mode) { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			btBody->setCollisionFlags(clearedCurrentFlags); // Just set the flags without Kin and Static
 | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			btBody->setCollisionFlags(clearedCurrentFlags | btCollisionObject::CF_CHARACTER_OBJECT); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (can_sleep) { | 
					
						
							|  |  |  | 			btBody->forceActivationState(ACTIVE_TAG); // ACTIVE_TAG 1
 | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			btBody->forceActivationState(DISABLE_DEACTIVATION); // DISABLE_DEACTIVATION 4
 | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 		if (PhysicsServer3D::BODY_MODE_STATIC != mode && PhysicsServer3D::BODY_MODE_KINEMATIC != mode) { | 
					
						
							| 
									
										
										
										
											2018-01-21 14:23:30 -03:00
										 |  |  | 			return; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		m_isStatic = true; | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		if (PhysicsServer3D::BODY_MODE_STATIC == mode) { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			btBody->setCollisionFlags(clearedCurrentFlags | btCollisionObject::CF_STATIC_OBJECT); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			btBody->setCollisionFlags(clearedCurrentFlags | btCollisionObject::CF_KINEMATIC_OBJECT); | 
					
						
							|  |  |  | 			set_transform__bullet(btBody->getWorldTransform()); // Set current Transform using kinematic method
 | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		btBody->forceActivationState(DISABLE_SIMULATION); // DISABLE_SIMULATION 5
 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	btBody->setMassProps(p_mass, localInertia); | 
					
						
							|  |  |  | 	btBody->updateInertiaTensor(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	reload_body(); | 
					
						
							|  |  |  | } |