Skip to content

Commit

Permalink
Merge pull request #132 from JerboaBurrow/softBodyTetris
Browse files Browse the repository at this point in the history
Soft body tetris
  • Loading branch information
Jerboa-app authored Dec 16, 2023
2 parents 79fa418 + d632432 commit 1654684
Show file tree
Hide file tree
Showing 21 changed files with 732 additions and 233 deletions.
10 changes: 7 additions & 3 deletions demo/desktop/softBodyTetris/config.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
config =
{
["timeStep"] = 1.0/1800.0,
["subSample"] = 2,
["timeStep"] = 1.0/(5*900.0),
["subSample"] = 10,
["cofr"] = 0.75
}

hop.configure(config);
hop.configure(config);

require("meshes")

math.randomseed(os.time())
59 changes: 59 additions & 0 deletions demo/desktop/softBodyTetris/loop.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
s = hop.maxCollisionPrimitiveSize()

if lastTime == nil then
lastTime = hop.timeMillis()

mesh = meshes[math.random(#meshes)]

x = 0.5 + math.random()*0.5 -0.25
y = 0.5

r = math.random(255)
g = math.random(255)
b = math.random(255)

o = {

["transform"] = {x,y,0.0,s},
["colour"] = {r/255,g/255,b/255,1.0},
["moveable"] = true,
["collisionMesh"] = mesh,
["meshParameters"] = {15000.0, 1.0, 0.2},
["name"] = ""

}

hop.loadObject(o)
end

time = hop.timeMillis()

if time-lastTime > 1000*5 then

if (hop.kineticEnergy() > 0.1) then
lastTime = hop.timeMillis()
else
mesh = meshes[math.random(#meshes)]

x = 0.5 + math.random()*0.5 -0.25
y = 1.25

r = math.random(255)
g = math.random(255)
b = math.random(255)

o = {

["transform"] = {x,y,0.0,s},
["colour"] = {r/255,g/255,b/255,1.0},
["moveable"] = true,
["collisionMesh"] = mesh,
["meshParameters"] = {15000.0, 1.0, 0.2},
["name"] = ""

}

hop.loadObject(o)
lastTime = hop.timeMillis()
end
end
142 changes: 142 additions & 0 deletions demo/desktop/softBodyTetris/meshes.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
meshes =
{
-- L
{
{-1.80769, -3.30769, 0.5},
{-0.807692, -3.30769, 0.5},
{0.192308, -3.30769, 0.5},
{0.192308, -1.30769, 0.5},
{-1.80769, -1.30769, 0.5},
{-1.80769, -2.30769, 0.5},
{-1.80769, -0.307692, 0.5},
{-1.80769, 0.692308, 0.5},
{-1.80769, 1.69231, 0.5},
{0.192308, -0.307692, 0.5},
{0.192308, 0.692308, 0.5},
{0.192308, 1.69231, 0.5},
{-1.80769, 2.69231, 0.5},
{-1.80769, 3.69231, 0.5},
{-1.80769, 4.69231, 0.5},
{-0.807692, 4.69231, 0.5},
{0.192308, 4.69231, 0.5},
{0.192308, 3.69231, 0.5},
{0.192308, 2.69231, 0.5},
{1.19231, -1.30769, 0.5},
{2.19231, -1.30769, 0.5},
{3.19231, -1.30769, 0.5},
{3.19231, -2.30769, 0.5},
{3.19231, -3.30769, 0.5},
{2.19231, -3.30769, 0.5},
{1.19231, -3.30769, 0.5}
},
-- rod
{
{-5.5, 1, 0.5},
{-5.5, 0, 0.5},
{-5.5, -1, 0.5},
{-4.5, -1, 0.5},
{-3.5, 1, 0.5},
{-4.5, 1, 0.5},
{-2.5, 1, 0.5},
{-1.5, 1, 0.5},
{-0.5, 1, 0.5},
{-0.5, -1, 0.5},
{-1.5, -1, 0.5},
{-3.5, -1, 0.5},
{-2.5, -1, 0.5},
{0.5, -1, 0.5},
{0.5, 1, 0.5},
{1.5, 1, 0.5},
{2.5, 1, 0.5},
{2.5, -1, 0.5},
{1.5, -1, 0.5},
{3.5, -1, 0.5},
{3.5, 1, 0.5},
{4.5, 1, 0.5},
{5.5, 1, 0.5},
{5.5, 0, 0.5},
{5.5, -1, 0.5},
{4.5, -1, 0.5}
},
-- square
{
{-2.5, -2.5, 0.5},
{-2.5, -1.5, 0.5},
{-2.5, -0.5, 0.5},
{-2.5, 0.5, 0.5},
{-2.5, 1.5, 0.5},
{-2.5, 2.5, 0.5},
{-1.5, 2.5, 0.5},
{-0.5, 2.5, 0.5},
{0.5, 2.5, 0.5},
{1.5, 2.5, 0.5},
{2.5, 2.5, 0.5},
{2.5, -2.5, 0.5},
{1.5, -2.5, 0.5},
{0.5, -2.5, 0.5},
{-0.5, -2.5, 0.5},
{-1.5, -2.5, 0.5},
{2.5, 1.5, 0.5},
{2.5, 0.5, 0.5},
{2.5, -0.5, 0.5},
{2.5, -1.5, 0.5}
},
-- T
{
{-1, -3.19231, 0.5},
{0, -3.19231, 0.5},
{1, -3.19231, 0.5},
{1, -2.19231, 0.5},
{1, -1.19231, 0.5},
{-1, -1.19231, 0.5},
{-1, -2.19231, 0.5},
{-1, -0.192308, 0.5},
{-1, 1.80769, 0.5},
{0, 1.80769, 0.5},
{1, 1.80769, 0.5},
{1, -0.192308, 0.5},
{-2, -0.192308, 0.5},
{-2, 1.80769, 0.5},
{-3, 1.80769, 0.5},
{-4, 1.80769, 0.5},
{-4, 0.807692, 0.5},
{-4, -0.192308, 0.5},
{-3, -0.192308, 0.5},
{2, -0.192308, 0.5},
{3, -0.192308, 0.5},
{4, -0.192308, 0.5},
{4, 0.807692, 0.5},
{4, 1.80769, 0.5},
{3, 1.80769, 0.5},
{2, 1.80769, 0.5}
},
-- zig
{
{0.5, -4, 0.5},
{1.5, -4, 0.5},
{2.5, -4, 0.5},
{2.5, -3, 0.5},
{2.5, -2, 0.5},
{0.5, -2, 0.5},
{0.5, -3, 0.5},
{0.5, -1, 0.5},
{2.5, -1, 0.5},
{2.5, 0, 0.5},
{2.5, 1, 0.5},
{1.5, 1, 0.5},
{0.5, 1, 0.5},
{-0.5, -1, 0.5},
{-1.5, -1, 0.5},
{-2.5, -1, 0.5},
{-2.5, 0, 0.5},
{-2.5, 1, 0.5},
{-0.5, 1, 0.5},
{-2.5, 2, 0.5},
{-2.5, 3, 0.5},
{-2.5, 4, 0.5},
{-1.5, 4, 0.5},
{-0.5, 4, 0.5},
{-0.5, 3, 0.5},
{-0.5, 2, 0.5}
}
}
22 changes: 19 additions & 3 deletions demo/desktop/softBodyTetris/standalone/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,28 @@ int main(int argc, char ** argv)

console.luaStore(&luaStore);
console.runFile("config.lua");
console.runFile("tetris.lua");
std::string status = console.luaStatus();
if (status != "LUA_OK") { WARN(status) >> log; }

high_resolution_clock::time_point t0, t1, tp0, tp1, tr0, tr1;

physics.stabaliseObjectParameters(&manager);

Hop::Debugging::CollisionMeshDebug collisionMeshDebug;

bool refreshObjectShaders = true;

while (display.isOpen())
{

if (display.getEvent(GLFW_KEY_SPACE).type == Hop::Display::EventType::PRESS) { paused = !paused; }

if (!paused)
{
console.runFile("loop.lua");
rendering.refreshShaders();
}

t0 = high_resolution_clock::now();

world->updateRegion(posX,posY);
Expand All @@ -97,7 +106,10 @@ int main(int argc, char ** argv)

collisions.centreOn(world.get()->getMapCenter());

physics.step(&manager, &collisions, world.get());
if (!paused)
{
physics.step(&manager, &collisions, world.get());
}

tp1 = high_resolution_clock::now();

Expand All @@ -108,6 +120,8 @@ int main(int argc, char ** argv)
rendering.draw(&manager, world.get());
refreshObjectShaders = false;

collisionMeshDebug.debugCollisionMesh(&manager, camera.getVP());

tr1 = high_resolution_clock::now();

if (debug)
Expand Down Expand Up @@ -146,7 +160,9 @@ int main(int argc, char ** argv)
"\n" <<
"update time: " << fixedLengthNumber(pdt+rdt,6) <<
"\n" <<
"Phys update / draw time: " << fixedLengthNumber(pdt,6) << "/" << fixedLengthNumber(rdt,6);
"Phys update / draw time: " << fixedLengthNumber(pdt,6) << "/" << fixedLengthNumber(rdt,6) <<
"\n" <<
"Kinetic Energy: " << fixedLengthNumber(physics.kineticEnergy(),6);

textRenderer.renderText(
font,
Expand Down
5 changes: 4 additions & 1 deletion demo/desktop/softBodyTetris/standalone/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ using namespace std::chrono;

#include <Console/console.h>

#include <Debug/collisionMeshDebug.h>

#include <Util/util.h>
#include <log.h>

Expand All @@ -44,7 +46,8 @@ const float MAX_SPEED = 1.0/60.0;
uint8_t frameId = 0;
double deltas[60];

bool debug = false;
bool debug = true;
bool paused = true;

const double deltaPhysics = 1.0/900.0;

Expand Down
39 changes: 0 additions & 39 deletions demo/desktop/softBodyTetris/tetris.lua

This file was deleted.

Loading

0 comments on commit 1654684

Please sign in to comment.