Skip to content
This repository has been archived by the owner on Jun 29, 2024. It is now read-only.

Commit

Permalink
upd
Browse files Browse the repository at this point in the history
  • Loading branch information
Ughuuu committed Dec 22, 2023
1 parent bdbb72f commit d1fa4fe
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 122 deletions.
4 changes: 2 additions & 2 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
branch = 4.1
[submodule "box2d"]
path = box2d
url = https://github.com/appsinacup/box2d
branch = common-assert-noop
url = https://github.com/erincatto/box2c
branch = main
[submodule "Godot-Physics-Tests"]
path = Godot-Physics-Tests
url = https://github.com/fabriceci/Godot-Physics-Tests
57 changes: 2 additions & 55 deletions SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -3,66 +3,13 @@ import os
import sys

env = SConscript("godot-cpp/SConstruct")
box2d_folder = "box2d/"
box2d_include = [
"include/",
"src/"
]
box2d_src = [
"collision/b2_broad_phase.cpp",
"collision/b2_chain_shape.cpp",
"collision/b2_circle_shape.cpp",
"collision/b2_collide_circle.cpp",
"collision/b2_collide_edge.cpp",
"collision/b2_collide_polygon.cpp",
"collision/b2_collision.cpp",
"collision/b2_distance.cpp",
"collision/b2_dynamic_tree.cpp",
"collision/b2_edge_shape.cpp",
"collision/b2_polygon_shape.cpp",
"collision/b2_time_of_impact.cpp",
"common/b2_block_allocator.cpp",
"common/b2_draw.cpp",
"common/b2_math.cpp",
"common/b2_settings.cpp",
"common/b2_stack_allocator.cpp",
"common/b2_timer.cpp",
"dynamics/b2_body.cpp",
"dynamics/b2_chain_circle_contact.cpp",
"dynamics/b2_chain_polygon_contact.cpp",
"dynamics/b2_circle_contact.cpp",
"dynamics/b2_contact.cpp",
"dynamics/b2_contact_manager.cpp",
"dynamics/b2_contact_solver.cpp",
"dynamics/b2_distance_joint.cpp",
"dynamics/b2_edge_circle_contact.cpp",
"dynamics/b2_edge_polygon_contact.cpp",
"dynamics/b2_fixture.cpp",
"dynamics/b2_friction_joint.cpp",
"dynamics/b2_gear_joint.cpp",
"dynamics/b2_island.cpp",
"dynamics/b2_joint.cpp",
"dynamics/b2_motor_joint.cpp",
"dynamics/b2_mouse_joint.cpp",
"dynamics/b2_polygon_circle_contact.cpp",
"dynamics/b2_polygon_contact.cpp",
"dynamics/b2_prismatic_joint.cpp",
"dynamics/b2_pulley_joint.cpp",
"dynamics/b2_revolute_joint.cpp",
"dynamics/b2_weld_joint.cpp",
"dynamics/b2_wheel_joint.cpp",
"dynamics/b2_world.cpp",
"dynamics/b2_world_callbacks.cpp",
"rope/b2_rope.cpp",
]
env.Prepend(CPPPATH=[box2d_folder + folder for folder in box2d_include])
env.Append(CPPDEFINES="B2_USER_SETTINGS")
env.Prepend(CPPPATH=["box2d/include", "box2d/src"])
# For the reference:
# - CCFLAGS are compilation flags shared between C and C++
# tweak this if you want to use different folders, or more folders, to store your source code in.
env.Append(CPPPATH=["src/"])
sources = [Glob("src/*.cpp"),Glob("src/bodies/*.cpp"),Glob("src/joints/*.cpp"),Glob("src/servers/*.cpp"),Glob("src/shapes/*.cpp"),Glob("src/spaces/*.cpp"),Glob("src/box2d-wrapper/*.cpp")]
sources.extend([box2d_folder + 'src/' + box2d_src_file for box2d_src_file in box2d_src])
sources.extend([Glob("box2d/src/*.c")])

if env["platform"] == "macos":
library = env.SharedLibrary(
Expand Down
2 changes: 1 addition & 1 deletion box2d
Submodule box2d updated from 774779 to b3a87a
40 changes: 15 additions & 25 deletions src/b2_user_settings.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#pragma once
#ifndef B2_USER_SETTINGS_H
#define B2_USER_SETTINGS_H

#include <stdarg.h>
#include <stdint.h>

#include <godot_cpp/core/error_macros.hpp>
#include <godot_cpp/core/memory.hpp>
#include <godot_cpp/variant/transform2d.hpp>
#include <godot_cpp/variant/vector2.hpp>

#include <box2d/b2_api.h>
#include <box2d/b2_types.h>
#include <box2d/box2d.h>

// Tunable Constants

Expand All @@ -24,7 +25,7 @@ class Box2DCollisionObject2D;
class Box2DShape2D;

// You can define this to inject whatever data you want in b2Body
struct B2_API b2BodyUserData {
struct b2BodyUserData {
b2BodyUserData() :
old_linear_velocity(0, 0), old_angular_velocity(0), constant_force(0, 0), constant_torque(0), collision_object(nullptr) {}

Expand All @@ -37,7 +38,7 @@ struct B2_API b2BodyUserData {
};

// You can define this to inject whatever data you want in b2Fixture
struct B2_API b2FixtureUserData {
struct b2FixtureUserData {
b2FixtureUserData() :
shape_idx(-1), transform(), collision_object(nullptr) {}

Expand All @@ -46,31 +47,20 @@ struct B2_API b2FixtureUserData {
Box2DCollisionObject2D *collision_object;
};

/// You can define this to inject whatever data you want in b2Joint
struct B2_API b2JointUserData {
b2JointUserData() :
pointer(0) {}

uintptr_t pointer; // For legacy compatibility
};

// Memory Allocation using Godot's functions

inline void *b2Alloc(int32 size) {
inline void *b2AllocGodot(uint32_t size) {
return memalloc(size);
}

inline void b2Free(void *mem) {
inline void b2FreeGodot(void *mem) {
memfree(mem);
}

// Default logging function
B2_API void b2Log_Default(const char *string, va_list args);

// Implement this to use your own logging.
inline void b2Log(const char *string, ...) {
va_list args;
va_start(args, string);
b2Log_Default(string, args);
va_end(args);
inline int b2AssertFcnGodot(const char* condition, const char* fileName, int lineNumber)
{
ERR_PRINT("Box2D assert: " + String(condition) + " " + String(fileName) + " line: " + rtos(lineNumber));
// don't assert it, just print error.
return 0;
}

#endif // B2_USER_SETTINGS_H
21 changes: 10 additions & 11 deletions src/box2d-wrapper/box2d_wrapper.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#include "box2d_wrapper.h"
#include "../b2_user_settings.h"
#include "../bodies/box2d_collision_object_2d.h"
#include <box2d/b2_distance.h>
#include <box2d/b2_time_of_impact.h>
#include <box2d/box2d.h>
#include <godot_cpp/templates/hash_set.hpp>

Expand All @@ -16,8 +14,8 @@ enum class ShapeType {
};

struct Box2DHolder {
HashMap<b2World *, ActiveBodyCallback> active_body_callbacks;
HashMap<b2World *, int> active_objects;
HashMap<b2WorldId , ActiveBodyCallback> active_body_callbacks;
HashMap<b2WorldId , int> active_objects;
};

Box2DHolder holder;
Expand Down Expand Up @@ -252,8 +250,8 @@ void box2d::body_apply_impulse(b2World *world_handle, b2Body *body_handle, const
body_handle->ApplyLinearImpulseToCenter(impulse, true);
}

void box2d::body_apply_impulse_at_point(b2World *world_handle,
b2Body *body_handle,
void box2d::body_apply_impulse_at_point(b2WorldId world_handle,
b2BodyId body_handle,
const b2Vec2 impulse,
const b2Vec2 point) {
body_handle->ApplyLinearImpulse(impulse, point, true);
Expand Down Expand Up @@ -740,8 +738,8 @@ bool box2d::intersect_ray(b2World *world_handle,
return callback.count;
}

b2World *box2d::invalid_world_handle() {
return nullptr;
b2WorldId box2d::invalid_world_handle() {
return b2_nullWorldId;
}
FixtureHandle box2d::invalid_fixture_handle() {
return FixtureHandle{
Expand Down Expand Up @@ -1200,9 +1198,10 @@ void box2d::world_set_contact_listener(b2World *world_handle,
world_handle->SetContactListener(callback);
}

void box2d::world_step(b2World *world_handle, const SimulationSettings *settings) {
world_handle->SetGravity(settings->gravity);
world_handle->Step(settings->dt, settings->max_velocity_iterations, settings->max_position_iterations);
void box2d::world_step(b2WorldId world_handle, const SimulationSettings *settings) {
//world_handle->SetGravity(settings->gravity);
// TODO set world gravity
b2World_Step(world_handle, settings->dt, settings->max_velocity_iterations, settings->max_position_iterations)
int active_objects = 0;
if (holder.active_body_callbacks.has(world_handle)) {
ActiveBodyCallback callback = holder.active_body_callbacks[world_handle];
Expand Down
41 changes: 20 additions & 21 deletions src/box2d-wrapper/box2d_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
#include "../b2_user_settings.h"

#include <box2d/box2d.h>
#include <box2d/distance.h>
#include <stdio.h>
#include <cstdint>
#include <godot_cpp/core/defs.hpp>
#include <godot_cpp/variant/transform2d.hpp>

/* Generated with cbindgen:0.26.0 */

class b2FixtureUserData;
class b2BodyUserData;

Expand All @@ -22,7 +21,7 @@ struct Material {
};

struct ShapeHandle {
b2Shape **handles;
b2ShapeId **handles;
int count;
};

Expand Down Expand Up @@ -57,7 +56,7 @@ struct PointHitInfo {
b2FixtureUserData user_data;
};

using QueryHandleExcludedCallback = bool (*)(b2World *world_handle,
using QueryHandleExcludedCallback = bool (*)(b2WorldId world_handle,
b2Fixture *collider_handle,
b2FixtureUserData user_data,
const QueryExcludedInfo *handle_excluded_info);
Expand Down Expand Up @@ -97,7 +96,7 @@ struct ContactResult {
};

struct ActiveBodyInfo {
b2Body *body_handle;
b2BodyId body_handle;
b2BodyUserData body_user_data;
};

Expand All @@ -109,7 +108,7 @@ struct CollisionFilterInfo {
bool is_valid;
};

using CollisionFilterCallback = bool (*)(b2World *world_handle, const CollisionFilterInfo *filter_info);
using CollisionFilterCallback = bool (*)(b2WorldId world_handle, const CollisionFilterInfo *filter_info);

struct CollisionEventInfo {
b2Fixture *collider1;
Expand All @@ -122,7 +121,7 @@ struct CollisionEventInfo {
bool is_valid;
};

using CollisionEventCallback = void (*)(b2World *world_handle, const CollisionEventInfo *event_info);
using CollisionEventCallback = void (*)(b2WorldId world_handle, const CollisionEventInfo *event_info);

struct ContactForceEventInfo {
b2Fixture *collider1;
Expand All @@ -132,7 +131,7 @@ struct ContactForceEventInfo {
bool is_valid;
};

using ContactForceEventCallback = bool (*)(b2World *world_handle,
using ContactForceEventCallback = bool (*)(b2WorldId world_handle,
const ContactForceEventInfo *event_info);

struct ContactPointInfo {
Expand All @@ -150,7 +149,7 @@ struct ContactPointInfo {
real_t tangent_impulse_2;
};

using ContactPointCallback = bool (*)(b2World *world_handle,
using ContactPointCallback = bool (*)(b2WorldId world_handle,
const ContactPointInfo *contact_info,
const ContactForceEventInfo *event_info);

Expand All @@ -162,7 +161,7 @@ struct OneWayDirection {
real_t last_timestep;
};

using CollisionModifyContactsCallback = OneWayDirection (*)(b2World *world_handle,
using CollisionModifyContactsCallback = OneWayDirection (*)(b2WorldId world_handle,
const CollisionFilterInfo *filter_info);

struct SimulationSettings {
Expand All @@ -175,21 +174,21 @@ struct SimulationSettings {
b2Vec2 Vector2_to_b2Vec2(godot::Vector2 vec);
godot::Vector2 b2Vec2_to_Vector2(b2Vec2 vec);

bool are_handles_equal(b2World *handle1, b2World *handle2);
bool are_handles_equal(b2Body *handle1, b2Body *handle2);
bool are_handles_equal(b2WorldId handle1, b2WorldId handle2);
bool are_handles_equal(b2BodyId handle1, b2BodyId handle2);
bool are_handles_equal(FixtureHandle handle1, FixtureHandle handle2);
bool are_handles_equal(ShapeHandle handle1, ShapeHandle handle2);
bool are_handles_equal(b2Joint *handle1, b2Joint *handle2);
bool are_handles_equal(b2JointId handle1, b2JointId handle2);
bool are_handles_equal(b2Fixture *handle1, b2Fixture *handle2);

void body_add_force(b2World *world_handle, b2Body *body_handle, const b2Vec2 force);
void body_add_force(b2WorldId world_handle, b2BodyId body_handle, const b2Vec2 force);

void body_add_torque(b2World *world_handle, b2Body *body_handle, real_t torque);
void body_add_torque(b2WorldId world_handle, b2BodyId body_handle, real_t torque);

void body_apply_impulse(b2World *world_handle, b2Body *body_handle, const b2Vec2 impulse);
void body_apply_impulse(b2WorldId world_handle, b2BodyId body_handle, const b2Vec2 impulse);

void body_apply_impulse_at_point(b2World *world_handle,
b2Body *body_handle,
void body_apply_impulse_at_point(b2WorldId world_handle,
b2BodyId body_handle,
const b2Vec2 impulse,
const b2Vec2 point);

Expand Down Expand Up @@ -325,7 +324,7 @@ size_t intersect_shape(b2World *world_handle,
const QueryExcludedInfo *handle_excluded_info,
double margin);

b2World *invalid_world_handle();
b2WorldId invalid_world_handle();
FixtureHandle invalid_fixture_handle();
b2Body *invalid_body_handle();
ShapeHandle invalid_shape_handle();
Expand Down Expand Up @@ -439,10 +438,10 @@ void world_set_active_body_callback(b2World *world_handle, ActiveBodyCallback ca
void world_set_collision_filter_callback(b2World *world_handle,
b2ContactFilter *callback);

void world_set_contact_listener(b2World *world_handle,
void world_set_contact_listener(b2WorldId world_handle,
b2ContactListener *callback);

void world_step(b2World *world_handle, const SimulationSettings *settings);
void world_step(b2WorldId world_handle, const SimulationSettings settings);

} // namespace box2d

Expand Down
7 changes: 3 additions & 4 deletions src/box2d_include.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ using namespace godot;

namespace box2d {

inline uint32_t handle_hash(b2World *handle) {
return hash_one_uint64(uint64_t(handle));
//uint64_t combined = uint64_t(handle.id) + (uint64_t(handle.generation) << 32);
//return hash_one_uint64(combined);
inline uint32_t handle_hash(b2WorldId handle) {
uint64_t combined = uint64_t(handle.index) + (uint64_t(handle.revision) << 32);
return hash_one_uint64(combined);
}
inline uint32_t handle_hash(b2Fixture *handle) {
return hash_one_uint64(uint64_t(handle));
Expand Down
2 changes: 1 addition & 1 deletion src/spaces/box2d_space_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ void Box2DSpace2D::step(real_t p_step) {
settings.gravity.y = default_gravity_dir.y * default_gravity_value;

ERR_FAIL_COND(!box2d::is_handle_valid(handle));
box2d::world_step(handle, &settings);
box2d::world_step(handle, settings);

// Needed only for one physics step to retrieve lost info
removed_colliders.clear();
Expand Down
4 changes: 2 additions & 2 deletions src/spaces/box2d_space_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Box2DSpace2D : public b2ContactFilter, public b2ContactListener {
Box2DDirectSpaceState2D *direct_access = nullptr;
RID rid;

b2World *handle = box2d::invalid_world_handle();
b2WorldId handle = box2d::invalid_world_handle();

struct RemovedColliderInfo {
RID rid;
Expand Down Expand Up @@ -106,7 +106,7 @@ class Box2DSpace2D : public b2ContactFilter, public b2ContactListener {
}

public:
_FORCE_INLINE_ b2World *get_handle() const { return handle; }
_FORCE_INLINE_ b2WorldId get_handle() const { return handle; }

_FORCE_INLINE_ void set_rid(const RID &p_rid) { rid = p_rid; }
_FORCE_INLINE_ RID get_rid() const { return rid; }
Expand Down

0 comments on commit d1fa4fe

Please sign in to comment.