Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resource: add customizable gravity vector #234

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,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
----------------------
Expand Down
8 changes: 7 additions & 1 deletion src/resource/physics_resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions src/resource/physics_resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ struct PhysicsConfigResource
u32 actors_offset;
u32 num_filters;
u32 filters_offset;
Vector3 gravity;
};

struct PhysicsMaterial
Expand Down
2 changes: 1 addition & 1 deletion src/resource/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions src/world/physics_world_bullet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down