2021-06-04 23:09:41 +00:00
|
|
|
/*************************************************************************/
|
2021-10-18 19:24:30 +00:00
|
|
|
/* godot_body_direct_state_2d.cpp */
|
2021-06-04 23:09:41 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
#include "godot_body_direct_state_2d.h"
|
2021-06-04 23:09:41 +00:00
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
#include "godot_body_2d.h"
|
|
|
|
#include "godot_physics_server_2d.h"
|
|
|
|
#include "godot_space_2d.h"
|
2021-06-04 23:09:41 +00:00
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
Vector2 GodotPhysicsDirectBodyState2D::get_total_gravity() const {
|
2021-06-04 23:09:41 +00:00
|
|
|
return body->gravity;
|
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
real_t GodotPhysicsDirectBodyState2D::get_total_angular_damp() const {
|
2021-06-04 23:09:41 +00:00
|
|
|
return body->area_angular_damp;
|
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
real_t GodotPhysicsDirectBodyState2D::get_total_linear_damp() const {
|
2021-06-04 23:09:41 +00:00
|
|
|
return body->area_linear_damp;
|
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
Vector2 GodotPhysicsDirectBodyState2D::get_center_of_mass() const {
|
2021-06-11 00:37:19 +00:00
|
|
|
return body->get_center_of_mass();
|
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
real_t GodotPhysicsDirectBodyState2D::get_inverse_mass() const {
|
2021-06-04 23:09:41 +00:00
|
|
|
return body->get_inv_mass();
|
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
real_t GodotPhysicsDirectBodyState2D::get_inverse_inertia() const {
|
2021-06-04 23:09:41 +00:00
|
|
|
return body->get_inv_inertia();
|
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
void GodotPhysicsDirectBodyState2D::set_linear_velocity(const Vector2 &p_velocity) {
|
2021-10-03 18:57:26 +00:00
|
|
|
body->wakeup();
|
2021-06-04 23:09:41 +00:00
|
|
|
body->set_linear_velocity(p_velocity);
|
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
Vector2 GodotPhysicsDirectBodyState2D::get_linear_velocity() const {
|
2021-06-04 23:09:41 +00:00
|
|
|
return body->get_linear_velocity();
|
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
void GodotPhysicsDirectBodyState2D::set_angular_velocity(real_t p_velocity) {
|
2021-10-03 18:57:26 +00:00
|
|
|
body->wakeup();
|
2021-06-04 23:09:41 +00:00
|
|
|
body->set_angular_velocity(p_velocity);
|
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
real_t GodotPhysicsDirectBodyState2D::get_angular_velocity() const {
|
2021-06-04 23:09:41 +00:00
|
|
|
return body->get_angular_velocity();
|
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
void GodotPhysicsDirectBodyState2D::set_transform(const Transform2D &p_transform) {
|
2021-06-04 23:09:41 +00:00
|
|
|
body->set_state(PhysicsServer2D::BODY_STATE_TRANSFORM, p_transform);
|
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
Transform2D GodotPhysicsDirectBodyState2D::get_transform() const {
|
2021-06-04 23:09:41 +00:00
|
|
|
return body->get_transform();
|
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
Vector2 GodotPhysicsDirectBodyState2D::get_velocity_at_local_position(const Vector2 &p_position) const {
|
2021-06-04 23:09:41 +00:00
|
|
|
return body->get_velocity_in_local_point(p_position);
|
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
void GodotPhysicsDirectBodyState2D::add_central_force(const Vector2 &p_force) {
|
2021-10-03 18:57:26 +00:00
|
|
|
body->wakeup();
|
2021-06-04 23:09:41 +00:00
|
|
|
body->add_central_force(p_force);
|
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
void GodotPhysicsDirectBodyState2D::add_force(const Vector2 &p_force, const Vector2 &p_position) {
|
2021-10-03 18:57:26 +00:00
|
|
|
body->wakeup();
|
2021-06-04 23:09:41 +00:00
|
|
|
body->add_force(p_force, p_position);
|
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
void GodotPhysicsDirectBodyState2D::add_torque(real_t p_torque) {
|
2021-10-03 18:57:26 +00:00
|
|
|
body->wakeup();
|
2021-06-04 23:09:41 +00:00
|
|
|
body->add_torque(p_torque);
|
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
void GodotPhysicsDirectBodyState2D::apply_central_impulse(const Vector2 &p_impulse) {
|
2021-10-03 18:57:26 +00:00
|
|
|
body->wakeup();
|
2021-06-04 23:09:41 +00:00
|
|
|
body->apply_central_impulse(p_impulse);
|
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
void GodotPhysicsDirectBodyState2D::apply_impulse(const Vector2 &p_impulse, const Vector2 &p_position) {
|
2021-10-03 18:57:26 +00:00
|
|
|
body->wakeup();
|
2021-06-04 23:09:41 +00:00
|
|
|
body->apply_impulse(p_impulse, p_position);
|
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
void GodotPhysicsDirectBodyState2D::apply_torque_impulse(real_t p_torque) {
|
2021-10-03 18:57:26 +00:00
|
|
|
body->wakeup();
|
2021-06-04 23:09:41 +00:00
|
|
|
body->apply_torque_impulse(p_torque);
|
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
void GodotPhysicsDirectBodyState2D::set_sleep_state(bool p_enable) {
|
2021-06-04 23:09:41 +00:00
|
|
|
body->set_active(!p_enable);
|
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
bool GodotPhysicsDirectBodyState2D::is_sleeping() const {
|
2021-06-04 23:09:41 +00:00
|
|
|
return !body->is_active();
|
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
int GodotPhysicsDirectBodyState2D::get_contact_count() const {
|
2021-06-04 23:09:41 +00:00
|
|
|
return body->contact_count;
|
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
Vector2 GodotPhysicsDirectBodyState2D::get_contact_local_position(int p_contact_idx) const {
|
2021-06-04 23:09:41 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, Vector2());
|
|
|
|
return body->contacts[p_contact_idx].local_pos;
|
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
Vector2 GodotPhysicsDirectBodyState2D::get_contact_local_normal(int p_contact_idx) const {
|
2021-06-04 23:09:41 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, Vector2());
|
|
|
|
return body->contacts[p_contact_idx].local_normal;
|
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
int GodotPhysicsDirectBodyState2D::get_contact_local_shape(int p_contact_idx) const {
|
2021-06-04 23:09:41 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, -1);
|
|
|
|
return body->contacts[p_contact_idx].local_shape;
|
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
RID GodotPhysicsDirectBodyState2D::get_contact_collider(int p_contact_idx) const {
|
2021-06-04 23:09:41 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, RID());
|
|
|
|
return body->contacts[p_contact_idx].collider;
|
|
|
|
}
|
2021-10-18 19:24:30 +00:00
|
|
|
Vector2 GodotPhysicsDirectBodyState2D::get_contact_collider_position(int p_contact_idx) const {
|
2021-06-04 23:09:41 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, Vector2());
|
|
|
|
return body->contacts[p_contact_idx].collider_pos;
|
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
ObjectID GodotPhysicsDirectBodyState2D::get_contact_collider_id(int p_contact_idx) const {
|
2021-06-04 23:09:41 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, ObjectID());
|
|
|
|
return body->contacts[p_contact_idx].collider_instance_id;
|
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
int GodotPhysicsDirectBodyState2D::get_contact_collider_shape(int p_contact_idx) const {
|
2021-06-04 23:09:41 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, 0);
|
|
|
|
return body->contacts[p_contact_idx].collider_shape;
|
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
Vector2 GodotPhysicsDirectBodyState2D::get_contact_collider_velocity_at_position(int p_contact_idx) const {
|
2021-06-04 23:09:41 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, Vector2());
|
|
|
|
return body->contacts[p_contact_idx].collider_velocity_at_pos;
|
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
PhysicsDirectSpaceState2D *GodotPhysicsDirectBodyState2D::get_space_state() {
|
2021-06-04 23:09:41 +00:00
|
|
|
return body->get_space()->get_direct_state();
|
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
real_t GodotPhysicsDirectBodyState2D::get_step() const {
|
2021-06-04 23:09:41 +00:00
|
|
|
return body->get_space()->get_last_step();
|
|
|
|
}
|