1 3.0物理系统PhysicsWorld

T07PhysicsWorld.h

#ifndef __T07PhysicsWorld_H__

#define __T07PhysicsWorld_H__

#include "T32.h"

class T07PhysicsWorld : public Layer

{

public:

CREATE_FUNC(T07PhysicsWorld);

bool init();

Scene* getScene(){ return (Scene*)getParent(); }

};

#endif

T07PhysicsWorld.cpp

#include "T07PhysicsWorld.h"

bool T07PhysicsWorld::init()

{

Layer::init();

PhysicsBody* bodyA;

PhysicsBody* bodyB;

{

//后面的三个参数值分别表示的是:密度,弹性值,摩擦力

PhysicsBody* body = PhysicsBody::createCircle(20, PhysicsMaterial(1.0f, 1.0f, 0.0f));

bodyA = body;

//创建精灵

Sprite* sprite = Sprite::create();

//设置

sprite->setPhysicsBody(body);

addChild(sprite);

//设置精灵的位置

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

//设置速度

body->setVelocity(Vect(100, 200));

}

{

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

bodyB = body;

Sprite* sprite = Sprite::create();

addChild(sprite);

sprite->setPhysicsBody(body);

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

}

{

bodyA->setContactTestBitmask(0x1);

bodyB->setContactTestBitmask(0x1);

bodyA->setGroup(1);

bodyB->setGroup(2);

//设置精灵的

auto ev = EventListenerPhysicsContactWithBodies::create(bodyA, bodyB);

ev->onContactBegin = [](PhysicsContact& contact){

CCLog("Began Contact...........");

return true;

};

_eventDispatcher->addEventListenerWithSceneGraphPriority(ev, this);

}

{

auto ev = EventListenerPhysicsContactWithShapes::create(bodyA->getShapes().at(0),

bodyB->getShapes().at(0));

ev->onContactBegin = [](PhysicsContact& contact){

CCLog("Shape Began Contact...........");

return true;

};

_eventDispatcher->addEventListenerWithSceneGraphPriority(ev, this);

}

{

auto ev = EventListenerPhysicsContactWithGroup::create(3);

ev->onContactBegin = [](PhysicsContact& contact){

CCLog("Group Began Contact...........");

return true;

};

_eventDispatcher->addEventListenerWithSceneGraphPriority(ev, this);

}

return true;

}

TMenu.h

#ifndef __TMenu_H__

#define __TMenu_H__

#include "T32.h"

class TMenu : public Layer

{

public:

CREATE_FUNC(TMenu);

bool init();

bool TouchBegan(Touch*, Event*);

};

#endif

TMenu.cpp

#include "TMenu.h"

#include "TBack.h"

#include "T01CPP11.h"

#include "T02Vector.h"

#include "T03Map.h"

#include "T04Label.h"

#include "T05Touch.h"

#include "T06Box2D.h"

#include "T07PhysicsWorld.h"

static const char* title[] = {

"T01CPP11",

"T02Vector",

"T04Label",

"T05Touch",

"T06Box2D",

"T07PhysicsWorld"

};

bool TMenu::init()

{

Layer::init();

Menu* menu = Menu::create();

addChild(menu);

for (int i = 0; i < sizeof(title) / sizeof(*title); ++i)

{

MenuItemFont* item = MenuItemFont::create(title[i], [](Ref* sender){

MenuItem* item = (MenuItem*)sender;

int i = item->getTag() - 1000;

Layer* l = NULL;

if (title[i] == "T01CPP11")   l = T01CPP11::create();

if (title[i] == "T02Vector")   l = T02Vector::create();

if (title[i] == "T04Label") l = T04Label::create();

if (title[i] == "T05Touch") l = T05Touch::create();

if (title[i] == "T06Box2D") l = T06Box2D::create();

if (title[i] == "T07PhysicsWorld") l = T07PhysicsWorld::create();

if (l)

{

TBack* b = TBack::create();

Scene* s = Scene::createWithPhysics();

PhysicsWorld* world = s->getPhysicsWorld();

world->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);

s->addChild(b);

s->addChild(l);

Director::getInstance()->pushScene(s);

}

});

menu->addChild(item);

item->setTag(1000 + i);

}

menu->alignItemsVertically();

// 触摸

auto ev = EventListenerTouchOneByOne::create();

#if 0

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

return true;

};

#endif

//ev->onTouchBegan = std::bind(&TMenu::TouchBegan, this, std::placeholders::_1, std::placeholders::_2);

ev->onTouchBegan = CC_CALLBACK_2(TMenu::TouchBegan, this);

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

setPositionY(getPositionY() + touch->getDelta().y);

};

_eventDispatcher->addEventListenerWithSceneGraphPriority(ev, this);

return true;

}

bool TMenu::TouchBegan(/*TMEnu* this, */Touch*, Event*)

{

return true;

}

运行结果:

射线的做法

T08RayCast.h

#ifndef __T08RayCast_H__

#define __T08RayCast_H__

#include "T32.h"

class T08RayCast :public Layer

{

public:

CREATE_FUNC(T08RayCast);

bool init();

void onEnter();

void update(float);

Sprite* _cat;

int _angle;

int _distance;

float _nearDis;

PhysicsShape* _food;

DrawNode* _drawNode;

};

#endif

T08RayCast.cpp

#include "T08RayCast.h"

void T08RayCast::onEnter()

{

Layer::onEnter();

Scene* scene = (Scene*)getParent();

scene->getPhysicsWorld()->setGravity(Vec2(0, 0));

}

bool T08RayCast::init()

{

Layer::init();

// 创建猫,猫不是Body,只是一个简单的精灵

{

Sprite* cat = Sprite::create("CloseNormal.png");

addChild(cat);

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

_cat = cat;

}

// 投放食物,食物必须是body

{

auto ev = EventListenerTouchOneByOne::create();

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

//得到触摸点

Vec2 pt = touch->getLocation();

//创建一个圆形的PhysicsBody

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

Sprite* sprite = Sprite::create();

sprite->setPhysicsBody(body);

addChild(sprite);

sprite->setPosition(pt);

return true;

};

_eventDispatcher->addEventListenerWithSceneGraphPriority(ev, this);

}

{

_angle = 0;      //角度

_distance = 100; //距离

_nearDis = _distance + 100;

_food = NULL;

_drawNode = NULL;

}

scheduleUpdate();

return true;

}

void T08RayCast::update(float dt)

{

Scene* scene = (Scene*)getParent();

PhysicsWorld* world = scene->getPhysicsWorld();

//获得猫的位置

Vec2 start = _cat->getPosition();

Vec2 end;

//当前时刻扫描到的终点位置

end.x = start.x + sinf(_angle / 180.0 * M_PI)*_distance;

end.y = start.y + cosf(_angle / 180.0 * M_PI)*_distance;

// 显示扫描距离

if (_drawNode) _drawNode->removeFromParent();

//下面的代码用于画线段

_drawNode = DrawNode::create();

_drawNode->drawSegment(start, end, 1, Color4F(1, 0, 0, 1));

addChild(_drawNode);

// 扫描回调函数

//bool(PhysicsWorld& world, const PhysicsRayCastInfo& info, void* data)

auto callback = [&](PhysicsWorld& world, const PhysicsRayCastInfo& info, void* data){

if (info.shape == NULL)

return true;

//如果点包含猫的点

float dis = info.contact.getDistance(_cat->getPosition());

if (dis < _nearDis)

{

_nearDis = dis;

_food = info.shape;

}

// 扫描到一个就不要再继续了

return false;

};

//通过rayCast画一条射线

world->rayCast(callback, start, end, NULL);

//每次角度加2

_angle += 2;

//如果角度为360

if (_angle == 360)

{

//如果存在食物

if (_food)

{

// 吃掉食物

Node* node = _food->getBody()->getNode();

//将猫的的位置数值到新的位置

_cat->setPosition(node->getPosition());

node->removeFromParent();

_food = NULL;

_nearDis = _distance + 100;

}

_angle = 0;

}

}

运行结果:

1 3.0物理系统PhysicsWorld

T07PhysicsWorld.h

#ifndef __T07PhysicsWorld_H__

#define __T07PhysicsWorld_H__

#include "T32.h"

class T07PhysicsWorld : public Layer

{

public:

CREATE_FUNC(T07PhysicsWorld);

bool init();

Scene* getScene(){ return (Scene*)getParent(); }

};

#endif

T07PhysicsWorld.cpp

#include "T07PhysicsWorld.h"

bool T07PhysicsWorld::init()

{

Layer::init();

PhysicsBody* bodyA;

PhysicsBody* bodyB;

{

//后面的三个参数值分别表示的是:密度,弹性值,摩擦力

PhysicsBody* body = PhysicsBody::createCircle(20, PhysicsMaterial(1.0f, 1.0f, 0.0f));

bodyA = body;

//创建精灵

Sprite* sprite = Sprite::create();

//设置

sprite->setPhysicsBody(body);

addChild(sprite);

//设置精灵的位置

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

//设置速度

body->setVelocity(Vect(100, 200));

}

{

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

bodyB = body;

Sprite* sprite = Sprite::create();

addChild(sprite);

sprite->setPhysicsBody(body);

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

}

{

bodyA->setContactTestBitmask(0x1);

bodyB->setContactTestBitmask(0x1);

bodyA->setGroup(1);

bodyB->setGroup(2);

//设置精灵的

auto ev = EventListenerPhysicsContactWithBodies::create(bodyA, bodyB);

ev->onContactBegin = [](PhysicsContact& contact){

CCLog("Began Contact...........");

return true;

};

_eventDispatcher->addEventListenerWithSceneGraphPriority(ev, this);

}

{

auto ev = EventListenerPhysicsContactWithShapes::create(bodyA->getShapes().at(0),

bodyB->getShapes().at(0));

ev->onContactBegin = [](PhysicsContact& contact){

CCLog("Shape Began Contact...........");

return true;

};

_eventDispatcher->addEventListenerWithSceneGraphPriority(ev, this);

}

{

auto ev = EventListenerPhysicsContactWithGroup::create(3);

ev->onContactBegin = [](PhysicsContact& contact){

CCLog("Group Began Contact...........");

return true;

};

_eventDispatcher->addEventListenerWithSceneGraphPriority(ev, this);

}

return true;

}

TMenu.h

#ifndef __TMenu_H__

#define __TMenu_H__

#include "T32.h"

class TMenu : public Layer

{

public:

CREATE_FUNC(TMenu);

bool init();

bool TouchBegan(Touch*, Event*);

};

#endif

TMenu.cpp

#include "TMenu.h"

#include "TBack.h"

#include "T01CPP11.h"

#include "T02Vector.h"

#include "T03Map.h"

#include "T04Label.h"

#include "T05Touch.h"

#include "T06Box2D.h"

#include "T07PhysicsWorld.h"

static const char* title[] = {

"T01CPP11",

"T02Vector",

"T04Label",

"T05Touch",

"T06Box2D",

"T07PhysicsWorld"

};

bool TMenu::init()

{

Layer::init();

Menu* menu = Menu::create();

addChild(menu);

for (int i = 0; i < sizeof(title) / sizeof(*title); ++i)

{

MenuItemFont* item = MenuItemFont::create(title[i], [](Ref* sender){

MenuItem* item = (MenuItem*)sender;

int i = item->getTag() - 1000;

Layer* l = NULL;

if (title[i] == "T01CPP11")   l = T01CPP11::create();

if (title[i] == "T02Vector")   l = T02Vector::create();

if (title[i] == "T04Label") l = T04Label::create();

if (title[i] == "T05Touch") l = T05Touch::create();

if (title[i] == "T06Box2D") l = T06Box2D::create();

if (title[i] == "T07PhysicsWorld") l = T07PhysicsWorld::create();

if (l)

{

TBack* b = TBack::create();

Scene* s = Scene::createWithPhysics();

PhysicsWorld* world = s->getPhysicsWorld();

world->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);

s->addChild(b);

s->addChild(l);

Director::getInstance()->pushScene(s);

}

});

menu->addChild(item);

item->setTag(1000 + i);

}

menu->alignItemsVertically();

// 触摸

auto ev = EventListenerTouchOneByOne::create();

#if 0

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

return true;

};

#endif

//ev->onTouchBegan = std::bind(&TMenu::TouchBegan, this, std::placeholders::_1, std::placeholders::_2);

ev->onTouchBegan = CC_CALLBACK_2(TMenu::TouchBegan, this);

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

setPositionY(getPositionY() + touch->getDelta().y);

};

_eventDispatcher->addEventListenerWithSceneGraphPriority(ev, this);

return true;

}

bool TMenu::TouchBegan(/*TMEnu* this, */Touch*, Event*)

{

return true;

}

运行结果:

射线的做法

T08RayCast.h

#ifndef __T08RayCast_H__

#define __T08RayCast_H__

#include "T32.h"

class T08RayCast :public Layer

{

public:

CREATE_FUNC(T08RayCast);

bool init();

void onEnter();

void update(float);

Sprite* _cat;

int _angle;

int _distance;

float _nearDis;

PhysicsShape* _food;

DrawNode* _drawNode;

};

#endif

T08RayCast.cpp

#include "T08RayCast.h"

void T08RayCast::onEnter()

{

Layer::onEnter();

Scene* scene = (Scene*)getParent();

scene->getPhysicsWorld()->setGravity(Vec2(0, 0));

}

bool T08RayCast::init()

{

Layer::init();

// 创建猫,猫不是Body,只是一个简单的精灵

{

Sprite* cat = Sprite::create("CloseNormal.png");

addChild(cat);

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

_cat = cat;

}

// 投放食物,食物必须是body

{

auto ev = EventListenerTouchOneByOne::create();

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

//得到触摸点

Vec2 pt = touch->getLocation();

//创建一个圆形的PhysicsBody

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

Sprite* sprite = Sprite::create();

sprite->setPhysicsBody(body);

addChild(sprite);

sprite->setPosition(pt);

return true;

};

_eventDispatcher->addEventListenerWithSceneGraphPriority(ev, this);

}

{

_angle = 0;      //角度

_distance = 100; //距离

_nearDis = _distance + 100;

_food = NULL;

_drawNode = NULL;

}

scheduleUpdate();

return true;

}

void T08RayCast::update(float dt)

{

Scene* scene = (Scene*)getParent();

PhysicsWorld* world = scene->getPhysicsWorld();

//获得猫的位置

Vec2 start = _cat->getPosition();

Vec2 end;

//当前时刻扫描到的终点位置

end.x = start.x + sinf(_angle / 180.0 * M_PI)*_distance;

end.y = start.y + cosf(_angle / 180.0 * M_PI)*_distance;

// 显示扫描距离

if (_drawNode) _drawNode->removeFromParent();

//下面的代码用于画线段

_drawNode = DrawNode::create();

_drawNode->drawSegment(start, end, 1, Color4F(1, 0, 0, 1));

addChild(_drawNode);

// 扫描回调函数

//bool(PhysicsWorld& world, const PhysicsRayCastInfo& info, void* data)

auto callback = [&](PhysicsWorld& world, const PhysicsRayCastInfo& info, void* data){

if (info.shape == NULL)

return true;

//如果点包含猫的点

float dis = info.contact.getDistance(_cat->getPosition());

if (dis < _nearDis)

{

_nearDis = dis;

_food = info.shape;

}

// 扫描到一个就不要再继续了

return false;

};

//通过rayCast画一条射线

world->rayCast(callback, start, end, NULL);

//每次角度加2

_angle += 2;

//如果角度为360

if (_angle == 360)

{

//如果存在食物

if (_food)

{

// 吃掉食物

Node* node = _food->getBody()->getNode();

//将猫的的位置数值到新的位置

_cat->setPosition(node->getPosition());

node->removeFromParent();

_food = NULL;

_nearDis = _distance + 100;

}

_angle = 0;

}

}

运行结果:

1.物理系统PhysicsWorld,RayCast相关推荐

  1. Cocos 物理系统

    官方文档链接:https://docs.cocos.com/creator/2.3/manual/zh/physics/physics/physics-manager.html Cocos 物理系统 ...

  2. Games104 Lecture 11 物理系统:高级应用

    物理系统:高级应用 1 角色控制器 1.1 构建一个控制器 1.2 角色控制器的特点和小细节: 2 布娃娃系统 Ragdoll 3 衣料模拟 3.1 基于mesh的衣料模拟 4 破坏模拟 5 载具模拟 ...

  3. [浅析]UE4物理系统

    虚幻引擎4使用 PhysX 3.3 物理引擎来模拟物理效果.所有物理运动(坠落或受力的物理形体)以及碰撞(物理形体的相互作用)都由 PhysX 管理. 一.Physx 1.1Physx简介 UE4.2 ...

  4. UE4 物理系统实现

    虚幻引擎4使用 PhysX 3.3 物理引擎来模拟物理效果.所有物理运动(坠落或受力的物理形体)以及碰撞(物理形体的相互作用)都由 PhysX 管理. 一.Physx 1.1Physx简介 UE4.2 ...

  5. Unity 物理系统

    Unity物理系统 ##1.人机交互 ++1.1.外部输入设备 ++++键盘 ++++触屏 ++++摇杆 ++++鼠标 ++1.2.Input类简介 ++++Input类是输入系统的接口,使用这个类能 ...

  6. 【物理篇】从零搭建2D物理系统①——刚体和碰撞检测事件

    前言 说到unity的物理系统,大家肯定第一反应肯定是"不就是rigidbody和collider那些东西吗,我会".但是提及背后的原理,我敢说99%的人是不知道的.unity的物 ...

  7. 【物理篇】从零搭建2D物理系统③——物体相交测试(完结)

    如何进行相交测试 在第一期文章中,我们最后留下来一个问题:如何判断一条射线和哪些物体相交? 这个问题分为两步来解决: 使用空间场景划分找到可能相交的物体(粗测阶段).(找到1,2,3这三个物体) 对上 ...

  8. 【iOS-Cocos2d游戏开发之十一】使用Box2d物理系统以及在cocos2d框架添加Box2d物理系统lib包的方法...

    为什么80%的码农都做不了架构师?>>>     李华明Himi 原创,转载务必在明显处注明: 转载自 [黑米GameDev街区] 原文链接:  http://www.himigam ...

  9. Cocos2d-x v3.0物理系统 利用PhysicsEditor创建多边形

    Cocos2d-x 3.0的新物理系统我就不必多说了,接触一段时间,感觉还是不错的.对于那些基本概念,网上的教程已经泛滥了,就不多说了,不过对于创建多边形物体的教程,还真不多,很多都是创建圆形和矩形, ...

最新文章

  1. Android 设置透明的方法
  2. tensorflow 加载模型
  3. cocos2dx java 调用lua_Cocos2d-x Lua实现从Android回调到Lua的方法
  4. 人脸识别_云端人脸识别-人脸识别SDK+API-人脸识别闸机解决方案
  5. 【字符串全排列】LeetCode 567. Permutation in String
  6. python random函数shuffle_Python|有趣的shuffle方法
  7. 继续SecureString
  8. Javascript:radio单击触发事件
  9. php100视频教程(全集)
  10. 【Python练习】乌龟吃鱼小游戏
  11. 书摘---创业36条军规3:创业人七大须知
  12. 阿尔克分享平面设计的基本常识,以及ps软件小知识
  13. java转大数据的学习路线
  14. 破解利器C32Asm和IDApro
  15. 周志明虚拟机最新版,大厂面试必备宝典
  16. 计算方法实验(五):高斯列主元消去法
  17. 大家常见的以ESP32为代表的WIFI、蓝牙双模模块,今天启明云端带来了一款低功耗、距离远的LoRa+蓝牙无线通讯模块WT5105-L1,我们一起看看这款模块在功能上有哪些优势?
  18. 百度图片下载器2.0
  19. 我“听”得见你爱的心跳
  20. HYDRUS模型应用高级培训班

热门文章

  1. (第一课)Python学习之蟒蛇绘制
  2. 【组原】机器字长、指令字长、存储字长、存储单元、存储字 的区分
  3. OpenCASCADE:Modeling Algorithms模块之Sweeping: Prism, Revolution and Pipe
  4. wxWidgets:wxWidgets 中的 Unicode 支持
  5. boost::signals2模块实现将参数从信号调用传递到槽的示例程序
  6. boost::regex模块部分正则表达式相关的测试程序
  7. boost::log模块实现将日志记录初始化到远程 syslog 服务器
  8. boost::geometry::is_convex用法的测试程序
  9. boost::describe模块实现pp_call的测试程序
  10. boost::coroutine2模块实现斐波那契数列的测试程序