Skip to content

Commit

Permalink
[update] two separate functions to update each physic step
Browse files Browse the repository at this point in the history
  • Loading branch information
MrsRina committed Sep 28, 2024
1 parent c9a3be2 commit 194337d
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 58 deletions.
14 changes: 13 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
cmake_minimum_required(VERSION 3.9)

set(CMAKE_CXX_STANDARD 17)
set(BICUDO_VERSIION 0.1.0)
set(BICUDO_VERSIION 1.1.0)

if(
CMAKE_CXX_COMPILER_ID STREQUAL "Clang"
OR
CMAKE_CXX_COMPILER_ID STREQUAL "GNU"
)

add_compile_options(
-O3
)
endif()


if(LINUX OR ANDROID)
file(GLOB ROCM_INCLUDE_DIR "/opt/rocm/include")
Expand Down
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ bicudo::init(&bicudo_runtime);
```

Running Bicudo is simple, be sure the application is calculating correctly the delta time.
```

Calling `bicudo::update(bicudo::runtime *p_runtime)` update position and process collision resolution.
```C++
// any framework media layer initialization
// bicudo initialization

Expand All @@ -66,6 +68,34 @@ while (mainloop) {
}
```

Alternatively there are two separate functions to update each physic step:
-- `bicudo::update_position(bicudo::runtime *p_runtime, bicudo::physics::placement *p_placement)`
-- `bicudo::update_collision(bicudo::runtime *p_runtime)`

```C++
// any framework media layer initialization
// bicudo initialization

while (mainloop) {
bicudo::dt = 0.16f; // 60fps

for (bicudo::physics::placement *&p_placements : bicudo_runtime.placement_list) {
bicudo::update_position(
&bicudo_runtime,
p_placements
);
}

bicudo::update_collisions(
&bicudo_runtime
);

bicudo::log::flush(); // flush log

// etc render
}
```

Rendering is application-side, Bicudo does not provide any way rendering engine, only geometry data.

```C++
Expand Down
14 changes: 7 additions & 7 deletions bicudo.sublime-project
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
"tab_size": 2,
"LSP":
{
"clangd":
{
"initializationOptions":
{
"clangd.compile-commands-dir": "./cmake-build-debug"
"clangd":
{
"initializationOptions":
{
"clangd.compile-commands-dir": "./cmake-build-debug"
}
}
}
}
}
},
"build_systems":
[
Expand Down
11 changes: 11 additions & 0 deletions include/bicudo/bicudo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "bicudo/physics/placement.hpp"
#include <cstdint>

#define bicudo_version "1.1.0"

namespace bicudo {
struct runtime {
public:
Expand Down Expand Up @@ -45,6 +47,15 @@ namespace bicudo {
void update(
bicudo::runtime *p_runtime
);

void update_position(
bicudo::runtime *p_runtime,
bicudo::physics::placement *p_placement
);

void update_collisions(
bicudo::runtime *p_runtime
);
}

#endif
4 changes: 4 additions & 0 deletions meow/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ if(
-static-libgcc
-static-libstdc++
)

add_compile_options(
-O3
)
endif()

if(LINUX)
Expand Down
18 changes: 11 additions & 7 deletions meow/src/meow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ int32_t main(int32_t, char**) {
.p_tag = "vakinha",
.mass = 2000.0f,
.friction = 0.8f,
.restitution = 1.0f,
.restitution = 0.2f,
.pos = {20, 20},
.size = {144, 144},
.acc = gravity
Expand All @@ -312,7 +312,7 @@ int32_t main(int32_t, char**) {
.p_tag = "gatinho",
.mass = 20.0f,
.friction = 0.8f,
.restitution = 1.0f,
.restitution = 0.2f,
.pos = {200, 20},
.size = {400, 50},
.acc = gravity
Expand All @@ -322,7 +322,7 @@ int32_t main(int32_t, char**) {
.p_tag = "terrain-bottom",
.mass = 0.0f,
.friction = 0.8f,
.restitution = 1.0f,
.restitution = 0.2f,
.inertia = 0.0f,
.pos = {200, 800},
.size = {1280, 50},
Expand All @@ -333,7 +333,7 @@ int32_t main(int32_t, char**) {
.p_tag = "terrain-top",
.mass = 0.0f,
.friction = 0.8f,
.restitution = 1.0f,
.restitution = 0.2f,
.inertia = 0.0f,
.pos = {200, 200},
.size = {1280, 50},
Expand All @@ -344,7 +344,7 @@ int32_t main(int32_t, char**) {
.p_tag = "terrain-left",
.mass = 0.0f,
.friction = 0.8f,
.restitution = 1.0f,
.restitution = 0.2f,
.inertia = 0.0f,
.pos = {200, 200},
.size = {50, 1280},
Expand All @@ -355,7 +355,7 @@ int32_t main(int32_t, char**) {
.p_tag = "terrain-right",
.mass = 0.0f,
.friction = 0.8f,
.restitution = 1.0f,
.restitution = 0.2f,
.inertia = 0.0f,
.pos = {900, 200},
.size = {50, 1280},
Expand Down Expand Up @@ -436,7 +436,11 @@ int32_t main(int32_t, char**) {
&meow::app.object_pickup_info
);

bicudo::update(&meow::app.bicudo);
for (bicudo::physics::placement *&p_placements : meow::app.bicudo.placement_list) {
bicudo::update_position(&meow::app.bicudo, p_placements);
}

bicudo::update_collisions(&meow::app.bicudo);
ekg::update();

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Expand Down
120 changes: 78 additions & 42 deletions src/bicudo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
void bicudo::init(
bicudo::runtime *p_runtime
) {
bicudo::log() << "Initializing Bicudo physics simulator!";
bicudo::log() << "Initializing Bicudo " << bicudo_version << "physics simulator!";

if (p_runtime->p_rocm_api) {
p_runtime->p_rocm_api->init();
Expand Down Expand Up @@ -52,68 +52,104 @@ void bicudo::erase(
bicudo::runtime *p_runtime,
bicudo::physics::placement *p_placement
) {
// meow
for (uint64_t it {}; it < p_runtime->placement_list.size(); it++) {
bicudo::physics::placement *&p_alive_placement {
p_runtime->placement_list.at(it)
};

if (p_alive_placement != nullptr && p_alive_placement == p_placement) {
p_runtime->placement_list.erase(p_runtime->placement_list.begin() + it);
break;
}
}
}

void bicudo::erase(
bicudo::runtime *p_runtime,
bicudo::id id
) {
// meow
for (uint64_t it {}; it < p_runtime->placement_list.size(); it++) {
bicudo::physics::placement *&p_alive_placement {
p_runtime->placement_list.at(it)
};

if (p_alive_placement != nullptr && p_alive_placement->id == id) {
p_runtime->placement_list.erase(p_runtime->placement_list.begin() + it);
break;
}
}
}

void bicudo::update(
bicudo::runtime *p_runtime
) {
bicudo::vec2 center {};
for (bicudo::physics::placement *&p_placement : p_runtime->placement_list) {
if ( // temp
bicudo::assert_float(p_placement->prev_size.x, p_placement->size.x)
||
bicudo::assert_float(p_placement->prev_size.y, p_placement->size.y)
) {
p_placement->prev_size = p_placement->size;
bicudo::splash_vertices(
p_placement->vertices.data(),
p_placement->pos,
p_placement->size
);
}
bicudo::update_position(
p_runtime,
p_placement
);
}

bicudo::physics_processor_update(
p_runtime
);
}

void bicudo::update_position(
bicudo::runtime *p_runtime,
bicudo::physics::placement *p_placement
) {
bicudo::vec2 center {};
if ( // temp
bicudo::assert_float(p_placement->prev_size.x, p_placement->size.x)
||
bicudo::assert_float(p_placement->prev_size.y, p_placement->size.y)
) {
p_placement->prev_size = p_placement->size;
bicudo::splash_vertices(
p_placement->vertices.data(),
p_placement->pos,
p_placement->size
);
}

p_placement->acc.y = (
p_runtime->gravity.y * (!bicudo::assert_float(p_placement->mass, 0.0f))
); // enable it

p_placement->min.x = 99999.0f;
p_placement->min.y = 99999.0f;
p_placement->max.x = -99999.0f;
p_placement->max.y = -99999.0f;
p_placement->acc.y = (
p_runtime->gravity.y * (!bicudo::assert_float(p_placement->mass, 0.0f))
); // enable it

p_placement->velocity += p_placement->acc * bicudo::dt;
p_placement->pos += p_placement->velocity;
p_placement->min.x = 99999.0f;
p_placement->min.y = 99999.0f;
p_placement->max.x = -99999.0f;
p_placement->max.y = -99999.0f;

p_placement->angular_velocity += p_placement->angular_acc * bicudo::dt;
p_placement->angle += p_placement->angular_velocity;
p_placement->velocity += p_placement->acc * bicudo::dt;
p_placement->pos += p_placement->velocity;

center.x = p_placement->pos.x + (p_placement->size.x / 2);
center.y = p_placement->pos.y + (p_placement->size.y / 2);
p_placement->angular_velocity += p_placement->angular_acc * bicudo::dt;
p_placement->angle += p_placement->angular_velocity;

for (bicudo::vec2 &vertex : p_placement->vertices) {
vertex += p_placement->velocity;
vertex = vertex.rotate(p_placement->angular_velocity, center);
center.x = p_placement->pos.x + (p_placement->size.x / 2);
center.y = p_placement->pos.y + (p_placement->size.y / 2);

p_placement->min.x = std::min(p_placement->min.x, vertex.x);
p_placement->min.y = std::min(p_placement->min.y, vertex.y);
p_placement->max.x = std::max(p_placement->max.x, vertex.x);
p_placement->max.y = std::max(p_placement->max.y, vertex.y);
}
for (bicudo::vec2 &vertex : p_placement->vertices) {
vertex += p_placement->velocity;
vertex = vertex.rotate(p_placement->angular_velocity, center);

bicudo::splash_edges_normalized(
p_placement->edges.data(),
p_placement->vertices.data()
);
p_placement->min.x = std::min(p_placement->min.x, vertex.x);
p_placement->min.y = std::min(p_placement->min.y, vertex.y);
p_placement->max.x = std::max(p_placement->max.x, vertex.x);
p_placement->max.y = std::max(p_placement->max.y, vertex.y);
}

bicudo::splash_edges_normalized(
p_placement->edges.data(),
p_placement->vertices.data()
);
}

void bicudo::update_collisions(
bicudo::runtime *p_runtime
) {
bicudo::physics_processor_update(
p_runtime
);
Expand Down

0 comments on commit 194337d

Please sign in to comment.