Skip to content

Commit

Permalink
Extremely hacky and bad physics
Browse files Browse the repository at this point in the history
  • Loading branch information
Hjaltesorgenfrei committed Mar 15, 2023
1 parent d0243a5 commit 98ad380
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 4 deletions.
13 changes: 13 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,44 @@
path = third_party/glfw
url = https://github.com/glfw/glfw
ignore = dirty
shallow = true
[submodule "ext/glm"]
path = third_party/glm
url = https://github.com/g-truc/glm
ignore = dirty
shallow = true
[submodule "ext/stb"]
path = third_party/stb
url = https://github.com/nothings/stb
ignore = dirty
shallow = true
[submodule "ext/tinyobjloader"]
path = third_party/tinyobjloader
url = https://github.com/tinyobjloader/tinyobjloader
ignore = dirty
shallow = true
[submodule "ext/VulkanMemoryAllocator"]
path = third_party/VulkanMemoryAllocator
url = https://github.com/GPUOpen-LibrariesAndSDKs/VulkanMemoryAllocator
ignore = dirty
shallow = true
[submodule "ext/imgui"]
path = third_party/imgui
url = https://github.com/ocornut/imgui
ignore = dirty
shallow = true
[submodule "ext/ImGuizmo"]
path = third_party/ImGuizmo
url = https://github.com/CedricGuillemet/ImGuizmo
ignore = dirty
shallow = true
[submodule "ext/ktx"]
path = third_party/ktx
url = https://github.com/KhronosGroup/KTX-Software
ignore = dirty
shallow = true
[submodule "third_party/bullet3"]
path = third_party/bullet3
url = https://github.com/bulletphysics/bullet3
ignore = dirty
shallow = true
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ add_compile_definitions(VULKAN_HPP_NO_STRUCT_CONSTRUCTORS) # Version 145 at leas
target_precompile_headers(vulkanologi PRIVATE ${Vulkan_INCLUDE_DIRS}/vulkan/vulkan.hpp third_party/glm/glm/glm.hpp)
target_precompile_headers(vulkanologi PRIVATE src/BehDevice.h src/WindowWrapper.h)

target_link_libraries(vulkanologi glfw glm tinyobjloader VulkanMemoryAllocator Vulkan::Vulkan ktx imgui stb)
target_link_libraries(vulkanologi glfw glm tinyobjloader VulkanMemoryAllocator Vulkan::Vulkan ktx imgui stb bullet3)

add_shader(vulkanologi shader.frag)
add_shader(vulkanologi shader.vert)
Expand Down
96 changes: 96 additions & 0 deletions src/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <cstdlib>
#include <chrono>
#include <thread>
// Include bullet
#include <btBulletDynamicsCommon.h>

#include "Renderer.h"
#include "Application.h"
Expand All @@ -13,6 +15,47 @@
#include "Path.h"
#include "Spline.h"

// dynamics world pointer
btDiscreteDynamicsWorld* dynamicsWorld;

class DebugDrawer : public btIDebugDraw {
public:
void drawLine(const btVector3 &from, const btVector3 &to, const btVector3 &color) override {
auto path = linePath(glm::vec3(from.x(), from.y(), from.z()), glm::vec3(to.x(), to.y(), to.z()), glm::vec3(color.x(), color.y(), color.z()));
paths.emplace_back(path);
}

void drawContactPoint(const btVector3 &PointOnB, const btVector3 &normalOnB, btScalar distance, int lifeTime, const btVector3 &color) override {
auto path = linePath(glm::vec3(PointOnB.x(), PointOnB.y(), PointOnB.z()), glm::vec3(PointOnB.x(), PointOnB.y(), PointOnB.z()) + glm::vec3(normalOnB.x(), normalOnB.y(), normalOnB.z()) * distance, glm::vec3(color.x(), color.y(), color.z()));
paths.emplace_back(path);
}

void reportErrorWarning(const char *warningString) override {
std::cout << "Warning: " << warningString << std::endl;
}

void draw3dText(const btVector3 &location, const char *textString) override {
std::cout << "Text: " << textString << std::endl;
}

void setDebugMode(int debugMode) override {
std::cout << "Debug mode: " << debugMode << std::endl;
}

int getDebugMode() const override {
return DBG_DrawWireframe;
}

// clear lines
void clearLines() override {
paths.clear();
}

std::vector<Path> paths;

};

DebugDrawer * debugDrawer;

void App::run() {
setupCallBacks(); // We create ImGui in the renderer, so callbacks have to happen before.
Expand Down Expand Up @@ -193,6 +236,10 @@ int App::drawFrame(float delta) {
frameInfo.paths.emplace_back(linePath(end, left, {0, 1, 0}));
frameInfo.paths.emplace_back(linePath(right, left, {0, 1, 0}));
}

dynamicsWorld->debugDrawWorld();
auto physicsPaths = debugDrawer->paths;
frameInfo.paths.insert(frameInfo.paths.end(), physicsPaths.begin(), physicsPaths.end());
}

if (!objects.empty()) {
Expand All @@ -209,6 +256,45 @@ int App::drawFrame(float delta) {
}

void App::mainLoop() {
// make bullet3 physics world
auto collisionConfiguration = new btDefaultCollisionConfiguration();
auto dispatcher = new btCollisionDispatcher(collisionConfiguration);
auto overlappingPairCache = new btDbvtBroadphase();
auto solver = new btSequentialImpulseConstraintSolver;
dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher, overlappingPairCache, solver, collisionConfiguration);
dynamicsWorld->setGravity(btVector3(0, -10, 0));

// Create a ground plane
auto groundShape = new btStaticPlaneShape(btVector3(0, 1, 0), 1);
auto groundTransform = btTransform();
groundTransform.setIdentity();
groundTransform.setOrigin(btVector3(0, -1, 0));
auto groundMass = 0.f;
auto groundLocalInertia = btVector3(0, 0, 0);
auto groundMotionState = new btDefaultMotionState(groundTransform);
auto groundRigidBodyCI = btRigidBody::btRigidBodyConstructionInfo(groundMass, groundMotionState, groundShape, groundLocalInertia);
auto groundRigidBody = new btRigidBody(groundRigidBodyCI);
dynamicsWorld->addRigidBody(groundRigidBody);

// add 5 dynamic boxes
auto colShape = new btBoxShape(btVector3(1, 1, 1));
auto startTransform = btTransform();
startTransform.setIdentity();
auto mass = 1.f;
auto localInertia = btVector3(0, 0, 0);
colShape->calculateLocalInertia(mass, localInertia);
for (int i = 0; i < 5; i++) {
startTransform.setOrigin(btVector3(2 * i, 10, 0));
auto myMotionState = new btDefaultMotionState(startTransform);
auto rbInfo = btRigidBody::btRigidBodyConstructionInfo(mass, myMotionState, colShape, localInertia);
auto body = new btRigidBody(rbInfo);
dynamicsWorld->addRigidBody(body);
}

// Add debug drawing
debugDrawer = new DebugDrawer();
dynamicsWorld->setDebugDrawer(debugDrawer);

auto timeStart = std::chrono::high_resolution_clock::now();
// objects.push_back(std::make_shared<RenderObject>(Mesh::LoadFromObj("resources/lost_empire.obj"), Material{}));
objects.push_back(std::make_shared<RenderObject>(createCube(glm::vec3{}), Material{}));
Expand All @@ -221,6 +307,9 @@ void App::mainLoop() {
timeStart = now;
glfwPollEvents();
processPressedKeys(delta.count());

debugDrawer->clearLines();
dynamicsWorld->stepSimulation(delta.count() / 1000.f, 10);

if (updateWindowSize) {
renderer->recreateSwapchain();
Expand All @@ -231,6 +320,13 @@ void App::mainLoop() {
renderer->recreateSwapchain();
}
}

delete debugDrawer;
delete dynamicsWorld;
delete solver;
delete overlappingPairCache;
delete dispatcher;
delete collisionConfiguration;
}

bool App::drawImGuizmo(glm::mat4* matrix) {
Expand Down
2 changes: 1 addition & 1 deletion src/Path.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Path {
public:
Path(bool loop = false);

const bool loop;
bool loop;

void addPoint(Point point);
Point getPoint(int index);
Expand Down
4 changes: 2 additions & 2 deletions src/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -575,8 +575,8 @@ void Renderer::createWireframePipeline() {
}

void Renderer::createLinePipeline() {
lineVertexBuffer = assetManager.allocatePersistentBuffer<Point>(10000, vk::BufferUsageFlagBits::eVertexBuffer);
lineIndexBuffer = assetManager.allocatePersistentBuffer<uint32_t>(10000 * 2, vk::BufferUsageFlagBits::eIndexBuffer);
lineVertexBuffer = assetManager.allocatePersistentBuffer<Point>(100000, vk::BufferUsageFlagBits::eVertexBuffer);
lineIndexBuffer = assetManager.allocatePersistentBuffer<uint32_t>(100000 * 2, vk::BufferUsageFlagBits::eIndexBuffer);

PipelineConfigurationInfo pipelineConfig{};
BehPipeline::defaultPipelineConfiguration(pipelineConfig);
Expand Down
19 changes: 19 additions & 0 deletions third_party/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,25 @@ add_subdirectory(tinyobjloader)
# VMA
add_subdirectory(VulkanMemoryAllocator)

# bullet3
set(BUILD_BULLET2_DEMOS OFF CACHE BOOL "Bullet Lib only" FORCE)
set(BUILD_CPU_DEMOS OFF CACHE BOOL "Bullet Lib only" FORCE)
set(BUILD_ENET OFF CACHE BOOL "Bullet Lib only" FORCE)
set(BUILD_EXTRAS OFF CACHE BOOL "Bullet Lib only" FORCE)
set(BUILD_OPENGL3_DEMOS OFF CACHE BOOL "Bullet Lib only" FORCE)
set(BUILD_SHARED_LIBS OFF CACHE BOOL "Bullet Lib only" FORCE)
set(BUILD_UNIT_TESTS OFF CACHE BOOL "Bullet Lib only" FORCE)
set(USE_MSVC_RUNTIME_LIBRARY_DLL ON CACHE BOOL "Bullet Lib only" FORCE)
set(USE_MSVC_INCREMENTAL_LINKING ON CACHE BOOL "Bullet Lib only" FORCE)

set(INSTALL_LIBS CACHE BOOL "Bullet Lib only" FORCE)
set(INSTALL_CMAKE_FILES CACHE BOOL "Bullet Lib only" FORCE)

add_subdirectory(bullet3)
add_library(bullet3 INTERFACE) # dummy target to make bullet3 a dependency
target_link_libraries(bullet3 INTERFACE BulletDynamics BulletCollision LinearMath Bullet3Common)
target_include_directories(bullet3 INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/bullet3/src)

# Imgui
include_directories(imgui)

Expand Down
1 change: 1 addition & 0 deletions third_party/bullet3
Submodule bullet3 added at 0e5947

0 comments on commit 98ad380

Please sign in to comment.