Commit 845a045f authored by Nor-s's avatar Nor-s
Browse files

Add Box2D object

parent b7b91322
No related merge requests found
Showing with 209 additions and 2 deletions
+209 -2
......@@ -4,15 +4,17 @@ find_package(glfw3 REQUIRED FATAL_ERROR)
find_package(glm REQUIRED FATAL_ERROR)
find_package(box2d REQUIRED FATAL_ERROR)
add_subdirectory(vkcpp)
add_subdirectory(hand)
add_subdirectory(box)
add_subdirectory(class)
add_executable(hand-window ./main.cpp)
add_executable(hand-window ./main.cpp)
target_compile_options(hand-window PRIVATE -Wall -Werror -O3 -std=c++17)
# include
target_include_directories(hand-window PUBLIC ./ ../)
target_link_libraries(hand-window vkcpp::app)
add_library(box_object STATIC ./box_object.h ./box_object.cpp box_manager.h
box_manager.cpp)
add_library(vkcpp::box ALIAS box_object)
target_compile_options(vkcpp PRIVATE -Wall -Werror -O3 -std=c++17)
# include
target_include_directories(box_object PUBLIC ./)
target_link_libraries(box_object box2d::box2d vkcpp::vkcpp)
#include "box_manager.h"
namespace box
{
BoxManager::BoxManager()
{
init_world();
init_ground();
}
BoxManager::~BoxManager()
{
world_.reset();
}
void BoxManager::init_world()
{
b2Vec2 gravity(0.0f, -10.0f);
world_ = std::make_unique<b2World>(gravity);
}
void BoxManager::init_ground()
{
b2BodyDef ground_body_def;
float height = vkcpp::MainCamera::getInstance()->get_height();
glm::vec2 pos = get_meters(0.0f, height * 2);
glm::vec2 ext = {get_meters(1000.0f), get_meters(height)};
ground_body_def.position.Set(pos.x, pos.y);
ground_body_ = create_body(&ground_body_def);
b2PolygonShape ground_box;
#ifndef NDEBUG
std::cout << "ground ext: " << ext.x << " " << ext.y << "\n";
glm::vec2 tmp1 = {get_pixels(ext.x), get_pixels(ext.y)};
std::cout << "ext: " << tmp1.x << " " << tmp1.y << "\n";
glm::vec2 tmp2 = get_pixels(pos.x, pos.y);
std::cout << "ground pos: " << pos.x << " " << pos.y << "\n";
std::cout << " pos: " << tmp2.x << " " << tmp2.y << "\n";
#endif
ground_box.SetAsBox(ext.x, ext.y);
ground_body_->CreateFixture(&ground_box, 0.0f);
}
b2Body *BoxManager::create_body(const b2BodyDef *body_def)
{
return world_->CreateBody(body_def);
}
void BoxManager::step()
{
world_->Step(time_step_, velocity_iterations_, position_iterations_);
}
glm::vec2 BoxManager::get_pixels(float meter_x, float meter_y)
{
float height = vkcpp::MainCamera::getInstance()->get_height();
return glm::vec2{pixels_per_meter_ * meter_x, height - pixels_per_meter_ * meter_y};
}
float BoxManager::get_pixels(float meter)
{
return meter * pixels_per_meter_;
}
glm::vec2 BoxManager::get_meters(float pixel_x, float pixel_y)
{
float height = vkcpp::MainCamera::getInstance()->get_height();
return glm::vec2{meter_per_pixels_ * pixel_x, meter_per_pixels_ * (height - pixel_y)};
}
float BoxManager::get_meters(float pixel)
{
return pixel * meter_per_pixels_;
}
}
#ifndef BOX_BOX_MANAGER_H
#define BOX_BOX_MANAGER_H
#include "vkcpp.h"
#include <box2d/box2d.h>
namespace box
{
class BoxManager : public vkcpp::Singleton<BoxManager>
{
private:
std::unique_ptr<b2World> world_;
std::unique_ptr<vkcpp::Object2D> ground_;
b2Body *ground_body_;
float time_step_ = 1.0f / 60.0f;
int32 velocity_iterations_ = 8;
int32 position_iterations_ = 3;
static constexpr float pixels_per_meter_ = 50.0f;
static constexpr float meter_per_pixels_ = 0.02f;
public:
BoxManager();
~BoxManager();
void init_world();
void init_ground();
b2Body *create_body(const b2BodyDef *body_def);
void step();
static glm::vec2 get_pixels(float meter_x, float meter_y);
static float get_pixels(float meter);
static glm::vec2 get_meters(float pixel_x, float pixel_y);
static float get_meters(float pixel);
};
}
#endif
\ No newline at end of file
#include "box_object.h"
#include "box_manager.h"
namespace box
{
BoxObject::BoxObject(
const vkcpp::Device *device,
const vkcpp::RenderStage *render_stage,
const vkcpp::CommandPool *command_pool,
const VkExtent3D &extent,
const glm::vec3 &position)
: vkcpp::Object2D(device, render_stage, command_pool, extent)
{
init_box_object(extent, position);
init_transform(position);
}
BoxObject::~BoxObject()
{
}
void BoxObject::init_box_object(const VkExtent3D &extent,
const glm::vec3 &position)
{
b2BodyDef bodyDef;
glm::vec2 meter_pos = BoxManager::get_meters(position.x, position.y);
glm::vec2 meter_ext = {BoxManager::get_meters(extent.width), BoxManager::get_meters(extent.height)};
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(meter_pos.x, meter_pos.y);
bodyDef.userData.pointer = reinterpret_cast<uintptr_t>(this);
body_ = BoxManager::getInstance()->create_body(&bodyDef);
b2PolygonShape dynamicBox;
dynamicBox.SetAsBox(meter_ext.x / 2.0, meter_ext.y / 2.0f);
b2FixtureDef fixtureDef;
fixtureDef.shape = &dynamicBox;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.3f;
#ifndef NDEBUG
std::cout << "box ext: " << meter_ext.x << " " << meter_ext.y << "\n";
glm::vec2 tmp1 = {BoxManager::get_pixels(meter_ext.x), BoxManager::get_pixels(meter_ext.y)};
std::cout << "ext: " << tmp1.x << " " << tmp1.y << "\n";
glm::vec2 tmp2 = BoxManager::get_pixels(meter_pos.x, meter_pos.y);
std::cout << "box pos: " << meter_pos.x << " " << meter_pos.y << "\n";
std::cout << " pos: " << tmp2.x << " " << tmp2.y << "\n";
#endif
body_->CreateFixture(&fixtureDef);
}
void BoxObject::update(uint32_t ubo_idx)
{
b2Vec2 pos = body_->GetPosition();
float angle = body_->GetAngle();
glm::vec2 pixel_pos = BoxManager::get_pixels(pos.x, pos.y);
move_xy(pixel_pos.x, pixel_pos.y);
rotate_z(angle);
#ifndef NDEBUG
std::cout << pos.x << " " << pos.y << "\n";
#endif
update_with_main_camera(ubo_idx);
}
}
#ifndef BOX_BOX_OBJECT_H
#define BOX_BOX_OBJECT_H
#include "vkcpp.h"
#include <box2d/box2d.h>
namespace box
{
class BoxObject : public vkcpp::Object2D
{
private:
b2Body *body_ = nullptr;
public:
BoxObject() = delete;
BoxObject(
const vkcpp::Device *device,
const vkcpp::RenderStage *render_stage,
const vkcpp::CommandPool *command_pool,
const VkExtent3D &extent,
const glm::vec3 &position);
virtual ~BoxObject();
void init_box_object(const VkExtent3D &extent,
const glm::vec3 &position);
virtual void update(uint32_t ubo_idx);
};
}
#endif
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment