1
PhysicsJoint的使用

T09Join.h

#ifndef__T09Joint_H__

#define__T09Joint_H__

#include"T32.h"

classT09Joint:
publicLayer

{

public:

CREATE_FUNC(T09Joint);

voidonEnter();

PhysicsWorld*getPhysicsWorld()

{

return((Scene*)getParent())->getPhysicsWorld();

}

Sprite*_A;

Sprite*_B;

Sprite*_C;

};

#endif

T09Join.cpp

#include"T09Join.h"

voidT09Joint::onEnter()

{

Layer::onEnter();

PhysicsBody*bodyA;

PhysicsBody*bodyB;

{

PhysicsBody*body=
PhysicsBody::createCircle(20);

bodyA=
body;

Sprite*sprite=
Sprite::create();

_A=
sprite;

sprite->setContentSize(Size(40,40));

sprite->setPhysicsBody(body);

sprite->setPosition(winSize.width/
2 - 50, winSize.height/
2 + 100);

addChild(sprite);

}

{

//PhysicsBody*body
= PhysicsBody::createEdgeBox(Size(40, 40));

PhysicsBody*body=
PhysicsBody::createBox(Size(40,40));

bodyB=
body;

Sprite*sprite=
Sprite::create();

sprite->setContentSize(Size(40,40));

_B=
sprite;

sprite->setPhysicsBody(body);

sprite->setPosition(winSize.width/
2 + 50, winSize.height/
2 + 100);

addChild(sprite);

}

{

PhysicsBody*body=
PhysicsBody::createEdgeBox(winSize,PhysicsMaterial(1.0f,1.0f,
0.0f));

Sprite*sprite=
Sprite::create();

addChild(sprite);

sprite->setPhysicsBody(body);

sprite->setPosition(winSize.width/
2, winSize.height/
2);

}

//
PhysicsJoint*joint = PhysicsJointDistance::construct(bodyA, bodyB, Vec2(0, 0),Vec2(0, 0));

//
PhysicsJoint*joint = PhysicsJointSpring::construct(bodyA, bodyB, Vec2(0, 0),Vec2(0, 0), 500.0f, 500.0f);

//
PhysicsJoint*joint = PhysicsJointPin::construct(bodyA, bodyB,Vec2(winSize.width / 2, winSize.height / 2));

PhysicsJoint*joint=
PhysicsJointMotor::construct(bodyA,bodyB,1.0);

{

autoev=
EventListenerTouchOneByOne::create();

ev->onTouchBegan=
[&](Touch*touch,Event*){

Pointpt=
touch->getLocation();

if(_A->getBoundingBox().containsPoint(pt));

{

_C=
_A;

returntrue;

}

if(_B->getBoundingBox().containsPoint(pt))

{

_C=
_B;

returntrue;

}

returnfalse;

};

ev->onTouchEnded=
[&](Touch*pTouch,Event*){

Vec2pt=
pTouch->getLocation()-
pTouch->getStartLocation();

_C->getPhysicsBody()->setVelocity(Vec2(pt));

};

}

}

05-11 11:09