Magic_Game/src/kiwano-physics/Body.h

178 lines
6.1 KiB
C
Raw Normal View History

2019-10-18 11:50:46 +08:00
// Copyright (c) 2018-2019 Kiwano - Nomango
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#pragma once
#include <kiwano-physics/helper.h>
#include <kiwano-physics/Shape.h>
2019-10-22 16:49:34 +08:00
#include <kiwano-physics/Fixture.h>
2019-10-31 20:34:38 +08:00
#include <kiwano-physics/Contact.h>
2019-10-18 11:50:46 +08:00
namespace kiwano
{
namespace physics
{
2019-10-30 23:12:18 +08:00
class PhysicWorld;
2019-10-18 11:50:46 +08:00
2019-10-30 23:12:18 +08:00
// <20><><EFBFBD><EFBFBD>
KGE_DECLARE_SMART_PTR(PhysicBody);
class KGE_API PhysicBody
: public virtual RefCounter
2019-10-18 11:50:46 +08:00
{
public:
enum class Type
{
Static = 0,
Kinematic,
Dynamic,
};
2019-10-30 23:12:18 +08:00
PhysicBody();
PhysicBody(b2Body* body, Actor* actor);
PhysicBody(PhysicWorld* world, Actor* actor);
PhysicBody(PhysicWorld* world, ActorPtr actor) : PhysicBody(world, actor.get()) {}
virtual ~PhysicBody();
2019-10-18 11:50:46 +08:00
2019-10-30 23:12:18 +08:00
// <20><>ʼ<EFBFBD><CABC>
void Init(PhysicWorld* world, Actor* actor);
2019-10-23 16:49:34 +08:00
2019-11-01 16:23:46 +08:00
// <20><><EFBFBD>Ӽо<D3BC>
PhysicFixture AddFixture(PhysicShape* shape, const PhysicFixture::Param& param);
2019-10-22 16:49:34 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״
2019-10-30 23:12:18 +08:00
PhysicFixture AddCircleShape(float radius, float density = 0.f);
PhysicFixture AddBoxShape(Vec2 const& size, float density = 0.f);
PhysicFixture AddPolygonShape(Vector<Point> const& vertexs, float density = 0.f);
PhysicFixture AddEdgeShape(Point const& p1, Point const& p2, float density = 0.f);
PhysicFixture AddChainShape(Vector<Point> const& vertexs, bool loop, float density = 0.f);
2019-10-22 16:49:34 +08:00
2019-10-30 23:12:18 +08:00
// <20><>ȡ<EFBFBD>о<EFBFBD>
2019-10-31 20:34:38 +08:00
PhysicFixture GetFixtureList() const { KGE_ASSERT(body_); PhysicFixture(body_->GetFixtureList()); }
2019-10-22 16:49:34 +08:00
// <20>Ƴ<EFBFBD><C6B3>о<EFBFBD>
2019-10-30 23:12:18 +08:00
void RemoveFixture(PhysicFixture const& fixture);
2019-10-22 16:49:34 +08:00
2019-10-31 20:34:38 +08:00
// <20><>ȡ<EFBFBD>Ӵ<EFBFBD><D3B4><EFBFBD>
PhysicContactEdge GetContactList() const { KGE_ASSERT(body_); PhysicContactEdge(body_->GetContactList()); }
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
uint16_t GetCategoryBits() const { return category_bits_; }
void SetCategoryBits(uint16_t category_bits);
// <20><>ײ<EFBFBD><D7B2><EFBFBD><EFBFBD>
uint16_t GetMaskBits() const { return mask_bits_; }
void SetMaskBits(uint16_t mask_bits);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int16_t GetGroupIndex() const { return group_index_; }
void SetGroupIndex(int16_t index);
2019-10-30 23:12:18 +08:00
// <20><>ת<EFBFBD>Ƕ<EFBFBD>
float GetBodyRotation() const { KGE_ASSERT(body_); return math::Radian2Degree(body_->GetAngle()); }
void SetBodyRotation(float angle) { SetBodyTransform(GetBodyPosition(), angle); }
// λ<><CEBB>
Point GetBodyPosition() const;
void SetBodyPosition(Point const& pos) { SetBodyTransform(pos, GetBodyRotation()); }
// λ<>ú<EFBFBD><C3BA><EFBFBD>ת<EFBFBD>
void SetBodyTransform(Point const& pos, float angle);
// <20><><EFBFBD><EFBFBD>
2019-10-22 16:49:34 +08:00
float GetMass() const { KGE_ASSERT(body_); return body_->GetMass(); }
2019-10-30 23:12:18 +08:00
// <20><><EFBFBD><EFBFBD>
float GetInertia() const { KGE_ASSERT(body_); return body_->GetInertia(); }
2019-10-28 17:25:06 +08:00
2019-10-30 23:12:18 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2019-11-01 16:23:46 +08:00
void GetMassData(float* mass, Point* center, float* inertia) const;
2019-10-30 23:12:18 +08:00
void SetMassData(float mass, Point const& center, float inertia);
void ResetMassData();
2019-10-28 17:25:06 +08:00
2019-10-30 23:12:18 +08:00
// <20><><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>
2019-10-22 16:49:34 +08:00
Point GetLocalPoint(Point const& world) const;
Point GetWorldPoint(Point const& local) const;
2019-10-30 23:12:18 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Point GetLocalCenter() const;
Point GetWorldCenter() const;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2019-10-22 16:49:34 +08:00
Type GetType() const { KGE_ASSERT(body_); return Type(body_->GetType()); }
void SetType(Type type) { KGE_ASSERT(body_); body_->SetType(static_cast<b2BodyType>(type)); }
2019-10-18 11:50:46 +08:00
2019-10-30 23:12:18 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
float GetGravityScale() const { KGE_ASSERT(body_); return body_->GetGravityScale(); }
void SetGravityScale(float scale) { KGE_ASSERT(body_); body_->SetGravityScale(scale); }
// ʩ<><CAA9>
2019-10-23 16:49:34 +08:00
void ApplyForce(Vec2 const& force, Point const& point, bool wake = true);
void ApplyForceToCenter(Vec2 const& force, bool wake = true);
2019-10-30 23:12:18 +08:00
// ʩ<><CAA9>Ť<EFBFBD><C5A4>
2019-10-23 16:49:34 +08:00
void ApplyTorque(float torque, bool wake = true);
2019-10-30 23:12:18 +08:00
// <20>̶<EFBFBD><CCB6><EFBFBD>ת
bool IsIgnoreRotation() const { KGE_ASSERT(body_); return body_->IsFixedRotation(); }
void SetIgnoreRotation(bool flag) { KGE_ASSERT(body_); body_->SetFixedRotation(flag); }
// <20>ӵ<EFBFBD>
bool IsBullet() const { KGE_ASSERT(body_); return body_->IsBullet(); }
void SetBullet(bool flag) { KGE_ASSERT(body_); body_->SetBullet(flag); }
// <20><><EFBFBD><EFBFBD>
bool IsAwake() const { KGE_ASSERT(body_); return body_->IsAwake(); }
void SetAwake(bool flag) { KGE_ASSERT(body_); body_->SetAwake(flag); }
bool IsSleepingAllowed() const { KGE_ASSERT(body_); return body_->IsSleepingAllowed(); }
void SetSleepingAllowed(bool flag) { KGE_ASSERT(body_); body_->SetSleepingAllowed(flag); }
// <20>״̬
bool IsActive() const { KGE_ASSERT(body_); return body_->IsActive(); }
void SetActive(bool flag) { KGE_ASSERT(body_); body_->SetActive(flag); }
2019-10-18 11:50:46 +08:00
2019-10-22 16:49:34 +08:00
Actor* GetActor() const { return actor_; }
void SetActor(Actor* actor) { actor_ = actor; }
2019-10-18 11:50:46 +08:00
2019-10-22 16:49:34 +08:00
b2Body* GetB2Body() { return body_; }
const b2Body* GetB2Body() const { return body_; }
2019-10-18 11:50:46 +08:00
void SetB2Body(b2Body* body);
2019-10-30 23:12:18 +08:00
PhysicWorld* GetWorld() { return world_; }
const PhysicWorld* GetWorld() const { return world_; }
void Destroy();
2019-10-18 11:50:46 +08:00
void UpdateActor();
void UpdateFromActor();
2019-10-31 20:34:38 +08:00
protected:
void UpdateFixtureFilter(b2Fixture* fixture);
2019-10-18 11:50:46 +08:00
protected:
Actor* actor_;
2019-10-30 23:12:18 +08:00
PhysicWorld* world_;
2019-10-18 11:50:46 +08:00
b2Body* body_;
2019-10-31 20:34:38 +08:00
uint16_t category_bits_;
uint16_t mask_bits_;
int16_t group_index_;
2019-10-18 11:50:46 +08:00
};
}
}