Merge pull request #4002 from boyu0/iss2771_physical

issue #2771 physical
This commit is contained in:
James Chen 2013-10-24 02:31:40 -07:00
commit 10329de46c
7 changed files with 106 additions and 38 deletions

View File

@ -43,7 +43,7 @@ class PhysicsJoint;
class PhysicsBodyInfo;
const PhysicsMaterial PHYSICSBODY_MATERIAL_DEFAULT = {1.0f, 1.0f, 1.0f};
const PhysicsMaterial PHYSICSBODY_MATERIAL_DEFAULT(1.0f, 1.0f, 1.0f);
/**
* A body affect by physics.

View File

@ -70,16 +70,21 @@ bool PhysicsJoint::init(cocos2d::PhysicsBody *a, cocos2d::PhysicsBody *b)
{
do
{
CC_BREAK_IF(a == nullptr || b == nullptr);
CC_BREAK_IF(!(_info = new PhysicsJointInfo(this)));
_bodyA = a;
_bodyA->retain();
_bodyA->_joints.push_back(this);
_bodyB = b;
_bodyB->retain();
_bodyB->_joints.push_back(this);
if (a != nullptr)
{
_bodyA = a;
_bodyA->retain();
_bodyA->_joints.push_back(this);
}
if (b != nullptr)
{
_bodyB = b;
_bodyB->retain();
_bodyB->_joints.push_back(this);
}
return true;
} while (false);

View File

@ -44,14 +44,20 @@ typedef struct PhysicsMaterial
float restitution;
float friction;
static PhysicsMaterial make(float density, float restitution, float friction)
{
PhysicsMaterial var = {density, restitution, friction};
return var;
}
PhysicsMaterial()
: density(0.0f)
, restitution(0.0f)
, friction(0.0f)
{}
PhysicsMaterial(float density, float restitution, float friction)
: density(density)
, restitution(restitution)
, friction(friction)
{}
}PhysicsMaterial;
const PhysicsMaterial PHYSICSSHAPE_MATERIAL_DEFAULT = {0.0f, 1.0f, 1.0f};
const PhysicsMaterial PHYSICSSHAPE_MATERIAL_DEFAULT(0.0f, 1.0f, 1.0f);
/**
* @brief A shape for body. You do not create PhysicsWorld objects directly, instead, you can view PhysicsBody to see how to create it.

View File

@ -88,6 +88,7 @@ public:
static void collisionSeparateCallbackFunc(cpArbiter *arb, cpSpace *space, PhysicsWorld *world);
static void rayCastCallbackFunc(cpShape *shape, cpFloat t, cpVect n, RayCastCallbackInfo *info);
static void rectQueryCallbackFunc(cpShape *shape, RectQueryCallbackInfo *info);
static void nearestPointQueryFunc(cpShape *shape, cpFloat distance, cpVect point, Array *arr);
public:
static bool continues;
@ -166,6 +167,15 @@ void PhysicsWorldCallback::rectQueryCallbackFunc(cpShape *shape, RectQueryCallba
info->data);
}
void PhysicsWorldCallback::nearestPointQueryFunc(cpShape *shape, cpFloat distance, cpVect point, Array *arr)
{
auto it = PhysicsShapeInfo::map.find(shape);
CC_ASSERT(it != PhysicsShapeInfo::map.end());
arr->addObject(it->second->shape);
}
bool PhysicsWorld::init()
{
_info = new PhysicsWorldInfo();
@ -561,9 +571,30 @@ void PhysicsWorld::rectQuery(PhysicsRectQueryCallback& callback, Rect rect, void
}
}
Array* getShapesAtPoint(Point point)
Array* PhysicsWorld::getShapesAtPoint(Point point)
{
Array* arr = Array::create();
cpSpaceNearestPointQuery(this->_info->space,
PhysicsHelper::point2cpv(point),
0,
CP_ALL_LAYERS,
CP_NO_GROUP,
(cpSpaceNearestPointQueryFunc)PhysicsWorldCallback::nearestPointQueryFunc,
arr);
return arr;
}
PhysicsShape* PhysicsWorld::getShapeAtPoint(Point point)
{
cpShape* shape = cpSpaceNearestPointQueryNearest(this->_info->space,
PhysicsHelper::point2cpv(point),
0,
CP_ALL_LAYERS,
CP_NO_GROUP,
nullptr);
return shape == nullptr ? nullptr : PhysicsShapeInfo::map.find(shape)->second->shape;
}
Array* PhysicsWorld::getAllBody() const

View File

@ -98,6 +98,7 @@ public:
void rayCast(PhysicsRayCastCallback& callback, Point point1, Point point2, void* data);
void rectQuery(PhysicsRectQueryCallback& callback, Rect rect, void* data);
Array* getShapesAtPoint(Point point);
PhysicsShape* getShapeAtPoint(Point point);
Array* getAllBody() const;
/** Register a listener to receive contact callbacks*/

View File

@ -50,7 +50,7 @@ namespace
return layer;
}
static const Color4F STATIC_COLOR = {1.0f, 0.0f, 0.0f, 1.0f};
static const Color4F STATIC_COLOR(1.0f, 0.0f, 0.0f, 1.0f);
}
@ -92,6 +92,7 @@ PhysicsDemo::PhysicsDemo()
: _scene(nullptr)
, _ball(nullptr)
, _spriteTexture(nullptr)
, _mouse(nullptr)
{
}
@ -296,7 +297,8 @@ namespace
-25,-8,0,63,-57,-29,-4,-1,-1,-13,-4,127,-64,31,-2,0,15,103,-1,-1,-57,-8,127,
-97,-25,-8,0,63,-61,-61,-4,127,-1,-29,-4,127,-64,15,-8,0,0,55,-1,-1,-121,-8,
127,-97,-25,-8,0,63,-61,-61,-4,127,-1,-29,-4,63,-64,15,-32,0,0,23,-1,-2,3,-16,
63,15,-61,-16,0,31,-127,-127,-8,31,-1,-127,-8,31,-128,7,-128,0,0};
63,15,-61,-16,0,31,-127,-127,-8,31,-1,-127,-8,31,-128,7,-128,0,0
};
static inline int get_pixel(int x, int y)
{
@ -365,6 +367,31 @@ Sprite* PhysicsDemo::makeTriangle(float x, float y, Size size, PhysicsMaterial m
return triangle;
}
void PhysicsDemo::onTouchesBegan(const std::vector<Touch*>& touches, Event* event)
{
for( auto &touch: touches)
{
auto location = touch->getLocation();
Array* arr = _scene->getPhysicsWorld()->getShapesAtPoint(location);
PhysicsShape* shape = nullptr;
for (Object* obj : *arr)
{
}
}
}
void PhysicsDemo::onTouchesMoved(const std::vector<Touch*>& touches, Event* event)
{
}
void PhysicsDemo::onTouchesEnded(const std::vector<Touch*>& touches, Event* event)
{
}
void PhysicsDemoLogoSmash::onEnter()
{
PhysicsDemo::onEnter();
@ -384,7 +411,7 @@ void PhysicsDemoLogoSmash::onEnter()
Node* ball = makeBall(2*(x - logo_width/2 + x_jitter) + VisibleRect::getVisibleRect().size.width/2,
2*(logo_height-y + y_jitter) + VisibleRect::getVisibleRect().size.height/2 - logo_height/2,
0.95f, PhysicsMaterial::make(1.0f, 0.0f, 0.0f));
0.95f, PhysicsMaterial(1.0f, 0.0f, 0.0f));
ball->getPhysicsBody()->setMass(1.0);
ball->getPhysicsBody()->setMoment(PHYSICS_INFINITY);
@ -396,7 +423,7 @@ void PhysicsDemoLogoSmash::onEnter()
}
auto bullet = makeBall(400, 0, 10, PhysicsMaterial::make(PHYSICS_INFINITY, 0, 0));
auto bullet = makeBall(400, 0, 10, PhysicsMaterial(PHYSICS_INFINITY, 0, 0));
bullet->getPhysicsBody()->setVelocity(Point(400, 0));
bullet->setPosition(Point(-1000, VisibleRect::getVisibleRect().size.height/2));
@ -427,7 +454,7 @@ void PhysicsDemoPyramidStack::onEnter()
{
for(int j=0; j<=i; j++)
{
addGrossiniAtPosition(VisibleRect::bottom() + Point((i/2 - j) * 11, (14 - i) * 23 + 100), 0.2);
addGrossiniAtPosition(VisibleRect::bottom() + Point((i/2 - j) * 11, (14 - i) * 23 + 100), 0.2f);
}
}
}
@ -644,7 +671,7 @@ void PhysicsDemoRayCast::update(float delta)
break;
}
_angle += 0.25f * M_PI / 180.0f;
_angle += 0.25f * (float)M_PI / 180.0f;
}
void PhysicsDemoRayCast::onTouchesEnded(const std::vector<Touch*>& touches, Event* event)
@ -687,18 +714,6 @@ void PhysicsDemoJoints::onEnter()
}
void PhysicsDemoJoints::onTouchesEnded(const std::vector<Touch*>& touches, Event* event)
{
//Add a new body/atlas sprite at the touched location
for( auto &touch: touches)
{
auto location = touch->getLocation();
}
}
std::string PhysicsDemoJoints::title()
{
return "Joints";

View File

@ -37,13 +37,18 @@ public:
void toggleDebugCallback(Object* sender);
void addGrossiniAtPosition(Point p, float scale = 1.0);
Sprite* makeBall(float x, float y, float radius, PhysicsMaterial material = {1.0f, 1.0f, 1.0f});
Sprite* makeBox(float x, float y, Size size, PhysicsMaterial material = {1.0f, 1.0f, 1.0f});
Sprite* makeTriangle(float x, float y, Size size, PhysicsMaterial material = {1.0f, 1.0f, 1.0f});
Sprite* makeBall(float x, float y, float radius, PhysicsMaterial material = PhysicsMaterial(1.0f, 1.0f, 1.0f));
Sprite* makeBox(float x, float y, Size size, PhysicsMaterial material = PhysicsMaterial(1.0f, 1.0f, 1.0f));
Sprite* makeTriangle(float x, float y, Size size, PhysicsMaterial material = PhysicsMaterial(1.0f, 1.0f, 1.0f));
void onTouchesBegan(const std::vector<Touch*>& touches, Event* event) override;
void onTouchesMoved(const std::vector<Touch*>& touches, Event* event) override;
void onTouchesEnded(const std::vector<Touch*>& touches, Event* event) override;
protected:
Texture2D* _spriteTexture; // weak ref
SpriteBatchNode* _ball;
DrawNode* _mouse;
};
class PhysicsDemoClickAdd : public PhysicsDemo
@ -99,10 +104,15 @@ private:
class PhysicsDemoJoints : public PhysicsDemo
{
public:
PhysicsDemoJoints();
public:
void onEnter() override;
std::string title() override;
void onTouchesEnded(const std::vector<Touch*>& touches, Event* event) override;
private:
PhysicsShape* _touchesShape;
};
#endif