#ifndef linePhysics_hpp
#define linePhysics_hpp
#include <stdio.h>
#include <iostream>
using namespace std;
#include "cocos2d.h"
USING_NS_CC;
struct _segment{
Point p1;
Point p2;
};
class LinePhysica : public Layer {
public:
static Scene* createScene();
bool init();
CREATE_FUNC(LinePhysica);
virtual bool onTouchBegan(Touch *touch, Event *unused_event);
virtual void onTouchMoved(Touch *touch, Event *unused_event);
virtual void onTouchEnded(Touch *touch, Event *unused_event);
virtual void onTouchCancelled(Touch *touch, Event *unused_event);
//一帧调用一次
// virtual void draw(Renderer *renderer, const Mat4 &transform, uint32_t flags);
// 或
void update(float dt);
void setPhysicsWorld(PhysicsWorld* world){_world = world;}
private:
Point pre_point;
Point cur_point;
_segment seg;
std::vector<_segment> segment;
PhysicsWorld* _world;
};
#endif /* linePhysics_hpp */
#include "linePhysics.hpp"
Scene* LinePhysica::createScene()
{
auto scene = Scene::createWithPhysics();
auto layer = LinePhysica::create();
layer->setPhysicsWorld(scene->getPhysicsWorld());
scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
//设置重力
scene->getPhysicsWorld()->setGravity(Vec2(0, -320));
scene->addChild(layer);
return scene;
}
bool LinePhysica::init()
{
if (!Layer::init()) {
return false;
}
auto visiableSize = Director::getInstance()->getVisibleSize();
auto body = PhysicsBody::createEdgeBox(visiableSize,PHYSICSBODY_MATERIAL_DEFAULT,5);
//设置恢复力和摩擦力
body->getShape(0)->setRestitution(1.f);
body->getShape(0)->setFriction(0);
//创建一个碰撞图形
auto edgeShape = Node::create();
//将图形和刚刚创建的世界绑定
// edgeShape->addComponent(body);
edgeShape->setPhysicsBody(body);
//设置图形的位置在屏幕中间
edgeShape->setPosition(Vec2(visiableSize.width/2, visiableSize.height/2));
//添加涂层
addChild(edgeShape);
auto sp = Sprite::create("ball.png");
auto body2 = PhysicsBody::PhysicsBody::createCircle(16);
// body2->setMass(2);
body2->setDynamic(true);
body2->getShape(0)->setRestitution(1);
// body2->getShape(0)->setFriction(0.2f);
body2->setCategoryBitmask(1);//分类掩码
body2->setCollisionBitmask(1);//碰撞掩码
body2->setContactTestBitmask(1);//接触测试掩码
sp->setPhysicsBody(body2);
sp->setPosition(Vec2(visiableSize.width/2, visiableSize.height/2));
this->addChild(sp);
auto touch_listener = EventListenerTouchOneByOne::create();
touch_listener->onTouchBegan = CC_CALLBACK_2(LinePhysica::onTouchBegan, this);
touch_listener->onTouchMoved = CC_CALLBACK_2(LinePhysica::onTouchMoved, this);
touch_listener->onTouchEnded = CC_CALLBACK_2(LinePhysica::onTouchEnded, this);
touch_listener->onTouchCancelled = CC_CALLBACK_2(LinePhysica::onTouchCancelled, this);
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(touch_listener, this);
this->scheduleUpdate();
return true;
}
bool LinePhysica::onTouchBegan(Touch *touch, Event *unused_event)
{
cout<<"begin!"<<endl;
Point location = touch->getLocation();
pre_point = cur_point = location;
return true;
}
void LinePhysica::onTouchMoved(Touch *touch, Event *unused_event)
{
cout<<"moved!"<<endl;
Point location = touch->getLocation();
cur_point = location;
if((pre_point - cur_point).getLengthSq()>25){
seg.p1=pre_point;
seg.p2=cur_point;
segment.push_back(seg);
pre_point=cur_point;
}
}
void LinePhysica::onTouchEnded(Touch *touch, Event *unused_event)
{
Point location = touch->getLocation();
cur_point = location;
if((pre_point - cur_point).getLengthSq()>25){
seg.p1=pre_point;
seg.p2=cur_point;
segment.push_back(seg);
pre_point=cur_point;
}
cout<<"end!"<<endl;
}
void LinePhysica::onTouchCancelled(Touch *touch, Event *unused_event)
{
}
//void LinePhysica::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
//
//{
//
DrawPrimitives::setDrawColor4B(0, 255, 255, 255);
glLineWidth(4);
// for (std::vector<_segment >::const_iterator i=segment.begin(); i!=segment.end(); i++)
// {
DrawPrimitives::drawLine(i->p1, i->p2);
创建一个静态的线状刚体.
// auto lineBody = PhysicsBody::createEdgeSegment(i->p1, i->p2);
// auto edgeNode = Node::create();
// edgeNode->setPhysicsBody(lineBody);
// addChild(edgeNode);
// }
// segment.clear();
//
//}
void LinePhysica::update(float dt)
{
for (std::vector<_segment >::const_iterator i=segment.begin(); i!=segment.end(); i++)
{
// DrawPrimitives::drawLine(i->p1, i->p2);
// 创建一个静态的线状刚体.
auto lineBody = PhysicsBody::createEdgeSegment(i->p1, i->p2);
auto edgeNode = Node::create();
edgeNode->setPhysicsBody(lineBody);
addChild(edgeNode);
}
segment.clear();
}
可以参考https://blog.csdn.net/llkk890320/article/details/25886613
cocos2d-x 3.0开发笔记---物理引擎封装 Physics深入学习