์์ :)
https://github.com/eazuooz/WindowAPI/commit/289a2c74cd90ae05d1bf149c9c05f6c7856d07c3
Rigidbody · eazuooz/WindowAPI@289a2c7
Show file tree Showing 9 changed files with 113 additions and 14 deletions.
github.com
์ด์ ๊ฐ๋จํ ๋ฌผ๋ฆฌ ์์คํ ๋์ถ๊ฐํด๋ณด์.
๊ฐ์ฒด(ๅ้ซ, Rigid body)๋ ๋ฌผ๋ฆฌํ์์ ํํ๊ฐ ๊ณ ์ ๋์ด ๋ณํ์ง ์๋ ๋ฌผ์ฒด๋ฅผ ๊ฐ๋ฆฌํจ๋ค. ๊ฐ์ฒด๋ ์ธ๋ ฅ์ด ๊ฐํด์ ธ๋ ๋ชจ์์ด๋ ํฌ๊ธฐ๊ฐ ๋ณํ๋์ง ์๋๋ค. ์ค์ ์ธ๊ณ์์ ๋ชจ๋ ๋ฌผ์ฒด๋ ์ธ๋ ฅ์ ๊ฐํจ์ ๋ฐ๋ผ ์กฐ๊ธ์ฉ ๋ชจ์์ด๋ ํฌ๊ธฐ๊ฐ ๋ณํ๋ ์ ์์ผ๋ฉฐ ๋ฐ๋ผ์ ๊ฐ์ฒด๋ผ๊ณ ํ ์ ์์ง๋ง, ๋ณํ๋๋ ์ ๋๊ฐ ๋ฌด์ํ ์ ์์ ๋งํผ ์๋ค๋ฉด ์ด๋ค ๋ฌผ์ฒด๋ฅผ ๊ฐ์ฒด๋ก ๊ฐ์ ํ๊ธฐ๋ ํ๋ค. ๋ฌผ์ฒด์ ์ด๋์ ๋ถ์ํ ๋์ ๋ณดํต ๋ฌผ์ฒด๋ฅผ ๊ฐ์ฒด๋ก ๊ฐ์ ํ๋ค.
๋ฆฌ์ง๋ ๋ฐ๋ ์ปดํฌ๋ํธ๋ฅผ ์ค๊ณํ๊ณ ์ถ๊ฐ ํด๋๋ก ํ๊ฒ ๋ค.
๋ค์ํ ๊ธฐ๋ฅ์ ๋ค์ด๊ฐ์ง์๊ณ ๊ฐ๋จํ ์ด๋์ ๋๋ง ํ์ ๋ถ์ฌ์ฃผ๋ ๋ฐฉ์์ผ๋ก ์ถ๊ฐ๋๋ค.
class Rigidbody : public Component
{
public:
Rigidbody();
~Rigidbody();
virtual void Tick() override;
virtual void Render(HDC hdc) override;
void AddForce(Vector2 force);
void SetMass(float mass) { mMass = mass; }
private:
float mMass;
Vector2 mForce;
Vector2 mAccelation;
Vector2 mVelocity;
};
void Rigidbody::Tick()
{
// F = M x A
// A = F / M
mAccelation = mForce / mMass;
// ์๋์ ๊ฐ์๋๋ฅผ ๋ํ๋ค
mVelocity += mAccelation * Time::DeltaTime();
// ์๋์ ๋ง๊ฒ ๋ฌผ์ฒด๋ฅผ ์ด๋์ํจ๋ค.
Vector2 pos = GetOwner()->GetPos();
pos += mVelocity * Time::DeltaTime();
GetOwner()->SetPos(pos);
mForce.clear();
}
void Rigidbody::AddForce(Vector2 force)
{
mForce += force;
}
์ด์ ํ๋ ์ด์ด์์ Pos๋ฅผ ์ด๋์ํค๋๊ฒ ์๋ ํ์ ์ฃผ์ด์ ์ด๋ํ๊ฒ๋ ์คํํด๋ณด์
void Player::Tick()
{
// ํค์
๋ ฅ์ ๋ฐ๋ฅธ ์ด๋
Vector2 vPos = GetPos();
// ์๊ฐ ๋๊ธฐํ
if (KEY_PREESED(KEY_CODE::W))
{
GetComponent<Rigidbody>()->AddForce(Vector2(0.0f, -200.0f));
}
if (KEY_PREESED(KEY_CODE::S))
{
GetComponent<Rigidbody>()->AddForce(Vector2(0.0f, 200.0f));
}
if (KEY_PREESED(KEY_CODE::A))
{
GetComponent<Rigidbody>()->AddForce(Vector2(-200.0f, 0.0f));
}
if (KEY_PREESED(KEY_CODE::D))
{
GetComponent<Rigidbody>()->AddForce(Vector2(200.0f, 0.0f));
}
}
๊ฐ๋จํ ์์๋ก ๋ง์ฐฐ๋ ฅ๊ณผ ์ค๋ ฅ์ ์ํ์ฝ๋๋ฅผ ์ฝ์ ํ์ต๋๋ค.
void Rigidbody::Tick()
{
// ์ค๋ ฅ์ด๋
//Vector2 pos = GetOwner()->GetPos();
//if (pos.y >= 980.0f && mForce.y == 0.0f)
//{
// return;
//}
//mGravity = Vector2(0.0f, 9.8f);
//mVelocity += (mGravity * mMass) * Time::DeltaTime();
//
//// F = M x A
//// A = F / M
//mAccelation = mForce / mMass;
//// ์๋์ ๊ฐ์๋๋ฅผ ๋ํ๋ค
//mVelocity += mAccelation * Time::DeltaTime();
//// ์๋์ ๋ง๊ฒ ๋ฌผ์ฒด๋ฅผ ์ด๋์ํจ๋ค.
//pos += mVelocity;
//GetOwner()->SetPos(pos);
//mForce.clear();
//์ด๋
// F = M x A
// A = F / M
mAccelation = mForce / mMass;
// ์๋์ ๊ฐ์๋๋ฅผ ๋ํ๋ค
mVelocity += mAccelation * Time::DeltaTime();
//๋ง์ฐฐ๋ ฅ ์กฐ๊ฑด ( ์ ์ฉ๋ ํ์ด ์๊ณ , ์๋๊ฐ 0 ์ด ์๋ ๋)
if ( !(mVelocity == Vector2::Zero) )
{
// ์๋์ ๋ฐ๋ ๋ฐฉํฅ์ผ๋ก ๋ง์ฐฐ๋ ฅ์ ์ ์ฉ
Vector2 friction = -mVelocity;
friction = friction.Normalize() * mFriction * mMass * Time::DeltaTime();
// ๋ง์ฐฐ๋ ฅ์ผ๋ก ์ธํ ์๋ ๊ฐ์๋์ด ํ์ฌ ์๋๋ณด๋ค ๋ ํฐ ๊ฒฝ์ฐ
if (mVelocity.Length() < friction.Length())
{
// ์๋๋ฅผ 0 ๋ก ๋ง๋ ๋ค.
mVelocity = Vector2(0.f, 0.f);
}
else
{
// ์๋์์ ๋ง์ฐฐ๋ ฅ์ผ๋ก ์ธํ ๋ฐ๋๋ฐฉํฅ์ผ๋ก ์๋๋ฅผ ์ฐจ๊ฐํ๋ค.
mVelocity += friction;
}
}
// ์๋์ ๋ง๊ฒ ๋ฌผ์ฒด๋ฅผ ์ด๋์ํจ๋ค.
Vector2 pos = GetOwner()->GetPos();
pos = pos + mVelocity * Time::DeltaTime();
GetOwner()->SetPos(pos);
mForce.clear();
}
'๐ Development Study > ๐ป Win32API' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Tile Map (0) | 2022.11.02 |
---|---|
Clone Object(๋ณต์ฌ ์์ฑ์) (0) | 2022.10.26 |
API Math (0) | 2022.10.20 |
Animator (1) | 2022.10.13 |
Object Delete (0) | 2022.10.12 |
๋๊ธ