Sfoglia il codice sorgente

resource: add customizable gravity vector

Daniele Bartolini 1 anno fa
parent
commit
9e3b8c22f6

+ 1 - 0
docs/changelog.rst

@@ -37,6 +37,7 @@ Changelog
 * Data Compiler: the data compiler will now print an error message instead of crashing when parsing malformed SJSON files.
 * Data Compiler: fixed and issue that caused some resources to be always marked as outdated in some circumnstances.
 * Fixed destroying units with a script component.
+* Added customizable gravity vector in global.physics_config resource.
 
 0.53.0 --- 30 Nov 2024
 ----------------------

+ 7 - 1
src/resource/physics_resource.cpp

@@ -595,6 +595,7 @@ namespace physics_config_resource_internal
 		Array<PhysicsMaterial> materials(default_allocator());
 		Array<PhysicsActor> actors(default_allocator());
 		CollisionFilterCompiler cfc(opts);
+		PhysicsConfigResource pcr;
 
 		// Parse materials
 		s32 err = 0;
@@ -610,9 +611,13 @@ namespace physics_config_resource_internal
 			err = parse_actors(obj["actors"], actors, opts);
 			ENSURE_OR_RETURN(err == 0, opts);
 		}
+		if (json_object::has(obj, "gravity")) {
+			pcr.gravity = RETURN_IF_ERROR(sjson::parse_vector3(obj["gravity"]), opts);
+		} else {
+			pcr.gravity = vector3(0.0f, 0.0f, -10.0f);
+		}
 
 		// Setup struct for writing
-		PhysicsConfigResource pcr;
 		pcr.version       = RESOURCE_HEADER(RESOURCE_VERSION_PHYSICS_CONFIG);
 		pcr.num_materials = array::size(materials);
 		pcr.num_actors    = array::size(actors);
@@ -635,6 +640,7 @@ namespace physics_config_resource_internal
 		opts.write(pcr.actors_offset);
 		opts.write(pcr.num_filters);
 		opts.write(pcr.filters_offset);
+		opts.write(pcr.gravity);
 
 		// Write materials
 		for (u32 i = 0; i < pcr.num_materials; ++i) {

+ 1 - 0
src/resource/physics_resource.h

@@ -32,6 +32,7 @@ struct PhysicsConfigResource
 	u32 actors_offset;
 	u32 num_filters;
 	u32 filters_offset;
+	Vector3 gravity;
 };
 
 struct PhysicsMaterial

+ 1 - 1
src/resource/types.h

@@ -82,7 +82,7 @@ struct Platform
 #define RESOURCE_VERSION_MATERIAL         RESOURCE_VERSION(5)
 #define RESOURCE_VERSION_MESH             RESOURCE_VERSION(6)
 #define RESOURCE_VERSION_PACKAGE          RESOURCE_VERSION(7)
-#define RESOURCE_VERSION_PHYSICS_CONFIG   RESOURCE_VERSION(2)
+#define RESOURCE_VERSION_PHYSICS_CONFIG   RESOURCE_VERSION(3)
 #define RESOURCE_VERSION_SCRIPT           RESOURCE_VERSION(4)
 #define RESOURCE_VERSION_SHADER           RESOURCE_VERSION(13)
 #define RESOURCE_VERSION_SOUND            RESOURCE_VERSION(1)

+ 3 - 3
src/world/physics_world_bullet.cpp

@@ -249,13 +249,13 @@ struct PhysicsWorldImpl
 			, physics_globals::_bt_configuration
 			);
 
-		_dynamics_world->setGravity(to_btVector3(vector3(0.0f, 0.0f, -10.0f)));
+		_config_resource = (PhysicsConfigResource *)rm.get(RESOURCE_TYPE_PHYSICS_CONFIG, STRING_ID_64("global", 0x0b2f08fe66e395c0));
+
+		_dynamics_world->setGravity(to_btVector3(_config_resource->gravity));
 		_dynamics_world->getCollisionWorld()->setDebugDrawer(&_debug_drawer);
 		_dynamics_world->setInternalTickCallback(tick_cb, this);
 		_dynamics_world->getPairCache()->setOverlapFilterCallback(&_filter_callback);
 
-		_config_resource = (PhysicsConfigResource *)rm.get(RESOURCE_TYPE_PHYSICS_CONFIG, STRING_ID_64("global", 0x0b2f08fe66e395c0));
-
 		_unit_destroy_callback.destroy = PhysicsWorldImpl::unit_destroyed_callback;
 		_unit_destroy_callback.user_data = this;
 		_unit_destroy_callback.node.next = NULL;