Now offsets use Transforms instead of just Vectors. Now capsule is rotated properly.

This commit is contained in:
Relintai 2020-06-30 15:23:27 +02:00
parent 25e9135925
commit 78cf61ffe5
5 changed files with 49 additions and 23 deletions

View File

@ -35,10 +35,10 @@ void MeshDataResource::set_array(const Array &p_arrays) {
_arrays = p_arrays.duplicate(true);
}
void MeshDataResource::add_collision_shape(const Vector3 &offset, const Ref<Shape> &shape) {
void MeshDataResource::add_collision_shape(const Transform &transform, const Ref<Shape> &shape) {
MDRData d;
d.offset = offset;
d.transform = transform;
d.shape = shape;
_collision_shapes.push_back(d);
@ -48,10 +48,10 @@ Ref<Shape> MeshDataResource::get_collision_shape(const int index) {
return _collision_shapes[index].shape;
}
Vector3 MeshDataResource::get_collision_shape_offset(const int index) {
ERR_FAIL_INDEX_V(index, _collision_shapes.size(), Vector3());
Transform MeshDataResource::get_collision_shape_offset(const int index) {
ERR_FAIL_INDEX_V(index, _collision_shapes.size(), Transform());
return _collision_shapes[index].offset;
return _collision_shapes[index].transform;
}
int MeshDataResource::get_collision_shape_count() const {
return _collision_shapes.size();
@ -61,7 +61,7 @@ Vector<Variant> MeshDataResource::get_collision_shapes() {
Vector<Variant> r;
for (int i = 0; i < _collision_shapes.size(); i++) {
#if VERSION_MAJOR < 4
r.push_back(_collision_shapes[i].offset);
r.push_back(_collision_shapes[i].transform);
r.push_back(_collision_shapes[i].shape.get_ref_ptr());
#else
r.push_back(_collision_shapes[i].offset);
@ -77,7 +77,7 @@ void MeshDataResource::set_collision_shapes(const Vector<Variant> &p_arrays) {
for (int i = 0; i < p_arrays.size(); i += 2) {
MDRData d;
d.offset = p_arrays[i];
d.transform = p_arrays[i];
d.shape = Ref<Shape>(p_arrays[i + 1]);
_collision_shapes.push_back(d);

View File

@ -51,9 +51,9 @@ public:
Array get_array();
void set_array(const Array &p_arrays);
void add_collision_shape(const Vector3 &offset, const Ref<Shape> &shape);
void add_collision_shape(const Transform &transform, const Ref<Shape> &shape);
Ref<Shape> get_collision_shape(const int index);
Vector3 get_collision_shape_offset(const int index);
Transform get_collision_shape_offset(const int index);
int get_collision_shape_count() const;
Vector<Variant> get_collision_shapes();
@ -65,7 +65,7 @@ public:
protected:
struct MDRData {
Ref<Shape> shape;
Vector3 offset;
Transform transform;
};
protected:

View File

@ -114,7 +114,7 @@ Error EditorImportColladaMdr::import(const String &p_source_file, const String &
Ref<Shape> shape = m->create_trimesh_shape();
if (!shape.is_null()) {
mdr->add_collision_shape(Vector3(), scale_shape(shape, scale));
mdr->add_collision_shape(Transform(), scale_shape(shape, scale));
}
} else if (collider_type == MeshDataResource::COLLIDER_TYPE_SINGLE_CONVEX_COLLISION_SHAPE) {
Ref<ArrayMesh> m;
@ -137,7 +137,7 @@ Error EditorImportColladaMdr::import(const String &p_source_file, const String &
}
for (int j = 0; j < shapes.size(); ++j) {
mdr->add_collision_shape(Vector3(), shapes[j]);
mdr->add_collision_shape(Transform(), shapes[j]);
}
} else if (collider_type == MeshDataResource::COLLIDER_TYPE_APPROXIMATED_BOX) {
Ref<ArrayMesh> m;
@ -155,7 +155,10 @@ Error EditorImportColladaMdr::import(const String &p_source_file, const String &
Vector3 pos = aabb.position;
pos += size / 2.0;
mdr->add_collision_shape(pos, shape);
Transform t;
t.origin = pos;
mdr->add_collision_shape(t, shape);
} else if (collider_type == MeshDataResource::COLLIDER_TYPE_APPROXIMATED_CAPSULE) {
Ref<ArrayMesh> m;
m.instance();
@ -173,7 +176,10 @@ Error EditorImportColladaMdr::import(const String &p_source_file, const String &
Vector3 pos = aabb.position;
pos += size / 2.0;
mdr->add_collision_shape(pos, shape);
Transform t = Transform(Basis().rotated(Vector3(1, 0, 0), M_PI_2));
t.origin = pos;
mdr->add_collision_shape(t, shape);
} else if (collider_type == MeshDataResource::COLLIDER_TYPE_APPROXIMATED_CYLINDER) {
Ref<ArrayMesh> m;
m.instance();
@ -191,7 +197,10 @@ Error EditorImportColladaMdr::import(const String &p_source_file, const String &
Vector3 pos = aabb.position;
pos += size / 2.0;
mdr->add_collision_shape(pos, shape);
Transform t;
t.origin = pos;
mdr->add_collision_shape(t, shape);
} else if (collider_type == MeshDataResource::COLLIDER_TYPE_APPROXIMATED_SPHERE) {
Ref<ArrayMesh> m;
m.instance();
@ -207,7 +216,10 @@ Error EditorImportColladaMdr::import(const String &p_source_file, const String &
Vector3 mid = aabb.get_size() / 2.0;
mdr->add_collision_shape(aabb.position + mid, shape);
Transform t;
t.origin = aabb.position + mid;
mdr->add_collision_shape(t, shape);
}
n->queue_delete();

View File

@ -115,7 +115,7 @@ Error EditorImportGLTFMdr::import(const String &p_source_file, const String &p_s
Ref<Shape> shape = m->create_trimesh_shape();
if (!shape.is_null()) {
mdr->add_collision_shape(Vector3(), scale_shape(shape, scale));
mdr->add_collision_shape(Transform(), scale_shape(shape, scale));
}
} else if (collider_type == MeshDataResource::COLLIDER_TYPE_SINGLE_CONVEX_COLLISION_SHAPE) {
Ref<ArrayMesh> m;
@ -125,7 +125,7 @@ Error EditorImportGLTFMdr::import(const String &p_source_file, const String &p_s
Ref<Shape> shape = mesh->create_convex_shape();
if (!shape.is_null()) {
mdr->add_collision_shape(Vector3(), scale_shape(shape, scale));
mdr->add_collision_shape(Transform(), scale_shape(shape, scale));
}
} else if (collider_type == MeshDataResource::COLLIDER_TYPE_MULTIPLE_CONVEX_COLLISION_SHAPES) {
Ref<ArrayMesh> m;
@ -139,7 +139,7 @@ Error EditorImportGLTFMdr::import(const String &p_source_file, const String &p_s
}
for (int j = 0; j < shapes.size(); ++j) {
mdr->add_collision_shape(Vector3(), shapes[j]);
mdr->add_collision_shape(Transform(), shapes[j]);
}
} else if (collider_type == MeshDataResource::COLLIDER_TYPE_APPROXIMATED_BOX) {
Ref<ArrayMesh> m;
@ -157,7 +157,10 @@ Error EditorImportGLTFMdr::import(const String &p_source_file, const String &p_s
Vector3 pos = aabb.position;
pos += size / 2.0;
mdr->add_collision_shape(pos, shape);
Transform t;
t.origin = pos;
mdr->add_collision_shape(t, shape);
} else if (collider_type == MeshDataResource::COLLIDER_TYPE_APPROXIMATED_CAPSULE) {
Ref<ArrayMesh> m;
m.instance();
@ -175,7 +178,10 @@ Error EditorImportGLTFMdr::import(const String &p_source_file, const String &p_s
Vector3 pos = aabb.position;
pos += size / 2.0;
mdr->add_collision_shape(pos, shape);
Transform t = Transform(Basis().rotated(Vector3(1, 0, 0), M_PI_2));
t.origin = pos;
mdr->add_collision_shape(t, shape);
} else if (collider_type == MeshDataResource::COLLIDER_TYPE_APPROXIMATED_CYLINDER) {
Ref<ArrayMesh> m;
m.instance();
@ -193,7 +199,10 @@ Error EditorImportGLTFMdr::import(const String &p_source_file, const String &p_s
Vector3 pos = aabb.position;
pos += size / 2.0;
mdr->add_collision_shape(pos, shape);
Transform t;
t.origin = pos;
mdr->add_collision_shape(t, shape);
} else if (collider_type == MeshDataResource::COLLIDER_TYPE_APPROXIMATED_SPHERE) {
Ref<ArrayMesh> m;
m.instance();
@ -209,7 +218,10 @@ Error EditorImportGLTFMdr::import(const String &p_source_file, const String &p_s
Vector3 mid = aabb.get_size() / 2.0;
mdr->add_collision_shape(aabb.position + mid, shape);
Transform t;
t.origin = aabb.position + mid;
mdr->add_collision_shape(t, shape);
}
n->queue_delete();

View File

@ -35,6 +35,8 @@ SOFTWARE.
#include "../mesh_data_resource.h"
#include "editor/import/editor_scene_importer_gltf.h"
#include "core/math/transform.h"
#include "core/version.h"
#if VERSION_MAJOR < 4