Skip to content

Commit

Permalink
[feature] event callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
MrsRina committed Sep 29, 2024
1 parent bd7c2fe commit ca2895c
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 37 deletions.
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.9)

set(CMAKE_CXX_STANDARD 17)
set(BICUDO_VERSIION 1.1.0)
set(BICUDO_VERSIION 1.2.0)

if(
CMAKE_CXX_COMPILER_ID STREQUAL "Clang"
Expand All @@ -14,7 +14,6 @@ if(
)
endif()


if(LINUX OR ANDROID)
file(GLOB ROCM_INCLUDE_DIR "/opt/rocm/include")
set(LIBRARY_OUTPUT_PATH "../lib/linux/")
Expand Down
29 changes: 27 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Bicudo 🐦
# Bicudo 🐤

Bicudo is a physics engine library being develop to process Separation Axis Theorem (SAT) and Newton's laws under GPU via an GPGPU API (ROCm/HIP, OpenCL, CUDA/HIP). The project contains an optionally client called Meow OpenGL-4 based to test the library.

Expand Down Expand Up @@ -144,9 +144,34 @@ bicudo::insert(
);
```

### Events Callback 🧼

Bicudo offers two events callback for invoke on collision, an pre collision before apply physics forces and an after.

```C++
void on_collision_pre_apply_forces(
bicudo::physics::placement *&p_a,
bicudo::physics::placement *&p_b
) {
bicudo::log() << "pre-forces: " << p_a->p_tag << " x " << p_b->p_tag;
p_a->velocity = {}; // you are able to modify the forces here
}

void on_collision(
bicudo::physics::placement *&p_a,
bicudo::physics::placement *&p_b
) {
bicudo::log() << "after forces: " << p_a->p_tag << " x " << p_b->p_tag;
p_a->velocity = {}; // you are able to modify the forces here too
}

bicudo_runtime.p_on_collision_pre_apply_forces = &on_collision_pre_apply_forces;
bicudo_runtime.p_on_collision = &on_collision;
```

---

# Bicudo Building 🔧🐦
# Bicudo Building 🔧🐤

Bicudo library requires all the three APIs include dir (ROCm/HIP, OpenCL, CUDA/HIP). For now only [ROCm/HIP](https://github.com/ROCm/HIP).
Install the ROCm-SDK, for Windows download the official AMD installer, for Linux run command from the package manager.
Expand Down
15 changes: 12 additions & 3 deletions include/bicudo/bicudo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,27 @@
#include "bicudo/physics/placement.hpp"
#include <cstdint>

#define bicudo_version "1.1.1"
#define bicudo_version "1.2.0"

namespace bicudo {
typedef void(*p_on_collision_pre_apply_forces)(bicudo::physics::placement*&, bicudo::physics::placement*&);
typedef void(*p_on_collision)(bicudo::physics::placement*&, bicudo::physics::placement*&);

struct runtime {
public:
public: // internal
std::vector<bicudo::physics::placement*> placement_list {};
bicudo::physics::collision_info_t collision_info {};
bicudo::id highest_object_id {};
public:
public: // config
float solve_accurace {1.0f};
float intertia_const {12.0f};
bicudo::vec2 gravity {};

bicudo::physics_runtime_type physics_runtime_type {bicudo::physics_runtime_type::CPU_SIDE};
bicudo::api::base *p_rocm_api {};

bicudo::p_on_collision_pre_apply_forces p_on_collision_pre_apply_forces {nullptr};
bicudo::p_on_collision p_on_collision {nullptr};
};

void init(
Expand Down
6 changes: 1 addition & 5 deletions include/bicudo/physics/placement.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ namespace bicudo::physics {
bool turn_off_gravity {};
bool was_collided {};
bicudo::vec2 prev_size {};
float prev_mass {};
};
}

Expand All @@ -47,11 +48,6 @@ namespace bicudo {
bicudo::physics::placement *p_placement,
float angle_dir
);

void physics_placement_mass(
bicudo::physics::placement *p_placement,
float mass
);
}

#endif
2 changes: 1 addition & 1 deletion include/bicudo/util/math.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#define bicudo_clamp_min(a, b) ((a) < (b) ? (b) : (a))
#define bicudo_clamp_max(a, b) ((a) > (b) ? (b) : (a))
#define bicudo_clamp(val, min, max) (val < min ? min : ((max < val) ? max : val))
#define bicudo_clamp(val, min, max) ((val) < (min) ? (min) : (val > max ? max : val))
#define bicudo_deg2rad(x) ((x) * 0.0174533f)
#define bicudo_lerp(a, b, t) ((a) - (b - a ) * (t)) // buu???????? repeated code omg!!!11 rp it

Expand Down
2 changes: 1 addition & 1 deletion meow/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.9)

set(CMAKE_CXX_STANDARD 17)
set(MEOW_VERSION 0.1.0)
set(MEOW_VERSION 1.0.0)

if(
CMAKE_CXX_COMPILER_ID STREQUAL "Clang"
Expand Down
18 changes: 17 additions & 1 deletion meow/src/meow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@

meow::application meow::app {};

void on_collided_meow_meow(
bicudo::physics::placement *&p_a,
bicudo::physics::placement *&p_b
) {
std::cout << "collided " << p_a->p_tag << " " << p_b->p_tag << std::endl;
}

void on_pre_collided_meow_meow_mu(
bicudo::physics::placement *&p_a,
bicudo::physics::placement *&p_b
) {
std::cout << "pre-mu " << p_a->p_tag << " " << p_b->p_tag << std::endl;
}

void meow::init() {
bicudo::log() << "Initializing Meow renderer and GUI bindings!";

Expand All @@ -28,7 +42,9 @@ void meow::init() {
meow::app.bicudo = {
.gravity = {},
.physics_runtime_type = bicudo::physics_runtime_type::CPU_SIDE,
.p_rocm_api = new bicudo::api::rocm()
.p_rocm_api = new bicudo::api::rocm(),
.p_on_collision_pre_apply_forces = &on_pre_collided_meow_meow_mu,
.p_on_collision = &on_collided_meow_meow
};

bicudo::init(&meow::app.bicudo);
Expand Down
24 changes: 17 additions & 7 deletions src/bicudo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ void bicudo::insert(
bicudo::runtime *p_runtime,
bicudo::physics::placement *p_placement
) {
bicudo::physics_placement_mass(
p_placement,
p_placement->mass
);

p_placement->vertices.resize(4);
bicudo::splash_vertices(
p_placement->vertices.data(),
Expand Down Expand Up @@ -101,9 +96,9 @@ void bicudo::update_position(
) {
bicudo::vec2 center {};
if ( // temp
bicudo::assert_float(p_placement->prev_size.x, p_placement->size.x)
!bicudo::assert_float(p_placement->prev_size.x, p_placement->size.x)
||
bicudo::assert_float(p_placement->prev_size.y, p_placement->size.y)
!bicudo::assert_float(p_placement->prev_size.y, p_placement->size.y)
) {
p_placement->prev_size = p_placement->size;
bicudo::splash_vertices(
Expand All @@ -113,6 +108,21 @@ void bicudo::update_position(
);
}

if ( // temp
!bicudo::assert_float(p_placement->prev_mass, p_placement->mass)
) {
if (bicudo::assert_float(p_placement->mass, 0.0f)) {
p_placement->inertia = 0.0f;
p_placement->mass = 0.0f;
} else {
p_placement->mass = p_placement->mass;
p_placement->inertia = (1.0f / p_placement->mass) * p_placement->size.magnitude_no_sq() / p_runtime->intertia_const;
p_placement->inertia = 1.0f / p_placement->inertia;
}

p_placement->prev_mass = p_placement->mass;
}

p_placement->acc.y = (
(p_runtime->gravity.y)
*
Expand Down
14 changes: 0 additions & 14 deletions src/physics/placement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,4 @@ void bicudo::physics_placement_rotate(
}

p_placement->angle += angle_dir;
}

void bicudo::physics_placement_mass(
bicudo::physics::placement *p_placement,
float mass
) {
if (bicudo::assert_float(mass, 0.0f)) {
p_placement->inertia = 0.0f;
p_placement->mass = 0.0f;
} else {
p_placement->mass = mass;
p_placement->inertia = (1.0f / mass) * p_placement->size.magnitude_no_sq() / 12;
p_placement->inertia = 1.0f / p_placement->inertia;
}
}
10 changes: 9 additions & 1 deletion src/physics/processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ void bicudo::physics_processor_update(

n = p_runtime->collision_info.normal;
total_mass = p_a->mass + p_b->mass;
num = p_runtime->collision_info.depth / total_mass * 1.0f;
num = p_runtime->collision_info.depth / total_mass * p_runtime->solve_accurace;
correction = n * num;

bicudo::physics_placement_move(
Expand All @@ -99,6 +99,10 @@ void bicudo::physics_processor_update(
correction * p_b->mass
);

if (p_runtime->p_on_collision_pre_apply_forces) {
p_runtime->p_on_collision_pre_apply_forces(p_a, p_b);
}

start = p_runtime->collision_info.start * (p_b->mass / total_mass);
end = p_runtime->collision_info.end * (p_a->mass / total_mass);
p = start + end;
Expand Down Expand Up @@ -166,6 +170,10 @@ void bicudo::physics_processor_update(

p_a->angular_velocity -= c1_cross * jt * p_a->inertia;
p_b->angular_velocity += c2_cross * jt * p_b->inertia;

if (p_runtime->p_on_collision) {
p_runtime->p_on_collision(p_a, p_b);
}
}
}
}
Expand Down

0 comments on commit ca2895c

Please sign in to comment.