Path: blob/master/scene/resources/3d/cylinder_shape_3d.cpp
9898 views
/**************************************************************************/1/* cylinder_shape_3d.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 "cylinder_shape_3d.h"3132#include "scene/resources/3d/primitive_meshes.h"33#include "servers/physics_server_3d.h"3435Vector<Vector3> CylinderShape3D::get_debug_mesh_lines() const {36float c_radius = get_radius();37float c_height = get_height();3839Vector<Vector3> points;4041Vector3 d(0, c_height * 0.5, 0);42for (int i = 0; i < 360; i++) {43float ra = Math::deg_to_rad((float)i);44float rb = Math::deg_to_rad((float)i + 1);45Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * c_radius;46Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * c_radius;4748points.push_back(Vector3(a.x, 0, a.y) + d);49points.push_back(Vector3(b.x, 0, b.y) + d);5051points.push_back(Vector3(a.x, 0, a.y) - d);52points.push_back(Vector3(b.x, 0, b.y) - d);5354if (i % 90 == 0) {55points.push_back(Vector3(a.x, 0, a.y) + d);56points.push_back(Vector3(a.x, 0, a.y) - d);57}58}5960return points;61}6263Ref<ArrayMesh> CylinderShape3D::get_debug_arraymesh_faces(const Color &p_modulate) const {64Array cylinder_array;65cylinder_array.resize(RS::ARRAY_MAX);66CylinderMesh::create_mesh_array(cylinder_array, radius, radius, height, 32);6768Vector<Color> colors;69const PackedVector3Array &verts = cylinder_array[RS::ARRAY_VERTEX];70const int32_t verts_size = verts.size();71for (int i = 0; i < verts_size; i++) {72colors.append(p_modulate);73}7475Ref<ArrayMesh> cylinder_mesh = memnew(ArrayMesh);76cylinder_array[RS::ARRAY_COLOR] = colors;77cylinder_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, cylinder_array);78return cylinder_mesh;79}8081real_t CylinderShape3D::get_enclosing_radius() const {82return Vector2(radius, height * 0.5).length();83}8485void CylinderShape3D::_update_shape() {86Dictionary d;87d["radius"] = radius;88d["height"] = height;89PhysicsServer3D::get_singleton()->shape_set_data(get_shape(), d);90Shape3D::_update_shape();91}9293void CylinderShape3D::set_radius(float p_radius) {94ERR_FAIL_COND_MSG(p_radius < 0, "CylinderShape3D radius cannot be negative.");95radius = p_radius;96_update_shape();97emit_changed();98}99100float CylinderShape3D::get_radius() const {101return radius;102}103104void CylinderShape3D::set_height(float p_height) {105ERR_FAIL_COND_MSG(p_height < 0, "CylinderShape3D height cannot be negative.");106height = p_height;107_update_shape();108emit_changed();109}110111float CylinderShape3D::get_height() const {112return height;113}114115void CylinderShape3D::_bind_methods() {116ClassDB::bind_method(D_METHOD("set_radius", "radius"), &CylinderShape3D::set_radius);117ClassDB::bind_method(D_METHOD("get_radius"), &CylinderShape3D::get_radius);118ClassDB::bind_method(D_METHOD("set_height", "height"), &CylinderShape3D::set_height);119ClassDB::bind_method(D_METHOD("get_height"), &CylinderShape3D::get_height);120121ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "height", PROPERTY_HINT_RANGE, "0.001,100,0.001,or_greater,suffix:m"), "set_height", "get_height");122ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0.001,100,0.001,or_greater,suffix:m"), "set_radius", "get_radius");123}124125CylinderShape3D::CylinderShape3D() :126Shape3D(PhysicsServer3D::get_singleton()->shape_create(PhysicsServer3D::SHAPE_CYLINDER)) {127_update_shape();128}129130131