๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“ Development Study/๐Ÿ’ป Win32API

Rigidbody

by eazuooz 2022. 10. 20.

์˜ˆ์ œ :)

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

๋Œ“๊ธ€