Skip to content

Commit

Permalink
Merge pull request #4 from Bytenol/1-algorithm-to-rasterize-circle
Browse files Browse the repository at this point in the history
Added beer
Bytenol authored Sep 21, 2024

Verified

This commit was signed with the committer’s verified signature.
daviehh daviehh
2 parents 05f0aa0 + 3535f88 commit 701cf38
Showing 7 changed files with 160 additions and 9 deletions.
Binary file added assets/banner.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions deps/web/[email protected]

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions deps/web/[email protected]

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion include/phy/RigidBody.h
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@ namespace phy
float mass = 1.0f;
float im = 1.0f; // moment of inertia
float angVel = 0.0f; // angular velocity
float rotation = 0.0f;

bool isFilled = true;
Vector2 pos;
Vector2 vel;
@@ -29,6 +29,10 @@ namespace phy

explicit RigidBody(const vertices_t& v);
void setRotation(float angle);
float getRotation() const;

private:
float rotation = 0.0f;
};


@@ -46,6 +50,11 @@ namespace phy
rotation = angle;
}

inline float RigidBody::getRotation() const
{
return rotation;
}

} // namespace phy


99 changes: 94 additions & 5 deletions index.html
Original file line number Diff line number Diff line change
@@ -4,10 +4,99 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Physics Simulation Tutorial</title>
<link rel="stylesheet" href="./deps/web/[email protected]">
</head>
<body>
<ul>
<li><a href="/pages/rigidBodyTest/main.html">rigidBodyTest</a></li>
</ul>
</body>
<body class="dark">
<nav class="left drawer l">
<header>
<nav>
<img src="./assets/banner.jpg" class="circle">
<h6>Cheers</h6>
</nav>
</header>
<a>
<i>home</i>
<div>Home</div>
</a>
<a>
<i>search</i>
<div>Search</div>
</a>
<a>
<i>share</i>
<div>Share</div>
</a>
<a>
<i>more_vert</i>
<div>More</div>
</a>
<div class="divider"></div>
<label>Label</label>
<a>
<i>widgets</i>
<div>Widgets</div>
</a>
<a>
<i>chat</i>
<div>Chat</div>
</a>
<a>
<i>help</i>
<div>Help</div>
</a>
</nav>

<nav class="left m">
<header>
<img src="./assets/banner.jpg" class="circle">
</header>
<a>
<i>home</i>
<div>Home</div>
</a>
<a>
<i>search</i>
<div>Search</div>
</a>
<a>
<i>share</i>
<div>Share</div>
</a>
<a>
<i>more_vert</i>
<div>More</div>
</a>
</nav>

<nav class="bottom s">
<a>
<i>home</i>
</a>
<a>
<i>search</i>
</a>
<a>
<i>share</i>
</a>
<a>
<i>more_vert</i>
</a>
</nav>

<main class="responsive">
<img src="./assets/banner.jpg" class="responsive round">
<h3>Welcome</h3>
<h5>The beer is ready!</h5>

<h3>Rigid Bodies</h3>
<details>
<summary>The following list contains Simulation of rigid bodies</summary>
<article>
<a class="row wave" href="./pages/rigidBodyTest/main.html">RigidBody Test</a>
<hr>
</article>
</details>

</main>
</body>
</html>
Binary file modified pages/rigidBodyTest/main.wasm
Binary file not shown.
55 changes: 52 additions & 3 deletions src/rigidBodyTest/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
/**
* @todo erase the error in the console
*
* @todo add normal force when the block is lying flat
* @todo erase the error in the browser console
* @todo remove html text area
* @todo fix timestep : (too fast now)
* @todo add proper forces when it's lying down
* @todo handle when tab switches
* @todo set size as window size
*/
#include <iostream>
#include <chrono>
@@ -27,6 +32,9 @@ void update(float dt, Canvas& cnv);
decltype(std::chrono::high_resolution_clock::now()) lastTime;

phy::RigidBody body;
phy::Vector2 acc, force;
float torque, alp;
const int floorY = 550;

int main(int argc, char* argv[])
{
@@ -47,14 +55,52 @@ void init(Canvas& cnv)
{ -50.0f, 25.0f },
});
body.pos.x = 300;
body.pos.y = 300;
body.pos.y = 0;
body.mass = 1;
body.im = 10000;
body.setRotation(45 * 3.14159f / 180);
lastTime = std::chrono::high_resolution_clock::now();
}


void update(float dt, Canvas& cnv)
{
body.pos += body.vel * dt;
body.setRotation(body.angVel * dt);

// check for collisions
for(auto it = body.vertices.begin(); it != body.vertices.end(); it++)
{
auto crossProduct = [](const phy::Vector2& a, const phy::Vector2& b) {
return a.x * b.y - a.y * b.x;
};

const auto nPos = body.pos + it->rotate(body.getRotation());
if(nPos.y >= floorY)
{
body.pos.y -= (nPos.y - floorY);
auto rp1 = it->rotate(body.getRotation());
auto g = phy::Vector2(rp1.y, -rp1.x).normalize() * (-body.angVel * rp1.getLength());
auto vp1 = body.vel + g;
auto normal = phy::Vector2(0, -1);
auto rp1Xnormal = crossProduct(rp1, normal);
const float cr = 0.4f;
const float impulse = -(1+cr)*vp1.dotProduct(normal)/(1/body.mass + rp1Xnormal*rp1Xnormal/body.im);
body.vel += normal * (impulse/body.mass);
body.angVel += rp1Xnormal * impulse/body.im;
}
}

// forces and torque
const auto weight = phy::Vector2(0.0f, body.mass * 10.0f);
force = weight;
torque = 0;
torque += -0.4f * body.angVel;

acc = force * (1/ body.mass);
alp = torque / body.im;
body.vel += acc * dt;
body.angVel += alp * dt;
}


@@ -70,6 +116,9 @@ void render(Canvas& canvas)
const auto p2 = body.pos + body.vertices[(i + 1 >= maxSize ? 0 : i + 1)];
SDL_RenderDrawLine(canvas.renderer, p1.x, p1.y, p2.x, p2.y);
}

SDL_SetRenderDrawColor(canvas.renderer, 255, 255, 255, 255);
SDL_RenderDrawLine(canvas.renderer, 0, floorY, 500, floorY);
}


0 comments on commit 701cf38

Please sign in to comment.