[deploy] Merge pull request #42 from KiwanoEngine/dev
Update physics::Fixture
This commit is contained in:
		
						commit
						0ed1a2e992
					
				|  | @ -69,40 +69,35 @@ namespace kiwano | ||||||
| 			UpdateFromActor(); | 			UpdateFromActor(); | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		PhysicFixture PhysicBody::AddShape(PhysicShape* shape, float density, float friction, float restitution) | 		PhysicFixture PhysicBody::AddFixture(PhysicShape* shape, const PhysicFixture::Param& param) | ||||||
| 		{ | 		{ | ||||||
| 			KGE_ASSERT(body_ && world_); | 			KGE_ASSERT(body_ && world_); | ||||||
| 
 | 			return PhysicFixture(this, shape, param); | ||||||
| 			if (shape) |  | ||||||
| 			{ |  | ||||||
| 				return PhysicFixture::Create(this, shape, density, friction, restitution); |  | ||||||
| 			} |  | ||||||
| 			return PhysicFixture(); |  | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		PhysicFixture PhysicBody::AddCircleShape(float radius, float density) | 		PhysicFixture PhysicBody::AddCircleShape(float radius, float density) | ||||||
| 		{ | 		{ | ||||||
| 			return AddShape(&PhysicCircleShape(radius), density); | 			return AddFixture(&PhysicCircleShape(radius), PhysicFixture::Param(density)); | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		PhysicFixture PhysicBody::AddBoxShape(Vec2 const& size, float density) | 		PhysicFixture PhysicBody::AddBoxShape(Vec2 const& size, float density) | ||||||
| 		{ | 		{ | ||||||
| 			return AddShape(&PhysicBoxShape(size), density); | 			return AddFixture(&PhysicBoxShape(size), PhysicFixture::Param(density)); | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		PhysicFixture PhysicBody::AddPolygonShape(Vector<Point> const& vertexs, float density) | 		PhysicFixture PhysicBody::AddPolygonShape(Vector<Point> const& vertexs, float density) | ||||||
| 		{ | 		{ | ||||||
| 			return AddShape(&PhysicPolygonShape(vertexs), density); | 			return AddFixture(&PhysicPolygonShape(vertexs), PhysicFixture::Param(density)); | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		PhysicFixture PhysicBody::AddEdgeShape(Point const& p1, Point const& p2, float density) | 		PhysicFixture PhysicBody::AddEdgeShape(Point const& p1, Point const& p2, float density) | ||||||
| 		{ | 		{ | ||||||
| 			return AddShape(&PhysicEdgeShape(p1, p2), density); | 			return AddFixture(&PhysicEdgeShape(p1, p2), PhysicFixture::Param(density)); | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		PhysicFixture PhysicBody::AddChainShape(Vector<Point> const& vertexs, bool loop, float density) | 		PhysicFixture PhysicBody::AddChainShape(Vector<Point> const& vertexs, bool loop, float density) | ||||||
| 		{ | 		{ | ||||||
| 			return AddShape(&PhysicChainShape(vertexs, loop), density); | 			return AddFixture(&PhysicChainShape(vertexs, loop), PhysicFixture::Param(density)); | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		void PhysicBody::RemoveFixture(PhysicFixture const& fixture) | 		void PhysicBody::RemoveFixture(PhysicFixture const& fixture) | ||||||
|  | @ -165,7 +160,7 @@ namespace kiwano | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		void PhysicBody::GetMassData(float* mass, Point* center, float* inertia) | 		void PhysicBody::GetMassData(float* mass, Point* center, float* inertia) const | ||||||
| 		{ | 		{ | ||||||
| 			KGE_ASSERT(body_ && world_); | 			KGE_ASSERT(body_ && world_); | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -52,8 +52,10 @@ namespace kiwano | ||||||
| 			// 初始化
 | 			// 初始化
 | ||||||
| 			void Init(PhysicWorld* world, Actor* actor); | 			void Init(PhysicWorld* world, Actor* actor); | ||||||
| 
 | 
 | ||||||
|  | 			// 警속셸야
 | ||||||
|  | 			PhysicFixture AddFixture(PhysicShape* shape, const PhysicFixture::Param& param); | ||||||
|  | 
 | ||||||
| 			// 添加形状
 | 			// 添加形状
 | ||||||
| 			PhysicFixture AddShape(PhysicShape* shape, float density = 0.f, float friction = 0.2f, float restitution = 0.f); |  | ||||||
| 			PhysicFixture AddCircleShape(float radius, float density = 0.f); | 			PhysicFixture AddCircleShape(float radius, float density = 0.f); | ||||||
| 			PhysicFixture AddBoxShape(Vec2 const& size, float density = 0.f); | 			PhysicFixture AddBoxShape(Vec2 const& size, float density = 0.f); | ||||||
| 			PhysicFixture AddPolygonShape(Vector<Point> const& vertexs, float density = 0.f); | 			PhysicFixture AddPolygonShape(Vector<Point> const& vertexs, float density = 0.f); | ||||||
|  | @ -99,7 +101,7 @@ namespace kiwano | ||||||
| 			float GetInertia() const						{ KGE_ASSERT(body_); return body_->GetInertia(); } | 			float GetInertia() const						{ KGE_ASSERT(body_); return body_->GetInertia(); } | ||||||
| 
 | 
 | ||||||
| 			// 质量数据
 | 			// 质量数据
 | ||||||
| 			void GetMassData(float* mass, Point* center, float* inertia); | 			void GetMassData(float* mass, Point* center, float* inertia) const; | ||||||
| 			void SetMassData(float mass, Point const& center, float inertia); | 			void SetMassData(float mass, Point const& center, float inertia); | ||||||
| 			void ResetMassData(); | 			void ResetMassData(); | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -38,7 +38,8 @@ namespace kiwano | ||||||
| 			SetB2Fixture(fixture); | 			SetB2Fixture(fixture); | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		PhysicFixture PhysicFixture::Create(PhysicBody* body, PhysicShape* shape, float density, float friction, float restitution) | 		PhysicFixture::PhysicFixture(PhysicBody* body, PhysicShape* shape, const Param& param) | ||||||
|  | 			: PhysicFixture() | ||||||
| 		{ | 		{ | ||||||
| 			KGE_ASSERT(body); | 			KGE_ASSERT(body); | ||||||
| 
 | 
 | ||||||
|  | @ -48,14 +49,13 @@ namespace kiwano | ||||||
| 
 | 
 | ||||||
| 				b2Body* b2body = body->GetB2Body(); | 				b2Body* b2body = body->GetB2Body(); | ||||||
| 				b2FixtureDef fd; | 				b2FixtureDef fd; | ||||||
| 				fd.density = density; | 				fd.density = param.density; | ||||||
| 				fd.friction = friction; | 				fd.friction = param.friction; | ||||||
| 				fd.restitution = restitution; | 				fd.restitution = param.restitution; | ||||||
| 				fd.shape = shape->GetB2Shape(); | 				fd.shape = shape->GetB2Shape(); | ||||||
| 				auto fixture = b2body->CreateFixture(&fd); | 				auto fixture = b2body->CreateFixture(&fd); | ||||||
| 				return PhysicFixture(fixture); | 				SetB2Fixture(fixture); | ||||||
| 			} | 			} | ||||||
| 			return PhysicFixture(); |  | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		PhysicBody* PhysicFixture::GetBody() | 		PhysicBody* PhysicFixture::GetBody() | ||||||
|  | @ -82,5 +82,36 @@ namespace kiwano | ||||||
| 			return PhysicFixture(fixture_->GetNext()); | 			return PhysicFixture(fixture_->GetNext()); | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
|  | 		void PhysicFixture::GetMassData(float* mass, Point* center, float* inertia) const | ||||||
|  | 		{ | ||||||
|  | 			KGE_ASSERT(fixture_); | ||||||
|  | 
 | ||||||
|  | 			const PhysicBody* body = GetBody(); | ||||||
|  | 			KGE_ASSERT(body); | ||||||
|  | 
 | ||||||
|  | 			const PhysicWorld* world = body->GetWorld(); | ||||||
|  | 			KGE_ASSERT(world); | ||||||
|  | 
 | ||||||
|  | 			b2MassData data; | ||||||
|  | 			fixture_->GetMassData(&data); | ||||||
|  | 
 | ||||||
|  | 			if (mass) *mass = data.mass; | ||||||
|  | 			if (center) *center = world->World2Stage(data.center); | ||||||
|  | 			if (inertia) *inertia = data.I; | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 		bool PhysicFixture::TestPoint(const Point& p) const | ||||||
|  | 		{ | ||||||
|  | 			KGE_ASSERT(fixture_); | ||||||
|  | 
 | ||||||
|  | 			const PhysicBody* body = GetBody(); | ||||||
|  | 			KGE_ASSERT(body); | ||||||
|  | 
 | ||||||
|  | 			const PhysicWorld* world = body->GetWorld(); | ||||||
|  | 			KGE_ASSERT(world); | ||||||
|  | 
 | ||||||
|  | 			return fixture_->TestPoint(world->Stage2World(p)); | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -32,10 +32,26 @@ namespace kiwano | ||||||
| 		class PhysicFixture | 		class PhysicFixture | ||||||
| 		{ | 		{ | ||||||
| 		public: | 		public: | ||||||
|  | 			struct Param | ||||||
|  | 			{ | ||||||
|  | 				float density = 0.f; | ||||||
|  | 				float friction = 0.2f; | ||||||
|  | 				float restitution = 0.f; | ||||||
|  | 				bool is_sensor = false; | ||||||
|  | 
 | ||||||
|  | 				Param() {} | ||||||
|  | 
 | ||||||
|  | 				Param(float density, float friction = 0.2f, float restitution = 0.f, bool is_sensor = false) | ||||||
|  | 					: density(density) | ||||||
|  | 					, friction(friction) | ||||||
|  | 					, restitution(restitution) | ||||||
|  | 					, is_sensor(is_sensor) | ||||||
|  | 				{} | ||||||
|  | 			}; | ||||||
|  | 
 | ||||||
| 			PhysicFixture(); | 			PhysicFixture(); | ||||||
| 			PhysicFixture(b2Fixture* fixture); | 			PhysicFixture(b2Fixture* fixture); | ||||||
| 
 | 			PhysicFixture(PhysicBody* body, PhysicShape* shape, const Param& param); | ||||||
| 			static PhysicFixture Create(PhysicBody* body, PhysicShape* shape, float density = 0.f, float friction = 0.2f, float restitution = 0.f); |  | ||||||
| 
 | 
 | ||||||
| 			// 物体
 | 			// 物体
 | ||||||
| 			PhysicBody* GetBody(); | 			PhysicBody* GetBody(); | ||||||
|  | @ -47,6 +63,28 @@ namespace kiwano | ||||||
| 			// 下一夹具 (同一物体上)
 | 			// 下一夹具 (同一物体上)
 | ||||||
| 			PhysicFixture GetNext() const; | 			PhysicFixture GetNext() const; | ||||||
| 
 | 
 | ||||||
|  | 			// 接触传感器
 | ||||||
|  | 			bool IsSensor() const					{ KGE_ASSERT(fixture_); return fixture_->IsSensor(); } | ||||||
|  | 			void SetSensor(bool sensor)				{ KGE_ASSERT(fixture_); fixture_->SetSensor(sensor); } | ||||||
|  | 
 | ||||||
|  | 			// 质量数据
 | ||||||
|  | 			void GetMassData(float* mass, Point* center, float* inertia) const; | ||||||
|  | 
 | ||||||
|  | 			// 密度
 | ||||||
|  | 			float GetDensity() const				{ KGE_ASSERT(fixture_); return fixture_->GetDensity(); } | ||||||
|  | 			void SetDensity(float density)			{ KGE_ASSERT(fixture_); fixture_->SetDensity(density); } | ||||||
|  | 
 | ||||||
|  | 			// 摩擦力
 | ||||||
|  | 			float GetFriction() const				{ KGE_ASSERT(fixture_); return fixture_->GetFriction(); } | ||||||
|  | 			void SetFriction(float friction)		{ KGE_ASSERT(fixture_); fixture_->SetFriction(friction); } | ||||||
|  | 
 | ||||||
|  | 			// 弹性恢复
 | ||||||
|  | 			float GetRestitution() const			{ KGE_ASSERT(fixture_); return fixture_->GetRestitution(); } | ||||||
|  | 			void SetRestitution(float restitution)	{ KGE_ASSERT(fixture_); fixture_->SetRestitution(restitution); } | ||||||
|  | 
 | ||||||
|  | 			// 点测试
 | ||||||
|  | 			bool TestPoint(const Point& p) const; | ||||||
|  | 
 | ||||||
| 			bool IsValid() const					{ return !!fixture_; } | 			bool IsValid() const					{ return !!fixture_; } | ||||||
| 
 | 
 | ||||||
| 			b2Fixture* GetB2Fixture()				{ return fixture_; } | 			b2Fixture* GetB2Fixture()				{ return fixture_; } | ||||||
|  |  | ||||||
|  | @ -21,6 +21,9 @@ | ||||||
| #pragma once | #pragma once | ||||||
| 
 | 
 | ||||||
| #include <kiwano-physics/Shape.h> | #include <kiwano-physics/Shape.h> | ||||||
|  | #include <kiwano-physics/Fixture.h> | ||||||
|  | #include <kiwano-physics/Contact.h> | ||||||
|  | #include <kiwano-physics/ContactListener.h> | ||||||
| #include <kiwano-physics/Body.h> | #include <kiwano-physics/Body.h> | ||||||
| #include <kiwano-physics/Joint.h> | #include <kiwano-physics/Joint.h> | ||||||
| #include <kiwano-physics/World.h> | #include <kiwano-physics/World.h> | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue