Parcourir la source

resource: fix compilation of colliders

Daniele Bartolini il y a 5 ans
Parent
commit
4e66ede0c3

+ 8 - 0
docs/changelog.rst

@@ -11,6 +11,14 @@ Changelog
 * Added Glossary
 * Added Glossary
 * Added license statement about "Your Game or Application"
 * Added license statement about "Your Game or Application"
 
 
+**Data Compiler**
+
+* Fixed compilation of collider volumes
+
+**Runtime**
+
+* Fixed loading of collider volumes
+
 0.40.0
 0.40.0
 ------
 ------
 *06 Jan 2021*
 *06 Jan 2021*

+ 17 - 15
src/resource/physics_resource.cpp

@@ -124,32 +124,37 @@ namespace physics_resource_internal
 		return JointType::COUNT;
 		return JointType::COUNT;
 	}
 	}
 
 
-	void compile_sphere(const Array<Vector3>& points, ColliderDesc& sd)
+	void compile_sphere(ColliderDesc& sd, const Array<Vector3>& points)
 	{
 	{
-		Sphere sphere;
-		sphere::reset(sphere);
-		sphere::add_points(sphere, array::size(points), array::begin(points));
+		AABB aabb;
+		aabb::from_points(aabb, array::size(points), array::begin(points));
+
+		const Vector3 origin = aabb::center(aabb);
+		sd.local_tm.t = vector4(origin.x, origin.y, origin.z, 1.0f);
 
 
-		sd.sphere.radius = sphere.r;
+		sd.sphere.radius = max(            0.0f, aabb.max.x - aabb.min.x);
+		sd.sphere.radius = max(sd.sphere.radius, aabb.max.y - aabb.min.y);
+		sd.sphere.radius = max(sd.sphere.radius, aabb.max.z - aabb.min.z);
+		sd.sphere.radius *= 0.5f;
 	}
 	}
 
 
-	void compile_capsule(const Array<Vector3>& points, ColliderDesc& sd)
+	void compile_capsule(ColliderDesc& sd, const Array<Vector3>& points)
 	{
 	{
 		AABB aabb;
 		AABB aabb;
 		aabb::from_points(aabb, array::size(points), array::begin(points));
 		aabb::from_points(aabb, array::size(points), array::begin(points));
 
 
-		const Vector3 origin = aabb::center(aabb) * sd.local_tm;
+		const Vector3 origin = aabb::center(aabb);
 		sd.local_tm.t = vector4(origin.x, origin.y, origin.z, 1.0f);
 		sd.local_tm.t = vector4(origin.x, origin.y, origin.z, 1.0f);
 		sd.capsule.radius = aabb::radius(aabb) / 2.0f;
 		sd.capsule.radius = aabb::radius(aabb) / 2.0f;
 		sd.capsule.height = (aabb.max.y - aabb.min.y) / 2.0f;
 		sd.capsule.height = (aabb.max.y - aabb.min.y) / 2.0f;
 	}
 	}
 
 
-	void compile_box(const Array<Vector3>& points, ColliderDesc& sd)
+	void compile_box(ColliderDesc& sd, const Array<Vector3>& points)
 	{
 	{
 		AABB aabb;
 		AABB aabb;
 		aabb::from_points(aabb, array::size(points), array::begin(points));
 		aabb::from_points(aabb, array::size(points), array::begin(points));
 
 
-		const Vector3 origin = aabb::center(aabb) * sd.local_tm;
+		const Vector3 origin = aabb::center(aabb);
 		sd.local_tm.t = vector4(origin.x, origin.y, origin.z, 1.0f);
 		sd.local_tm.t = vector4(origin.x, origin.y, origin.z, 1.0f);
 		sd.box.half_size = (aabb.max - aabb.min) * 0.5f;
 		sd.box.half_size = (aabb.max - aabb.min) * 0.5f;
 	}
 	}
@@ -244,9 +249,6 @@ namespace physics_resource_internal
 				);
 				);
 			sjson::parse(node, node_data);
 			sjson::parse(node, node_data);
 
 
-			Matrix4x4 matrix_local = sjson::parse_matrix4x4(node["matrix_local"]);
-			cd.local_tm = matrix_local;
-
 			JsonArray positions(ta);
 			JsonArray positions(ta);
 			sjson::parse_array(positions, geometry["position"]);
 			sjson::parse_array(positions, geometry["position"]);
 
 
@@ -273,9 +275,9 @@ namespace physics_resource_internal
 
 
 			switch (cd.type)
 			switch (cd.type)
 			{
 			{
-			case ColliderType::SPHERE:      compile_sphere(points, cd); break;
-			case ColliderType::CAPSULE:     compile_capsule(points, cd); break;
-			case ColliderType::BOX:         compile_box(points, cd); break;
+			case ColliderType::SPHERE:      compile_sphere(cd, points); break;
+			case ColliderType::CAPSULE:     compile_capsule(cd, points); break;
+			case ColliderType::BOX:         compile_box(cd, points); break;
 			case ColliderType::CONVEX_HULL: break;
 			case ColliderType::CONVEX_HULL: break;
 			case ColliderType::MESH:        break;
 			case ColliderType::MESH:        break;
 			case ColliderType::HEIGHTFIELD:
 			case ColliderType::HEIGHTFIELD:

+ 1 - 1
src/resource/types.h

@@ -60,7 +60,7 @@ struct UnitResource;
 #define RESOURCE_VERSION_STATE_MACHINE    RESOURCE_VERSION(3)
 #define RESOURCE_VERSION_STATE_MACHINE    RESOURCE_VERSION(3)
 #define RESOURCE_VERSION_CONFIG           RESOURCE_VERSION(1)
 #define RESOURCE_VERSION_CONFIG           RESOURCE_VERSION(1)
 #define RESOURCE_VERSION_FONT             RESOURCE_VERSION(1)
 #define RESOURCE_VERSION_FONT             RESOURCE_VERSION(1)
-#define RESOURCE_VERSION_UNIT             RESOURCE_VERSION(7)
+#define RESOURCE_VERSION_UNIT             RESOURCE_VERSION(8)
 #define RESOURCE_VERSION_LEVEL            (RESOURCE_VERSION_UNIT + 4) //!< Level embeds UnitResource
 #define RESOURCE_VERSION_LEVEL            (RESOURCE_VERSION_UNIT + 4) //!< Level embeds UnitResource
 #define RESOURCE_VERSION_MATERIAL         RESOURCE_VERSION(2)
 #define RESOURCE_VERSION_MATERIAL         RESOURCE_VERSION(2)
 #define RESOURCE_VERSION_MESH             RESOURCE_VERSION(4)
 #define RESOURCE_VERSION_MESH             RESOURCE_VERSION(4)

+ 1 - 1
src/world/physics_world_bullet.cpp

@@ -355,7 +355,7 @@ struct PhysicsWorldImpl
 
 
 		ColliderInstanceData cid;
 		ColliderInstanceData cid;
 		cid.unit         = unit;
 		cid.unit         = unit;
-		cid.local_tm     = MATRIX4X4_IDENTITY;
+		cid.local_tm     = sd->local_tm;
 		cid.vertex_array = vertex_array;
 		cid.vertex_array = vertex_array;
 		cid.shape        = child_shape;
 		cid.shape        = child_shape;
 		cid.next.i       = UINT32_MAX;
 		cid.next.i       = UINT32_MAX;