Skip to content

Commit

Permalink
move sPhysics and sCollision into Physics folder, add lua_applyForce …
Browse files Browse the repository at this point in the history
…to console
  • Loading branch information
Jerboa-app committed Dec 20, 2023
1 parent 44767eb commit 82c4eac
Show file tree
Hide file tree
Showing 16 changed files with 112 additions and 34 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ file(GLOB SRC
"src/Console/*.cpp"
"src/System/Rendering/*.cpp"
"src/System/Sound/*.cpp"
"src/System/Physics/*.cpp"
)

if (NOT ANDROID)
Expand Down
4 changes: 2 additions & 2 deletions demo/android/perlinWorld/app/src/main/cpp/hopAndroid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

#include <Object/entityComponentSystem.h>

#include <System/sPhysics.h>
#include <System/Physics/sPhysics.h>
#include <System/Rendering/sRender.h>
#include <System/sCollision.h>
#include <System/Physics/sCollision.h>

#include <World/world.h>
#include <World/marchingWorld.h>
Expand Down
4 changes: 2 additions & 2 deletions demo/desktop/perlinWorld/sfml/sfmlPerlinWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ using namespace std::chrono;

#include <Object/entityComponentSystem.h>

#include <System/sPhysics.h>
#include <System/Physics/sPhysics.h>
#include <System/Rendering/sRender.h>
#include <System/sCollision.h>
#include <System/Physics/sCollision.h>

#include <World/world.h>
#include <World/marchingWorld.h>
Expand Down
4 changes: 2 additions & 2 deletions demo/desktop/perlinWorld/standalone/perlinWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ using namespace std::chrono;

#include <Object/entityComponentSystem.h>

#include <System/sPhysics.h>
#include <System/Physics/sPhysics.h>
#include <System/Rendering/sRender.h>
#include <System/sCollision.h>
#include <System/Physics/sCollision.h>

#include <World/world.h>
#include <World/marchingWorld.h>
Expand Down
4 changes: 2 additions & 2 deletions demo/desktop/softBodyTetris/sfml/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ using namespace std::chrono;

#include <Object/entityComponentSystem.h>

#include <System/sPhysics.h>
#include <System/Physics/sPhysics.h>
#include <System/Rendering/sRender.h>
#include <System/sCollision.h>
#include <System/Physics/sCollision.h>

#include <World/world.h>
#include <World/marchingWorld.h>
Expand Down
4 changes: 2 additions & 2 deletions demo/desktop/softBodyTetris/standalone/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ using namespace std::chrono;

#include <Object/entityComponentSystem.h>

#include <System/sPhysics.h>
#include <System/Physics/sPhysics.h>
#include <System/Rendering/sRender.h>
#include <System/sCollision.h>
#include <System/Physics/sCollision.h>
#include <System/Sound/sSound.h>

#include <World/world.h>
Expand Down
13 changes: 11 additions & 2 deletions include/Console/console.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
#include <Console/LuaNumber.h>
#include <Console/LuaString.h>
#include <Console/LuaTable.h>
#include <System/sPhysics.h>
#include <System/sCollision.h>
#include <System/Physics/sPhysics.h>
#include <System/Physics/sCollision.h>
#include <log.h>
#include <Object/id.h>

#include <memory>
#include <vector>
Expand Down Expand Up @@ -118,21 +119,29 @@ namespace Hop

int timeMillis(lua_State * lua);

int lua_applyForce(lua_State * lua);

// register lib

const luaL_Reg hopLib[] =
{
{"loadObject", &dispatchEntityComponentSystem<&EntityComponentSystem::lua_loadObject>},
{"getTransform", &dispatchEntityComponentSystem<&EntityComponentSystem::lua_getTransform>},
{"setTransform", &dispatchEntityComponentSystem<&EntityComponentSystem::lua_setTransform>},
///////////////////////////////////////////////////////////////////////////////////////////
{"maxCollisionPrimitiveSize",&dispatchWorld<&AbstractWorld::lua_worldMaxCollisionPrimitiveSize>},
///////////////////////////////////////////////////////////////////////////////////////////
{"setPhysicsTimeStep",&dispatchsPhysics<&sPhysics::lua_setTimeStep>},
{"setPhysicsSubSamples",&dispatchsPhysics<&sPhysics::lua_setSubSamples>},
{"kineticEnergy", &dispatchsPhysics<&sPhysics::lua_kineticEnergy>},
{"setGravity", &dispatchsPhysics<&sPhysics::lua_setGravity>},
///////////////////////////////////////////////////////////////////
{"setCoefRestitution",&dispatchsCollision<&sCollision::lua_setCOR>},
////////////////////////////////////////////////////////////////////
{"setLoopRoutines",&dispatchLoopRoutines<&LoopRoutines::lua_setRoutines>},
{"configure", &configure},
{"timeMillis", &timeMillis},
{"applyForce", &lua_applyForce},
{NULL, NULL}
};

Expand Down
4 changes: 2 additions & 2 deletions include/Object/entityComponentSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

#include <System/Rendering/sRender.h>
#include <System/Rendering/sSpriteRender.h>
#include <System/sPhysics.h>
#include <System/sCollision.h>
#include <System/Physics/sPhysics.h>
#include <System/Physics/sCollision.h>
#include <System/Sound/sSound.h>

#include <unordered_map>
Expand Down
File renamed without changes.
13 changes: 10 additions & 3 deletions include/System/sPhysics.h → include/System/Physics/sPhysics.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
#include <Maths/special.h>
#include <Thread/threadPool.h>
#include <Component/componentArray.h>
#include <System/sCollision.h>
#include <System/Physics/sCollision.h>
#include <World/world.h>
#include <Console/lua.h>

namespace Hop::Object
{
Expand Down Expand Up @@ -68,13 +69,15 @@ namespace Hop::System::Physics
EntityComponentSystem * m,
Id & i,
double fx,
double fy
double fy,
bool global = false
);

void applyForce(
EntityComponentSystem * m,
double fx,
double fy
double fy,
bool global = false
);

// automatically compute stable simulation parameters
Expand Down Expand Up @@ -140,6 +143,10 @@ namespace Hop::System::Physics
void setSubSamples(unsigned s){subSamples = s;}
void setGravity(double g, double nx, double ny){gravity = g; ngx = nx; ngy = ny;}

// Lua

int lua_setGravity(lua_State * lua);

private:

void update(EntityComponentSystem * m, ThreadPool * workers = nullptr);
Expand Down
5 changes: 0 additions & 5 deletions include/System/sPlayer.h

This file was deleted.

31 changes: 31 additions & 0 deletions src/Console/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,35 @@ namespace Hop

}

int lua_applyForce(lua_State * lua)
{
LuaNumber fx, fy;
LuaString sid;

int n = lua_gettop(lua);

if (n != 3)
{
lua_pushliteral(lua,"expected id and force vector, fx, fy as argument");
return lua_error(lua);
}

sid.read(lua, 1);
Hop::Object::Id id(sid.characters);

fx.read(lua, 2);
fy.read(lua, 3);

LuaExtraSpace * store = *static_cast<LuaExtraSpace**>(lua_getextraspace(lua));

if (!store->ecs->hasComponent<cPhysics>(id))
{
lua_pushliteral(lua, "object has no physics component");
return lua_error(lua);
}

store->physics->applyForce(store->ecs, fx, fy, true);

return 0;
}
}
31 changes: 31 additions & 0 deletions src/System/Physics/LuaBindings/lua_physics.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <System/Physics/sPhysics.h>
#include <Console/LuaNumber.h>
#include <Console/LuaString.h>

namespace Hop::System::Physics
{
int sPhysics::lua_setGravity(lua_State * lua)
{

LuaNumber gx, gy;

int n = lua_gettop(lua);

if (n != 2)
{
lua_pushliteral(lua,"expected gravity vector, gx, gy as argument");
return lua_error(lua);
}

gx.read(lua, 1);
gy.read(lua, 2);

double norm = std::sqrt(gx*gx+gy*gy);

setGravity(norm, double(gx)/norm, double(gy)/norm);

return 0;

}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <System/sCollision.h>
#include <System/Physics/sCollision.h>

namespace Hop::System::Physics
{
Expand Down
16 changes: 10 additions & 6 deletions src/System/sPhysics.cpp → src/System/Physics/sPhysics.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <System/sPhysics.h>
#include <System/Physics/sPhysics.h>

#include <chrono>
using namespace std::chrono;
Expand Down Expand Up @@ -178,7 +178,8 @@ namespace Hop::System::Physics
EntityComponentSystem * m,
Id & i,
double fx,
double fy
double fy,
bool global
)
{

Expand All @@ -190,7 +191,7 @@ namespace Hop::System::Physics
cCollideable & data = collideables.get(i);
if (!data.mesh.isRigid())
{
data.mesh.applyForce(fx, fy);
data.mesh.applyForce(fx, fy, global);
}
else
{
Expand All @@ -209,7 +210,8 @@ namespace Hop::System::Physics
void sPhysics::applyForce(
EntityComponentSystem * m,
double fx,
double fy
double fy,
bool global
)
{

Expand All @@ -223,7 +225,7 @@ namespace Hop::System::Physics
cCollideable & data = collideables.get(*it);
if (!data.mesh.isRigid())
{
data.mesh.applyForce(fx, fy);
data.mesh.applyForce(fx, fy, global);
continue;
}
}
Expand Down Expand Up @@ -288,4 +290,6 @@ namespace Hop::System::Physics
return (dt*1.05)*gravity/radius;
}

}
}

#include <System/Physics/LuaBindings/lua_physics.cpp>
10 changes: 5 additions & 5 deletions tools/meshEditor/main.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef MAIN
#define MAIN
#ifndef MAIN_H
#define MAIN_H

#include <iostream>
#include <sstream>
Expand All @@ -23,9 +23,9 @@ using namespace std::chrono;

#include <Object/entityComponentSystem.h>

#include <System/sPhysics.h>
#include <System/Physics/sPhysics.h>
#include <System/Rendering/sRender.h>
#include <System/sCollision.h>
#include <System/Physics/sCollision.h>

#include <World/world.h>
#include <World/marchingWorld.h>
Expand Down Expand Up @@ -88,4 +88,4 @@ using Hop::Logging::WARN;

using Hop::Util::fixedLengthNumber;

#endif /* MAIN */
#endif /* MAIN_H */

0 comments on commit 82c4eac

Please sign in to comment.