Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cocos/physics/CCPhysicsBody.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
21 changes: 13 additions & 8 deletions cocos/physics/CCPhysicsJoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
18 changes: 12 additions & 6 deletions cocos/physics/CCPhysicsShape.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
33 changes: 32 additions & 1 deletion cocos/physics/CCPhysicsWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class PhysicsWorldCallback
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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions cocos/physics/CCPhysicsWorld.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class PhysicsWorld
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*/
Expand Down
51 changes: 33 additions & 18 deletions samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand Down Expand Up @@ -92,6 +92,7 @@ PhysicsDemo::PhysicsDemo()
: _scene(nullptr)
, _ball(nullptr)
, _spriteTexture(nullptr)
, _mouse(nullptr)
{

}
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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();
Expand All @@ -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);
Expand All @@ -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));
Expand Down Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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";
Expand Down
18 changes: 14 additions & 4 deletions samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,18 @@ class PhysicsDemo : public BaseTest
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
Expand Down Expand Up @@ -99,10 +104,15 @@ class PhysicsDemoRayCast : public PhysicsDemo

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