Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/jolt_physics/shapes/jolt_convex_polygon_shape_3d.cpp
10278 views
1
/**************************************************************************/
2
/* jolt_convex_polygon_shape_3d.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "jolt_convex_polygon_shape_3d.h"
32
33
#include "../jolt_project_settings.h"
34
#include "../misc/jolt_type_conversions.h"
35
36
#include "Jolt/Physics/Collision/Shape/ConvexHullShape.h"
37
38
JPH::ShapeRefC JoltConvexPolygonShape3D::_build() const {
39
const int vertex_count = (int)vertices.size();
40
41
if (unlikely(vertex_count == 0)) {
42
return nullptr;
43
}
44
45
ERR_FAIL_COND_V_MSG(vertex_count < 3, nullptr, vformat("Failed to build Jolt Physics convex polygon shape with %s. It must have a vertex count of at least 3. This shape belongs to %s.", to_string(), _owners_to_string()));
46
47
JPH::Array<JPH::Vec3> jolt_vertices;
48
jolt_vertices.reserve((size_t)vertex_count);
49
50
const Vector3 *vertices_begin = &vertices[0];
51
const Vector3 *vertices_end = vertices_begin + vertex_count;
52
53
for (const Vector3 *vertex = vertices_begin; vertex != vertices_end; ++vertex) {
54
jolt_vertices.emplace_back((float)vertex->x, (float)vertex->y, (float)vertex->z);
55
}
56
57
const float min_half_extent = _calculate_aabb().get_shortest_axis_size() * 0.5f;
58
const float actual_margin = MIN(margin, min_half_extent * JoltProjectSettings::collision_margin_fraction);
59
60
const JPH::ConvexHullShapeSettings shape_settings(jolt_vertices, actual_margin);
61
const JPH::ShapeSettings::ShapeResult shape_result = shape_settings.Create();
62
ERR_FAIL_COND_V_MSG(shape_result.HasError(), nullptr, vformat("Failed to build Jolt Physics convex polygon shape with %s. It returned the following error: '%s'. This shape belongs to %s.", to_string(), to_godot(shape_result.GetError()), _owners_to_string()));
63
64
return shape_result.Get();
65
}
66
67
AABB JoltConvexPolygonShape3D::_calculate_aabb() const {
68
AABB result;
69
70
for (int i = 0; i < vertices.size(); ++i) {
71
if (i == 0) {
72
result.position = vertices[i];
73
} else {
74
result.expand_to(vertices[i]);
75
}
76
}
77
78
return result;
79
}
80
81
Variant JoltConvexPolygonShape3D::get_data() const {
82
return vertices;
83
}
84
85
void JoltConvexPolygonShape3D::set_data(const Variant &p_data) {
86
ERR_FAIL_COND(p_data.get_type() != Variant::PACKED_VECTOR3_ARRAY);
87
88
vertices = p_data;
89
90
aabb = _calculate_aabb();
91
92
destroy();
93
}
94
95
void JoltConvexPolygonShape3D::set_margin(float p_margin) {
96
if (unlikely(margin == p_margin)) {
97
return;
98
}
99
100
margin = p_margin;
101
102
destroy();
103
}
104
105
String JoltConvexPolygonShape3D::to_string() const {
106
return vformat("{vertex_count=%d margin=%f}", vertices.size(), margin);
107
}
108
109