Skip to content

Commit

Permalink
Merge pull request #161 from JerboaBurrow/add_object_io
Browse files Browse the repository at this point in the history
adds colour io, object deletion, ghosts, set hard bounds on all four …
  • Loading branch information
Jerboa-app authored Jan 31, 2024
2 parents a0379e7 + 2ab8942 commit bfde90f
Show file tree
Hide file tree
Showing 14 changed files with 199 additions and 41 deletions.
12 changes: 10 additions & 2 deletions demo/desktop/softBodyTetris/loop.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
s = (1.0-hop.maxCollisionPrimitiveSize()*4.0)/(3*9)

if objects == nil then
objects = {}
end

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

Expand Down Expand Up @@ -27,13 +31,16 @@ if lastTime == nil then

}

hop.loadObject(o)
id = hop.loadObject(o)
table.insert(objects, id)
end

time = hop.timeMillis()

if time-lastTime > 1000*5 then

hop.setColour(objects[1], 1.0,0.0,0.0,1.0)

if (hop.kineticEnergy() > 0.1) then
lastTime = hop.timeMillis()
else
Expand Down Expand Up @@ -61,7 +68,8 @@ if time-lastTime > 1000*5 then

}

hop.loadObject(o)
id = hop.loadObject(o)
table.insert(objects, id)
lastTime = hop.timeMillis()
end
end
4 changes: 3 additions & 1 deletion demo/desktop/softBodyTetris/standalone/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ int main(int argc, char ** argv)

jGL::DesktopDisplay display(glm::ivec2(resX,resY),"Soft Body Tetris", conf);

display.setFrameLimit(60);

glewInit();

jGLInstance = std::move(std::make_shared<jGL::GL::OpenGLInstance>(display.getRes()));
Expand All @@ -30,7 +32,7 @@ int main(int argc, char ** argv)
float posX = 0.0;
float posY = 0.0;

Hop::World::FiniteBoundary mapBounds(0,0,16,16,true);
Hop::World::FiniteBoundary mapBounds(0,0,16,16,true,false,true,true);
Hop::World::FixedSource mapSource;

world = std::make_unique<TileWorld>
Expand Down
4 changes: 3 additions & 1 deletion include/Component/cPhysics.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ namespace Hop::Object::Component
double friction;

bool isMoveable;
bool isGhost;

cPhysics(double x, double y, double t)
: x(x), y(y), lastX(x), lastY(y), lastTheta(t),
Expand All @@ -60,7 +61,8 @@ namespace Hop::Object::Component
fx(0.0),fy(0.0), omega(0.0), tau(0.0),
translationalDrag(td), rotationalDrag(rd),
friction(f),
isMoveable(true)
isMoveable(true),
isGhost(false)
{}

cPhysics() = default;
Expand Down
1 change: 1 addition & 0 deletions include/Component/componentArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ namespace Hop::Object::Component

virtual ~AbstractComponentArray() = default;
virtual void objectFreed(Id i) = 0;
virtual void remove(Id & i) = 0;

};

Expand Down
5 changes: 4 additions & 1 deletion include/Console/console.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,14 +256,17 @@ namespace Hop

static int load_hopLib(lua_State * lua)
{
luaL_Reg hopLib[18] =
luaL_Reg hopLib[21] =
{
{"loadObject", &dispatchEntityComponentSystem<&EntityComponentSystem::lua_loadObject>},
{"deleteObject", &dispatchEntityComponentSystem<&EntityComponentSystem::lua_deleteObject>},
{"getTransform", &dispatchEntityComponentSystem<&EntityComponentSystem::lua_getTransform>},
{"setTransform", &dispatchEntityComponentSystem<&EntityComponentSystem::lua_setTransform>},
{"removeFromMeshByTag", &dispatchEntityComponentSystem<&EntityComponentSystem::lua_removeFromMeshByTag>},
{"meshBoundingBox", &dispatchEntityComponentSystem<&EntityComponentSystem::lua_meshBoundingBox>},
{"meshBoundingBoxByTag", &dispatchEntityComponentSystem<&EntityComponentSystem::lua_meshBoundingBoxByTag>},
{"getColour", &dispatchEntityComponentSystem<&EntityComponentSystem::lua_getColour>},
{"setColour", &dispatchEntityComponentSystem<&EntityComponentSystem::lua_setColour>},
///////////////////////////////////////////////////////////////////////////////////////////
{"maxCollisionPrimitiveSize",&dispatchWorld<&AbstractWorld::lua_worldMaxCollisionPrimitiveSize>},
///////////////////////////////////////////////////////////////////////////////////////////
Expand Down
4 changes: 4 additions & 0 deletions include/Object/entityComponentSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ namespace Hop::Object
// Lua bindings

int lua_loadObject(lua_State * lua);
int lua_deleteObject(lua_State * lua);

int lua_getTransform(lua_State * lua);
int lua_setTransform(lua_State * lua);
Expand All @@ -251,6 +252,9 @@ namespace Hop::Object
int lua_meshBoundingBox(lua_State * lua);
int lua_meshBoundingBoxByTag(lua_State * lua);

int lua_getColour(lua_State * lua);
int lua_setColour(lua_State * lua);


private:

Expand Down
18 changes: 15 additions & 3 deletions include/World/boundary.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ namespace Hop::World
int my,
int Mx,
int My,
bool hard = false,
bool hardBottom = false,
bool hardTop = false,
bool hardLeft = false,
bool hardRight = false,
int px = 0,
int py = 0
)
Expand All @@ -44,9 +47,13 @@ namespace Hop::World
maxX(Mx),
maxY(My),
periodicX(px),
periodicY(py)
periodicY(py),
hardTop(hardTop),
hardBottom(hardBottom),
hardLeft(hardLeft),
hardRight(hardRight)
{
hardOutOfBounds = hard;
hardOutOfBounds = hardBottom || hardTop || hardLeft || hardRight;
}


Expand All @@ -65,6 +72,10 @@ namespace Hop::World
}

const bool isHard() const { return hardOutOfBounds; }
const bool isHardTop() const { return hardTop; }
const bool isHardBottom() const { return hardBottom; }
const bool isHardLeft() const { return hardLeft; }
const bool isHardRight() const { return hardRight; }

double getMinX() const { return minX; }
double getMinY() const { return minY; }
Expand All @@ -75,6 +86,7 @@ namespace Hop::World

int minX, minY, maxX, maxY;
bool periodicX, periodicY;
bool hardTop, hardBottom, hardLeft, hardRight;

};

Expand Down
2 changes: 1 addition & 1 deletion include/jGL
13 changes: 13 additions & 0 deletions src/Collision/cellList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ namespace Hop::System::Physics
for (auto it = objects.begin(); it != objects.cend(); it++)
{

cPhysics & pData = dataP.get(*it);

if (pData.isGhost)
{
continue;
}

cCollideable & data = dataC.get(*it);

uint64_t meshSize = data.mesh.size();
Expand Down Expand Up @@ -553,6 +560,12 @@ namespace Hop::System::Physics
{
cCollideable & c = dataC.get(*it);
cPhysics & p = dataP.get(*it);

if (p.isGhost)
{
continue;
}

resolver->handleObjectWorldCollision(
*it,
c,
Expand Down
41 changes: 22 additions & 19 deletions src/Collision/collisionMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ namespace Hop::System::Physics
}
}

if (!needsInit)
if (!needsInit && physics.isMoveable)
{
// should be vectorisable, split with prior and next loop
// to make branchless. Indeed we got down to O(1e-7) from O(1e-6)
Expand Down Expand Up @@ -236,29 +236,32 @@ namespace Hop::System::Physics

centerOfMassWorld(transform.x, transform.y);

double dx = 0.0;
double dy = 0.0;
for (unsigned i = 0; i < vertices.size(); i++)
if (physics.isMoveable)
{
worldVertices[i]->stepGlobal
(
dt, dtdt, physics, gx, gy, dx, dy
);
}

gx = 0.0;
gy = 0.0;
double dx = 0.0;
double dy = 0.0;

for (unsigned i = 0; i < vertices.size(); i++)
{
worldVertices[i]->stepGlobal
(
dt, dtdt, physics, gx, gy, dx, dy
);
}

transform.x += dx;
transform.y += dy;
gx = 0.0;
gy = 0.0;

physics.vx += transform.x;
physics.vy += transform.y;
transform.x += dx;
transform.y += dy;

physics.vx /= dt;
physics.vy /= dt;
physics.vx += transform.x;
physics.vy += transform.y;

kineticEnergy += (dx*dx+dy*dy)/(dt*dt);
physics.vx /= dt;
physics.vy /= dt;
kineticEnergy += (dx*dx+dy*dy)/(dt*dt);
}

for (auto p : worldVertices)
{
Expand Down
8 changes: 4 additions & 4 deletions src/Collision/springDashotResolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2248,7 +2248,7 @@ namespace Hop::System::Physics
double fy = 0.0;
double r2 = c->r*c->r;

if(c->x - bounds.getMinX() < c->r)
if(bounds.isHardLeft() && c->x - bounds.getMinX() < c->r)
{
double d2 = pointLineSegmentDistanceSquared<double>
(
Expand All @@ -2264,7 +2264,7 @@ namespace Hop::System::Physics
}
}

if(c->x - bounds.getMaxX() < c->r)
if(bounds.isHardRight() && c->x - bounds.getMaxX() < c->r)
{
double d2 = pointLineSegmentDistanceSquared<double>
(
Expand All @@ -2280,7 +2280,7 @@ namespace Hop::System::Physics
}
}

if(c->y - bounds.getMinY() < c->r)
if(bounds.isHardBottom() && c->y - bounds.getMinY() < c->r)
{
double d2 = pointLineSegmentDistanceSquared<double>
(
Expand All @@ -2296,7 +2296,7 @@ namespace Hop::System::Physics
}
}

if(c->y - bounds.getMaxY() < c->r)
if(bounds.isHardTop() && c->y - bounds.getMaxY() < c->r)
{
double d2 = pointLineSegmentDistanceSquared<double>
(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,27 @@ namespace Hop::Object
using Hop::System::Physics::CollisionPrimitive;
using Hop::System::Physics::RectanglePrimitive;

int EntityComponentSystem::lua_deleteObject(lua_State * lua)
{
LuaString sid;

int n = lua_gettop(lua);

if (n != 1)
{
lua_pushliteral(lua,"expected id as argument");
return lua_error(lua);
}

sid.read(lua, 1);

Id id(sid.characters);

remove(id);

return 0;
}

int EntityComponentSystem::lua_loadObject(lua_State * lua)
{
LuaArray<4> colour, transform, util;
Expand All @@ -47,7 +68,7 @@ namespace Hop::Object

LuaString shader, name;

LuaBool isMoveable;
LuaBool isMoveable, isGhost;

LuaTable<LuaVec> collisionMesh;

Expand All @@ -60,6 +81,7 @@ namespace Hop::Object

meshParameters.elements = {CollisionPrimitive::RIGID, 1.0, 1.0};
isMoveable.bit = true;
isGhost.bit = false;

transDrag.n = DEFAULT_TRANSLATIONAL_DRAG;
rotDrag.n = DEFAULT_ROTATIONAL_DRAG;
Expand Down Expand Up @@ -89,6 +111,7 @@ namespace Hop::Object
shader.read(lua, "shader");

isMoveable.read(lua, "moveable");
isGhost.read(lua, "ghost");

isPhysics = collisionMesh.read(lua, "collisionMesh");

Expand Down Expand Up @@ -190,11 +213,10 @@ namespace Hop::Object
cPhysics(x,y,theta, transDrag.n, rotDrag.n, bodyInertia.n, bodyMass.n, bodyFriction.n)
);

if (!isMoveable.bit)
{
cPhysics & data = getComponent<cPhysics>(pid);
data.isMoveable = false;
}
cPhysics & data = getComponent<cPhysics>(pid);
data.isMoveable = isMoveable.bit;
data.isGhost = isGhost.bit;


if (collisionMesh.size() > 0)
{
Expand Down
Loading

0 comments on commit bfde90f

Please sign in to comment.