diff --git a/demo/desktop/perlinWorld/config.lua b/demo/desktop/perlinWorld/config.lua index b76733c1..608de2c0 100644 --- a/demo/desktop/perlinWorld/config.lua +++ b/demo/desktop/perlinWorld/config.lua @@ -2,7 +2,8 @@ config = { ["timeStep"] = 1.0/1800.0, ["subSample"] = 5, - ["cofr"] = 0.9 + ["cofr"] = 0.1 } -hop.configure(config); \ No newline at end of file +hop.configure(config); +dofile("mix.lua") \ No newline at end of file diff --git a/demo/desktop/perlinWorld/main.cpp b/demo/desktop/perlinWorld/main.cpp index fd05acf5..a6bfb725 100644 --- a/demo/desktop/perlinWorld/main.cpp +++ b/demo/desktop/perlinWorld/main.cpp @@ -99,7 +99,6 @@ int main(int argc, char ** argv) console.luaStore(&luaStore); - console.runFile("mix.lua"); console.runFile("config.lua"); std::string status = console.luaStatus(); if (status != "LUA_OK") { WARN(status) >> log; } @@ -129,7 +128,7 @@ int main(int argc, char ** argv) moving[i] = false; } } - + if (moving[0]) { posY += MAX_SPEED / camera.getZoomLevel(); } if (moving[1]) { posY -= MAX_SPEED / camera.getZoomLevel(); } if (moving[2]) { posX -= MAX_SPEED / camera.getZoomLevel(); } diff --git a/demo/desktop/perlinWorld/mix.lua b/demo/desktop/perlinWorld/mix.lua index 71a05f21..fb69918b 100644 --- a/demo/desktop/perlinWorld/mix.lua +++ b/demo/desktop/perlinWorld/mix.lua @@ -1,4 +1,4 @@ -s = 3.0*hop.maxCollisionPrimitiveSize() +s = 1.9*hop.maxCollisionPrimitiveSize() math.randomseed(os.time()) @@ -9,9 +9,9 @@ xw = 1.0 x = xs; y = ys; -for i = 1,100 do +for i = 1,128 do - selection = math.random(6) + selection = math.random(7) theta = 2.0*3.14159 * math.random(10000) / 10000.0; @@ -145,6 +145,18 @@ for i = 1,100 do } + elseif selection == 7 then + object = { + ["transform"] = {x,y,0.0,s}, + ["colour"] = {200/255,200/255,250/255,1.0}, + --["shader"] = "lineSegmentObjectShader", + ["moveable"] = true, + ["collisionMesh"] = + { + {-0.5,-0.5,-0.5,0.5,0.5,0.5,0.5,-0.5} -- model space + }, + ["name"] = "object" + } end diff --git a/include/Collision/collisionMesh.h b/include/Collision/collisionMesh.h index 9280acc0..90e50110 100644 --- a/include/Collision/collisionMesh.h +++ b/include/Collision/collisionMesh.h @@ -83,6 +83,22 @@ namespace Hop::System::Physics lry == rhs.lry; } + double height() + { + double u = llx-lrx; + double v = lly-lry; + + return std::sqrt(u*u+v*v); + } + + double width() + { + double u = ulx-llx; + double v = uly-lly; + + return std::sqrt(u*u+v*v); + } + double llx, lly, ulx, uly, urx, ury, lrx, lry; }; diff --git a/include/Collision/collisionPrimitive.h b/include/Collision/collisionPrimitive.h index 753036e5..bb4bd4c2 100644 --- a/include/Collision/collisionPrimitive.h +++ b/include/Collision/collisionPrimitive.h @@ -18,6 +18,10 @@ namespace Hop::System::Physics const uint64_t LAST_INSIDE_COUNTER_MAX = 60; + /** + * @brief A primitive shape (circle) for collisions. + * + */ struct CollisionPrimitive { @@ -25,6 +29,17 @@ namespace Hop::System::Physics CollisionPrimitive() = default; + /** + * @brief Construct a new Collision Primitive (circle) + * + * @param x centre x coordinate + * @param y centre y coordinate + * @param r radius + * @param t tag (for sub meshing) + * @param k stiffness (default is rigid) + * @param d damping (only for soft meshes) + * @param m mass (only for soft meshes) + */ CollisionPrimitive ( double x, @@ -88,6 +103,13 @@ namespace Hop::System::Physics yp = oy; } + /** + * @brief Applied a force generating the require torque. + * + * @param omega torque. + * @param cx reference point x. + * @param cy reference point y. + */ void applyTorque ( double omega, @@ -127,6 +149,15 @@ namespace Hop::System::Physics ); } + /** + * @brief Apply internal (spring/drag) forces to this primitive in a soft meshes. + * + * @param dt timestep. + * @param dtdt timestep^2. + * @param translationalDrag translational drag coefficient. + * @param nox new x coordinate. + * @param noy new y coordinate. + */ void step ( double dt, @@ -171,6 +202,17 @@ namespace Hop::System::Physics } + /** + * @brief Apply external forces to this mesh point in a soft mesh. + * + * @param dt timestep. + * @param dtdt timestep^2. + * @param physics physics component. + * @param gx global force in x. + * @param gy global force in y. + * @param dx resultant change in x. + * @param dy resultant change in y. + */ void stepGlobal ( double dt, @@ -204,12 +246,30 @@ namespace Hop::System::Physics }; + /** + * @brief A rectangular primitive collision shape. + * + */ struct RectanglePrimitive : public CollisionPrimitive { RectanglePrimitive() : CollisionPrimitive(0.0,0.0,0.0,0,CollisionPrimitive::RIGID,0.0,0.0) {} + /** + * @brief Construct a new Rectangle Primitive from vertices. + * + * @param llx lower left x. + * @param lly lower left y. + * @param ulx upper left x. + * @param uly upper left y. + * @param urx upper right x. + * @param ury upper right y. + * @param lrx lower right x. + * @param lry lower right y. + * @param t tag (for sub meshing) + * @param k stiffness (default is rigid). + */ RectanglePrimitive ( double llx, double lly, @@ -256,6 +316,11 @@ namespace Hop::System::Physics royp=0.0; } + /** + * @brief Convert to a Rectangle. + * + * @return Hop::Maths::Rectangle + */ Hop::Maths::Rectangle getRect() { return Hop::Maths::Rectangle @@ -269,6 +334,10 @@ namespace Hop::System::Physics ); } + /** + * @brief Recalculate axes (vector for length and width). + * + */ void resetAxes() { axis1x = llx-lrx; @@ -316,6 +385,12 @@ namespace Hop::System::Physics } + /** + * @brief Rotate clockwise from precomputed cosine and sine. + * + * @param cosine cos(\theta) + * @param sine sin(\theta) + */ void rotateClockWise(double cosine, double sine) { Hop::Maths::rotateClockWise(llx, lly, cosine, sine); @@ -348,22 +423,22 @@ namespace Hop::System::Physics r *= s; } - void translate(double x, double y) + void translate(double dx, double dy) { - llx += x; - lly += y; + llx += dx; + lly += dy; - ulx += x; - uly += y; + ulx += dx; + uly += dy; - urx += x; - ury += y; + urx += dx; + ury += dy; - lrx += x; - lry += y; + lrx += dx; + lry += dy; - x += x; - y += y; + x += dx; + y += dy; } double llx, lly; diff --git a/include/Debug/collisionMeshDebug.h b/include/Debug/collisionMeshDebug.h index 18c4245a..5a0a5663 100644 --- a/include/Debug/collisionMeshDebug.h +++ b/include/Debug/collisionMeshDebug.h @@ -23,6 +23,7 @@ namespace Hop::Debugging using Hop::Object::Component::cCollideable; using Hop::System::Physics::CollisionPrimitive; using Hop::System::Physics::MeshPoint; + using Hop::System::Physics::MeshRectangle; using Hop::Object::Component::cRenderable; using Hop::Object::Component::cTransform; @@ -33,7 +34,8 @@ namespace Hop::Debugging CollisionMeshDebug(std::shared_ptr jgl) : refresh(true), - shapes(jgl->createShapeRenderer(256)), + circles(jgl->createShapeRenderer(256)), + rectangles(jgl->createShapeRenderer(256)), circleShader(std::make_shared ( jGL::GL::glShapeRenderer::shapeVertexShader, @@ -60,11 +62,11 @@ namespace Hop::Debugging bool refresh; - std::shared_ptr shapes; + std::shared_ptr circles, rectangles; std::shared_ptr circleShader, rectangleShader; - std::vector circlePos; + std::vector circlePos, rectanglePos; }; diff --git a/src/Collision/collisionMesh.cpp b/src/Collision/collisionMesh.cpp index 9f237299..6cdf3b15 100644 --- a/src/Collision/collisionMesh.cpp +++ b/src/Collision/collisionMesh.cpp @@ -54,9 +54,8 @@ namespace Hop::System::Physics lw->r = lv->r; lw->rotateClockWise(c, s); - lw->scale(std::max(transform.scaleX, transform.scaleY)*2.0); + lw->scale(std::max(transform.scaleX, transform.scaleY)); lw->translate(transform.x, transform.y); - } } } diff --git a/src/Debug/collisionMeshDebug.cpp b/src/Debug/collisionMeshDebug.cpp index 5b915963..7849ca6d 100644 --- a/src/Debug/collisionMeshDebug.cpp +++ b/src/Debug/collisionMeshDebug.cpp @@ -24,12 +24,16 @@ namespace Hop::Debugging double theta, scale; - shapes->setProjection(proj); + circles->setProjection(proj); + rectangles->setProjection(proj); citer = objects.cbegin(); - shapes->clear(); + circles->clear(); + rectangles->clear(); circlePos.clear(); circlePos.reserve(s); + rectanglePos.clear(); + rectanglePos.reserve(s); while (citer != cend) { uint64_t p = 0; @@ -47,34 +51,43 @@ namespace Hop::Debugging { CollisionPrimitive * cp = (c.mesh[i].get()); MeshPoint * cpmodel = c.mesh.getModelVertex(i).get(); - //Rectangle * r = dynamic_cast(cp); + MeshRectangle * r = dynamic_cast(cpmodel); - // if (r != nullptr) - // { - // // TODO jGL needs to be able to draw rects - // } - // else - // { - // // TODO jGL needs to be able to draw rects - // } - - std::string sid = to_string(citer->first)+"-"+std::to_string(i); - circlePos.push_back(jGL::Transform(cp->x, cp->y, theta, scale*2.0*cpmodel->r)); - shapes->add( - { - &circlePos.back(), - &ren.colour - }, - sid, - ren.priority - ); + if (r != nullptr) + { + std::string sid = to_string(citer->first)+"-"+std::to_string(i); + rectanglePos.push_back(jGL::Transform(cp->x, cp->y, theta, r->width()*scale, r->height()*scale)); + rectangles->add( + { + &rectanglePos.back(), + &ren.colour + }, + sid, + ren.priority + ); + } + else + { + std::string sid = to_string(citer->first)+"-"+std::to_string(i); + circlePos.push_back(jGL::Transform(cp->x, cp->y, theta, scale*2.0*cpmodel->r)); + circles->add( + { + &circlePos.back(), + &ren.colour + }, + sid, + ren.priority + ); + } + p++; } } citer++; } - shapes->draw(circleShader); + circles->draw(circleShader); + rectangles->draw(rectangleShader); }