1/**************************************************************************/
2/* vehicle_body_3d_gizmo_plugin.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 "vehicle_body_3d_gizmo_plugin.h"
32
33#include "editor/editor_settings.h"
34#include "editor/plugins/node_3d_editor_plugin.h"
35#include "scene/3d/vehicle_body_3d.h"
36
37VehicleWheel3DGizmoPlugin::VehicleWheel3DGizmoPlugin() {
38 Color gizmo_color = EDITOR_GET("editors/3d_gizmos/gizmo_colors/shape");
39 create_material("shape_material", gizmo_color);
40}
41
42bool VehicleWheel3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
43 return Object::cast_to<VehicleWheel3D>(p_spatial) != nullptr;
44}
45
46String VehicleWheel3DGizmoPlugin::get_gizmo_name() const {
47 return "VehicleWheel3D";
48}
49
50int VehicleWheel3DGizmoPlugin::get_priority() const {
51 return -1;
52}
53
54void VehicleWheel3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
55 VehicleWheel3D *car_wheel = Object::cast_to<VehicleWheel3D>(p_gizmo->get_node_3d());
56
57 p_gizmo->clear();
58
59 Vector<Vector3> points;
60
61 float r = car_wheel->get_radius();
62 const int skip = 10;
63 for (int i = 0; i <= 360; i += skip) {
64 float ra = Math::deg_to_rad((float)i);
65 float rb = Math::deg_to_rad((float)i + skip);
66 Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * r;
67 Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * r;
68
69 points.push_back(Vector3(0, a.x, a.y));
70 points.push_back(Vector3(0, b.x, b.y));
71
72 const int springsec = 4;
73
74 for (int j = 0; j < springsec; j++) {
75 float t = car_wheel->get_suspension_rest_length() * 5;
76 points.push_back(Vector3(a.x, i / 360.0 * t / springsec + j * (t / springsec), a.y) * 0.2);
77 points.push_back(Vector3(b.x, (i + skip) / 360.0 * t / springsec + j * (t / springsec), b.y) * 0.2);
78 }
79 }
80
81 //travel
82 points.push_back(Vector3(0, 0, 0));
83 points.push_back(Vector3(0, car_wheel->get_suspension_rest_length(), 0));
84
85 //axis
86 points.push_back(Vector3(r * 0.2, car_wheel->get_suspension_rest_length(), 0));
87 points.push_back(Vector3(-r * 0.2, car_wheel->get_suspension_rest_length(), 0));
88 //axis
89 points.push_back(Vector3(r * 0.2, 0, 0));
90 points.push_back(Vector3(-r * 0.2, 0, 0));
91
92 //forward line
93 points.push_back(Vector3(0, -r, 0));
94 points.push_back(Vector3(0, -r, r * 2));
95 points.push_back(Vector3(0, -r, r * 2));
96 points.push_back(Vector3(r * 2 * 0.2, -r, r * 2 * 0.8));
97 points.push_back(Vector3(0, -r, r * 2));
98 points.push_back(Vector3(-r * 2 * 0.2, -r, r * 2 * 0.8));
99
100 Ref<Material> material = get_material("shape_material", p_gizmo);
101
102 p_gizmo->add_lines(points, material);
103 p_gizmo->add_collision_segments(points);
104}
105