(四)cocos2d -关节的使用(绳子)

cocos2d 中有很多中关节类,关节是为了让两个独立物体之间建立某种联系,使之犹如一体

常见的关节类有:
PhysicsJointXXX

含义 举例说明
PhysicsJointDistance 设定两个刚体间的固定距离
PhysicsJointFixed 将两个刚体结合在了一起
PhysicsJointGear 使一对刚提的角速度比率保持一定比例 比如齿轮
PhysicsJointGroove 一个刚体连到线上,另一个连到点上
PhysicsJointLimit 限制两个刚体间的最大距离 比如流星锤
PhysicsJointMotor 两个刚体的相对角速度保持一个常数
PhysicsJointPin 两个刚体独立的围绕锚点进行旋转
PhysicsJointRatchet 与套筒扳手的工作类似,单向旋转,不能转回来,比如自行车的飞轮,正想踏实有动力的,反向是没有动力的
PhysicsJointRotoryLimit 与限制关节类似,增加了自旋 地球绕太阳运动(地球同时在自旋))
PhysicsJointRotarySpring 与弹簧关节相似,增加自旋 汽车的悬挂系统
PhysicsJointSpring 永泰黄来连接两个物理刚体

使用方式 (绳子为例)

使用 PhysicsJointDistance 将多个节点连接起来,实现绳子下落摆动的效果

基本步骤:

  1. 固定一个端点,并且设置body
  2. 循环增加节点,将节点addChild() 添加到场景中
  3. 相邻选两个节点创建一个关节 PhysicsJointDistance::cons
  4. 将关节 添加到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
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值