完成碰撞检测

This commit is contained in:
aozhiwei 2019-03-18 19:31:49 +08:00
parent e232db9547
commit 15df4fa1b2
4 changed files with 83 additions and 7 deletions

View File

@ -1,9 +1,77 @@
#include "precompile.h"
#include <algorithm>
#include "entity.h"
#include "collider.h"
static bool IntersectAabbCircle(AabbCollider* a, CircleCollider* b)
{
if (b->pos.x >= a->_min.x && b->pos.x <= a->_max.x &&
b->pos.y >= a->_min.y && b->pos.y <= a->_max.y) {
return true;
}
Vector2D nearest_point(a8::Clamp(b->pos.x, a->_min.x, a->_max.y),
a8::Clamp(b->pos.y, a->_min.y, a->_max.y));
Vector2D i = b->pos - nearest_point;
float n = a8::LengthSqr(i);
return n < b->rad*b->rad;
}
static bool IntersectAabbAabb(AabbCollider* a, AabbCollider* b)
{
Vector2D a_v = (a->_max - a->_min) * 0.5f;
Vector2D a_center = a->_min + a_v;
Vector2D b_v = (b->_max - b->_min) * 0.5f;
Vector2D b_center = b->_min + b_v;
Vector2D z = b_center - a_center;
float u = a_v.x + b_v.x - std::abs(z.x);
return u > 0;
}
static bool IntersectCircleCircle(CircleCollider* a, CircleCollider* b)
{
float t = a->rad + b->rad;
Vector2D d = b->pos - a->pos;
float n = a8::LengthSqr(d);
return n < t*t;
}
bool ColliderComponent::Intersect(ColliderComponent* b)
{
switch (type) {
case CT_Aabb:
{
switch (b->type) {
case CT_Aabb:
{
return IntersectAabbAabb((AabbCollider*)this, (AabbCollider*)b);
}
break;
case CT_Circle:
{
return IntersectAabbCircle((AabbCollider*)this, (CircleCollider*)b);
}
break;
}
};
break;
case CT_Circle:
{
switch (b->type) {
case CT_Aabb:
{
return IntersectAabbCircle((AabbCollider*)b, (CircleCollider*)this);
}
break;
case CT_Circle:
{
return IntersectCircleCircle((CircleCollider*)this, (CircleCollider*)b);
}
break;
}
}
break;
}
return false;
}

View File

@ -40,12 +40,15 @@ Player* Room::GetPlayerByAccountId(const std::string& accountid)
}
Player* Room::GetPlayerByUniId(unsigned short uniid)
{
Entity* entity = GetEntityByUniId(uniid);
return entity->entity_type == ET_Player && entity->entity_subtype == EST_Player ? (Player*)entity : nullptr;
}
Entity* Room::GetEntityByUniId(unsigned short uniid)
{
auto itr = uniid_hash_.find(uniid);
return itr != uniid_hash_.end() &&
itr->second->entity_type == ET_Player &&
itr->second->entity_subtype == EST_Player ?
(Player*)itr->second : nullptr;
return itr != uniid_hash_.end() ? itr->second : nullptr;
}
int Room::AliveCount()
@ -65,7 +68,8 @@ void Room::AddPlayer(Player* hum)
unsigned short Room::AllocUniid()
{
return ++current_uniid;
while (GetEntityByUniId(++current_uniid)) {}
return current_uniid;
}
void Room::ShuaAndroid()
@ -90,6 +94,9 @@ void Room::ShuaAndroid()
bool Room::RandomPos(Human* hum, float distance, Vector2D& out_pos)
{
for (auto& pair : uniid_hash_) {
}
return false;
}

View File

@ -36,12 +36,13 @@ public:
int AliveCount();
Player* GetPlayerByAccountId(const std::string& accountid);
Player* GetPlayerByUniId(unsigned short uniid);
Entity* GetEntityByUniId(unsigned short uniid);
void AddPlayer(Player* hum);
void AddBullet(Bullet* bullet);
unsigned short AllocUniid();
void ShuaAndroid();
bool RandomPos(Human* hum, float distance, Vector2D& out_pos);
Human* SearchEnemy(Human* hum);
void AddBullet(Bullet* bullet);
private:
unsigned short current_uniid = 0;

@ -1 +1 @@
Subproject commit e2f4d2c9a4bb653ad6b37413d3dc3d9ca217a8de
Subproject commit 1eaf2da087ab028b1b859551605bdf8a32f683e5