Path: blob/master/scene/resources/3d/sphere_shape_3d.cpp
9898 views
/**************************************************************************/1/* sphere_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 "sphere_shape_3d.h"3132#include "scene/resources/3d/primitive_meshes.h"33#include "servers/physics_server_3d.h"3435Vector<Vector3> SphereShape3D::get_debug_mesh_lines() const {36float r = get_radius();3738Vector<Vector3> points;3940for (int i = 0; i <= 360; i++) {41float ra = Math::deg_to_rad((float)i);42float rb = Math::deg_to_rad((float)i + 1);43Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * r;44Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * r;4546points.push_back(Vector3(a.x, 0, a.y));47points.push_back(Vector3(b.x, 0, b.y));48points.push_back(Vector3(0, a.x, a.y));49points.push_back(Vector3(0, b.x, b.y));50points.push_back(Vector3(a.x, a.y, 0));51points.push_back(Vector3(b.x, b.y, 0));52}5354return points;55}5657Ref<ArrayMesh> SphereShape3D::get_debug_arraymesh_faces(const Color &p_modulate) const {58Array sphere_array;59sphere_array.resize(RS::ARRAY_MAX);60SphereMesh::create_mesh_array(sphere_array, radius, radius * 2, 32);6162Vector<Color> colors;63const PackedVector3Array &verts = sphere_array[RS::ARRAY_VERTEX];64const int32_t verts_size = verts.size();65for (int i = 0; i < verts_size; i++) {66colors.append(p_modulate);67}6869Ref<ArrayMesh> sphere_mesh = memnew(ArrayMesh);70sphere_array[RS::ARRAY_COLOR] = colors;71sphere_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, sphere_array);72return sphere_mesh;73}7475real_t SphereShape3D::get_enclosing_radius() const {76return radius;77}7879void SphereShape3D::_update_shape() {80PhysicsServer3D::get_singleton()->shape_set_data(get_shape(), radius);81Shape3D::_update_shape();82}8384void SphereShape3D::set_radius(float p_radius) {85ERR_FAIL_COND_MSG(p_radius < 0, "SphereShape3D radius cannot be negative.");86radius = p_radius;87_update_shape();88emit_changed();89}9091float SphereShape3D::get_radius() const {92return radius;93}9495void SphereShape3D::_bind_methods() {96ClassDB::bind_method(D_METHOD("set_radius", "radius"), &SphereShape3D::set_radius);97ClassDB::bind_method(D_METHOD("get_radius"), &SphereShape3D::get_radius);9899ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0.001,100,0.001,or_greater,suffix:m"), "set_radius", "get_radius");100}101102SphereShape3D::SphereShape3D() :103Shape3D(PhysicsServer3D::get_singleton()->shape_create(PhysicsServer3D::SHAPE_SPHERE)) {104set_radius(0.5);105}106107108