티스토리 뷰
* 인프런에 있는 "홍정모의 게임 만들기 연습 문제 패키지" 강의를 바탕으로 작성된 글입니다.
1. 초기 위치, 초기 속도, 반발률 등을 바꿔서 실행시켜보기.
// 초기 위치 : (-0.8, 0.7) 왼쪽 위
// 초기 속도 : (5.0, 0.0) 오른쪽
// 반발률 : 0.6
#include "Game2D.h"
#include "RandomNumberGenerator.h"
#include <vector>
#include <memory>
namespace shyplants
{
class RigidCircle
{
public:
vec2 pos;
vec2 vel;
float radius = 0.1f;
void draw()
{
beginTransformation();
{
translate(pos);
drawFilledCircle(Colors::hotpink, radius - 1e-3f);
setLineWidth(2.0f);
drawWiredCircle(Colors::black, radius);
}
endTransformation();
}
void update(const float & dt)
{
static const vec2 gravity = vec2(0.0f, -9.8f);
static const float coef_res = 0.6f; // Coefficient of restitution : 반발 계수
static const float coef_friction = 0.99f; // friction (not physical)
// numerical integration
vel += gravity * dt;
pos += vel * dt;
// wall collision, friction
if ((1.0f - pos.x) <= radius) // right wall
{
pos.x = 1.0f - radius;
if(vel.x >= 0.0f)
vel.x *= -1.0f * coef_res;
}
if (pos.x <= -1.0f + radius) // left wall
{
pos.x = -1.0f + radius;
if (vel.x <= 0.0f)
vel.x *= -1.0f * coef_res;
}
if (pos.y <= -1.0f + radius) // ground
{
pos.y = -1.0f + radius;
if (vel.y <= 0.0f)
vel.y *= -1.0f * coef_res;
vel.x *= coef_friction;
}
}
};
class Example : public Game2D
{
public:
RigidCircle rigid_body;
Example()
: Game2D()
{
reset();
}
void reset()
{
// Initial position and velocity
rigid_body.pos = vec2(-0.8f, 0.7f);
rigid_body.vel = vec2(5.0f, 0.0f); // 5m/s
}
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
{
// physics update
rigid_body.update(getTimeStep() * 0.6f);
// draw
drawWall();
rigid_body.draw();
// reset button
if (isKeyPressedAndReleased(GLFW_KEY_R)) reset();
}
};

'개발 > 게임개발' 카테고리의 다른 글
c++ 게임만들기 연습문제 3.3 (1) | 2023.03.04 |
---|---|
c++ 게임만들기 연습문제 3.2 (0) | 2023.03.02 |
c++ 게임만들기 연습문제 2.5 (0) | 2023.03.01 |
c++ 게임만들기 연습문제 2.4 (0) | 2023.03.01 |
c++ 게임만들기 연습문제 2.3 (0) | 2023.02.28 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 언리얼 프로젝트 재생성 자동화
- ndisplay
- opengl
- C++게임
- tetris
- 코드포스
- 백준 2365
- 백준
- 숫자판 만들기
- 언리얼 프로젝트 재생성
- ICPC 후기
- 언리얼 자동화
- Codeforces
- Unreal Engine
- BOJ 27469
- 퀸 움직이기
- BOJ 2365
- Python
- 정보올림피아드
- 백준 27469
- 홍정모의 게임 만들기 연습 문제 패키지
- C++게임개발
- 초등부
- DP
- 브레젠험 알고리즘
- OpenVDB
- 테트리스
- unreal enigne
- UE5.3
- pygame
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함