Skip to content

Commit

Permalink
Fix rects (#203)
Browse files Browse the repository at this point in the history
* Fix rect drawing, and rect update
  • Loading branch information
Jerboa-app authored Nov 30, 2024
1 parent 99c1a0f commit 72a4185
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 46 deletions.
5 changes: 3 additions & 2 deletions demo/desktop/perlinWorld/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ config =
{
["timeStep"] = 1.0/1800.0,
["subSample"] = 5,
["cofr"] = 0.9
["cofr"] = 0.1
}

hop.configure(config);
hop.configure(config);
dofile("mix.lua")
3 changes: 1 addition & 2 deletions demo/desktop/perlinWorld/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -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(); }
Expand Down
18 changes: 15 additions & 3 deletions demo/desktop/perlinWorld/mix.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
s = 3.0*hop.maxCollisionPrimitiveSize()
s = 1.9*hop.maxCollisionPrimitiveSize()

math.randomseed(os.time())

Expand All @@ -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;

Expand Down Expand Up @@ -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


Expand Down
16 changes: 16 additions & 0 deletions include/Collision/collisionMesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
97 changes: 86 additions & 11 deletions include/Collision/collisionPrimitive.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,28 @@ namespace Hop::System::Physics

const uint64_t LAST_INSIDE_COUNTER_MAX = 60;

/**
* @brief A primitive shape (circle) for collisions.
*
*/
struct CollisionPrimitive
{

static constexpr double RIGID = 1e6;

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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -269,6 +334,10 @@ namespace Hop::System::Physics
);
}

/**
* @brief Recalculate axes (vector for length and width).
*
*/
void resetAxes()
{
axis1x = llx-lrx;
Expand Down Expand Up @@ -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<double>(llx, lly, cosine, sine);
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 5 additions & 3 deletions include/Debug/collisionMeshDebug.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -33,7 +34,8 @@ namespace Hop::Debugging

CollisionMeshDebug(std::shared_ptr<jGL::jGLInstance> jgl)
: refresh(true),
shapes(jgl->createShapeRenderer(256)),
circles(jgl->createShapeRenderer(256)),
rectangles(jgl->createShapeRenderer(256)),
circleShader(std::make_shared<jGL::GL::glShader>
(
jGL::GL::glShapeRenderer::shapeVertexShader,
Expand All @@ -60,11 +62,11 @@ namespace Hop::Debugging

bool refresh;

std::shared_ptr<jGL::ShapeRenderer> shapes;
std::shared_ptr<jGL::ShapeRenderer> circles, rectangles;

std::shared_ptr<jGL::Shader> circleShader, rectangleShader;

std::vector<cTransform> circlePos;
std::vector<cTransform> circlePos, rectanglePos;

};

Expand Down
3 changes: 1 addition & 2 deletions src/Collision/collisionMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
}
}
Expand Down
59 changes: 36 additions & 23 deletions src/Debug/collisionMeshDebug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -47,34 +51,43 @@ namespace Hop::Debugging
{
CollisionPrimitive * cp = (c.mesh[i].get());
MeshPoint * cpmodel = c.mesh.getModelVertex(i).get();
//Rectangle * r = dynamic_cast<Rectangle*>(cp);
MeshRectangle * r = dynamic_cast<MeshRectangle*>(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);

}

Expand Down

0 comments on commit 72a4185

Please sign in to comment.