Skip to content

Commit

Permalink
collision callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
goopey7 committed Jan 4, 2024
1 parent b8bbd3c commit 2323b46
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 17 deletions.
22 changes: 16 additions & 6 deletions components/PlayerInput.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
#include "PlayerInput.h"
#include <goop/Input.h>
#include <imgui.h>
#include <GLFW/glfw3.h>
#include <goop/Input.h>
#include <goop/Physics.h>
#include <imgui.h>
#include <iostream>

// Gets called when the game starts
void PlayerInput::init()
{
//...
auto& rb = getComponent<goop::RigidbodyComponent>();
rb.onCollisionEnter = [](goop::Entity other)
{
std::cout << "onCollisionEnter() << other: " << other.getComponent<goop::TagComponent>().tag
<< std::endl;
};
rb.onCollisionExit = [](goop::Entity other)
{
std::cout << "onCollisionExit() << other: " << other.getComponent<goop::TagComponent>().tag
<< std::endl;
};
}

// Gets called every frame
Expand All @@ -26,11 +36,11 @@ void PlayerInput::update(float dt)
{
auto& rb = getComponent<goop::RigidbodyComponent>();
goop::applyImpulse(rb, glm::vec3(0, 5.f, 0));
std::cout<<"pressed space"<<std::endl;
std::cout << "pressed space" << std::endl;
}
if (goop::isKeyReleased(ImGuiKey_Space))
{
std::cout<<"released space"<<std::endl;
std::cout << "released space" << std::endl;
}
}

Expand All @@ -39,5 +49,5 @@ void PlayerInput::update(float dt)
void PlayerInput::gui()
{
// ImGui::Text("Hello from %s", name.c_str());
//...
//...
}
3 changes: 2 additions & 1 deletion editor/EditorApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,8 @@ void EditorApp::gui()
}
if (!e.hasComponent<goop::RigidbodyComponent>() && ImGui::Button("Rigidbody"))
{
e.addComponent<goop::RigidbodyComponent>();
auto& rbc = e.addComponent<goop::RigidbodyComponent>();
rbc.entity = e;
if (goop::sys::gPhysics->isInitialized())
{
goop::sys::gPhysics->addRigidBody(&e.getComponent<goop::RigidbodyComponent>(),
Expand Down
9 changes: 7 additions & 2 deletions goop/Components.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,18 @@ struct RigidbodyComponent
{
float mass;
float box[3];
RigidbodyComponent() : mass(1.0f)
bool isColliding = false;
std::function<void(goop::Entity)> onCollisionEnter = [](goop::Entity other) {};
std::function<void(goop::Entity)> onCollisionExit = [](goop::Entity other) {};
goop::Entity entity;
RigidbodyComponent() : mass(1.0f), entity(entt::null, nullptr)
{
box[0] = 1.0f;
box[1] = 1.0f;
box[2] = 1.0f;
}
RigidbodyComponent(float mass, float box[3]) : mass(mass)
RigidbodyComponent(float mass, float box[3])
: mass(mass), entity(goop::Entity(entt::null, nullptr))
{
memcpy(this->box, box, sizeof(float) * 3);
}
Expand Down
3 changes: 2 additions & 1 deletion goop/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ void Scene::loadScene(nlohmann::json& startScene)
{
json& box = component["box"];
float size[3] = {box["x"], box["y"], box["z"]};
e.addComponent<goop::RigidbodyComponent>(component["mass"], size);
auto& rb = e.addComponent<goop::RigidbodyComponent>(component["mass"], size);
rb.entity = e;
}
else if (component["type"] == "camera")
{
Expand Down
54 changes: 53 additions & 1 deletion goop/sys/platform/bullet/Physics_Bullet.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "Physics_Bullet.h"
#include <glm/gtc/quaternion.hpp>
#include <iostream>

#ifdef GOOP_PHYSICS_BULLET
const std::unique_ptr<goop::sys::Physics> goop::sys::gPhysics =
Expand Down Expand Up @@ -79,6 +80,58 @@ void Physics_Bullet::simulate(float dt)
glm::vec3 euler =
glm::degrees(glm::eulerAngles(glm::quat(rot.w(), rot.x(), rot.y(), rot.z())));
transforms[rbc]->rotation = euler;

// check entry and exit of collisions
int numManifolds = dynamicsWorld->getDispatcher()->getNumManifolds();
for (int i = 0; i < numManifolds; i++)
{
btPersistentManifold* contactManifold =
dynamicsWorld->getDispatcher()->getManifoldByIndexInternal(i);
const btCollisionObject* obA = contactManifold->getBody0();
const btCollisionObject* obB = contactManifold->getBody1();

if (obA == rb || obB == rb)
{
int numContacts = contactManifold->getNumContacts();
// find other rbc
RigidbodyComponent* otherRbc = nullptr;
if (obA == rb)
{
for (auto& [rbc, rb] : rigidBodies)
{
if (rb == obB)
{
otherRbc = rbc;
break;
}
}
}
else
{
for (auto& [rbc, rb] : rigidBodies)
{
if (rb == obA)
{
otherRbc = rbc;
break;
}
}
}
if (numContacts > 0)
{
if (!rbc->isColliding)
{
rbc->isColliding = true;
rbc->onCollisionEnter(otherRbc->entity);
}
}
else if (rbc->isColliding)
{
rbc->isColliding = false;
rbc->onCollisionExit(otherRbc->entity);
}
}
}
}
}

Expand Down Expand Up @@ -131,4 +184,3 @@ void Physics_Bullet::applyImpulse(RigidbodyComponent* rbc, glm::vec3 impulse)
{
rigidBodies[rbc]->applyCentralImpulse(btVector3(impulse.x, impulse.y, impulse.z));
}

12 changes: 6 additions & 6 deletions imgui.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ DockId=0x00000006,0

[Window][Viewport]
Pos=262,27
Size=1735,1469
Size=603,1435
Collapsed=0
DockId=0x00000003,0

[Window][Inspector]
Pos=1999,27
Size=249,1469
Pos=867,27
Size=249,1435
Collapsed=0
DockId=0x00000004,0

Expand All @@ -27,7 +27,7 @@ Collapsed=0

[Window][Dockspace]
Pos=0,19
Size=2256,1485
Size=1124,1451
Collapsed=0

[Window][Example: Log]
Expand All @@ -54,12 +54,12 @@ Collapsed=0

[Window][Scene]
Pos=8,27
Size=252,1469
Size=252,1435
Collapsed=0
DockId=0x00000007,0

[Docking][Data]
DockSpace ID=0x33675C32 Window=0x5B816B74 Pos=8,27 Size=2240,1469 Split=Y
DockSpace ID=0x33675C32 Window=0x5B816B74 Pos=10,59 Size=1108,1435 Split=Y
DockNode ID=0x00000001 Parent=0x33675C32 SizeRef=1954,445 Split=X Selected=0x13926F0B
DockNode ID=0x00000005 Parent=0x00000001 SizeRef=1806,941 Split=X Selected=0x13926F0B
DockNode ID=0x00000007 Parent=0x00000005 SizeRef=252,1435 Selected=0xE192E354
Expand Down

0 comments on commit 2323b46

Please sign in to comment.