Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/resources/2d/world_boundary_shape_2d.cpp
9898 views
1
/**************************************************************************/
2
/* world_boundary_shape_2d.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 "world_boundary_shape_2d.h"
32
33
#include "core/math/geometry_2d.h"
34
#include "servers/physics_server_2d.h"
35
#include "servers/rendering_server.h"
36
37
bool WorldBoundaryShape2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {
38
const Vector2 shape_center = distance * normal;
39
// Orthogonal part of the shape editor gizmo (the flat line).
40
const Vector2 ortho_segment_a = shape_center - normal.orthogonal() * 100;
41
const Vector2 ortho_segment_b = shape_center + normal.orthogonal() * 100;
42
const Vector2 ortho_closest = Geometry2D::get_closest_point_to_segment(p_point, ortho_segment_a, ortho_segment_b);
43
if (p_point.distance_to(ortho_closest) < p_tolerance) {
44
return true;
45
}
46
// Normal part of the shape editor gizmo (the arrow).
47
const Vector2 normal_segment_a = shape_center;
48
const Vector2 normal_segment_b = shape_center + normal * 30;
49
const Vector2 normal_closest = Geometry2D::get_closest_point_to_segment(p_point, normal_segment_a, normal_segment_b);
50
if (p_point.distance_to(normal_closest) < p_tolerance) {
51
return true;
52
}
53
return false;
54
}
55
56
void WorldBoundaryShape2D::_update_shape() {
57
Array arr = { normal, distance };
58
PhysicsServer2D::get_singleton()->shape_set_data(get_rid(), arr);
59
emit_changed();
60
}
61
62
void WorldBoundaryShape2D::set_normal(const Vector2 &p_normal) {
63
// Can be non-unit but prevent zero.
64
ERR_FAIL_COND(p_normal.is_zero_approx());
65
if (normal == p_normal) {
66
return;
67
}
68
normal = p_normal;
69
_update_shape();
70
}
71
72
void WorldBoundaryShape2D::set_distance(real_t p_distance) {
73
if (distance == p_distance) {
74
return;
75
}
76
distance = p_distance;
77
_update_shape();
78
}
79
80
Vector2 WorldBoundaryShape2D::get_normal() const {
81
return normal;
82
}
83
84
real_t WorldBoundaryShape2D::get_distance() const {
85
return distance;
86
}
87
88
void WorldBoundaryShape2D::draw(const RID &p_to_rid, const Color &p_color) {
89
Vector2 point = distance * normal;
90
real_t line_width = 3.0;
91
92
// Draw collision shape line.
93
PackedVector2Array line_points = {
94
point - normal.orthogonal() * 100,
95
point - normal.orthogonal() * 60,
96
point + normal.orthogonal() * 60,
97
point + normal.orthogonal() * 100
98
};
99
100
Color transparent_color = Color(p_color, 0);
101
PackedColorArray line_colors = {
102
transparent_color,
103
p_color,
104
p_color,
105
transparent_color
106
};
107
108
RS::get_singleton()->canvas_item_add_polyline(p_to_rid, line_points, line_colors, line_width);
109
110
// Draw arrow.
111
Color arrow_color = p_color.inverted();
112
113
Transform2D xf;
114
xf.rotate(normal.angle());
115
116
Vector<Vector2> arrow_points = {
117
xf.xform(Vector2(distance + line_width / 2, -2.5)),
118
xf.xform(Vector2(distance + 20, -2.5)),
119
xf.xform(Vector2(distance + 20, -10)),
120
xf.xform(Vector2(distance + 40, 0)),
121
xf.xform(Vector2(distance + 20, 10)),
122
xf.xform(Vector2(distance + 20, 2.5)),
123
xf.xform(Vector2(distance + line_width / 2, 2.5)),
124
};
125
126
RS::get_singleton()->canvas_item_add_polyline(p_to_rid, arrow_points, { arrow_color }, line_width / 2);
127
}
128
129
Rect2 WorldBoundaryShape2D::get_rect() const {
130
Vector2 point = distance * normal;
131
132
Vector2 l1[2] = { point - normal.orthogonal() * 100, point + normal.orthogonal() * 100 };
133
Vector2 l2[2] = { point, point + normal * 30 };
134
Rect2 rect;
135
rect.position = l1[0];
136
rect.expand_to(l1[1]);
137
rect.expand_to(l2[0]);
138
rect.expand_to(l2[1]);
139
return rect;
140
}
141
142
real_t WorldBoundaryShape2D::get_enclosing_radius() const {
143
return distance;
144
}
145
146
void WorldBoundaryShape2D::_bind_methods() {
147
ClassDB::bind_method(D_METHOD("set_normal", "normal"), &WorldBoundaryShape2D::set_normal);
148
ClassDB::bind_method(D_METHOD("get_normal"), &WorldBoundaryShape2D::get_normal);
149
150
ClassDB::bind_method(D_METHOD("set_distance", "distance"), &WorldBoundaryShape2D::set_distance);
151
ClassDB::bind_method(D_METHOD("get_distance"), &WorldBoundaryShape2D::get_distance);
152
153
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "normal"), "set_normal", "get_normal");
154
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "distance", PROPERTY_HINT_RANGE, "-1024,1024,0.01,or_greater,or_less,suffix:px"), "set_distance", "get_distance");
155
}
156
157
WorldBoundaryShape2D::WorldBoundaryShape2D() :
158
Shape2D(PhysicsServer2D::get_singleton()->world_boundary_shape_create()) {
159
_update_shape();
160
}
161
162