Path: blob/master/scene/2d/physics/static_body_2d.cpp
9906 views
/**************************************************************************/1/* static_body_2d.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "static_body_2d.h"3132#ifndef NAVIGATION_2D_DISABLED33#include "scene/resources/2d/capsule_shape_2d.h"34#include "scene/resources/2d/circle_shape_2d.h"35#include "scene/resources/2d/concave_polygon_shape_2d.h"36#include "scene/resources/2d/convex_polygon_shape_2d.h"37#include "scene/resources/2d/navigation_mesh_source_geometry_data_2d.h"38#include "scene/resources/2d/navigation_polygon.h"39#include "scene/resources/2d/rectangle_shape_2d.h"40#include "servers/navigation_server_2d.h"41#endif // NAVIGATION_2D_DISABLED4243Callable StaticBody2D::_navmesh_source_geometry_parsing_callback;44RID StaticBody2D::_navmesh_source_geometry_parser;4546void StaticBody2D::set_constant_linear_velocity(const Vector2 &p_vel) {47constant_linear_velocity = p_vel;4849PhysicsServer2D::get_singleton()->body_set_state(get_rid(), PhysicsServer2D::BODY_STATE_LINEAR_VELOCITY, constant_linear_velocity);50}5152void StaticBody2D::set_constant_angular_velocity(real_t p_vel) {53constant_angular_velocity = p_vel;5455PhysicsServer2D::get_singleton()->body_set_state(get_rid(), PhysicsServer2D::BODY_STATE_ANGULAR_VELOCITY, constant_angular_velocity);56}5758Vector2 StaticBody2D::get_constant_linear_velocity() const {59return constant_linear_velocity;60}6162real_t StaticBody2D::get_constant_angular_velocity() const {63return constant_angular_velocity;64}6566void StaticBody2D::set_physics_material_override(const Ref<PhysicsMaterial> &p_physics_material_override) {67if (physics_material_override.is_valid()) {68physics_material_override->disconnect_changed(callable_mp(this, &StaticBody2D::_reload_physics_characteristics));69}7071physics_material_override = p_physics_material_override;7273if (physics_material_override.is_valid()) {74physics_material_override->connect_changed(callable_mp(this, &StaticBody2D::_reload_physics_characteristics));75}76_reload_physics_characteristics();77}7879Ref<PhysicsMaterial> StaticBody2D::get_physics_material_override() const {80return physics_material_override;81}8283void StaticBody2D::_reload_physics_characteristics() {84if (physics_material_override.is_null()) {85PhysicsServer2D::get_singleton()->body_set_param(get_rid(), PhysicsServer2D::BODY_PARAM_BOUNCE, 0);86PhysicsServer2D::get_singleton()->body_set_param(get_rid(), PhysicsServer2D::BODY_PARAM_FRICTION, 1);87} else {88PhysicsServer2D::get_singleton()->body_set_param(get_rid(), PhysicsServer2D::BODY_PARAM_BOUNCE, physics_material_override->computed_bounce());89PhysicsServer2D::get_singleton()->body_set_param(get_rid(), PhysicsServer2D::BODY_PARAM_FRICTION, physics_material_override->computed_friction());90}91}9293#ifndef NAVIGATION_2D_DISABLED94void StaticBody2D::navmesh_parse_init() {95ERR_FAIL_NULL(NavigationServer2D::get_singleton());96if (!_navmesh_source_geometry_parser.is_valid()) {97_navmesh_source_geometry_parsing_callback = callable_mp_static(&StaticBody2D::navmesh_parse_source_geometry);98_navmesh_source_geometry_parser = NavigationServer2D::get_singleton()->source_geometry_parser_create();99NavigationServer2D::get_singleton()->source_geometry_parser_set_callback(_navmesh_source_geometry_parser, _navmesh_source_geometry_parsing_callback);100}101}102103void StaticBody2D::navmesh_parse_source_geometry(const Ref<NavigationPolygon> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData2D> p_source_geometry_data, Node *p_node) {104StaticBody2D *static_body = Object::cast_to<StaticBody2D>(p_node);105106if (static_body == nullptr) {107return;108}109110NavigationPolygon::ParsedGeometryType parsed_geometry_type = p_navigation_mesh->get_parsed_geometry_type();111if (!(parsed_geometry_type == NavigationPolygon::PARSED_GEOMETRY_STATIC_COLLIDERS || parsed_geometry_type == NavigationPolygon::PARSED_GEOMETRY_BOTH)) {112return;113}114115uint32_t parsed_collision_mask = p_navigation_mesh->get_parsed_collision_mask();116if (!(static_body->get_collision_layer() & parsed_collision_mask)) {117return;118}119120List<uint32_t> shape_owners;121static_body->get_shape_owners(&shape_owners);122123for (uint32_t shape_owner : shape_owners) {124if (static_body->is_shape_owner_disabled(shape_owner)) {125continue;126}127128const int shape_count = static_body->shape_owner_get_shape_count(shape_owner);129130for (int shape_index = 0; shape_index < shape_count; shape_index++) {131Ref<Shape2D> s = static_body->shape_owner_get_shape(shape_owner, shape_index);132133if (s.is_null()) {134continue;135}136137const Transform2D static_body_xform = p_source_geometry_data->root_node_transform * static_body->get_global_transform() * static_body->shape_owner_get_transform(shape_owner);138139RectangleShape2D *rectangle_shape = Object::cast_to<RectangleShape2D>(*s);140if (rectangle_shape) {141Vector<Vector2> shape_outline;142143const Vector2 &rectangle_size = rectangle_shape->get_size();144145shape_outline.resize(5);146shape_outline.write[0] = static_body_xform.xform(-rectangle_size * 0.5);147shape_outline.write[1] = static_body_xform.xform(Vector2(rectangle_size.x, -rectangle_size.y) * 0.5);148shape_outline.write[2] = static_body_xform.xform(rectangle_size * 0.5);149shape_outline.write[3] = static_body_xform.xform(Vector2(-rectangle_size.x, rectangle_size.y) * 0.5);150shape_outline.write[4] = static_body_xform.xform(-rectangle_size * 0.5);151152p_source_geometry_data->add_obstruction_outline(shape_outline);153}154155CapsuleShape2D *capsule_shape = Object::cast_to<CapsuleShape2D>(*s);156if (capsule_shape) {157const real_t capsule_height = capsule_shape->get_height();158const real_t capsule_radius = capsule_shape->get_radius();159160Vector<Vector2> shape_outline;161const real_t turn_step = Math::TAU / 12.0;162shape_outline.resize(14);163int shape_outline_inx = 0;164for (int i = 0; i < 12; i++) {165Vector2 ofs = Vector2(0, (i > 3 && i <= 9) ? capsule_height * 0.5 - capsule_radius : -capsule_height * 0.5 + capsule_radius);166167shape_outline.write[shape_outline_inx] = static_body_xform.xform(Vector2(Math::sin(i * turn_step), -Math::cos(i * turn_step)) * capsule_radius + ofs);168shape_outline_inx += 1;169if (i == 3 || i == 9) {170shape_outline.write[shape_outline_inx] = static_body_xform.xform(Vector2(Math::sin(i * turn_step), -Math::cos(i * turn_step)) * capsule_radius - ofs);171shape_outline_inx += 1;172}173}174175p_source_geometry_data->add_obstruction_outline(shape_outline);176}177178CircleShape2D *circle_shape = Object::cast_to<CircleShape2D>(*s);179if (circle_shape) {180const real_t circle_radius = circle_shape->get_radius();181182Vector<Vector2> shape_outline;183int circle_edge_count = 12;184shape_outline.resize(circle_edge_count);185186const real_t turn_step = Math::TAU / real_t(circle_edge_count);187for (int i = 0; i < circle_edge_count; i++) {188shape_outline.write[i] = static_body_xform.xform(Vector2(Math::cos(i * turn_step), Math::sin(i * turn_step)) * circle_radius);189}190191p_source_geometry_data->add_obstruction_outline(shape_outline);192}193194ConcavePolygonShape2D *concave_polygon_shape = Object::cast_to<ConcavePolygonShape2D>(*s);195if (concave_polygon_shape) {196Vector<Vector2> shape_outline = concave_polygon_shape->get_segments();197198for (int i = 0; i < shape_outline.size(); i++) {199shape_outline.write[i] = static_body_xform.xform(shape_outline[i]);200}201202p_source_geometry_data->add_obstruction_outline(shape_outline);203}204205ConvexPolygonShape2D *convex_polygon_shape = Object::cast_to<ConvexPolygonShape2D>(*s);206if (convex_polygon_shape) {207Vector<Vector2> shape_outline = convex_polygon_shape->get_points();208209for (int i = 0; i < shape_outline.size(); i++) {210shape_outline.write[i] = static_body_xform.xform(shape_outline[i]);211}212213p_source_geometry_data->add_obstruction_outline(shape_outline);214}215}216}217}218#endif // NAVIGATION_2D_DISABLED219220void StaticBody2D::_bind_methods() {221ClassDB::bind_method(D_METHOD("set_constant_linear_velocity", "vel"), &StaticBody2D::set_constant_linear_velocity);222ClassDB::bind_method(D_METHOD("set_constant_angular_velocity", "vel"), &StaticBody2D::set_constant_angular_velocity);223ClassDB::bind_method(D_METHOD("get_constant_linear_velocity"), &StaticBody2D::get_constant_linear_velocity);224ClassDB::bind_method(D_METHOD("get_constant_angular_velocity"), &StaticBody2D::get_constant_angular_velocity);225226ClassDB::bind_method(D_METHOD("set_physics_material_override", "physics_material_override"), &StaticBody2D::set_physics_material_override);227ClassDB::bind_method(D_METHOD("get_physics_material_override"), &StaticBody2D::get_physics_material_override);228229ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "physics_material_override", PROPERTY_HINT_RESOURCE_TYPE, "PhysicsMaterial"), "set_physics_material_override", "get_physics_material_override");230ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "constant_linear_velocity", PROPERTY_HINT_NONE, "suffix:px/s"), "set_constant_linear_velocity", "get_constant_linear_velocity");231ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "constant_angular_velocity", PROPERTY_HINT_NONE, U"radians_as_degrees,suffix:\u00B0/s"), "set_constant_angular_velocity", "get_constant_angular_velocity");232}233234StaticBody2D::StaticBody2D(PhysicsServer2D::BodyMode p_mode) :235PhysicsBody2D(p_mode) {236}237238239