티스토리 뷰
* 인프런에 있는 "홍정모의 게임 만들기 연습 문제 패키지" 강의를 바탕으로 작성된 글입니다.
1. 3개 이상의 물체 테스트 해보기.
#include "Game2D.h"
#include "RandomNumberGenerator.h"
#include "RigidCircle.h"
#include <vector>
#include <memory>
namespace shyplants
{
class Example : public Game2D
{
public:
RigidCircle rb[3];
Example()
: Game2D()
{
reset();
}
void reset()
{
// Initial position and velocity
rb[0].pos = vec2(0.0f, 0.5f);
rb[0].vel = vec2(0.0f, 0.0f);
rb[0].color = Colors::hotpink;
rb[0].radius = 0.03f;
rb[0].mass = 1.0f;
rb[0].fixed = true;
rb[1].pos = vec2(0.3f, 0.3f);
rb[1].vel = vec2(0.0f, 0.0f);
rb[1].color = Colors::yellow;
rb[1].radius = 0.03f;
rb[1].mass = rb[0].mass * std::pow(rb[1].radius / rb[0].radius, 2.0f);
rb[1].fixed = false;
rb[2].pos = vec2(0.1f, 0.2f);
rb[2].vel = vec2(0.0f, 0.0f);
rb[2].color = Colors::yellow;
rb[2].radius = 0.03f;
rb[2].mass = rb[0].mass * std::pow(rb[2].radius / rb[0].radius, 2.0f);
rb[1].fixed = false;
}
void drawWall()
{
setLineWidth(5.0f);
drawLine(Colors::blue, { -1.0f, -1.0f }, Colors::blue, { 1.0f, -1.0f });
drawLine(Colors::blue, { 1.0f, -1.0f }, Colors::blue, { 1.0f, 1.0f });
drawLine(Colors::blue, { -1.0f, -1.0f }, Colors::blue, { -1.0f, 1.0f });
}
void update() override
{
const float dt = getTimeStep() * 0.4f;
const float epsilon = 0.5f;
// coefficients
const vec2 gravity(0.0f, -9.8f);
const float l0 = 0.5f;
const float coeff_k = 100.0f;
const float coeff_d = 10.0f;
// update rb1 ~ rb2 (Note: rb0 is fixed)
for (int i = 1; i < 3; ++i)
{
const auto distance = (rb[i].pos - rb[i-1].pos).getMagnitude();
const auto direction = (rb[i].pos - rb[i-1].pos) / distance;
// compute stiffness force
const auto spring_force = direction * -(distance - l0) * coeff_k;
// compute damping force
const auto damping_force = direction * -(rb[i].vel - rb[i-1].vel).getDotProduct(direction) * coeff_d;
const auto accel = gravity + (spring_force + damping_force) / rb[i].mass;
rb[i].vel += accel * dt;
if (!rb[i - 1].fixed) rb[i - 1].vel -= accel * dt;
}
for (int i = 1; i < 3; ++i)
{
rb[1].pos += rb[1].vel * dt;
rb[2].pos += rb[2].vel * dt;
}
// draw
drawWall();
// spring
for (int i = 1; i < 3; ++i) {
drawLine(Colors::red, rb[i-1].pos, Colors::red, rb[i].pos);
}
// mass points
for (int i = 0; i < 3; ++i) {
rb[i].draw();
}
// reset button
if (isKeyPressedAndReleased(GLFW_KEY_R)) reset();
}
};
}

2. 영상 후반부 옷감 테스트 해보기.
#include "Game2D.h"
#include "RandomNumberGenerator.h"
#include "RigidCircle.h"
#include <vector>
#include <memory>
namespace shyplants
{
class Example : public Game2D
{
public:
std::vector<RigidCircle*> rCircles;
int row = 6, col = 6;
Example()
: Game2D()
{
reset();
}
void reset()
{
rCircles.clear();
// Initial position and velocity
vec2 pos = vec2(-0.5f, 0.5f);
vec2 vel = vec2(0, 0);
float radius = 0.03f;
float mass = 1.0f;
for (int r = 0; r < row; ++r) {
for (int c = 0; c < col; ++c) {
vec2 cur_pos = pos + vec2(1.0f / (col - 1) * c, -1.0f / (row - 1) * r);
bool fixed = (r == 0 && (c == 0 || c == col - 1));
RGB color = fixed ? Colors::gold : Colors::blue;
rCircles.emplace_back(new RigidCircle(cur_pos, vel, color, radius, mass, fixed));
}
}
}
void drawWall()
{
setLineWidth(5.0f);
drawLine(Colors::blue, { -1.0f, -1.0f }, Colors::blue, { 1.0f, -1.0f });
drawLine(Colors::blue, { 1.0f, -1.0f }, Colors::blue, { 1.0f, 1.0f });
drawLine(Colors::blue, { -1.0f, -1.0f }, Colors::blue, { -1.0f, 1.0f });
}
bool OOB(int y, int x) {
return y < 0 || y >= row || x < 0 || x >= col;
}
int min(int a, int b) {
return a < b ? a : b;
}
int max(int a, int b) {
return a > b ? a : b;
}
void update() override
{
const float dt = getTimeStep() * 0.4f;
const float epsilon = 0.5f;
// coefficients
const vec2 gravity(0.0f, -9.8f);
const float l0 = 1.0f / (max(row, col) - 1) / 1.2f;
const float coeff_k = 150.0f;
const float coeff_d = 10.0f;
const float coef_res = 0.8f; // coefficient of restitution
const float coef_friction = 0.99f; // friction (not physical)
// update rCircle
const int dy[] = { -1, -1, -1, 0, 0 };
const int dx[] = { -1, 0, 1, -1, 1 };
for (int r = 0; r < row; ++r) {
for (int c = 0; c < col; ++c) {
const int cur_pos = r * col + c;
vec2 tot_spring_force = vec2(0, 0);
vec2 tot_damping_force = vec2(0, 0);
for (int dir = 0; dir < 5; ++dir) {
if (OOB(r + dy[dir], c + dx[dir])) continue;
const int prev_pos = (r + dy[dir]) * col + (c + dx[dir]);
const auto distance = (rCircles[cur_pos]->pos - rCircles[prev_pos]->pos).getMagnitude();
const auto direction = (rCircles[cur_pos]->pos - rCircles[prev_pos]->pos) / distance;
// compute stiffness force
const auto spring_force = direction * -(distance - l0) * coeff_k;
tot_spring_force += spring_force;
// compute damping force
const auto damping_force = direction * -(rCircles[cur_pos]->vel - rCircles[prev_pos]->vel).getDotProduct(direction) * coeff_d;
tot_damping_force += damping_force;
if (!rCircles[prev_pos]->fixed)
rCircles[prev_pos]->vel -= (spring_force + damping_force) / rCircles[prev_pos]->mass * dt;
}
const auto accel = gravity + (tot_spring_force + tot_damping_force) / rCircles[cur_pos]->mass;
if (!rCircles[cur_pos]->fixed)
rCircles[cur_pos]->vel += accel * dt;
}
}
for (auto& c : rCircles) if (!c->fixed) {
c->pos += c->vel * dt;
if (c->pos.y <= -1.0f + c->radius) // ground
{
c->pos.y = -1.0f + c->radius;
if (c->vel.y <= 0.0f)
c->vel.y *= -1.0f * coef_res;
c->vel.x *= coef_friction;
}
}
// draw
drawWall();
// spring
for (int r = 0; r < row; ++r) {
for (int c = 0; c < col; ++c) {
if (r == row - 1 && c == col - 1) break;
if(c == col-1 && r < row-1)
drawLine(Colors::red, rCircles[r*col + c]->pos, Colors::red, rCircles[r*col + col + c]->pos);
else if(r == row-1 && c < col-1)
drawLine(Colors::red, rCircles[r*col + c]->pos, Colors::red, rCircles[r*col + c + 1]->pos);
else {
drawLine(Colors::red, rCircles[r*col + c]->pos, Colors::red, rCircles[r*col + c + 1]->pos);
drawLine(Colors::red, rCircles[r*col + c]->pos, Colors::red, rCircles[r*col + col + c + 1]->pos);
drawLine(Colors::red, rCircles[r*col + c + 1]->pos, Colors::red, rCircles[r*col + col + c]->pos);
drawLine(Colors::red, rCircles[r*col + c]->pos, Colors::red, rCircles[r*col + col + c]->pos);
}
}
}
// mass points
for (auto& c : rCircles) {
c->draw();
}
// reset button
if (isKeyPressedAndReleased(GLFW_KEY_R)) reset();
}
};
}

'개발 > 게임개발' 카테고리의 다른 글
[C/C++ 콘솔게임] 타이틀(알파벳) 생성기 (0) | 2023.05.07 |
---|---|
c++ 게임만들기 연습문제 3.4 (0) | 2023.03.05 |
c++ 게임만들기 연습문제 3.2 (0) | 2023.03.02 |
c++ 게임만들기 연습문제 3.1 (0) | 2023.03.02 |
c++ 게임만들기 연습문제 2.5 (0) | 2023.03.01 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 정보올림피아드
- 언리얼 프로젝트 재생성
- Unreal Engine
- 홍정모의 게임 만들기 연습 문제 패키지
- 언리얼 프로젝트 재생성 자동화
- ndisplay
- pygame
- 브레젠험 알고리즘
- C++게임
- BOJ 2365
- Codeforces
- DP
- Python
- tetris
- 코드포스
- 백준 2365
- BOJ 27469
- opengl
- OpenVDB
- 백준
- C++게임개발
- 퀸 움직이기
- 숫자판 만들기
- ICPC 후기
- 테트리스
- 초등부
- 백준 27469
- unreal enigne
- UE5.3
- 언리얼 자동화
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함