coco2d-x 中box2d选中刚体

时间:2022-09-01 18:36:31

普通的选中这里就不说了,box2d中提供了,一个比较效率比较高的快速选中类,那就是b2QueryCallback

#ifndef QueryCallback_H
#define QueryCallback_H

#include <Box2D/Box2D.h>
class QueryCallback :
	public b2QueryCallback
{
public:
	QueryCallback(void);
	QueryCallback(const b2Vec2& point);
	~QueryCallback(void);
	bool ReportFixture(b2Fixture* fixture);


	b2Vec2 m_point;
	b2Fixture* m_fixture;
	std::vector<b2Fixture*> m_vectFixture;
};
#endif

#include "QueryCallback.h"


QueryCallback::QueryCallback(void)
{

}

QueryCallback::QueryCallback(const b2Vec2& point)
{
	m_point = point;
	m_fixture = NULL;
}

QueryCallback::~QueryCallback(void)
{
}

bool QueryCallback::ReportFixture(b2Fixture* fixture)
{
	b2Body* body = fixture->GetBody();
	if (body->GetType() == b2_dynamicBody)
	{
		bool inside = fixture->TestPoint(m_point);
		if (inside)
		{
			m_fixture = fixture;
			m_vectFixture.push_back(fixture);
			// We are done, terminate the query.
			return true;
		}
	}

	// Continue the query.
	return true;
}

通过这个类就可以快速的选中某个刚体回调,效率也比较高