cocos2d 中有很多中关节类,关节是为了让两个独立物体之间建立某种联系,使之犹如一体
常见的关节类有:
PhysicsJointXXX
类 | 含义 | 举例说明 |
---|---|---|
PhysicsJointDistance | 设定两个刚体间的固定距离 | |
PhysicsJointFixed | 将两个刚体结合在了一起 | |
PhysicsJointGear | 使一对刚提的角速度比率保持一定比例 | 比如齿轮 |
PhysicsJointGroove | 一个刚体连到线上,另一个连到点上 | |
PhysicsJointLimit | 限制两个刚体间的最大距离 | 比如流星锤 |
PhysicsJointMotor | 两个刚体的相对角速度保持一个常数 | |
PhysicsJointPin | 两个刚体独立的围绕锚点进行旋转 | |
PhysicsJointRatchet | 与套筒扳手的工作类似,单向旋转,不能转回来,比如自行车的飞轮,正想踏实有动力的,反向是没有动力的 | |
PhysicsJointRotoryLimit | 与限制关节类似,增加了自旋 | 地球绕太阳运动(地球同时在自旋)) |
PhysicsJointRotarySpring | 与弹簧关节相似,增加自旋 | 汽车的悬挂系统 |
PhysicsJointSpring | 永泰黄来连接两个物理刚体 |
使用方式 (绳子为例)
使用 PhysicsJointDistance 将多个节点连接起来,实现绳子下落摆动的效果
基本步骤:
- 固定一个端点,并且设置body
- 循环增加节点,将节点addChild() 添加到场景中
- 相邻选两个节点创建一个关节 PhysicsJointDistance::cons
- 将关节 添加到PhysicsWorld对象 中
demo代码片段:
#include "HelloWorldScene.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
USING_NS_CC;
using namespace cocostudio::timeline;
#define PHYSICS_MATERIAL1 PhysicsMaterial(1,1,0)
#define PHYSICS_MATERIAL2 PhysicsMaterial(1,0,0)
PhysicsWorld * world;
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
//auto scene = Scene::create();
// 创建带物理特性的世界
auto scene = Scene::createWithPhysics();
world = scene->getPhysicsWorld();
scene->getPhysicsWorld()->setGravity(Vec2(0, -980)